From f067911977b878bc758c6b3310fef7f01b88d270 Mon Sep 17 00:00:00 2001 From: Michael Roterman Date: Mon, 18 Nov 2013 22:30:40 +0100 Subject: [PATCH] Refactoring the models big-time - Removed any knowledge of collaborators from the models. - Added factories to create the models by the retrieved data from the API. - Added a Movie repository that is able to load an movie providing the client and id ( by calling the API segment and passing the data on to the factory ). - Starting to refactor some bits of the API segment, removing the ->api() function call in the client. ( For now I just added getMovieApi(), but the whole api() method will vanish ). - Added a GenericCollectionFactory to create collections of objects we don't care much about (yet) to filter or ...? - Created some extra submodels for Movies --- examples/movies/model/all.php | 91 ++++++++++++++ examples/movies/model/get.php | 41 +----- lib/Tmdb/Client.php | 8 ++ lib/Tmdb/Common/ObjectHydrator.php | 90 +++++++++++++ lib/Tmdb/Factory/AbstractFactory.php | 88 +------------ .../Common/GenericCollectionFactory.php | 44 +++++++ lib/Tmdb/Factory/Common/ImageFactory.php | 2 +- lib/Tmdb/Factory/MovieFactory.php | 105 +++++++--------- lib/Tmdb/Factory/People/CastFactory.php | 12 +- lib/Tmdb/Factory/People/CrewFactory.php | 12 +- lib/Tmdb/Factory/People/PeopleFactory.php | 99 +++------------ lib/Tmdb/Model/Collection/Credits.php | 12 +- lib/Tmdb/Model/Collection/Credits/Crew.php | 19 --- .../Cast.php => Common/AbstractTrailer.php} | 13 +- lib/Tmdb/Model/Common/Trailer/Youtube.php | 118 ++++++++++++++++++ lib/Tmdb/Model/Movie.php | 89 +++++++------ lib/Tmdb/Model/Movie/AlternativeTitle.php | 18 +-- lib/Tmdb/Model/Movie/Keyword.php | 64 ++++++++++ lib/Tmdb/Model/Movie/Release.php | 84 +++++++++++++ lib/Tmdb/Model/Movie/Translation.php | 84 +++++++++++++ lib/Tmdb/Model/Person/AbstractMember.php | 2 +- lib/Tmdb/Model/Person/CastMember.php | 2 +- lib/Tmdb/Model/Person/CrewMember.php | 2 +- lib/Tmdb/Repository/AbstractRepository.php | 60 +++++++++ lib/Tmdb/Repository/MovieRepository.php | 47 +++++++ 25 files changed, 837 insertions(+), 369 deletions(-) create mode 100644 examples/movies/model/all.php create mode 100644 lib/Tmdb/Common/ObjectHydrator.php create mode 100644 lib/Tmdb/Factory/Common/GenericCollectionFactory.php delete mode 100644 lib/Tmdb/Model/Collection/Credits/Crew.php rename lib/Tmdb/Model/{Collection/Credits/Cast.php => Common/AbstractTrailer.php} (58%) create mode 100644 lib/Tmdb/Model/Common/Trailer/Youtube.php create mode 100644 lib/Tmdb/Model/Movie/Keyword.php create mode 100644 lib/Tmdb/Model/Movie/Release.php create mode 100644 lib/Tmdb/Model/Movie/Translation.php create mode 100644 lib/Tmdb/Repository/AbstractRepository.php create mode 100644 lib/Tmdb/Repository/MovieRepository.php diff --git a/examples/movies/model/all.php b/examples/movies/model/all.php new file mode 100644 index 00000000..42776cf6 --- /dev/null +++ b/examples/movies/model/all.php @@ -0,0 +1,91 @@ + + * @copyright (c) 2013, Michael Roterman + * @version 0.0.1 + */ +require_once('../../../vendor/autoload.php'); +require_once('../../../apikey.php'); + +$token = new \Tmdb\ApiToken(TMDB_API_KEY); +$client = new \Tmdb\Client($token); + +// This is optional, but if you want lots of data this is the way. +$append = new \Tmdb\Model\Movie\QueryParameter\AppendToResponse(array( + \Tmdb\Model\Movie\QueryParameter\AppendToResponse::ALTERNATIVE_TITLES, + \Tmdb\Model\Movie\QueryParameter\AppendToResponse::CHANGES, + \Tmdb\Model\Movie\QueryParameter\AppendToResponse::CREDITS, + \Tmdb\Model\Movie\QueryParameter\AppendToResponse::IMAGES, + \Tmdb\Model\Movie\QueryParameter\AppendToResponse::KEYWORDS, + \Tmdb\Model\Movie\QueryParameter\AppendToResponse::LISTS, + \Tmdb\Model\Movie\QueryParameter\AppendToResponse::RELEASES, + \Tmdb\Model\Movie\QueryParameter\AppendToResponse::REVIEWS, + \Tmdb\Model\Movie\QueryParameter\AppendToResponse::SIMILAR_MOVIES, + \Tmdb\Model\Movie\QueryParameter\AppendToResponse::TRAILERS, + \Tmdb\Model\Movie\QueryParameter\AppendToResponse::TRANSLATIONS, +)); + +$repository = new \Tmdb\Repository\MovieRepository($client); +$movie = $repository->load(87421, array($append)); + +echo $movie->getTitle() . "\n"; + +echo "Alternative Titles\n"; + +foreach($movie->getAlternativeTitles() as $title) { + printf(" - %s [%s]\n", $title->getTitle(), $title->getIso31661()); +} + +echo "Cast\n"; + +foreach($movie->getCredits()->getCast() as $person) { + printf(" - %s as %s\n", $person->getName(), $person->getCharacter()); +} + +echo "Crew\n"; + +foreach($movie->getCredits()->getCrew() as $person) { + printf(" - %s as %s\n", $person->getName(), $person->getJob()); +} + +echo "Images\n"; + +foreach($movie->getImages() as $image) { + printf(" - %s\n", $image->getFilePath()); +} + +echo "Genres\n"; + +foreach($movie->getGenres() as $genre) { + printf(" - %s\n", $genre->getName()); +} + +echo "Keywords\n"; + +foreach($movie->getKeywords() as $keyword) { + printf(" - %s [%s]\n", $keyword->getName(), $keyword->getId()); +} + +echo "Releases\n"; + +foreach($movie->getReleases() as $release) { + printf(" - %s on %s\n", $release->getIso31661(), $release->getReleaseDate()); +} + +echo "Translations\n"; + +foreach($movie->getTranslations() as $translation) { + printf(" - %s\n", $translation->getName()); +} + +echo "Trailers\n"; + +foreach($movie->getTrailers() as $trailer) { + printf(" - %s\n", $trailer->getUrl()); +} \ No newline at end of file diff --git a/examples/movies/model/get.php b/examples/movies/model/get.php index b2960f5d..58a31d7c 100644 --- a/examples/movies/model/get.php +++ b/examples/movies/model/get.php @@ -16,42 +16,9 @@ $token = new \Tmdb\ApiToken(TMDB_API_KEY); $client = new \Tmdb\Client($token); -$append = new \Tmdb\Model\Movie\QueryParameter\AppendToResponse(array( - \Tmdb\Model\Movie\QueryParameter\AppendToResponse::ALTERNATIVE_TITLES, - \Tmdb\Model\Movie\QueryParameter\AppendToResponse::CHANGES, - \Tmdb\Model\Movie\QueryParameter\AppendToResponse::CREDITS, - \Tmdb\Model\Movie\QueryParameter\AppendToResponse::IMAGES, - \Tmdb\Model\Movie\QueryParameter\AppendToResponse::KEYWORDS, - \Tmdb\Model\Movie\QueryParameter\AppendToResponse::LISTS, - \Tmdb\Model\Movie\QueryParameter\AppendToResponse::RELEASES, - \Tmdb\Model\Movie\QueryParameter\AppendToResponse::REVIEWS, - \Tmdb\Model\Movie\QueryParameter\AppendToResponse::SIMILAR_MOVIES, - \Tmdb\Model\Movie\QueryParameter\AppendToResponse::TRAILERS, - \Tmdb\Model\Movie\QueryParameter\AppendToResponse::TRANSLATIONS, -)); +$repository = new \Tmdb\Repository\MovieRepository($client); +$movie = $repository->load(87421); -$movie = \Tmdb\Factory\MovieFactory::load($client, 87421, array($append)); +printf('

%s

', $movie->getTitle()); -echo $movie->getTitle() . "\n"; - -echo "Cast\n"; - -foreach($movie->getCredits()->getCast() as $person) { - printf(" - %s as %s\n", $person->getName(), $person->getCharacter()); -} - -foreach($movie->getCredits()->getCrew() as $person) { - printf(" - %s as %s\n", $person->getName(), $person->getJob()); -} - -echo "Images\n"; - -foreach($movie->getImages() as $image) { - printf(" - %s\n", $image->getFilePath()); -} - -echo "Genres\n"; - -foreach($movie->getGenres() as $genre) { - printf(" - %s\n", $genre->getName()); -} \ No newline at end of file +printf('

%s

', $movie->getOverview()); \ No newline at end of file diff --git a/lib/Tmdb/Client.php b/lib/Tmdb/Client.php index f56459de..d5d256ac 100644 --- a/lib/Tmdb/Client.php +++ b/lib/Tmdb/Client.php @@ -79,6 +79,14 @@ public function setToken(Token $token) return $this; } + /** + * @return Api\Movies + */ + public function getMovieApi() + { + return new Api\Movies($this); + } + /** * Return the relevant API object * diff --git a/lib/Tmdb/Common/ObjectHydrator.php b/lib/Tmdb/Common/ObjectHydrator.php new file mode 100644 index 00000000..7e622a60 --- /dev/null +++ b/lib/Tmdb/Common/ObjectHydrator.php @@ -0,0 +1,90 @@ + + * @copyright (c) 2013, Michael Roterman + * @version 0.0.1 + */ +namespace Tmdb\Common; + +use Tmdb\Exception\RuntimeException; +use Tmdb\Model\AbstractModel; + +class ObjectHydrator { + /** + * Hydrate the object with data + * + * @param AbstractModel $object + * @param array $data + * @return $this + * @throws RuntimeException + */ + public static function hydrate(AbstractModel $object, $data = array()) + { + if (!empty($data)) { + foreach ($data as $k => $v) { + if (in_array($k, $object::$_properties)) { + + $method = self::camelize( + sprintf('set_%s', $k) + ); + + if (!method_exists($object, $method)) { + throw new RuntimeException(sprintf( + 'Trying to call method "%s" on "%s" but it does not exist or is private.', + $method, + get_class($object) + )); + } + + $object->$method($v); + } + } + } + + return $object; + } + + /** + * Transforms an under_scored_string to a camelCasedOne + * + * @see https://gist.github.com/troelskn/751517 + * + * @param $candidate + * @return string + */ + private function camelize($candidate) + { + return lcfirst( + implode('', + array_map('ucfirst', + array_map('strtolower', + explode('_', $candidate + ) + ) + ) + ) + ); + } + + /** + * Transforms a camelCasedString to an under_scored_one + * + * @see https://gist.github.com/troelskn/751517 + * + * @param $camelized + * @return string + */ + private function uncamelize($camelized) { + return implode('_', + array_map('strtolower', + preg_split('/([A-Z]{1}[^A-Z]*)/', $camelized, -1, PREG_SPLIT_DELIM_CAPTURE|PREG_SPLIT_NO_EMPTY) + ) + ); + } +} \ No newline at end of file diff --git a/lib/Tmdb/Factory/AbstractFactory.php b/lib/Tmdb/Factory/AbstractFactory.php index ae22f943..261acd91 100644 --- a/lib/Tmdb/Factory/AbstractFactory.php +++ b/lib/Tmdb/Factory/AbstractFactory.php @@ -12,9 +12,8 @@ */ namespace Tmdb\Factory; -use Tmdb\Exception\RuntimeException; +use Tmdb\Common\ObjectHydrator; use Tmdb\Model\AbstractModel; -use Tmdb\Model\Common\QueryParameter\QueryParameterInterface; abstract class AbstractFactory { /** @@ -33,94 +32,15 @@ abstract public static function create(array $data = array()); */ abstract public static function createCollection(array $data = array()); - /** - * Process query parameters - * - * @param array $parameters - * @return array - */ - protected function parseQueryParameters(array $parameters = array()) - { - foreach($parameters as $key => $candidate) { - if ($candidate instanceof QueryParameterInterface) { - unset($parameters[$key]); - - $parameters[$candidate->getKey()] = $candidate->getValue(); - } - } - - return $parameters; - } - /** * Hydrate the object with data * * @param AbstractModel $object * @param array $data - * @return $this - * @throws RuntimeException - */ - public function hydrate(AbstractModel $object, array $data = array()) - { - if (!empty($data)) { - foreach ($data as $k => $v) { - if (in_array($k, $object::$_properties)) { - - $method = self::camelize( - sprintf('set_%s', $k) - ); - - if (!method_exists($object, $method)) { - throw new RuntimeException(sprintf( - 'Trying to call method "%s" on "%s" but it does not exist or is private.', - $method, - get_class($object) - )); - } - - $object->$method($v); - } - } - } - - return $object; - } - - /** - * Transforms an under_scored_string to a camelCasedOne - * - * @see https://gist.github.com/troelskn/751517 - * - * @param $candidate - * @return string + * @return AbstractModel */ - private function camelize($candidate) + public function hydrate(AbstractModel $object, $data = array()) { - return lcfirst( - implode('', - array_map('ucfirst', - array_map('strtolower', - explode('_', $candidate - ) - ) - ) - ) - ); - } - - /** - * Transforms a camelCasedString to an under_scored_one - * - * @see https://gist.github.com/troelskn/751517 - * - * @param $camelized - * @return string - */ - private function uncamelize($camelized) { - return implode('_', - array_map('strtolower', - preg_split('/([A-Z]{1}[^A-Z]*)/', $camelized, -1, PREG_SPLIT_DELIM_CAPTURE|PREG_SPLIT_NO_EMPTY) - ) - ); + return ObjectHydrator::hydrate($object, $data); } } \ No newline at end of file diff --git a/lib/Tmdb/Factory/Common/GenericCollectionFactory.php b/lib/Tmdb/Factory/Common/GenericCollectionFactory.php new file mode 100644 index 00000000..a0a4fc72 --- /dev/null +++ b/lib/Tmdb/Factory/Common/GenericCollectionFactory.php @@ -0,0 +1,44 @@ + + * @copyright (c) 2013, Michael Roterman + * @version 0.0.1 + */ +namespace Tmdb\Factory\Common; + +use Tmdb\Common\ObjectHydrator; +use Tmdb\Model\Common\Collection; + +class GenericCollectionFactory { + /** + * {@inheritdoc} + */ + public static function create(array $data = array(), $class) + { + return self::createCollection($data, $class); + } + + /** + * {@inheritdoc} + */ + public static function createCollection(array $data = array(), $class) + { + if (is_object($class)) { + $class = get_class($class); + } + + $collection = new Collection(); + + foreach($data as $item) { + $collection->add(null, ObjectHydrator::hydrate(new $class(), $item)); + } + + return $collection; + } +} \ No newline at end of file diff --git a/lib/Tmdb/Factory/Common/ImageFactory.php b/lib/Tmdb/Factory/Common/ImageFactory.php index 7bc04558..77c8dbd5 100644 --- a/lib/Tmdb/Factory/Common/ImageFactory.php +++ b/lib/Tmdb/Factory/Common/ImageFactory.php @@ -56,7 +56,7 @@ public static function createCollectionFromMovie(array $data = array()) } } } - + return self::createCollection($collection); } diff --git a/lib/Tmdb/Factory/MovieFactory.php b/lib/Tmdb/Factory/MovieFactory.php index 482c9842..01bbb0d5 100644 --- a/lib/Tmdb/Factory/MovieFactory.php +++ b/lib/Tmdb/Factory/MovieFactory.php @@ -12,10 +12,13 @@ */ namespace Tmdb\Factory; -use Tmdb\Api\Movies; use Tmdb\Client; +use Tmdb\Factory\Common\GenericCollectionFactory; use Tmdb\Factory\Common\ImageFactory; +use Tmdb\Factory\People\CastFactory; +use Tmdb\Factory\People\CrewFactory; use Tmdb\Model\Common\Collection; +use Tmdb\Model\Common\Trailer\Youtube; use Tmdb\Model\Movie; class MovieFactory extends AbstractFactory { @@ -30,31 +33,21 @@ public static function create(array $data = array()) $movie = new Movie(); -// if (array_key_exists('alternative_titles', $data) && array_key_exists('titles', $data['alternative_titles'])) { -// $movie->setAlternativeTitles(Movie::collectGenericCollection($client, $data['alternative_titles']['titles'], new AlternativeTitle())); -// } + if (array_key_exists('alternative_titles', $data) && array_key_exists('titles', $data['alternative_titles'])) { + $movie->setAlternativeTitles( + GenericCollectionFactory::createCollection($data['alternative_titles']['titles'], new Movie\AlternativeTitle()) + ); + } - $casts = array(); - $credits = $movie->getCredits(); + if (array_key_exists('credits', $data)) { + if (array_key_exists('cast', $data['credits'])) { + $movie->getCredits()->setCast(CastFactory::createCollection($data['credits']['cast'])); + } - /** Credits */ -// if (array_key_exists('credits', $data)) { -// $casts = $data['credits']; -// } -// -// if (array_key_exists('casts', $data)) { -// $casts = $data['casts']; -// } -// -// if (array_key_exists('cast', $casts)) { -// $credits->setCast($casts['cast']); -// } -// -// if (array_key_exists('crew', $casts)) { -// $credits->setCrew($casts['crew']); -// } -// -// $movie->setCredits($credits); + if (array_key_exists('crew', $data['credits'])) { + $movie->getCredits()->setCrew(CrewFactory::createCollection($data['credits']['crew'])); + } + } /** Genres */ if (array_key_exists('genres', $data)) { @@ -65,29 +58,37 @@ public static function create(array $data = array()) if (array_key_exists('images', $data)) { $movie->setImages(ImageFactory::createCollectionFromMovie($data['images'])); } -// -// /** Keywords */ -// if (array_key_exists('keywords', $data)) { -// } -// -// if (array_key_exists('releases', $data)) { -// } -// -// if (array_key_exists('trailers', $data)) { -// } -// -// if (array_key_exists('translations', $data)) { -// } -// -// if (array_key_exists('similar_movies', $data)) { -// } -// + + /** Keywords */ + if (array_key_exists('keywords', $data)) { + $movie->setKeywords(GenericCollectionFactory::createCollection($data['keywords']['keywords'], new Movie\Keyword())); + } + + if (array_key_exists('releases', $data)) { + $movie->setReleases(GenericCollectionFactory::createCollection($data['releases']['countries'], new Movie\Release())); + } + + /** + * @TODO actually implement more providers? ( Can't seem to find any quicktime related trailers anyways? ). For now KISS + */ + if (array_key_exists('trailers', $data)) { + $movie->setTrailers(GenericCollectionFactory::createCollection($data['trailers']['youtube'], new Youtube())); + } + + if (array_key_exists('translations', $data)) { + $movie->setTranslations(GenericCollectionFactory::createCollection($data['translations']['translations'], new Movie\Translation())); + } + + if (array_key_exists('similar_movies', $data)) { + $movie->setSimilarMovies(GenericCollectionFactory::createCollection($data['similar_movies']['results'], new Movie())); + } + // if (array_key_exists('reviews', $data)) { // } -// + // if (array_key_exists('lists', $data)) { // } -// + // if (array_key_exists('changes', $data)) { // } @@ -107,20 +108,4 @@ public static function createCollection(array $data = array()) return $collection; } - - - /** - * Load a movie with the given identifier - * - * @param Client $client - * @param $id - * @param $parameters - * @return $this - */ - public static function load(Client $client, $id, array $parameters = array()) { - $data = $client->api('movies')->getMovie($id, parent::parseQueryParameters($parameters)); - - return self::create($data); - } - -} \ No newline at end of file +} \ No newline at end of file diff --git a/lib/Tmdb/Factory/People/CastFactory.php b/lib/Tmdb/Factory/People/CastFactory.php index 17bae3a9..7fe309c6 100644 --- a/lib/Tmdb/Factory/People/CastFactory.php +++ b/lib/Tmdb/Factory/People/CastFactory.php @@ -12,20 +12,16 @@ */ namespace Tmdb\Factory\People; -use Tmdb\Factory\AbstractFactory; use Tmdb\Model\Collection\People\Cast; -use Tmdb\Model\Collection\People; -class CastFactory extends AbstractFactory +class CastFactory extends PeopleFactory { /** * {@inheritdoc} */ public static function create(array $data = array()) { - $cast = new Cast(); - - return $cast->hydrate($data); + return parent::create($data); } /** @@ -33,10 +29,10 @@ public static function create(array $data = array()) */ public static function createCollection(array $data = array()) { - $collection = new People(); + $collection = new Cast(); foreach($data as $item) { - $collection->add(null, self::create($item)); + $collection->add(null, parent::create($item)); } return $collection; diff --git a/lib/Tmdb/Factory/People/CrewFactory.php b/lib/Tmdb/Factory/People/CrewFactory.php index 5b592bf6..16358744 100644 --- a/lib/Tmdb/Factory/People/CrewFactory.php +++ b/lib/Tmdb/Factory/People/CrewFactory.php @@ -12,20 +12,16 @@ */ namespace Tmdb\Factory\People; -use Tmdb\Factory\AbstractFactory; use Tmdb\Model\Collection\People\Crew; -use Tmdb\Model\Collection\People; -class CrewFactory extends AbstractFactory +class CrewFactory extends PeopleFactory { /** * {@inheritdoc} */ public static function create(array $data = array()) { - $crew = new Crew(); - - return $crew->hydrate($data); + return parent::create($data); } /** @@ -33,10 +29,10 @@ public static function create(array $data = array()) */ public static function createCollection(array $data = array()) { - $collection = new People(); + $collection = new Crew(); foreach($data as $item) { - $collection->add(null, self::create($item)); + $collection->add(null, parent::create($item)); } return $collection; diff --git a/lib/Tmdb/Factory/People/PeopleFactory.php b/lib/Tmdb/Factory/People/PeopleFactory.php index 28b57220..06111a36 100644 --- a/lib/Tmdb/Factory/People/PeopleFactory.php +++ b/lib/Tmdb/Factory/People/PeopleFactory.php @@ -10,87 +10,38 @@ * @copyright (c) 2013, Michael Roterman * @version 0.0.1 */ -namespace Tmdb\Factory; +namespace Tmdb\Factory\People; -use Tmdb\Api\Movies; +use Symfony\Component\Yaml\Exception\RuntimeException; use Tmdb\Client; +use Tmdb\Factory\AbstractFactory; use Tmdb\Model\Common\Collection; -use Tmdb\Model\Movie; +use Tmdb\Model\Person\CastMember; +use Tmdb\Model\Person\CrewMember; -class MovieFactory extends AbstractFactory { +class PeopleFactory extends AbstractFactory { /** * {@inheritdoc} */ public static function create(array $data = array()) { - if (!$data) { - return null; - } - - $movie = new Movie(); - -// if (array_key_exists('alternative_titles', $data) && array_key_exists('titles', $data['alternative_titles'])) { -// $movie->setAlternativeTitles(Movie::collectGenericCollection($client, $data['alternative_titles']['titles'], new AlternativeTitle())); -// } - - $casts = array(); - $credits = $movie->getCredits(); - - /** Credits */ - if (array_key_exists('credits', $data)) { - $casts = $data['credits']; - } - - if (array_key_exists('casts', $data)) { - $casts = $data['casts']; - } + $person = null; - if (array_key_exists('cast', $casts)) { - $credits->setCast($casts['cast']); + if (array_key_exists('character', $data)) { + $person = new CastMember(); } - if (array_key_exists('crew', $casts)) { - $credits->setCrew($casts['crew']); + if (array_key_exists('job', $data)) { + $person = new CrewMember(); } - $movie->setCredits($credits); - - /** Genres */ - if (array_key_exists('genres', $data)) { - $movie->setGenres(GenreFactory::createCollection($data['genres'])); + if (null === $person) { + throw new RuntimeException(sprintf( + 'Was unable to determine the type of person by the data provided for #%d', $data['id'] + )); } -// -// /** Images */ -// if (array_key_exists('images', $data)) { -// $movie->setImages(parent::collectImages($client, $data['images'])); -// } -// -// /** Keywords */ -// if (array_key_exists('keywords', $data)) { -// } -// -// if (array_key_exists('releases', $data)) { -// } -// -// if (array_key_exists('trailers', $data)) { -// } -// -// if (array_key_exists('translations', $data)) { -// } -// -// if (array_key_exists('similar_movies', $data)) { -// } -// -// if (array_key_exists('reviews', $data)) { -// } -// -// if (array_key_exists('lists', $data)) { -// } -// -// if (array_key_exists('changes', $data)) { -// } - return parent::hydrate($movie, $data); + return parent::hydrate($person, $data); } /** @@ -106,20 +57,4 @@ public static function createCollection(array $data = array()) return $collection; } - - - /** - * Load a movie with the given identifier - * - * @param Client $client - * @param $id - * @param $parameters - * @return $this - */ - public static function load(Client $client, $id, array $parameters = array()) { - $data = $client->api('movies')->getMovie($id, parent::parseQueryParameters($parameters)); - - return self::create($data); - } - -} \ No newline at end of file +} \ No newline at end of file diff --git a/lib/Tmdb/Model/Collection/Credits.php b/lib/Tmdb/Model/Collection/Credits.php index 3368c8ec..ad476ee7 100644 --- a/lib/Tmdb/Model/Collection/Credits.php +++ b/lib/Tmdb/Model/Collection/Credits.php @@ -36,17 +36,17 @@ public function __construct() } /** - * @param \Tmdb\Model\Collection\People\Cast $cast + * @param Cast $cast * @return $this */ - public function setCast($cast) + public function setCast(Cast $cast) { $this->cast = $cast; return $this; } /** - * @return \Tmdb\Model\Collection\People\Cast + * @return Cast */ public function getCast() { @@ -54,17 +54,17 @@ public function getCast() } /** - * @param \Tmdb\Model\Collection\People\Crew $crew + * @param Crew $crew * @return $this */ - public function setCrew($crew) + public function setCrew(Crew $crew) { $this->crew = $crew; return $this; } /** - * @return \Tmdb\Model\Collection\People\Crew + * @return Crew */ public function getCrew() { diff --git a/lib/Tmdb/Model/Collection/Credits/Crew.php b/lib/Tmdb/Model/Collection/Credits/Crew.php deleted file mode 100644 index 622f2d39..00000000 --- a/lib/Tmdb/Model/Collection/Credits/Crew.php +++ /dev/null @@ -1,19 +0,0 @@ - - * @copyright (c) 2013, Michael Roterman - * @version 0.0.1 - */ -namespace Tmdb\Model\Collection\Credits; - -use Tmdb\Model\Collection\People\Crew as BaseCrew; - -class Crew extends BaseCrew { - -} \ No newline at end of file diff --git a/lib/Tmdb/Model/Collection/Credits/Cast.php b/lib/Tmdb/Model/Common/AbstractTrailer.php similarity index 58% rename from lib/Tmdb/Model/Collection/Credits/Cast.php rename to lib/Tmdb/Model/Common/AbstractTrailer.php index 9781fc23..d78216e7 100644 --- a/lib/Tmdb/Model/Collection/Credits/Cast.php +++ b/lib/Tmdb/Model/Common/AbstractTrailer.php @@ -10,10 +10,15 @@ * @copyright (c) 2013, Michael Roterman * @version 0.0.1 */ -namespace Tmdb\Model\Collection\Credits; +namespace Tmdb\Model\Common; -use Tmdb\Model\Collection\People\Cast as BaseCast; - -class Cast extends BaseCast { +use Tmdb\Model\AbstractModel; +abstract class AbstractTrailer extends AbstractModel { + /** + * Returns the http url to the trailer + * + * @return string + */ + abstract public function getUrl(); } \ No newline at end of file diff --git a/lib/Tmdb/Model/Common/Trailer/Youtube.php b/lib/Tmdb/Model/Common/Trailer/Youtube.php new file mode 100644 index 00000000..1e2e7fdc --- /dev/null +++ b/lib/Tmdb/Model/Common/Trailer/Youtube.php @@ -0,0 +1,118 @@ + + * @copyright (c) 2013, Michael Roterman + * @version 0.0.1 + */ +namespace Tmdb\Model\Common\Trailer; + +use Tmdb\Model\Common\AbstractTrailer; + +class Youtube extends AbstractTrailer { + + const URL = 'http://www.youtube.com/watch?v=%s'; + + private $name; + private $size; + private $source; + private $type; + + public static $_properties = array( + 'name', + 'size', + 'source', + 'type' + ); + + /** + * Retrieve the url to the source + * + * @todo add bonus hd=1 query parameters, but we'd need some easy way of configuring this behaviour + * + * @return string + */ + public function getUrl() + { + return sprintf(self::URL, $this->source); + } + + /** + * @param mixed $name + * @return $this + */ + public function setName($name) + { + $this->name = $name; + return $this; + } + + /** + * @return mixed + */ + public function getName() + { + return $this->name; + } + + /** + * @param mixed $size + * @return $this + */ + public function setSize($size) + { + $this->size = $size; + return $this; + } + + /** + * @return mixed + */ + public function getSize() + { + return $this->size; + } + + /** + * @param mixed $source + * @return $this + */ + public function setSource($source) + { + $this->source = $source; + return $this; + } + + /** + * @return mixed + */ + public function getSource() + { + return $this->source; + } + + /** + * @param mixed $type + * @return $this + */ + public function setType($type) + { + $this->type = $type; + return $this; + } + + /** + * @return mixed + */ + public function getType() + { + return $this->type; + } + + +} \ No newline at end of file diff --git a/lib/Tmdb/Model/Movie.php b/lib/Tmdb/Model/Movie.php index 13fd66f8..5b7dfcd7 100644 --- a/lib/Tmdb/Model/Movie.php +++ b/lib/Tmdb/Model/Movie.php @@ -21,6 +21,12 @@ use Tmdb\Model\Common\Country; use Tmdb\Model\Common\SpokenLanguage; +use Tmdb\Model\Movie\AlternativeTitle; +use Tmdb\Model\Movie\Keyword; +use Tmdb\Model\Movie\Release; +use Tmdb\Model\Movie\Translation; +use Tmdb\Model\Person\CastMember; +use Tmdb\Model\Person\CrewMember; class Movie extends AbstractModel { /** @@ -197,6 +203,8 @@ class Movie extends AbstractModel { /** * Properties that are available in the API * + * These properties are hydrated by the ObjectHydrator, all the other properties are handled by the factory. + * * @var array */ public static $_properties = array( @@ -204,7 +212,6 @@ class Movie extends AbstractModel { 'backdrop_path', 'belongs_to_collection', 'budget', -// 'genres', // populated by the fromArray method 'homepage', 'id', 'imdb_id', @@ -212,37 +219,37 @@ class Movie extends AbstractModel { 'overview', 'popularity', 'poster_path', -// 'production_companies', // populated by the fromArray method -// 'production_countries', // populated by the fromArray method -// 'release_date', // populated by the fromArray method 'revenue', 'runtime', -// 'spoken_languages', // populated by the fromArray method 'status', 'tagline', 'title', 'vote_average', 'vote_count', - 'alternative_titles', -// 'changes', // populated by the fromArray method - 'credits', -// 'images', // populated by the fromArray method -// 'keywords', // populated by the fromArray method -// 'lists', // populated by the fromArray method -// 'releases', // populated by the fromArray method -// 'similar_movies', // populated by the fromArray method -// 'trailers', // populated by the fromArray method -// 'translations', // populated by the fromArray method ); /** * Constructor + * + * Set all default collections */ public function __construct() { - $this->genres = new Genres(); - $this->images = new Images(); - $this->credits = new Credits(); + $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(); + $this->credits = new Credits(); + $this->images = new Images(); + $this->keywords = new Collection(); + $this->lists = new Collection(); + $this->releases = new Collection(); + $this->similarMovies = new Collection(); + $this->trailers = new Collection(); + $this->translations = new Collection(); } /** @@ -678,17 +685,17 @@ public function getVoteCount() } /** - * @param People $cast + * @param People\Cast $cast * @return $this */ - public function setCast(People $cast) + public function setCast(People\Cast $cast) { $this->credits->setCast($cast); return $this; } /** - * @return Person[] + * @return CastMember[] */ public function getCast() { @@ -696,17 +703,17 @@ public function getCast() } /** - * @param People $crew + * @param People\Crew $crew * @return $this */ - public function setCrew($crew) + public function setCrew(People\Crew $crew) { $this->credits->setCrew($crew); return $this; } /** - * @return Person[] + * @return CrewMember[] */ public function getCrew() { @@ -714,7 +721,7 @@ public function getCrew() } /** - * @param \Tmdb\Model\Common\Collection $alternativeTitles + * @param Collection $alternativeTitles * @return $this */ public function setAlternativeTitles($alternativeTitles) @@ -724,7 +731,7 @@ public function setAlternativeTitles($alternativeTitles) } /** - * @return \Tmdb\Model\Common\Collection + * @return AlternativeTitle[] */ public function getAlternativeTitles() { @@ -753,7 +760,7 @@ public function getBudget() * @param Credits $credits * @return $this */ - public function setCredits($credits) + public function setCredits(Credits $credits) { $this->credits = $credits; return $this; @@ -768,7 +775,7 @@ public function getCredits() } /** - * @param \Tmdb\Model\Common\Collection $keywords + * @param Collection $keywords * @return $this */ public function setKeywords($keywords) @@ -778,7 +785,7 @@ public function setKeywords($keywords) } /** - * @return \Tmdb\Model\Common\Collection + * @return Keyword[] */ public function getKeywords() { @@ -786,7 +793,7 @@ public function getKeywords() } /** - * @param \Tmdb\Model\Common\Collection $lists + * @param Collection $lists * @return $this */ public function setLists($lists) @@ -796,7 +803,7 @@ public function setLists($lists) } /** - * @return \Tmdb\Model\Common\Collection + * @return Collection */ public function getLists() { @@ -804,17 +811,17 @@ public function getLists() } /** - * @param \Tmdb\Model\Common\Collection $releases + * @param Collection $releases * @return $this */ - public function setReleases($releases) + public function setReleases(Collection $releases) { $this->releases = $releases; return $this; } /** - * @return \Tmdb\Model\Common\Collection + * @return Release[] */ public function getReleases() { @@ -822,7 +829,7 @@ public function getReleases() } /** - * @param \Tmdb\Model\Common\Collection $similarMovies + * @param Collection $similarMovies * @return $this */ public function setSimilarMovies($similarMovies) @@ -832,7 +839,7 @@ public function setSimilarMovies($similarMovies) } /** - * @return \Tmdb\Model\Common\Collection + * @return Movie[] */ public function getSimilarMovies() { @@ -840,7 +847,7 @@ public function getSimilarMovies() } /** - * @param \Tmdb\Model\Common\Collection $trailers + * @param Collection $trailers * @return $this */ public function setTrailers($trailers) @@ -850,7 +857,9 @@ public function setTrailers($trailers) } /** - * @return \Tmdb\Model\Common\Collection + * @todo fix the phpdoc when we have actually implemented support for more providers in the future + * + * @return \Tmdb\Model\Common\Trailer\Youtube[] */ public function getTrailers() { @@ -858,7 +867,7 @@ public function getTrailers() } /** - * @param \Tmdb\Model\Common\Collection $translations + * @param Collection $translations * @return $this */ public function setTranslations($translations) @@ -868,7 +877,7 @@ public function setTranslations($translations) } /** - * @return \Tmdb\Model\Common\Collection + * @return Translation[] */ public function getTranslations() { diff --git a/lib/Tmdb/Model/Movie/AlternativeTitle.php b/lib/Tmdb/Model/Movie/AlternativeTitle.php index 183cebff..b7db2e4a 100644 --- a/lib/Tmdb/Model/Movie/AlternativeTitle.php +++ b/lib/Tmdb/Model/Movie/AlternativeTitle.php @@ -12,7 +12,6 @@ */ namespace Tmdb\Model\Movie; -use Tmdb\Client; use Tmdb\Model\AbstractModel; class AlternativeTitle extends AbstractModel { @@ -20,26 +19,11 @@ class AlternativeTitle extends AbstractModel { private $iso31661; private $title; - protected static $_properties = array( + public static $_properties = array( 'iso_3166_1', 'title', ); - /** - * Convert an array to an hydrated object - * - * @param Client $client - * @param array $data - * @return $this - */ - public static function fromArray(Client $client, array $data) - { - $title = new AlternativeTitle(); - //$title->setClient($client); - - return $title->hydrate($data); - } - /** * @param mixed $iso31661 * @return $this diff --git a/lib/Tmdb/Model/Movie/Keyword.php b/lib/Tmdb/Model/Movie/Keyword.php new file mode 100644 index 00000000..c2fa21b2 --- /dev/null +++ b/lib/Tmdb/Model/Movie/Keyword.php @@ -0,0 +1,64 @@ + + * @copyright (c) 2013, Michael Roterman + * @version 0.0.1 + */ +namespace Tmdb\Model\Movie; + +use Tmdb\Model\AbstractModel; + +class Keyword extends AbstractModel { + + private $id; + private $name; + + public static $_properties = array( + 'id', + 'name', + ); + + /** + * @param mixed $id + * @return $this + */ + public function setId($id) + { + $this->id = $id; + return $this; + } + + /** + * @return mixed + */ + public function getId() + { + return $this->id; + } + + /** + * @param mixed $name + * @return $this + */ + public function setName($name) + { + $this->name = $name; + return $this; + } + + /** + * @return mixed + */ + public function getName() + { + return $this->name; + } + + +} \ No newline at end of file diff --git a/lib/Tmdb/Model/Movie/Release.php b/lib/Tmdb/Model/Movie/Release.php new file mode 100644 index 00000000..b1578751 --- /dev/null +++ b/lib/Tmdb/Model/Movie/Release.php @@ -0,0 +1,84 @@ + + * @copyright (c) 2013, Michael Roterman + * @version 0.0.1 + */ +namespace Tmdb\Model\Movie; + +use Tmdb\Model\AbstractModel; + +class Release extends AbstractModel { + + private $iso31661; + private $certification; + private $releaseDate; + + public static $_properties = array( + 'iso_3166_1', + 'certification', + 'release_date' + ); + + /** + * @param mixed $certification + * @return $this + */ + public function setCertification($certification) + { + $this->certification = $certification; + return $this; + } + + /** + * @return mixed + */ + public function getCertification() + { + return $this->certification; + } + + /** + * @param mixed $iso31661 + * @return $this + */ + public function setIso31661($iso31661) + { + $this->iso31661 = $iso31661; + return $this; + } + + /** + * @return mixed + */ + public function getIso31661() + { + return $this->iso31661; + } + + /** + * @param mixed $releaseDate + * @return $this + */ + public function setReleaseDate($releaseDate) + { + $this->releaseDate = $releaseDate; + return $this; + } + + /** + * @return mixed + */ + public function getReleaseDate() + { + return $this->releaseDate; + } + + +} \ No newline at end of file diff --git a/lib/Tmdb/Model/Movie/Translation.php b/lib/Tmdb/Model/Movie/Translation.php new file mode 100644 index 00000000..495bcaf6 --- /dev/null +++ b/lib/Tmdb/Model/Movie/Translation.php @@ -0,0 +1,84 @@ + + * @copyright (c) 2013, Michael Roterman + * @version 0.0.1 + */ +namespace Tmdb\Model\Movie; + +use Tmdb\Model\AbstractModel; + +class Translation extends AbstractModel { + + private $iso6391; + private $name; + private $englishName; + + public static $_properties = array( + 'iso_639_1', + 'name', + 'english_name' + ); + + /** + * @param mixed $englishName + * @return $this + */ + public function setEnglishName($englishName) + { + $this->englishName = $englishName; + return $this; + } + + /** + * @return mixed + */ + public function getEnglishName() + { + return $this->englishName; + } + + /** + * @param mixed $iso6391 + * @return $this + */ + public function setIso6391($iso6391) + { + $this->iso6391 = $iso6391; + return $this; + } + + /** + * @return mixed + */ + public function getIso6391() + { + return $this->iso6391; + } + + /** + * @param mixed $name + * @return $this + */ + public function setName($name) + { + $this->name = $name; + return $this; + } + + /** + * @return mixed + */ + public function getName() + { + return $this->name; + } + + +} \ No newline at end of file diff --git a/lib/Tmdb/Model/Person/AbstractMember.php b/lib/Tmdb/Model/Person/AbstractMember.php index 5d65a17c..f23d3504 100644 --- a/lib/Tmdb/Model/Person/AbstractMember.php +++ b/lib/Tmdb/Model/Person/AbstractMember.php @@ -20,7 +20,7 @@ abstract class AbstractMember extends AbstractModel { private $name; private $profilePath; - protected static $_properties = array( + public static $_properties = array( 'id', 'name', 'profile_path' diff --git a/lib/Tmdb/Model/Person/CastMember.php b/lib/Tmdb/Model/Person/CastMember.php index c248a93a..5ccfeb7b 100644 --- a/lib/Tmdb/Model/Person/CastMember.php +++ b/lib/Tmdb/Model/Person/CastMember.php @@ -20,7 +20,7 @@ class CastMember extends AbstractMember implements PersonInterface { private $character; private $order; - protected static $_properties = array( + public static $_properties = array( 'id', 'name', 'character', diff --git a/lib/Tmdb/Model/Person/CrewMember.php b/lib/Tmdb/Model/Person/CrewMember.php index 7a027bd5..97859ced 100644 --- a/lib/Tmdb/Model/Person/CrewMember.php +++ b/lib/Tmdb/Model/Person/CrewMember.php @@ -20,7 +20,7 @@ class CrewMember extends AbstractMember implements PersonInterface { private $department; private $job; - protected static $_properties = array( + public static $_properties = array( 'id', 'name', 'department', diff --git a/lib/Tmdb/Repository/AbstractRepository.php b/lib/Tmdb/Repository/AbstractRepository.php new file mode 100644 index 00000000..610763be --- /dev/null +++ b/lib/Tmdb/Repository/AbstractRepository.php @@ -0,0 +1,60 @@ + + * @copyright (c) 2013, Michael Roterman + * @version 0.0.1 + */ +namespace Tmdb\Repository; + +use Tmdb\Client; +use Tmdb\Model\Common\QueryParameter\QueryParameterInterface; + +abstract class AbstractRepository { + + private static $client = null; + + /** + * Constructor + * + * @param Client $client + */ + public function __construct(Client $client) + { + self::$client = $client; + } + + /** + * Return the client + * + * @return Client + */ + public function getClient() + { + return self::$client; + } + + /** + * Process query parameters + * + * @param array $parameters + * @return array + */ + protected function parseQueryParameters(array $parameters = array()) + { + foreach($parameters as $key => $candidate) { + if ($candidate instanceof QueryParameterInterface) { + unset($parameters[$key]); + + $parameters[$candidate->getKey()] = $candidate->getValue(); + } + } + + return $parameters; + } +} \ No newline at end of file diff --git a/lib/Tmdb/Repository/MovieRepository.php b/lib/Tmdb/Repository/MovieRepository.php new file mode 100644 index 00000000..855c9b7e --- /dev/null +++ b/lib/Tmdb/Repository/MovieRepository.php @@ -0,0 +1,47 @@ + + * @copyright (c) 2013, Michael Roterman + * @version 0.0.1 + */ +namespace Tmdb\Repository; + +use Tmdb\Factory\MovieFactory; +use Tmdb\Model\Movie; + +class MovieRepository extends AbstractRepository { + /** + * Load a movie with the given identifier + * + * @param $id + * @param $parameters + * @return Movie + */ + public static function load($id, array $parameters = array()) { + $data = parent::getClient()->getMovieApi()->getMovie($id, parent::parseQueryParameters($parameters)); + + return MovieFactory::create($data); + } + + /** + * If you obtained an movie model which is not completely hydrated, you can use this function. ( E.g. similar_movies ) + * + * Do note that you will have to provide the AppendToResponse object into the parameters array of which data + * you would like to obtain. + * + * array(AppendToResponse(array(...))) + * + * @param Movie $movie + * @param array $parameters + * @return Movie + */ + public function refresh(Movie $movie, array $parameters = array()) { + return self::load($movie->getId(), $parameters); + } +} \ No newline at end of file