Skip to content

Commit

Permalink
Fixing up colletions, factories and cleaning up deprecated methods fr…
Browse files Browse the repository at this point in the history
…om models.
  • Loading branch information
wtfzdotnet committed Feb 4, 2014
1 parent 2d13dc5 commit 589262a
Show file tree
Hide file tree
Showing 13 changed files with 143 additions and 136 deletions.
58 changes: 58 additions & 0 deletions lib/Tmdb/Factory/ChangesFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?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 <[email protected]>
* @copyright (c) 2013, Michael Roterman
* @version 0.0.1
*/
namespace Tmdb\Factory;

use Tmdb\Model\Change;
use Tmdb\Model\Collection\Changes;

class ChangesFactory extends AbstractFactory
{
/**
* {@inheritdoc}
* @return \Tmdb\Model\Change
*/
public function create(array $data = array())
{
return $this->hydrate(new Change(), $data);
}

/**
* {@inheritdoc}
*/
public function createCollection(array $data = array())
{
$collection = new Changes();

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;
}
}
39 changes: 2 additions & 37 deletions lib/Tmdb/Factory/PeopleFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Tmdb\Factory\People\CrewFactory;
use Tmdb\Model\Collection\People\Cast;
use Tmdb\Model\Collection\People\Crew;
use Tmdb\Model\Collection\People;
use Tmdb\Model\Common\GenericCollection;
use Tmdb\Model\Person\CastMember;
use Tmdb\Model\Person\CrewMember;
Expand Down Expand Up @@ -121,7 +122,7 @@ protected function applyCredits(array $data = array(), Person $person) {
public function createCollection(array $data = array(), Person\AbstractMember $person = null, $collection = null)
{
if (!$collection) {
$collection = new GenericCollection();
$collection = new People();
}

if (array_key_exists('results', $data)) {
Expand All @@ -135,42 +136,6 @@ public function createCollection(array $data = array(), Person\AbstractMember $p
return $collection;
}

/**
* @param \Tmdb\Factory\People\CastFactory $castFactory
* @return $this
*/
public function setCastFactory($castFactory)
{
$this->castFactory = $castFactory;
return $this;
}

/**
* @return \Tmdb\Factory\People\CastFactory
*/
public function getCastFactory()
{
return $this->castFactory;
}

/**
* @param \Tmdb\Factory\People\CrewFactory $crewFactory
* @return $this
*/
public function setCrewFactory($crewFactory)
{
$this->crewFactory = $crewFactory;
return $this;
}

/**
* @return \Tmdb\Factory\People\CrewFactory
*/
public function getCrewFactory()
{
return $this->crewFactory;
}

/**
* @param \Tmdb\Factory\ImageFactory $imageFactory
* @return $this
Expand Down
4 changes: 1 addition & 3 deletions lib/Tmdb/Model/Collection/Changes.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,4 @@
*/
namespace Tmdb\Model\Collection;

use Tmdb\Model\Common\GenericCollection;

class Changes extends GenericCollection {}
class Changes extends ResultCollection {}
8 changes: 1 addition & 7 deletions lib/Tmdb/Model/Collection/Images.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,7 @@ public function getImages()
* @return null
*/
public function getImage($id) {
foreach($this->data as $image) {
if ($id === $image->getId()) {
return $image;
}
}

return null;
return $this->filterId($id);
}

/**
Expand Down
10 changes: 2 additions & 8 deletions lib/Tmdb/Model/Collection/Keywords.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,10 @@ public function getKeywords()
* Retrieve a keyword from the collection
*
* @param $id
* @return null
* @return Keyword
*/
public function getKeyword($id) {
foreach($this->data as $keyword) {
if ($id === $keyword->getId()) {
return $keyword;
}
}

return null;
return $this->filterId($id);
}

/**
Expand Down
8 changes: 1 addition & 7 deletions lib/Tmdb/Model/Collection/People.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,7 @@ public function getPeople()
* @return Person
*/
public function getPerson($id) {
foreach($this->data as $person) {
if ($id === $person->getId()) {
return $person;
}
}

return null;
return $this->filterId($id);
}

/**
Expand Down
10 changes: 8 additions & 2 deletions lib/Tmdb/Model/Common/GenericCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -310,11 +310,17 @@ public function offsetUnset($offset)
*/
public function filterId($id)
{
return $this->filter(
$result = $this->filter(
function($key, $value) use ($id) {
if ($value->getId() == $id) { return $value; }
if ($value->getId() == $id) { return true; }
}
);

if ($result && 1 === count($result)) {
return array_shift($this->data);
}

return null;
}

/**
Expand Down
36 changes: 0 additions & 36 deletions lib/Tmdb/Model/Movie.php
Original file line number Diff line number Diff line change
Expand Up @@ -687,42 +687,6 @@ public function getVoteCount()
return $this->voteCount;
}

/**
* @param People\Cast $cast
* @return $this
*/
public function setCast(People\Cast $cast)
{
$this->credits->setCast($cast);
return $this;
}

/**
* @return CastMember[]
*/
public function getCast()
{
return $this->credits->getCast();
}

/**
* @param People\Crew $crew
* @return $this
*/
public function setCrew(People\Crew $crew)
{
$this->credits->setCrew($crew);
return $this;
}

/**
* @return CrewMember[]
*/
public function getCrew()
{
return $this->credits->getCrew();
}

/**
* @param GenericCollection $alternativeTitles
* @return $this
Expand Down
29 changes: 5 additions & 24 deletions lib/Tmdb/Repository/ChangesRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@
*/
namespace Tmdb\Repository;

use Tmdb\Exception\NotImplementedException;
use Tmdb\Factory\MovieFactory;
use Tmdb\Factory\PeopleFactory;
use Tmdb\Factory\ChangesFactory;
use Tmdb\Model\Collection\People;
use Tmdb\Model\Query\ChangesQuery;

Expand All @@ -36,7 +34,7 @@ class ChangesRepository extends AbstractRepository {
public function getMovieChanges(ChangesQuery $query, array $headers = array()) {
$data = $this->getApi()->getMovieChanges($query->toArray(), $this->parseHeaders($headers));

return $this->getMovieFactory()->createCollection($data);
return $this->getFactory()->createCollection($data);
}

/**
Expand All @@ -56,7 +54,7 @@ public function getMovieChanges(ChangesQuery $query, array $headers = array()) {
public function getPeopleChanges(ChangesQuery $query, array $headers = array()) {
$data = $this->getApi()->getPersonChanges($query->toArray(), $this->parseHeaders($headers));

return $this->getPeopleFactory()->createCollection($data);
return $this->getFactory()->createCollection($data);
}

/**
Expand All @@ -69,29 +67,12 @@ public function getApi()
return $this->getClient()->getChangesApi();
}

/**
* @return PeopleFactory
*/
public function getPeopleFactory()
{
return new PeopleFactory();
}

/**
* @return MovieFactory
*/
public function getMovieFactory()
{
return new MovieFactory();
}

/**
* Changes does not support a generic factory
*
* @throws NotImplementedException
* @return null|\Tmdb\Factory\FactoryInterface
* @return ChangesFactory
*/
public function getFactory(){
throw new NotImplementedException('Discover does not support a generic factory.');
return new ChangesFactory();
}
}
63 changes: 63 additions & 0 deletions test/Tmdb/Tests/Factory/ChangesFactoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?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 <[email protected]>
* @copyright (c) 2013, Michael Roterman
* @version 0.0.1
*/
namespace Tmdb\Tests\Factory;

use Tmdb\Factory\CompanyFactory;
use Tmdb\Model\Collection\Changes;
use Tmdb\Model\Company;

class ChangesFactoryTest extends TestCase
{
const COMPANY_ID = 1;

private $data;

/**
* @var Changes
*/
private $changes;

public function setUp()
{
$this->data = $this->loadByFile('changes/movies.json');

/**
* @var CompanyFactory $factory
*/
$factory = $this->getFactory();

/**
* @var Company $company
*/
$this->changes = $factory->createCollection($this->data);
}

/**
* @test
*/
public function shouldCreateCollection()
{
$this->assertEquals(true, !empty($this->changes));

$this->assertEquals(1, $this->changes->getPage());
$this->assertEquals(12, $this->changes->getTotalPages());
$this->assertEquals(1151, $this->changes->getTotalResults());

$this->assertEquals(100, count($this->changes));
}

protected function getFactoryClass()
{
return 'Tmdb\Factory\ChangesFactory';
}
}
1 change: 1 addition & 0 deletions test/Tmdb/Tests/Factory/MovieFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ public function shouldBeFunctional()
$this->assertInstanceOf('Tmdb\Model\Collection\Genres', $this->movie->getGenres());
$this->assertEquals('http://www.riddick-movie.com', $this->movie->getHomepage());
$this->assertEquals(87421, $this->movie->getId());
$this->assertEquals('Riddick', $this->movie->getTitle());
$this->assertEquals('Riddick', $this->movie->getOriginalTitle());
$this->assertEquals('Betrayed by his own kind and left for dead on a desolate planet, Riddick fights for survival against alien predators and becomes more powerful and dangerous than ever before. Soon bounty hunters from throughout the galaxy descend on Riddick only to find themselves pawns in his greater scheme for revenge. With his enemies right where he wants them, Riddick unleashes a vicious attack of vengeance before returning to his home planet of Furya to save it from destruction.', $this->movie->getOverview());
$this->assertEquals(93.491722439366, $this->movie->getPopularity());
Expand Down
12 changes: 0 additions & 12 deletions test/Tmdb/Tests/Repository/ChangesRepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,6 @@ public function shouldGetPeopleChanges()
$repository->getPeopleChanges($query);
}

/**
* There is no generic factory for changes so it should never be called.
*
* @expectedException Tmdb\Exception\NotImplementedException
* @test
*/
public function getFactoryShouldThrowException()
{
$repository = $this->getRepositoryWithMockedHttpClient();
$repository->getFactory();
}

protected function getApiClass()
{
return 'Tmdb\Api\Changes';
Expand Down
1 change: 1 addition & 0 deletions test/Tmdb/Tests/Resources/changes/movies.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"results":[{"id":253759,"adult":false},{"id":243914,"adult":false},{"id":14324,"adult":false},{"id":253760,"adult":false},{"id":164159,"adult":false},{"id":253761,"adult":false},{"id":10315,"adult":false},{"id":44942,"adult":false},{"id":50457,"adult":false},{"id":15037,"adult":false},{"id":11926,"adult":false},{"id":22798,"adult":false},{"id":44215,"adult":false},{"id":93053,"adult":false},{"id":53505,"adult":false},{"id":122662,"adult":false},{"id":253762,"adult":false},{"id":19850,"adult":false},{"id":47088,"adult":false},{"id":17680,"adult":false},{"id":15285,"adult":false},{"id":40722,"adult":false},{"id":43943,"adult":false},{"id":33542,"adult":false},{"id":251865,"adult":false},{"id":46837,"adult":false},{"id":253763,"adult":false},{"id":47407,"adult":false},{"id":7839,"adult":false},{"id":11831,"adult":false},{"id":20618,"adult":false},{"id":5548,"adult":false},{"id":50687,"adult":false},{"id":205724,"adult":false},{"id":253764,"adult":false},{"id":603,"adult":false},{"id":605,"adult":false},{"id":253765,"adult":false},{"id":62177,"adult":false},{"id":253766,"adult":false},{"id":253767,"adult":false},{"id":253768,"adult":false},{"id":253276,"adult":false},{"id":253769,"adult":false},{"id":253770,"adult":false},{"id":253771,"adult":false},{"id":6973,"adult":false},{"id":389,"adult":false},{"id":253772,"adult":false},{"id":77459,"adult":false},{"id":253773,"adult":false},{"id":53913,"adult":false},{"id":253714,"adult":false},{"id":253774,"adult":false},{"id":253775,"adult":false},{"id":253776,"adult":false},{"id":253511,"adult":false},{"id":253777,"adult":false},{"id":10378,"adult":false},{"id":100402,"adult":false},{"id":253778,"adult":false},{"id":253779,"adult":false},{"id":253780,"adult":false},{"id":253781,"adult":false},{"id":57619,"adult":false},{"id":10013,"adult":false},{"id":25566,"adult":false},{"id":253782,"adult":false},{"id":126838,"adult":false},{"id":109513,"adult":false},{"id":253783,"adult":false},{"id":16632,"adult":false},{"id":51730,"adult":false},{"id":133805,"adult":false},{"id":253784,"adult":false},{"id":152747,"adult":false},{"id":204709,"adult":false},{"id":126755,"adult":false},{"id":9924,"adult":false},{"id":58559,"adult":false},{"id":120932,"adult":false},{"id":41428,"adult":false},{"id":200713,"adult":false},{"id":157370,"adult":false},{"id":97199,"adult":false},{"id":157841,"adult":false},{"id":240745,"adult":false},{"id":84659,"adult":false},{"id":253785,"adult":false},{"id":36810,"adult":false},{"id":250482,"adult":false},{"id":17979,"adult":false},{"id":253786,"adult":false},{"id":6575,"adult":false},{"id":122906,"adult":false},{"id":80797,"adult":false},{"id":166076,"adult":false},{"id":15301,"adult":false},{"id":50620,"adult":false},{"id":50619,"adult":false}],"page":1,"total_pages":12,"total_results":1151}

0 comments on commit 589262a

Please sign in to comment.