Skip to content

Commit

Permalink
Clean up the Genre API
Browse files Browse the repository at this point in the history
  • Loading branch information
wtfzdotnet committed Jan 19, 2014
1 parent f548710 commit a0fa24a
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 24 deletions.
24 changes: 24 additions & 0 deletions examples/genres/model/all.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php
/**
* This file is part of the Tmdb PHP API created by Michael Roterman.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @package Tmdb
* @author Michael Roterman <michael@wtfz.net>
* @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);

$repository = new \Tmdb\Repository\GenreRepository($client);
$genres = $repository->loadCollection();

foreach($genres as $genre) {
var_dump($genre);
}
8 changes: 1 addition & 7 deletions examples/genres/model/get.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,4 @@
$repository = new \Tmdb\Repository\GenreRepository($client);
$genre = $repository->load(28);

var_dump($genre);

$genres = $repository->loadCollection();

foreach($genres as $genre) {
var_dump($genre);
}
var_dump($genre);
15 changes: 15 additions & 0 deletions lib/Tmdb/Model/Common/GenericCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,21 @@ public function offsetUnset($offset)
unset($this->data[$offset]);
}

/**
* Filter by id
*
* @param integer $id
* @return GenericCollection
*/
public function filterId($id)
{
return $this->filter(
function($key, $value) use ($id) {
if ($value->getId() == $id) { return $value; }
}
);
}

/**
* Filter by language ISO 639-1 code.
*
Expand Down
21 changes: 4 additions & 17 deletions lib/Tmdb/Repository/GenreRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,33 +26,20 @@ class GenreRepository extends AbstractRepository {
* @return Genre
*/
public function load($id, array $parameters = array(), array $headers = array()) {
$data = $this->getApi()->getGenre($id, $this->parseQueryParameters($parameters), $this->parseHeaders($headers));

return GenreFactory::create($data);
return $this->loadCollection($parameters, $headers)->filterId($id);
}

/**
* If you obtained an genre model which is not completely hydrated, you can use this function.
* Get the list of genres.
*
* @param Genre $genre
* @param array $parameters
* @param array $headers
* @return Genre
*/
public function refresh(Genre $genre, array $parameters = array(), array $headers = array()) {
return $this->load($genre->getId(), $parameters, $headers);
}

/**
* Get the list of genres.
*
* @param array $options
* @return GenericCollection
*/
public function loadCollection(array $options = array())
public function loadCollection(array $parameters = array(), array $headers = array())
{
return $this->createCollection(
$this->getApi()->getGenres($options)
$this->getApi()->getGenres($parameters, $headers)
);
}

Expand Down

0 comments on commit a0fa24a

Please sign in to comment.