Skip to content

Commit

Permalink
Adding generic and image related collection filters
Browse files Browse the repository at this point in the history
  • Loading branch information
wtfzdotnet committed Jan 15, 2014
1 parent 27ef5db commit 0070d2f
Show file tree
Hide file tree
Showing 5 changed files with 252 additions and 31 deletions.
38 changes: 27 additions & 11 deletions examples/movies/model/all.php
Original file line number Diff line number Diff line change
Expand Up @@ -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() . "<br/>";

echo "Alternative Titles<br/>";

foreach($movie->getAlternativeTitles() as $title) {
foreach($movie->getAlternativeTitles()->filterCountry('US') as $title) {
printf(" - %s [%s]<br/>", $title->getTitle(), $title->getIso31661());
}

echo "Cast<br/>";

foreach($movie->getCredits()->getCast() as $person) {
echo $imageHelper->getHtml($person->getProfile(), 'w45');
printf(" - %s as %s<br/>", $person->getName(), $person->getCharacter());
}

echo "Crew<br/>";

foreach($movie->getCredits()->getCrew() as $person) {
echo $imageHelper->getHtml($person->getProfile(), 'w45');
printf(" - %s as %s<br/>", $person->getName(), $person->getJob());
}

echo "Images<br/>";

$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<br/>", $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<br/>";

foreach($movie->getGenres() as $genre) {
Expand All @@ -67,13 +83,13 @@

echo "Releases<br/>";

foreach($movie->getReleases() as $release) {
foreach($movie->getReleases()->filterCountry('US') as $release) {
printf(" - %s on %s<br/>", $release->getIso31661(), $release->getReleaseDate()->format('d-m-Y'));
}

echo "Translations<br/>";

foreach($movie->getTranslations() as $translation) {
foreach($movie->getTranslations()->filterLanguage('en') as $translation) {
printf(" - %s<br/>", $translation->getName());
}

Expand Down
27 changes: 24 additions & 3 deletions lib/Tmdb/Helper/ImageHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
'<img src="%s" width="%s" height="%s" />',
$this->getUrl($image, $size),
$image->getWidth(),
$image->getHeight()
$width,
$height
);
}
}
141 changes: 140 additions & 1 deletion lib/Tmdb/Model/Collection/Images.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,143 @@ public function addImage(Image $image)
{
$this->add(null, $image);
}
}

/**
* 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;
}
}
75 changes: 60 additions & 15 deletions lib/Tmdb/Model/Common/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down Expand Up @@ -59,4 +45,63 @@ public function get($key) {

return parent::get($key);
}
}

/**
* 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; }
}
);
}
}
2 changes: 1 addition & 1 deletion lib/Tmdb/Model/Movie.php
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,7 @@ public function setImages(Images $images)
}

/**
* @return Image
* @return Images Image[]
*/
public function getImages()
{
Expand Down

0 comments on commit 0070d2f

Please sign in to comment.