Skip to content

Commit

Permalink
Fixing Discover collection type
Browse files Browse the repository at this point in the history
  • Loading branch information
wtfzdotnet committed Feb 8, 2014
1 parent baf1706 commit cc4a865
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 3 deletions.
2 changes: 1 addition & 1 deletion PROGRESS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
Expand Down
34 changes: 34 additions & 0 deletions lib/Tmdb/Factory/AbstractFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
*
Expand Down
4 changes: 2 additions & 2 deletions lib/Tmdb/Repository/DiscoverRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand Down

0 comments on commit cc4a865

Please sign in to comment.