From 589262abf10fd579eb15328e9dce438c3b3e32c9 Mon Sep 17 00:00:00 2001 From: Michael Roterman Date: Tue, 4 Feb 2014 01:13:43 +0100 Subject: [PATCH] Fixing up colletions, factories and cleaning up deprecated methods from models. --- lib/Tmdb/Factory/ChangesFactory.php | 58 +++++++++++++++++ lib/Tmdb/Factory/PeopleFactory.php | 39 +----------- lib/Tmdb/Model/Collection/Changes.php | 4 +- lib/Tmdb/Model/Collection/Images.php | 8 +-- lib/Tmdb/Model/Collection/Keywords.php | 10 +-- lib/Tmdb/Model/Collection/People.php | 8 +-- lib/Tmdb/Model/Common/GenericCollection.php | 10 ++- lib/Tmdb/Model/Movie.php | 36 ----------- lib/Tmdb/Repository/ChangesRepository.php | 29 ++------- .../Tmdb/Tests/Factory/ChangesFactoryTest.php | 63 +++++++++++++++++++ test/Tmdb/Tests/Factory/MovieFactoryTest.php | 1 + .../Repository/ChangesRepositoryTest.php | 12 ---- test/Tmdb/Tests/Resources/changes/movies.json | 1 + 13 files changed, 143 insertions(+), 136 deletions(-) create mode 100644 lib/Tmdb/Factory/ChangesFactory.php create mode 100644 test/Tmdb/Tests/Factory/ChangesFactoryTest.php create mode 100644 test/Tmdb/Tests/Resources/changes/movies.json diff --git a/lib/Tmdb/Factory/ChangesFactory.php b/lib/Tmdb/Factory/ChangesFactory.php new file mode 100644 index 00000000..9147ebc9 --- /dev/null +++ b/lib/Tmdb/Factory/ChangesFactory.php @@ -0,0 +1,58 @@ + + * @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; + } +} diff --git a/lib/Tmdb/Factory/PeopleFactory.php b/lib/Tmdb/Factory/PeopleFactory.php index 5c001736..322b2e24 100644 --- a/lib/Tmdb/Factory/PeopleFactory.php +++ b/lib/Tmdb/Factory/PeopleFactory.php @@ -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; @@ -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)) { @@ -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 diff --git a/lib/Tmdb/Model/Collection/Changes.php b/lib/Tmdb/Model/Collection/Changes.php index 9b9b3077..ff37e8dd 100644 --- a/lib/Tmdb/Model/Collection/Changes.php +++ b/lib/Tmdb/Model/Collection/Changes.php @@ -12,6 +12,4 @@ */ namespace Tmdb\Model\Collection; -use Tmdb\Model\Common\GenericCollection; - -class Changes extends GenericCollection {} +class Changes extends ResultCollection {} diff --git a/lib/Tmdb/Model/Collection/Images.php b/lib/Tmdb/Model/Collection/Images.php index 35e7b18c..17f2a631 100644 --- a/lib/Tmdb/Model/Collection/Images.php +++ b/lib/Tmdb/Model/Collection/Images.php @@ -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); } /** diff --git a/lib/Tmdb/Model/Collection/Keywords.php b/lib/Tmdb/Model/Collection/Keywords.php index d76e0c96..4977dfac 100644 --- a/lib/Tmdb/Model/Collection/Keywords.php +++ b/lib/Tmdb/Model/Collection/Keywords.php @@ -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); } /** diff --git a/lib/Tmdb/Model/Collection/People.php b/lib/Tmdb/Model/Collection/People.php index 6f46467f..baacf8fa 100644 --- a/lib/Tmdb/Model/Collection/People.php +++ b/lib/Tmdb/Model/Collection/People.php @@ -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); } /** diff --git a/lib/Tmdb/Model/Common/GenericCollection.php b/lib/Tmdb/Model/Common/GenericCollection.php index a4c25f40..6001743e 100644 --- a/lib/Tmdb/Model/Common/GenericCollection.php +++ b/lib/Tmdb/Model/Common/GenericCollection.php @@ -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; } /** diff --git a/lib/Tmdb/Model/Movie.php b/lib/Tmdb/Model/Movie.php index b372af99..a6a183df 100644 --- a/lib/Tmdb/Model/Movie.php +++ b/lib/Tmdb/Model/Movie.php @@ -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 diff --git a/lib/Tmdb/Repository/ChangesRepository.php b/lib/Tmdb/Repository/ChangesRepository.php index a1d039f1..4fe177a5 100644 --- a/lib/Tmdb/Repository/ChangesRepository.php +++ b/lib/Tmdb/Repository/ChangesRepository.php @@ -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; @@ -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); } /** @@ -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); } /** @@ -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(); } } diff --git a/test/Tmdb/Tests/Factory/ChangesFactoryTest.php b/test/Tmdb/Tests/Factory/ChangesFactoryTest.php new file mode 100644 index 00000000..b3c0cb59 --- /dev/null +++ b/test/Tmdb/Tests/Factory/ChangesFactoryTest.php @@ -0,0 +1,63 @@ + + * @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'; + } +} \ No newline at end of file diff --git a/test/Tmdb/Tests/Factory/MovieFactoryTest.php b/test/Tmdb/Tests/Factory/MovieFactoryTest.php index a190b65f..46eb858a 100644 --- a/test/Tmdb/Tests/Factory/MovieFactoryTest.php +++ b/test/Tmdb/Tests/Factory/MovieFactoryTest.php @@ -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()); diff --git a/test/Tmdb/Tests/Repository/ChangesRepositoryTest.php b/test/Tmdb/Tests/Repository/ChangesRepositoryTest.php index e4d86259..0dc0499d 100644 --- a/test/Tmdb/Tests/Repository/ChangesRepositoryTest.php +++ b/test/Tmdb/Tests/Repository/ChangesRepositoryTest.php @@ -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'; diff --git a/test/Tmdb/Tests/Resources/changes/movies.json b/test/Tmdb/Tests/Resources/changes/movies.json new file mode 100644 index 00000000..3696a438 --- /dev/null +++ b/test/Tmdb/Tests/Resources/changes/movies.json @@ -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} \ No newline at end of file