From 2d13dc5f9391fedb943d636b8e22c5b4cb764c3e Mon Sep 17 00:00:00 2001 From: Michael Roterman Date: Tue, 4 Feb 2014 00:11:58 +0100 Subject: [PATCH] Adjusting credits dependency to latest API changes. --- lib/Tmdb/Common/ObjectHydrator.php | 2 +- lib/Tmdb/Factory/PeopleFactory.php | 95 +++++++++++- lib/Tmdb/Model/Collection/Changes.php | 4 +- .../Collection/Credits/CombinedCredits.php | 17 +++ .../Model/Collection/Credits/MovieCredits.php | 17 +++ .../Model/Collection/Credits/TvCredits.php | 17 +++ lib/Tmdb/Model/Person.php | 140 +++++++++++++++--- test/Tmdb/Tests/Factory/MovieFactoryTest.php | 2 +- test/Tmdb/Tests/Factory/PeopleFactoryTest.php | 51 ++++++- test/Tmdb/Tests/Model/PersonTest.php | 26 +++- 10 files changed, 332 insertions(+), 39 deletions(-) create mode 100644 lib/Tmdb/Model/Collection/Credits/CombinedCredits.php create mode 100644 lib/Tmdb/Model/Collection/Credits/MovieCredits.php create mode 100644 lib/Tmdb/Model/Collection/Credits/TvCredits.php diff --git a/lib/Tmdb/Common/ObjectHydrator.php b/lib/Tmdb/Common/ObjectHydrator.php index 1b9a73b7..abada7e4 100644 --- a/lib/Tmdb/Common/ObjectHydrator.php +++ b/lib/Tmdb/Common/ObjectHydrator.php @@ -64,7 +64,7 @@ public function hydrate(AbstractModel $object, $data = array()) * @param string $candidate * @return string */ - private function camelize($candidate) + public function camelize($candidate) { return lcfirst( implode('', diff --git a/lib/Tmdb/Factory/PeopleFactory.php b/lib/Tmdb/Factory/PeopleFactory.php index d1fcde1a..5c001736 100644 --- a/lib/Tmdb/Factory/PeopleFactory.php +++ b/lib/Tmdb/Factory/PeopleFactory.php @@ -12,6 +12,11 @@ */ namespace Tmdb\Factory; +use Tmdb\Common\ObjectHydrator; +use Tmdb\Factory\People\CastFactory; +use Tmdb\Factory\People\CrewFactory; +use Tmdb\Model\Collection\People\Cast; +use Tmdb\Model\Collection\People\Crew; use Tmdb\Model\Common\GenericCollection; use Tmdb\Model\Person\CastMember; use Tmdb\Model\Person\CrewMember; @@ -23,6 +28,16 @@ class PeopleFactory extends AbstractFactory { */ private $imageFactory; + /** + * @var People\CastFactory + */ + private $castFactory; + + /** + * @var People\CrewFactory + */ + private $crewFactory; + /** * Constructor */ @@ -59,15 +74,55 @@ public function create(array $data = array(), Person\AbstractMember $person = nu $person->setProfile($this->getImageFactory()->createFromPath($data['profile_path'], 'profile_path')); } + /** Credits */ + if ($person instanceof Person) { + $this->applyCredits($data, $person); + } + return $this->hydrate($person, $data); } + /** + * Apply credits + * + * @param array $data + * @param Person $person + */ + protected function applyCredits(array $data = array(), Person $person) { + $hydrator = new ObjectHydrator(); + $types = array('movie_credits', 'tv_credits', 'combined_credits'); + + foreach($types as $type) { + if (array_key_exists($type, $data)) { + $method = $hydrator->camelize(sprintf('get_%s', $type)); + + if (array_key_exists('cast', $data[$type])) { + $person->$method()->setCast($this->createCollection( + $data[$type]['cast'], + new CastMember(), + new Cast()) + ); + } + + if (array_key_exists('crew', $data[$type])) { + $person->$method()->setCrew($this->createCollection( + $data[$type]['crew'], + new CrewMember(), + new Crew()) + ); + } + } + } + } + /** * {@inheritdoc} */ - public function createCollection(array $data = array(), Person\AbstractMember $person = null) + public function createCollection(array $data = array(), Person\AbstractMember $person = null, $collection = null) { - $collection = new GenericCollection(); + if (!$collection) { + $collection = new GenericCollection(); + } if (array_key_exists('results', $data)) { $data = $data['results']; @@ -80,6 +135,42 @@ 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 e57796b9..9b9b3077 100644 --- a/lib/Tmdb/Model/Collection/Changes.php +++ b/lib/Tmdb/Model/Collection/Changes.php @@ -10,8 +10,8 @@ * @copyright (c) 2013, Michael Roterman * @version 0.0.1 */ -namespace Tmdb\Model\Common\Collection; +namespace Tmdb\Model\Collection; use Tmdb\Model\Common\GenericCollection; -class Images extends GenericCollection {} +class Changes extends GenericCollection {} diff --git a/lib/Tmdb/Model/Collection/Credits/CombinedCredits.php b/lib/Tmdb/Model/Collection/Credits/CombinedCredits.php new file mode 100644 index 00000000..a94d8ace --- /dev/null +++ b/lib/Tmdb/Model/Collection/Credits/CombinedCredits.php @@ -0,0 +1,17 @@ + + * @copyright (c) 2013, Michael Roterman + * @version 0.0.1 + */ +namespace Tmdb\Model\Collection\Credits; + +use Tmdb\Model\Collection\Credits; + +class CombinedCredits extends Credits {} diff --git a/lib/Tmdb/Model/Collection/Credits/MovieCredits.php b/lib/Tmdb/Model/Collection/Credits/MovieCredits.php new file mode 100644 index 00000000..2430ab43 --- /dev/null +++ b/lib/Tmdb/Model/Collection/Credits/MovieCredits.php @@ -0,0 +1,17 @@ + + * @copyright (c) 2013, Michael Roterman + * @version 0.0.1 + */ +namespace Tmdb\Model\Collection\Credits; + +use Tmdb\Model\Collection\Credits; + +class MovieCredits extends Credits {} diff --git a/lib/Tmdb/Model/Collection/Credits/TvCredits.php b/lib/Tmdb/Model/Collection/Credits/TvCredits.php new file mode 100644 index 00000000..70c2e852 --- /dev/null +++ b/lib/Tmdb/Model/Collection/Credits/TvCredits.php @@ -0,0 +1,17 @@ + + * @copyright (c) 2013, Michael Roterman + * @version 0.0.1 + */ +namespace Tmdb\Model\Collection\Credits; + +use Tmdb\Model\Collection\Credits; + +class TvCredits extends Credits {} diff --git a/lib/Tmdb/Model/Person.php b/lib/Tmdb/Model/Person.php index 13158b59..6b893615 100644 --- a/lib/Tmdb/Model/Person.php +++ b/lib/Tmdb/Model/Person.php @@ -20,22 +20,89 @@ class Person extends AbstractModel implements PersonInterface { + /** + * @var bool + */ private $adult; + + /** + * @var array + */ private $alsoKnownAs = array(); + + /** + * @var string + */ private $biography; + /** + * @var \DateTime + */ private $birthday; + + /** + * @var \DateTime|boolean + */ private $deathday; + + /** + * @var string + */ private $homepage; + + /** + * @var integer + */ private $id; + + /** + * @var string + */ private $name; - private $department; - private $job; + + /** + * @var string + */ private $placeOfBirth; + + /** + * @var string + */ private $profilePath; + + /** + * @var ProfileImage + */ private $profile; + /** + * @var Collection\Credits + * @deprecated + */ protected $credits; + + /** + * @var Credits\MovieCredits + */ + protected $movieCredits; + + /** + * @var Credits\TvCredits + */ + protected $tvCredits; + + /** + * @var Credits\CombinedCredits + */ + protected $combinedCredits; + + /** + * @var Collection\Images + */ protected $images; + + /** + * @var Common\GenericCollection + */ protected $changes; public static $_properties = array( @@ -58,9 +125,12 @@ class Person extends AbstractModel implements PersonInterface { */ public function __construct() { - $this->credits = new Credits(); - $this->images = new Images(); - $this->changes = new GenericCollection(); + $this->credits = new Credits(); + $this->movieCredits = new Credits\MovieCredits(); + $this->tvCredits = new Credits\TvCredits(); + $this->combinedCredits = new Credits\CombinedCredits(); + $this->images = new Images(); + $this->changes = new GenericCollection(); } /** @@ -181,10 +251,14 @@ public function getCredits() */ public function setDeathday($deathday) { - if (!$deathday instanceof \DateTime) { + if (!$deathday instanceof \DateTime && !empty($deathday)) { $deathday = new \DateTime($deathday); } + if (empty($deathday)) { + $deathday = false; + } + $this->deathday = $deathday; return $this; } @@ -306,56 +380,74 @@ public function getProfilePath() } /** - * @param mixed $department + * @param ProfileImage $profile * @return $this */ - public function setDepartment($department) + public function setProfile(ProfileImage $profile) { - $this->department = $department; + $this->profile = $profile; return $this; } /** - * @return mixed + * @return ProfileImage + */ + public function getProfile() + { + return $this->profile; + } + + /** + * @param \Tmdb\Model\Collection\Credits\CombinedCredits $combinedCredits + * @return $this */ - public function getDepartment() + public function setCombinedCredits($combinedCredits) { - return $this->department; + $this->combinedCredits = $combinedCredits; + return $this; } /** - * @param mixed $job + * @return \Tmdb\Model\Collection\Credits\CombinedCredits + */ + public function getCombinedCredits() + { + return $this->combinedCredits; + } + + /** + * @param \Tmdb\Model\Collection\Credits\MovieCredits $movieCredits * @return $this */ - public function setJob($job) + public function setMovieCredits($movieCredits) { - $this->job = $job; + $this->movieCredits = $movieCredits; return $this; } /** - * @return mixed + * @return \Tmdb\Model\Collection\Credits\MovieCredits */ - public function getJob() + public function getMovieCredits() { - return $this->job; + return $this->movieCredits; } /** - * @param ProfileImage $profile + * @param \Tmdb\Model\Collection\Credits\TvCredits $tvCredits * @return $this */ - public function setProfile(ProfileImage $profile) + public function setTvCredits($tvCredits) { - $this->profile = $profile; + $this->tvCredits = $tvCredits; return $this; } /** - * @return ProfileImage + * @return \Tmdb\Model\Collection\Credits\TvCredits */ - public function getProfile() + public function getTvCredits() { - return $this->profile; + return $this->tvCredits; } } diff --git a/test/Tmdb/Tests/Factory/MovieFactoryTest.php b/test/Tmdb/Tests/Factory/MovieFactoryTest.php index 8ce0b71c..a190b65f 100644 --- a/test/Tmdb/Tests/Factory/MovieFactoryTest.php +++ b/test/Tmdb/Tests/Factory/MovieFactoryTest.php @@ -48,7 +48,7 @@ public function shouldConstructMovie() */ public function shouldBeAbleToSetFactories() { - $factory = new MovieFactory(); + $factory = $this->getFactory(); $class = new \stdClass(); diff --git a/test/Tmdb/Tests/Factory/PeopleFactoryTest.php b/test/Tmdb/Tests/Factory/PeopleFactoryTest.php index 534248ad..ec32e3c3 100644 --- a/test/Tmdb/Tests/Factory/PeopleFactoryTest.php +++ b/test/Tmdb/Tests/Factory/PeopleFactoryTest.php @@ -20,9 +20,11 @@ class PeopleFactoryTest extends TestCase { /** - * @test + * @var Person */ - public function shouldConstructPerson() + private $person; + + public function setUp() { /** * @var PeopleFactory $factory @@ -30,15 +32,23 @@ public function shouldConstructPerson() $factory = $this->getFactory(); $data = $this->loadByFile('person/get.json'); + $data['biography'] = 'external'; + /** * @var Person $person */ - $person = $factory->create($data); + $this->person = $factory->create($data); + } - $this->assertInstanceOf('Tmdb\Model\Person', $person); + /** + * @test + */ + public function shouldConstructPerson() + { + $this->assertInstanceOf('Tmdb\Model\Person', $this->person); - $this->assertInstanceOf('Tmdb\Model\Collection\Images', $person->getImages()); - $this->assertInstanceOf('Tmdb\Model\Image\ProfileImage', $person->getProfile()); + $this->assertInstanceOf('Tmdb\Model\Collection\Images', $this->person->getImages()); + $this->assertInstanceOf('Tmdb\Model\Image\ProfileImage', $this->person->getProfile()); } /** @@ -97,6 +107,35 @@ public function shouldBeAbleToDissectResults() $this->assertEquals(2, count($collection)); } + /** + * @test + */ + public function shouldBeFunctional() + { + $alsoKnownAs = $this->person->getAlsoKnownAs(); + + $this->assertEquals(false, $this->person->getAdult()); + $this->assertEquals(true, empty($alsoKnownAs)); + $this->assertEquals('external', $this->person->getBiography()); + $this->assertInstanceOf('\DateTime', $this->person->getBirthday()); + $this->assertEquals(false, $this->person->getDeathday()); + $this->assertEquals('', $this->person->getHomepage()); + $this->assertEquals(33, $this->person->getId()); + //@todo + //$this->assertEquals('nm0000641', $this->person->getImdbId()); + $this->assertEquals('Gary Sinise', $this->person->getName()); + $this->assertEquals('Blue Island, Illinois, USA', $this->person->getPlaceOfBirth()); + //@todo + //$this->assertEquals(1.99498054250796, $this->person->getPopularity()); + $this->assertInstanceOf('Tmdb\Model\Image\ProfileImage', $this->person->getProfile()); + $this->assertEquals('/h9YwlLHANaQzaTVkVwxnxLbvCY4.jpg', $this->person->getProfilePath()); + $this->assertInstanceOf('Tmdb\Model\Collection\Images', $this->person->getImages()); + $this->assertInstanceOf('Tmdb\Model\Common\GenericCollection', $this->person->getChanges()); + $this->assertInstanceOf('Tmdb\Model\Collection\Credits\CombinedCredits', $this->person->getCombinedCredits()); + $this->assertInstanceOf('Tmdb\Model\Collection\Credits\MovieCredits', $this->person->getMovieCredits()); + $this->assertInstanceOf('Tmdb\Model\Collection\Credits\TvCredits', $this->person->getTvCredits()); + } + protected function getFactoryClass() { return 'Tmdb\Factory\PeopleFactory'; diff --git a/test/Tmdb/Tests/Model/PersonTest.php b/test/Tmdb/Tests/Model/PersonTest.php index 424101fc..8f67320f 100644 --- a/test/Tmdb/Tests/Model/PersonTest.php +++ b/test/Tmdb/Tests/Model/PersonTest.php @@ -26,10 +26,30 @@ public function shouldConstructGenres() $this->assertInstancesOf( $person, array( - 'getCredits' => 'Tmdb\Model\Collection\Credits', - 'getImages' => 'Tmdb\Model\Collection\Images', - 'getChanges' => 'Tmdb\Model\Common\GenericCollection' + 'getCredits' => 'Tmdb\Model\Collection\Credits', + 'getImages' => 'Tmdb\Model\Collection\Images', + 'getChanges' => 'Tmdb\Model\Common\GenericCollection', + 'getCombinedCredits' => 'Tmdb\Model\Collection\Credits\CombinedCredits', + 'getMovieCredits' => 'Tmdb\Model\Collection\Credits\MovieCredits', + 'getTvCredits' => 'Tmdb\Model\Collection\Credits\TvCredits', ) ); } + + /** + * @test + */ + public function shouldBeAbleToReplaceCollections() + { + $factory = new Person(); + $class = new \stdClass(); + + $factory->setCombinedCredits($class); + $factory->setMovieCredits($class); + $factory->setTvCredits($class); + + $this->assertInstanceOf('stdClass', $factory->getCombinedCredits()); + $this->assertInstanceOf('stdClass', $factory->getMovieCredits()); + $this->assertInstanceOf('stdClass', $factory->getTvCredits()); + } } \ No newline at end of file