From cc4a86569dd232bbef6b11528988b815704a4bcf Mon Sep 17 00:00:00 2001 From: Michael Roterman Date: Sat, 8 Feb 2014 22:25:29 +0100 Subject: [PATCH] Fixing Discover collection type --- PROGRESS.md | 2 +- lib/Tmdb/Factory/AbstractFactory.php | 34 ++++++++++++++++++++++ lib/Tmdb/Repository/DiscoverRepository.php | 4 +-- 3 files changed, 37 insertions(+), 3 deletions(-) diff --git a/PROGRESS.md b/PROGRESS.md index 65e36dce..af2137a0 100644 --- a/PROGRESS.md +++ b/PROGRESS.md @@ -17,7 +17,7 @@ The states for now defined as; | Changes | Done | | Collections | Done | | Companies | Done | -| Credits | Review | +| Credits | **TODO** | | Discover | Review | | Find | Review | | Genres | Review | diff --git a/lib/Tmdb/Factory/AbstractFactory.php b/lib/Tmdb/Factory/AbstractFactory.php index ba75d318..5dbe5eec 100644 --- a/lib/Tmdb/Factory/AbstractFactory.php +++ b/lib/Tmdb/Factory/AbstractFactory.php @@ -14,6 +14,7 @@ use Tmdb\Common\ObjectHydrator; use Tmdb\Model\AbstractModel; +use Tmdb\Model\Collection\ResultCollection; use Tmdb\Model\Common\GenericCollection; abstract class AbstractFactory implements FactoryInterface { @@ -55,6 +56,39 @@ protected function createGenericCollection(array $data = array(), $class) return $collection; } + /** + * Create a result collection + * + * @param array $data + * @return GenericCollection + */ + public function createResultCollection(array $data = array()) + { + $collection = new ResultCollection(); + + if (array_key_exists('page', $data)) { + $collection->setPage($data['page']); + } + + if (array_key_exists('total_pages', $data)) { + $collection->setTotalPages($data['total_pages']); + } + + if (array_key_exists('total_results', $data)) { + $collection->setTotalResults($data['total_results']); + } + + if (array_key_exists('results', $data)) { + $data = $data['results']; + } + + foreach($data as $item) { + $collection->add(null, $this->create($item)); + } + + return $collection; + } + /** * Hydrate the object with data * diff --git a/lib/Tmdb/Repository/DiscoverRepository.php b/lib/Tmdb/Repository/DiscoverRepository.php index cd411b9e..e9fb9696 100644 --- a/lib/Tmdb/Repository/DiscoverRepository.php +++ b/lib/Tmdb/Repository/DiscoverRepository.php @@ -39,7 +39,7 @@ public function discoverMovies(DiscoverMoviesQuery $query, array $headers = arra $data = $this->getApi()->discoverMovies($query, $headers); - return $this->getMovieFactory()->createCollection($data); + return $this->getMovieFactory()->createResultCollection($data); } /** @@ -53,7 +53,7 @@ public function discoverMovies(DiscoverMoviesQuery $query, array $headers = arra public function discoverTv(DiscoverTvQuery $query, array $headers = array()) { $data = $this->getApi()->discoverTv($query->toArray(), $headers); - return $this->getTvFactory()->createCollection($data); + return $this->getTvFactory()->createResultCollection($data); } /**