From 5edf3ed2357e21990ae00355ce8316e661d8f6c7 Mon Sep 17 00:00:00 2001 From: Michael Roterman Date: Wed, 6 Nov 2013 01:05:24 +0100 Subject: [PATCH] Making a start of moving the ugly fromArray methods to dedicated Factories --- examples/movies/model/get.php | 34 ++++----- lib/Tmdb/Factory/GenreFactory.php | 97 +++++++++++++++++++++++++ lib/Tmdb/Factory/MovieFactory.php | 97 +++++++++++++++++++++++++ lib/Tmdb/Model/Common/DataCollector.php | 62 ++++++++++++++++ 4 files changed, 273 insertions(+), 17 deletions(-) create mode 100644 lib/Tmdb/Factory/GenreFactory.php create mode 100644 lib/Tmdb/Factory/MovieFactory.php create mode 100644 lib/Tmdb/Model/Common/DataCollector.php diff --git a/examples/movies/model/get.php b/examples/movies/model/get.php index cd96e907..e9791d17 100644 --- a/examples/movies/model/get.php +++ b/examples/movies/model/get.php @@ -34,23 +34,23 @@ echo $movie->getTitle() . "\n"; -echo "Cast\n"; - -foreach($movie->credits->cast 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"; +//echo "Cast\n"; +// +//foreach($movie->credits->cast 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()); diff --git a/lib/Tmdb/Factory/GenreFactory.php b/lib/Tmdb/Factory/GenreFactory.php new file mode 100644 index 00000000..080455e2 --- /dev/null +++ b/lib/Tmdb/Factory/GenreFactory.php @@ -0,0 +1,97 @@ + + * @copyright (c) 2013, B-Found Internet Marketing & Services + * @version 0.0.1 + */ + +namespace Tmdb\Factory; + +use Tmdb\Model\Movie; + +class MovieFactory { + /** + * Convert an array to an hydrated object + * + * @param array $data + * @return $this + */ + public static function create(array $data = array()) + { + if (!$data) { + return null; + } + + $movie = new Movie($data['id']); + +// 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']; +// } +// +// if (array_key_exists('cast', $casts)) { +// $credits->setCast(parent::collectCast($client, $casts['cast'])); +// } +// +// if (array_key_exists('crew', $casts)) { +// $credits->setCrew(parent::collectCrew($client, $casts['crew'])); +// } +// +// $movie->setCredits($credits); +// + /** Genres */ + if (array_key_exists('genres', $data)) { + $movie->setGenres(parent::collectGenres($client, $data['genres'])); + } +// +// /** 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 $movie->hydrate($data); + } + +} \ No newline at end of file diff --git a/lib/Tmdb/Factory/MovieFactory.php b/lib/Tmdb/Factory/MovieFactory.php new file mode 100644 index 00000000..faaeb426 --- /dev/null +++ b/lib/Tmdb/Factory/MovieFactory.php @@ -0,0 +1,97 @@ + + * @copyright (c) 2013, B-Found Internet Marketing & Services + * @version 0.0.1 + */ + +namespace Tmdb\Factory; + +use Tmdb\Model\Movie; + +class MovieFactory { + /** + * Convert an array to an hydrated object + * + * @param array $data + * @return $this + */ + public static function create(array $data = array()) + { + if (!$data) { + return null; + } + + $movie = new Movie($data['id']); + +// 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']; +// } +// +// if (array_key_exists('cast', $casts)) { +// $credits->setCast(parent::collectCast($client, $casts['cast'])); +// } +// +// if (array_key_exists('crew', $casts)) { +// $credits->setCrew(parent::collectCrew($client, $casts['crew'])); +// } +// +// $movie->setCredits($credits); +// +// /** Genres */ +// if (array_key_exists('genres', $data)) { +// $movie->setGenres(parent::collectGenres($client, $data['genres'])); +// } +// +// /** 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 $movie->hydrate($data); + } + +} \ No newline at end of file diff --git a/lib/Tmdb/Model/Common/DataCollector.php b/lib/Tmdb/Model/Common/DataCollector.php new file mode 100644 index 00000000..93e82ba0 --- /dev/null +++ b/lib/Tmdb/Model/Common/DataCollector.php @@ -0,0 +1,62 @@ + + * @copyright (c) 2013, Michael Roterman + * @version 0.0.1 + */ +namespace Tmdb\Model\Common; + +use Guzzle\Common\Collection as GuzzleCollection; + +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 + * + * @param string $key + * @param mixed $value + * @return GuzzleCollection + */ + public function add($key, $value) { + if ($key === null && is_object($value)) { + $key = spl_object_hash($value); + } + + return parent::add($key, $value); + } + + /** + * Allow support for getting objects + * + * @param string $key + * @return mixed|null + */ + public function get($key) { + if (is_object($key)) { + $key = spl_object_hash($key); + } + + return parent::get($key); + } +} \ No newline at end of file