diff --git a/examples/movies/model/all.php b/examples/movies/model/all.php
index 82c26f99..d5d50513 100644
--- a/examples/movies/model/all.php
+++ b/examples/movies/model/all.php
@@ -18,41 +18,57 @@
$token = new \Tmdb\ApiToken(TMDB_API_KEY);
$client = new \Tmdb\Client($token);
-$repository = new \Tmdb\Repository\MovieRepository($client);
-$movie = $repository->load(87421);
+$configRepository = new \Tmdb\Repository\ConfigurationRepository($client);
+$config = $configRepository->load();
+
+$imageHelper = new \Tmdb\Helper\ImageHelper($config);
+$repository = new \Tmdb\Repository\MovieRepository($client);
+$movie = $repository->load(87421);
echo $movie->getTitle() . "
";
echo "Alternative Titles
";
-foreach($movie->getAlternativeTitles() as $title) {
+foreach($movie->getAlternativeTitles()->filterCountry('US') as $title) {
printf(" - %s [%s]
", $title->getTitle(), $title->getIso31661());
}
echo "Cast
";
foreach($movie->getCredits()->getCast() as $person) {
+ echo $imageHelper->getHtml($person->getProfile(), 'w45');
printf(" - %s as %s
", $person->getName(), $person->getCharacter());
}
echo "Crew
";
foreach($movie->getCredits()->getCrew() as $person) {
+ echo $imageHelper->getHtml($person->getProfile(), 'w45');
printf(" - %s as %s
", $person->getName(), $person->getJob());
}
echo "Images
";
-$configRepository = new \Tmdb\Repository\ConfigurationRepository($client);
-$config = $configRepository->load();
-
-$imageHelper = new \Tmdb\Helper\ImageHelper($config);
-foreach($movie->getImages() as $image) {
- echo $imageHelper->getHtml($image);
+// All collection classes support filtering by closure functions, provided by the Guzzle collection implementation.
+foreach($movie->getImages()->filter(
+ function($key, $value){
+ if ($value->getIso6391() == 'en' && $value instanceof \Tmdb\Model\Image\PosterImage) { return true; }
+ }
+ ) as $image) {
+ echo $imageHelper->getHtml($image, 'w154', 150);
printf(" - %s
", $imageHelper->getUrl($image));
}
+// There are however some sensible default filters available for most collections
+$backdrop = $movie
+ ->getImages()
+ ->filterBackdrops()
+ ->filterBestVotedImage()
+;
+
+echo $imageHelper->getHtml($backdrop, 'original', '1024');
+
echo "Genres
";
foreach($movie->getGenres() as $genre) {
@@ -67,13 +83,13 @@
echo "Releases
";
-foreach($movie->getReleases() as $release) {
+foreach($movie->getReleases()->filterCountry('US') as $release) {
printf(" - %s on %s
", $release->getIso31661(), $release->getReleaseDate()->format('d-m-Y'));
}
echo "Translations
";
-foreach($movie->getTranslations() as $translation) {
+foreach($movie->getTranslations()->filterLanguage('en') as $translation) {
printf(" - %s
", $translation->getName());
}
diff --git a/lib/Tmdb/Helper/ImageHelper.php b/lib/Tmdb/Helper/ImageHelper.php
index 313af778..20d3977d 100644
--- a/lib/Tmdb/Helper/ImageHelper.php
+++ b/lib/Tmdb/Helper/ImageHelper.php
@@ -54,12 +54,33 @@ public function getUrl(Image $image, $size = 'original') {
* @param string $size
* @return string
*/
- public function getHtml(Image $image, $size = 'original') {
+ public function getHtml(Image $image, $size = 'original', $width = null, $height = null) {
+ if (null == $image->getFilePath()) {
+ return '';
+ }
+
+ $aspectRatio = $image->getAspectRatio();
+ if (null !== $width && null == $height && $aspectRatio !== null) {
+ $height = round($width / $aspectRatio);
+ }
+
+ if (null !== $height && null == $width && $aspectRatio !== null) {
+ $width = round($height * $aspectRatio);
+ }
+
+ if (null == $width) {
+ $width = $image->getWidth();
+ }
+
+ if (null == $height) {
+ $height = $image->getHeight();
+ }
+
return sprintf(
'
',
$this->getUrl($image, $size),
- $image->getWidth(),
- $image->getHeight()
+ $width,
+ $height
);
}
}
\ No newline at end of file
diff --git a/lib/Tmdb/Model/Collection/Images.php b/lib/Tmdb/Model/Collection/Images.php
index b708b0ae..121981cd 100644
--- a/lib/Tmdb/Model/Collection/Images.php
+++ b/lib/Tmdb/Model/Collection/Images.php
@@ -53,4 +53,143 @@ public function addImage(Image $image)
{
$this->add(null, $image);
}
-}
\ No newline at end of file
+
+ /**
+ * Filter poster images
+ *
+ * @return Images
+ */
+ public function filterPosters()
+ {
+ return $this->filter(
+ function($key, $value) {
+ if ($value instanceof Image\PosterImage) { return true; }
+ }
+ );
+ }
+
+ /**
+ * Filter backdrop images
+ *
+ * @return Images
+ */
+ public function filterBackdrops()
+ {
+ return $this->filter(
+ function($key, $value) {
+ if ($value instanceof Image\BackdropImage) { return true; }
+ }
+ );
+ }
+
+ /**
+ * Filter profile images
+ *
+ * @return Images
+ */
+ public function filterProfile()
+ {
+ return $this->filter(
+ function($key, $value) {
+ if ($value instanceof Image\ProfileImage) { return true; }
+ }
+ );
+ }
+
+ /**
+ * Filter still images
+ *
+ * @return Images
+ */
+ public function filterStills()
+ {
+ return $this->filter(
+ function($key, $value) {
+ if ($value instanceof Image\StillImage) { return true; }
+ }
+ );
+ }
+
+ /**
+ * Filter by image size
+ *
+ * @param $width
+ * @return Images
+ */
+ public function filterMaxWidth($width)
+ {
+ return $this->filter(
+ function($key, $value) use ($width) {
+ if ($value->getWidth() <= $width && $value->getWidth() !== null) { return true; }
+ }
+ );
+ }
+
+ /**
+ * Filter by image size
+ *
+ * @param $width
+ * @return Images
+ */
+ public function filterMinWidth($width)
+ {
+ return $this->filter(
+ function($key, $value) use ($width) {
+ if ($value->getWidth() >= $width && $value->getWidth() !== null) { return true; }
+ }
+ );
+ }
+
+ /**
+ * Filter by image size
+ *
+ * @param $height
+ * @return Images
+ */
+ public function filterMaxHeight($height)
+ {
+ return $this->filter(
+ function($key, $value) use ($height) {
+ if ($value->getHeight() <= $height && $value->getHeight() !== null) { return true; }
+ }
+ );
+ }
+
+ /**
+ * Filter by image size
+ *
+ * @param $height
+ * @return Images
+ */
+ public function filterMinHeight($height)
+ {
+ return $this->filter(
+ function($key, $value) use ($height) {
+ if ($value->getHeight() >= $height && $value->getHeight() !== null) { return true; }
+ }
+ );
+ }
+
+ /**
+ * Return a single image that is rated highest
+ *
+ * @return null|Image
+ */
+ public function filterBestVotedImage()
+ {
+ $currentImage = null;
+ $voteAverage = 0;
+
+ /**
+ * @var $image Image
+ */
+ foreach($this as $image) {
+ if ($image->getVoteAverage() > $voteAverage) {
+ $voteAverage = $image->getVoteAverage();
+ $currentImage = $image;
+ }
+ }
+
+ return $currentImage;
+ }
+}
\ No newline at end of file
diff --git a/lib/Tmdb/Model/Common/Collection.php b/lib/Tmdb/Model/Common/Collection.php
index 93e82ba0..a7b0739b 100644
--- a/lib/Tmdb/Model/Common/Collection.php
+++ b/lib/Tmdb/Model/Common/Collection.php
@@ -17,20 +17,6 @@
class Collection extends GuzzleCollection {
protected $data = array();
- /**
- * Allow adding objects to the collection
- *
- * @param $object
- */
- public function addObject($object)
- {
- if (!is_object($object)) {
- return;
- }
-
- $this->add(null, $object);
- }
-
/**
* Allow support for adding objects
*
@@ -59,4 +45,63 @@ public function get($key) {
return parent::get($key);
}
-}
\ No newline at end of file
+
+ /**
+ * Allow adding objects to the collection
+ *
+ * @param $object
+ */
+ public function addObject($object)
+ {
+ if (!is_object($object)) {
+ return;
+ }
+
+ $this->add(null, $object);
+ }
+
+ /**
+ * Filter by language ISO 639-1 code.
+ *
+ * @param string $language
+ * @return Collection
+ */
+ public function filterLanguage($language = 'en')
+ {
+ return $this->filter(
+ function($key, $value) use ($language) {
+ if ($value->getIso6391() == $language) { return true; }
+ }
+ );
+ }
+
+ /**
+ * Filter by country ISO 3166-1 code.
+ *
+ * @param string $country
+ * @return Collection
+ */
+ public function filterCountry($country = 'US')
+ {
+ return $this->filter(
+ function($key, $value) use ($country) {
+ if ($value->getIso31661() == $country) { return true; }
+ }
+ );
+ }
+
+ /**
+ * Filter by adult content
+ *
+ * @param boolean $adult
+ * @return Collection
+ */
+ public function filterAdult($adult = false)
+ {
+ return $this->filter(
+ function($key, $value) use ($adult) {
+ if ($value->getAdult() == $adult) { return true; }
+ }
+ );
+ }
+}
\ No newline at end of file
diff --git a/lib/Tmdb/Model/Movie.php b/lib/Tmdb/Model/Movie.php
index 2428eb3d..ffdd98af 100644
--- a/lib/Tmdb/Model/Movie.php
+++ b/lib/Tmdb/Model/Movie.php
@@ -398,7 +398,7 @@ public function setImages(Images $images)
}
/**
- * @return Image
+ * @return Images Image[]
*/
public function getImages()
{