diff --git a/lib/Tmdb/Factory/AbstractFactory.php b/lib/Tmdb/Factory/AbstractFactory.php index 4d548154..ae22f943 100644 --- a/lib/Tmdb/Factory/AbstractFactory.php +++ b/lib/Tmdb/Factory/AbstractFactory.php @@ -12,6 +12,10 @@ */ namespace Tmdb\Factory; +use Tmdb\Exception\RuntimeException; +use Tmdb\Model\AbstractModel; +use Tmdb\Model\Common\QueryParameter\QueryParameterInterface; + abstract class AbstractFactory { /** * Convert an array to an hydrated object @@ -28,4 +32,95 @@ abstract public static function create(array $data = array()); * @return $this */ abstract public static function createCollection(array $data = array()); + + /** + * Process query parameters + * + * @param array $parameters + * @return array + */ + protected function parseQueryParameters(array $parameters = array()) + { + foreach($parameters as $key => $candidate) { + if ($candidate instanceof QueryParameterInterface) { + unset($parameters[$key]); + + $parameters[$candidate->getKey()] = $candidate->getValue(); + } + } + + return $parameters; + } + + /** + * Hydrate the object with data + * + * @param AbstractModel $object + * @param array $data + * @return $this + * @throws RuntimeException + */ + public function hydrate(AbstractModel $object, array $data = array()) + { + if (!empty($data)) { + foreach ($data as $k => $v) { + if (in_array($k, $object::$_properties)) { + + $method = self::camelize( + sprintf('set_%s', $k) + ); + + if (!method_exists($object, $method)) { + throw new RuntimeException(sprintf( + 'Trying to call method "%s" on "%s" but it does not exist or is private.', + $method, + get_class($object) + )); + } + + $object->$method($v); + } + } + } + + return $object; + } + + /** + * Transforms an under_scored_string to a camelCasedOne + * + * @see https://gist.github.com/troelskn/751517 + * + * @param $candidate + * @return string + */ + private function camelize($candidate) + { + return lcfirst( + implode('', + array_map('ucfirst', + array_map('strtolower', + explode('_', $candidate + ) + ) + ) + ) + ); + } + + /** + * Transforms a camelCasedString to an under_scored_one + * + * @see https://gist.github.com/troelskn/751517 + * + * @param $camelized + * @return string + */ + private function uncamelize($camelized) { + return implode('_', + array_map('strtolower', + preg_split('/([A-Z]{1}[^A-Z]*)/', $camelized, -1, PREG_SPLIT_DELIM_CAPTURE|PREG_SPLIT_NO_EMPTY) + ) + ); + } } \ No newline at end of file diff --git a/lib/Tmdb/Factory/Common/ImageFactory.php b/lib/Tmdb/Factory/Common/ImageFactory.php new file mode 100644 index 00000000..7bc04558 --- /dev/null +++ b/lib/Tmdb/Factory/Common/ImageFactory.php @@ -0,0 +1,63 @@ + + * @copyright (c) 2013, Michael Roterman + * @version 0.0.1 + */ +namespace Tmdb\Factory\Common; + +use Tmdb\Factory\AbstractFactory; +use Tmdb\Model\Common\Collection\Images; +use Tmdb\Model\Image; + +class ImageFactory extends AbstractFactory +{ + /** + * {@inheritdoc} + */ + public static function create(array $data = array()) + { + return parent::hydrate(new Image(), $data); + } + + /** + * {@inheritdoc} + */ + public static function createCollection(array $data = array()) + { + $collection = new Images(); + + foreach($data as $item) { + $collection->add(null, self::create($item)); + } + + return $collection; + } + + /** + * {@inheritdoc} + */ + public static function createCollectionFromMovie(array $data = array()) + { + $collection = array(); + + foreach($data as $format => $formatCollection) { + foreach($formatCollection as $item) { + if (array_key_exists($format, Image::$_formats)) { + $item['format'] = Image::$_formats[$format]; + + $collection[] = $item; + } + } + } + + return self::createCollection($collection); + } + +} \ No newline at end of file diff --git a/lib/Tmdb/Factory/GenreFactory.php b/lib/Tmdb/Factory/GenreFactory.php index 1832c812..33b96075 100644 --- a/lib/Tmdb/Factory/GenreFactory.php +++ b/lib/Tmdb/Factory/GenreFactory.php @@ -12,18 +12,18 @@ */ namespace Tmdb\Factory; +use Tmdb\Client; use Tmdb\Model\Collection\Genres; use Tmdb\Model\Genre; -class GenreFactory { +class GenreFactory extends AbstractFactory +{ /** * {@inheritdoc} */ public static function create(array $data = array()) { - $genre = new Genre(); - - return $genre->hydrate($data); + return parent::hydrate(new Genre(), $data); } /** @@ -40,4 +40,18 @@ public static function createCollection(array $data = array()) return $collection; } + /** + * Load a genre with the given identifier + * + * @param Client $client + * @param $id + * @param $parameters + * @return $this + */ + public static function load(Client $client, $id, array $parameters = array()) { + $data = $client->api('genres')->getGenre($id, parent::parseQueryParameters($parameters)); + + return self::create($data); + } + } \ No newline at end of file diff --git a/lib/Tmdb/Factory/MovieFactory.php b/lib/Tmdb/Factory/MovieFactory.php index 4a4a8800..482c9842 100644 --- a/lib/Tmdb/Factory/MovieFactory.php +++ b/lib/Tmdb/Factory/MovieFactory.php @@ -13,6 +13,8 @@ namespace Tmdb\Factory; use Tmdb\Api\Movies; +use Tmdb\Client; +use Tmdb\Factory\Common\ImageFactory; use Tmdb\Model\Common\Collection; use Tmdb\Model\Movie; @@ -28,41 +30,41 @@ public static function create(array $data = array()) $movie = new Movie(); - if (array_key_exists('alternative_titles', $data) && array_key_exists('titles', $data['alternative_titles'])) { - $movie->setAlternativeTitles(Movie::collectGenericCollection($client, $data['alternative_titles']['titles'], new AlternativeTitle())); - } +// if (array_key_exists('alternative_titles', $data) && array_key_exists('titles', $data['alternative_titles'])) { +// $movie->setAlternativeTitles(Movie::collectGenericCollection($client, $data['alternative_titles']['titles'], new AlternativeTitle())); +// } $casts = array(); $credits = $movie->getCredits(); /** Credits */ - if (array_key_exists('credits', $data)) { - $casts = $data['credits']; - } - - if (array_key_exists('casts', $data)) { - $casts = $data['casts']; - } - - if (array_key_exists('cast', $casts)) { - $credits->setCast(parent::collectCast($client, $casts['cast'])); - } - - if (array_key_exists('crew', $casts)) { - $credits->setCrew(parent::collectCrew($client, $casts['crew'])); - } - - $movie->setCredits($credits); +// if (array_key_exists('credits', $data)) { +// $casts = $data['credits']; +// } +// +// if (array_key_exists('casts', $data)) { +// $casts = $data['casts']; +// } +// +// if (array_key_exists('cast', $casts)) { +// $credits->setCast($casts['cast']); +// } // +// if (array_key_exists('crew', $casts)) { +// $credits->setCrew($casts['crew']); +// } +// +// $movie->setCredits($credits); + /** Genres */ if (array_key_exists('genres', $data)) { $movie->setGenres(GenreFactory::createCollection($data['genres'])); } -// -// /** Images */ -// if (array_key_exists('images', $data)) { -// $movie->setImages(parent::collectImages($client, $data['images'])); -// } + + /** Images */ + if (array_key_exists('images', $data)) { + $movie->setImages(ImageFactory::createCollectionFromMovie($data['images'])); + } // // /** Keywords */ // if (array_key_exists('keywords', $data)) { @@ -89,7 +91,7 @@ public static function create(array $data = array()) // if (array_key_exists('changes', $data)) { // } - return $movie->hydrate($data); + return parent::hydrate($movie, $data); } /** @@ -105,4 +107,20 @@ public static function createCollection(array $data = array()) return $collection; } + + + /** + * Load a movie with the given identifier + * + * @param Client $client + * @param $id + * @param $parameters + * @return $this + */ + public static function load(Client $client, $id, array $parameters = array()) { + $data = $client->api('movies')->getMovie($id, parent::parseQueryParameters($parameters)); + + return self::create($data); + } + } \ No newline at end of file diff --git a/lib/Tmdb/Model/AbstractModel.php b/lib/Tmdb/Model/AbstractModel.php index 2888f459..4f2eb7ec 100644 --- a/lib/Tmdb/Model/AbstractModel.php +++ b/lib/Tmdb/Model/AbstractModel.php @@ -13,22 +13,9 @@ namespace Tmdb\Model; use Tmdb\Client; -use Tmdb\Exception\RuntimeException; - -use Tmdb\Model\Collection\Credits\Cast; -use Tmdb\Model\Collection\Credits\Crew; -use Tmdb\Model\Collection\Genres; -use Tmdb\Model\Collection\People; - -use Tmdb\Model\Common\Collection\Images; - -use Tmdb\Model\Common\Collection; -use Tmdb\Model\Common\QueryParameter\QueryParameterInterface; -use Tmdb\Model\Person\CastMember; -use Tmdb\Model\Person\CrewMember; class AbstractModel { - protected static $_properties; + public static $_properties; protected $_data = array(); protected $_client = null; @@ -69,94 +56,4 @@ public function api($api) return $this->getClient()->api($api); } - /** - * Hydrate the object with data - * - * @param array $data - * @return $this - * @throws \Tmdb\Exception\RuntimeException - */ - public function hydrate(array $data = array()) - { - if (!empty($data)) { - foreach ($data as $k => $v) { - if (in_array($k, static::$_properties)) { - - $method = $this->camelize( - sprintf('set_%s', $k) - ); - - if (!method_exists($this, $method)) { - throw new RuntimeException(sprintf( - 'Trying to call method "%s" on "%s" but it does not exist or is private.', - $method, - get_class($this) - )); - } - - $this->$method($v); - } - } - } - - return $this; - } - - /** - * Process query parameters - * - * @param array $parameters - * @return array - */ - protected function parseQueryParameters(array $parameters = array()) - { - foreach($parameters as $key => $candidate) { - if ($candidate instanceof QueryParameterInterface) { - unset($parameters[$key]); - - $parameters[$candidate->getKey()] = $candidate->getValue(); - } - } - - return $parameters; - } - - /** - * Transforms an under_scored_string to a camelCasedOne - * - * @see https://gist.github.com/troelskn/751517 - * - * @param $candidate - * @return string - */ - private function camelize($candidate) - { - return lcfirst( - implode('', - array_map('ucfirst', - array_map('strtolower', - explode('_', $candidate - ) - ) - ) - ) - ); - } - - /** - * Transforms a camelCasedString to an under_scored_one - * - * @see https://gist.github.com/troelskn/751517 - * - * @param $camelized - * @return string - */ - private function uncamelize($camelized) { - return implode('_', - array_map('strtolower', - preg_split('/([A-Z]{1}[^A-Z]*)/', $camelized, -1, PREG_SPLIT_DELIM_CAPTURE|PREG_SPLIT_NO_EMPTY) - ) - ); - } - } \ No newline at end of file diff --git a/lib/Tmdb/Model/Genre.php b/lib/Tmdb/Model/Genre.php index 525c3028..8c8cae55 100644 --- a/lib/Tmdb/Model/Genre.php +++ b/lib/Tmdb/Model/Genre.php @@ -20,7 +20,7 @@ class Genre extends AbstractModel { private $id; private $name; - protected static $_properties = array( + public static $_properties = array( 'id', 'name', ); @@ -40,20 +40,6 @@ public static function fromArray(Client $client, array $data) return $genre->hydrate($data); } - /** - * Load a genre with the given identifier - * - * @param Client $client - * @param $id - * @param $parameters - * @return $this - */ - public static function load(Client $client, $id, array $parameters = array()) { - $data = $client->api('genres')->getGenre($id, parent::parseQueryParameters($parameters)); - - return GenreFactory::create($data); - } - /** * @param mixed $id * @return $this diff --git a/lib/Tmdb/Model/Image.php b/lib/Tmdb/Model/Image.php index b04ee294..df94a817 100644 --- a/lib/Tmdb/Model/Image.php +++ b/lib/Tmdb/Model/Image.php @@ -31,7 +31,7 @@ class Image extends AbstractModel { protected $id; protected $type; - protected static $_properties = array( + public static $_properties = array( 'file_path', 'width', 'height', @@ -39,7 +39,7 @@ class Image extends AbstractModel { 'aspect_ratio' ); - protected static $_types = array( + public static $_formats = array( 'posters' => self::FORMAT_POSTER, 'backdrops' => self::FORMAT_BACKDROP, 'profiles' => self::FORMAT_PROFILE, diff --git a/lib/Tmdb/Model/Movie.php b/lib/Tmdb/Model/Movie.php index eb9e388d..13fd66f8 100644 --- a/lib/Tmdb/Model/Movie.php +++ b/lib/Tmdb/Model/Movie.php @@ -12,23 +12,17 @@ */ namespace Tmdb\Model; -use Tmdb\Client; - -use Tmdb\Factory\MovieFactory; use Tmdb\Model\Common\Collection; use Tmdb\Model\Common\Collection\Images; use Tmdb\Model\Collection\Credits; - use Tmdb\Model\Collection\Genres; use Tmdb\Model\Collection\People; use Tmdb\Model\Common\Country; use Tmdb\Model\Common\SpokenLanguage; -use Tmdb\Model\Movie\AlternativeTitle; class Movie extends AbstractModel { - /** * @var bool */ @@ -205,7 +199,7 @@ class Movie extends AbstractModel { * * @var array */ - protected static $_properties = array( + public static $_properties = array( 'adult', 'backdrop_path', 'belongs_to_collection', @@ -251,20 +245,6 @@ public function __construct() $this->credits = new Credits(); } - /** - * Load a movie with the given identifier - * - * @param Client $client - * @param $id - * @param $parameters - * @return $this - */ - public static function load(Client $client, $id, array $parameters = array()) { - $data = $client->api('movies')->getMovie($id, parent::parseQueryParameters($parameters)); - - return MovieFactory::create($data); - } - /** * @param boolean $adult * @return $this