From 27ef5dbfa941ff24e324bb758bdc92c5b3ccc3d0 Mon Sep 17 00:00:00 2001 From: Michael Roterman Date: Tue, 14 Jan 2014 23:30:46 +0100 Subject: [PATCH] Expanding the Factories to better support Image generation and adding properties to the relative objects to provide this functionality ( e.g. posterPath has a cousin poster now which holds a true Image object ). --- examples/movies/model/get.php | 4 +- examples/tv/model/show.php | 2 - lib/Tmdb/Factory/ImageFactory.php | 106 ++++++++++++++++++++-- lib/Tmdb/Factory/MovieFactory.php | 10 +- lib/Tmdb/Factory/People/PeopleFactory.php | 4 + lib/Tmdb/Model/Image/BackdropImage.php | 17 ++++ lib/Tmdb/Model/Image/LogoImage.php | 17 ++++ lib/Tmdb/Model/Image/PosterImage.php | 17 ++++ lib/Tmdb/Model/Image/ProfileImage.php | 17 ++++ lib/Tmdb/Model/Image/StillImage.php | 17 ++++ lib/Tmdb/Model/Movie.php | 49 +++++++++- lib/Tmdb/Model/Person/AbstractMember.php | 19 ++++ 12 files changed, 263 insertions(+), 16 deletions(-) create mode 100644 lib/Tmdb/Model/Image/BackdropImage.php create mode 100644 lib/Tmdb/Model/Image/LogoImage.php create mode 100644 lib/Tmdb/Model/Image/PosterImage.php create mode 100644 lib/Tmdb/Model/Image/ProfileImage.php create mode 100644 lib/Tmdb/Model/Image/StillImage.php diff --git a/examples/movies/model/get.php b/examples/movies/model/get.php index 58a31d7c..904b9ce0 100644 --- a/examples/movies/model/get.php +++ b/examples/movies/model/get.php @@ -19,6 +19,4 @@ $repository = new \Tmdb\Repository\MovieRepository($client); $movie = $repository->load(87421); -printf('

%s

', $movie->getTitle()); - -printf('

%s

', $movie->getOverview()); \ No newline at end of file +var_dump($movie); \ No newline at end of file diff --git a/examples/tv/model/show.php b/examples/tv/model/show.php index 6fb8039f..e5f51809 100644 --- a/examples/tv/model/show.php +++ b/examples/tv/model/show.php @@ -19,6 +19,4 @@ $repository = new \Tmdb\Repository\TvRepository($client); $tvShow = $repository->load(1396); -printf('

%s

', $tvShow->getName()); - var_dump($tvShow); \ No newline at end of file diff --git a/lib/Tmdb/Factory/ImageFactory.php b/lib/Tmdb/Factory/ImageFactory.php index 10d9d4b0..069c4881 100644 --- a/lib/Tmdb/Factory/ImageFactory.php +++ b/lib/Tmdb/Factory/ImageFactory.php @@ -20,9 +20,99 @@ class ImageFactory extends AbstractFactory /** * {@inheritdoc} */ - public static function create(array $data = array()) + public static function create(array $data = array(), $key = null) { - return parent::hydrate(new Image(), $data); + $type = self::resolveImageType($key); + + if (is_string($data)) { + $data = array( + 'file_path' => $data + ); + } + + return parent::hydrate($type, $data); + } + + /** + * Create an image instance based on the path and type, e.g. + * + * '/xkQ5yWnMjpC2bGmu7GsD66AAoKO.jpg', 'backdrop_path' + * + * @param $path + * @param $key + * @return \Tmdb\Model\Image + */ + public static function createFromPath($path, $key) + { + return parent::hydrate( + self::resolveImageType($key), + array('file_path' => $path) + ); + } + + /** + * Return possible image type keys + * + * @return array + */ + public static function getPossibleKeys() + { + return array( + 'poster', + 'posters', + 'poster_path', + 'backdrop', + 'backdrops', + 'backdrop_path', + 'profile', + 'profiles', + 'profile_path', + 'logo', + 'logos', + 'logo_path', + ); + } + + private function resolveImageType($key = null) + { + switch($key) { + case 'poster': + case 'posters': + case 'poster_path': + $object = new Image\PosterImage(); + break; + + case 'backdrop': + case 'backdrops': + case 'backdrop_path': + $object = new Image\BackdropImage(); + break; + + case 'profile': + case 'profiles': + case 'profile_path': + $object = new Image\ProfileImage(); + break; + + case 'logo': + case 'logos': + case 'logo_path': + $object = new Image\LogoImage(); + break; + + case 'still': + case 'stills': + case 'still_path': + $object = new Image\StillImage(); + break; + + case 'file_path': + default: + $object = new Image(); + break; + } + + return $object; } /** @@ -30,10 +120,10 @@ public static function create(array $data = array()) */ public static function createCollection(array $data = array()) { - $collection = new Images(); + $collection = new Images(); foreach($data as $item) { - $collection->add(null, self::create($item)); + $collection->addObject(self::create($item)); } return $collection; @@ -44,19 +134,19 @@ public static function createCollection(array $data = array()) */ public static function createImageCollection(array $data = array()) { - $collection = array(); + $collection = new Images(); foreach($data as $format => $formatCollection) { foreach($formatCollection as $item) { if (array_key_exists($format, Image::$_formats)) { - $item['format'] = Image::$_formats[$format]; + $item = self::create($item, $format); - $collection[] = $item; + $collection->addObject($item); } } } - return self::createCollection($collection); + return $collection; } /** diff --git a/lib/Tmdb/Factory/MovieFactory.php b/lib/Tmdb/Factory/MovieFactory.php index 14657bf1..774a7b9d 100644 --- a/lib/Tmdb/Factory/MovieFactory.php +++ b/lib/Tmdb/Factory/MovieFactory.php @@ -54,10 +54,18 @@ public static function create(array $data = array()) } /** Images */ + if (array_key_exists('backdrop_path', $data)) { + $movie->setBackdrop(ImageFactory::createFromPath($data['backdrop_path'], 'backdrop_path')); + } + if (array_key_exists('images', $data)) { $movie->setImages(ImageFactory::createCollectionFromMovie($data['images'])); } + if (array_key_exists('poster_path', $data)) { + $movie->setPoster(ImageFactory::createFromPath($data['poster_path'], 'poster_path')); + } + /** Keywords */ if (array_key_exists('keywords', $data)) { $movie->setKeywords(GenericCollectionFactory::createCollection($data['keywords']['keywords'], new Movie\Keyword())); @@ -79,7 +87,7 @@ public static function create(array $data = array()) } if (array_key_exists('similar_movies', $data)) { - $movie->setSimilarMovies(GenericCollectionFactory::createCollection($data['similar_movies']['results'], new Movie())); + $movie->setSimilarMovies(self::createCollection($data['similar_movies']['results'])); } // if (array_key_exists('reviews', $data)) { diff --git a/lib/Tmdb/Factory/People/PeopleFactory.php b/lib/Tmdb/Factory/People/PeopleFactory.php index 4cfebd89..6d559c67 100644 --- a/lib/Tmdb/Factory/People/PeopleFactory.php +++ b/lib/Tmdb/Factory/People/PeopleFactory.php @@ -45,6 +45,10 @@ public static function create(array $data = array(), Person\AbstractMember $pers $person->setImages(ImageFactory::createCollectionFromPeople($data['images'])); } + if (array_key_exists('profile_path', $data)) { + $person->setProfile(ImageFactory::createFromPath($data['profile_path'], 'profile_path')); + } + return parent::hydrate($person, $data); } diff --git a/lib/Tmdb/Model/Image/BackdropImage.php b/lib/Tmdb/Model/Image/BackdropImage.php new file mode 100644 index 00000000..4d6b10b8 --- /dev/null +++ b/lib/Tmdb/Model/Image/BackdropImage.php @@ -0,0 +1,17 @@ + + * @copyright (c) 2013, Michael Roterman + * @version 0.0.1 + */ +namespace Tmdb\Model\Image; + +use Tmdb\Model\Image; + +class BackdropImage extends Image {} \ No newline at end of file diff --git a/lib/Tmdb/Model/Image/LogoImage.php b/lib/Tmdb/Model/Image/LogoImage.php new file mode 100644 index 00000000..c83d7843 --- /dev/null +++ b/lib/Tmdb/Model/Image/LogoImage.php @@ -0,0 +1,17 @@ + + * @copyright (c) 2013, Michael Roterman + * @version 0.0.1 + */ +namespace Tmdb\Model\Image; + +use Tmdb\Model\Image; + +class LogoImage extends Image {} \ No newline at end of file diff --git a/lib/Tmdb/Model/Image/PosterImage.php b/lib/Tmdb/Model/Image/PosterImage.php new file mode 100644 index 00000000..2dcd03d4 --- /dev/null +++ b/lib/Tmdb/Model/Image/PosterImage.php @@ -0,0 +1,17 @@ + + * @copyright (c) 2013, Michael Roterman + * @version 0.0.1 + */ +namespace Tmdb\Model\Image; + +use Tmdb\Model\Image; + +class PosterImage extends Image {} \ No newline at end of file diff --git a/lib/Tmdb/Model/Image/ProfileImage.php b/lib/Tmdb/Model/Image/ProfileImage.php new file mode 100644 index 00000000..ba6a7e5d --- /dev/null +++ b/lib/Tmdb/Model/Image/ProfileImage.php @@ -0,0 +1,17 @@ + + * @copyright (c) 2013, Michael Roterman + * @version 0.0.1 + */ +namespace Tmdb\Model\Image; + +use Tmdb\Model\Image; + +class ProfileImage extends Image {} \ No newline at end of file diff --git a/lib/Tmdb/Model/Image/StillImage.php b/lib/Tmdb/Model/Image/StillImage.php new file mode 100644 index 00000000..ea7587c1 --- /dev/null +++ b/lib/Tmdb/Model/Image/StillImage.php @@ -0,0 +1,17 @@ + + * @copyright (c) 2013, Michael Roterman + * @version 0.0.1 + */ +namespace Tmdb\Model\Image; + +use Tmdb\Model\Image; + +class StillImage extends Image {} \ No newline at end of file diff --git a/lib/Tmdb/Model/Movie.php b/lib/Tmdb/Model/Movie.php index 68ee98b7..2428eb3d 100644 --- a/lib/Tmdb/Model/Movie.php +++ b/lib/Tmdb/Model/Movie.php @@ -35,10 +35,15 @@ class Movie extends AbstractModel { private $adult = false; /** - * @var Image + * @var string */ private $backdropPath; + /** + * @var Image + */ + private $backdrop; + /** * @var Collection */ @@ -89,6 +94,11 @@ class Movie extends AbstractModel { /** * @var Image */ + private $poster; + + /** + * @var string + */ private $posterPath; /** @@ -238,7 +248,6 @@ public function __construct() $this->genres = new Genres(); $this->productionCompanies = new Collection(); $this->productionCountries = new Collection(); - $this->releaseDate = new Collection(); $this->spokenLanguages = new Collection(); $this->alternativeTitles = new Collection(); $this->changes = new Collection(); @@ -883,4 +892,40 @@ public function getTranslations() { return $this->translations; } + + /** + * @param \Tmdb\Model\Image $backdrop + * @return $this + */ + public function setBackdrop($backdrop) + { + $this->backdrop = $backdrop; + return $this; + } + + /** + * @return \Tmdb\Model\Image + */ + public function getBackdrop() + { + return $this->backdrop; + } + + /** + * @param \Tmdb\Model\Image $poster + * @return $this + */ + public function setPoster($poster) + { + $this->poster = $poster; + return $this; + } + + /** + * @return \Tmdb\Model\Image + */ + public function getPoster() + { + return $this->poster; + } } \ No newline at end of file diff --git a/lib/Tmdb/Model/Person/AbstractMember.php b/lib/Tmdb/Model/Person/AbstractMember.php index f23d3504..a78a675e 100644 --- a/lib/Tmdb/Model/Person/AbstractMember.php +++ b/lib/Tmdb/Model/Person/AbstractMember.php @@ -19,6 +19,7 @@ abstract class AbstractMember extends AbstractModel { private $id; private $name; private $profilePath; + private $profile; public static $_properties = array( 'id', @@ -98,4 +99,22 @@ public function getOrder() return $this->order; } + /** + * @param mixed $profile + * @return $this + */ + public function setProfile($profile) + { + $this->profile = $profile; + return $this; + } + + /** + * @return mixed + */ + public function getProfile() + { + return $this->profile; + } + } \ No newline at end of file