From dca82775d4884b64b9faeb9a6e8ea9cfbc5df098 Mon Sep 17 00:00:00 2001 From: Michael Roterman Date: Sun, 19 Jan 2014 00:12:16 +0100 Subject: [PATCH] Adding Changes API section, added Query object to support this behaviour. --- .../changes/model/{get.php => movies.php} | 16 +++- examples/changes/model/people.php | 34 +++++++ lib/Tmdb/Factory/MovieFactory.php | 4 + lib/Tmdb/Factory/People/PeopleFactory.php | 4 + lib/Tmdb/Model/Change.php | 73 ++++++++++++++ lib/Tmdb/Model/Changes.php | 87 ----------------- .../Collection/QueryParametersCollection.php | 17 ++++ .../Model/Collection/ResultCollection.php | 95 +++++++++++++++++++ lib/Tmdb/Model/Query/Changes.php | 85 ----------------- lib/Tmdb/Model/Query/ChangesQuery.php | 56 +++++++++++ lib/Tmdb/Repository/ChangesRepository.php | 14 +-- 11 files changed, 304 insertions(+), 181 deletions(-) rename examples/changes/model/{get.php => movies.php} (61%) create mode 100644 examples/changes/model/people.php create mode 100644 lib/Tmdb/Model/Change.php delete mode 100644 lib/Tmdb/Model/Changes.php create mode 100644 lib/Tmdb/Model/Collection/QueryParametersCollection.php create mode 100644 lib/Tmdb/Model/Collection/ResultCollection.php delete mode 100644 lib/Tmdb/Model/Query/Changes.php create mode 100644 lib/Tmdb/Model/Query/ChangesQuery.php diff --git a/examples/changes/model/get.php b/examples/changes/model/movies.php similarity index 61% rename from examples/changes/model/get.php rename to examples/changes/model/movies.php index 868c4463..973794c0 100644 --- a/examples/changes/model/get.php +++ b/examples/changes/model/movies.php @@ -16,7 +16,19 @@ $token = new \Tmdb\ApiToken(TMDB_API_KEY); $client = new \Tmdb\Client($token); -$changes = new \Tmdb\Model\Changes($client); +$query = new \Tmdb\Model\Query\ChangesQuery(); -$response = $changes->page(2)->execute(); +$from = new \DateTime('01-01-2012'); +$to = new \DateTime('08-01-2012'); + +$query + ->page(1) + ->from($from) + ->to($to) +; + +$repository = new \Tmdb\Repository\ChangesRepository($client); +$response = $repository->getMovieChanges($query); + +var_dump($response); diff --git a/examples/changes/model/people.php b/examples/changes/model/people.php new file mode 100644 index 00000000..21bf229c --- /dev/null +++ b/examples/changes/model/people.php @@ -0,0 +1,34 @@ + + * @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); + +$query = new \Tmdb\Model\Query\ChangesQuery(); + +$from = new \DateTime('01-01-2012'); +$to = new \DateTime('08-01-2012'); + +$query + ->page(1) + ->from($from) + ->to($to) +; + +$repository = new \Tmdb\Repository\ChangesRepository($client); +$response = $repository->getPeopleChanges($query); + +var_dump($response); + diff --git a/lib/Tmdb/Factory/MovieFactory.php b/lib/Tmdb/Factory/MovieFactory.php index e8f367c3..d0d12b5e 100644 --- a/lib/Tmdb/Factory/MovieFactory.php +++ b/lib/Tmdb/Factory/MovieFactory.php @@ -109,6 +109,10 @@ public static function createCollection(array $data = array()) { $collection = new GenericCollection(); + if (array_key_exists('results', $data)) { + $data = $data['results']; + } + foreach($data as $item) { $collection->add(null, self::create($item)); } diff --git a/lib/Tmdb/Factory/People/PeopleFactory.php b/lib/Tmdb/Factory/People/PeopleFactory.php index 568c69ac..9822818b 100644 --- a/lib/Tmdb/Factory/People/PeopleFactory.php +++ b/lib/Tmdb/Factory/People/PeopleFactory.php @@ -59,6 +59,10 @@ public static function createCollection(array $data = array(), Person\AbstractMe { $collection = new GenericCollection(); + if (array_key_exists('results', $data)) { + $data = $data['results']; + } + foreach($data as $item) { $collection->add(null, self::create($item, $person)); } diff --git a/lib/Tmdb/Model/Change.php b/lib/Tmdb/Model/Change.php new file mode 100644 index 00000000..a184001b --- /dev/null +++ b/lib/Tmdb/Model/Change.php @@ -0,0 +1,73 @@ + + * @copyright (c) 2013, Michael Roterman + * @version 0.0.1 + */ +namespace Tmdb\Model; + +use Tmdb\Client; + +class Change extends AbstractModel { + /** + * @var integer + */ + private $id; + + /** + * @var boolean + */ + private $adult; + + /** + * @var array + */ + public static $_properties = array( + 'id', + 'adult' + ); + + /** + * @param boolean $adult + * @return $this + */ + public function setAdult($adult) + { + $this->adult = $adult; + return $this; + } + + /** + * @return boolean + */ + public function getAdult() + { + return $this->adult; + } + + /** + * @param int $id + * @return $this + */ + public function setId($id) + { + $this->id = $id; + return $this; + } + + /** + * @return int + */ + public function getId() + { + return $this->id; + } + + +} \ No newline at end of file diff --git a/lib/Tmdb/Model/Changes.php b/lib/Tmdb/Model/Changes.php deleted file mode 100644 index 98dad5b9..00000000 --- a/lib/Tmdb/Model/Changes.php +++ /dev/null @@ -1,87 +0,0 @@ - - * @copyright (c) 2013, Michael Roterman - * @version 0.0.1 - */ -namespace Tmdb\Model; - -use Tmdb\Model\Changes\Change; -use Tmdb\Model\Common\GenericCollection; - -class Changes extends AbstractModel { - - private $from = null; - private $to = null; - private $page = null; - - /** - * Set the from parameter - * - * @param \DateTime $date - * @return $this - */ - public function from(\DateTime $date) - { - $this->from = $date->format('Y-m-d'); - - return $this; - } - - /** - * Set the to parameter - * - * @param \DateTime $date - * @return $this - */ - public function to(\DateTime $date) - { - $this->to = $date->format('Y-m-d'); - - return $this; - } - - /** - * Set the page parameter - * - * @param int $page - * @return $this - */ - public function page($page = 1) { - $this->page = (int) $page; - - return $this; - } - - /** - * Execute the current state - * - * @return GenericCollection - */ - public function execute() - { - $collection = new GenericCollection(); - - $response = $this->getClient()->api('changes')->getMovieChanges(array( - 'from' => $this->from, - 'to' => $this->to, - 'page' => $this->page - )); - - if (!empty($response)) { - foreach($response['results'] as $change) { - $collection->add(null, Change::fromArray($this->getClient(), $change)); - } - } - - return $collection; - } - - //abstract public function getEntity(); -} \ No newline at end of file diff --git a/lib/Tmdb/Model/Collection/QueryParametersCollection.php b/lib/Tmdb/Model/Collection/QueryParametersCollection.php new file mode 100644 index 00000000..d7afeabe --- /dev/null +++ b/lib/Tmdb/Model/Collection/QueryParametersCollection.php @@ -0,0 +1,17 @@ + + * @copyright (c) 2013, Michael Roterman + * @version 0.0.1 + */ +namespace Tmdb\Model\Collection; + +use Tmdb\Model\Common\GenericCollection; + +class QueryParametersCollection extends GenericCollection {} diff --git a/lib/Tmdb/Model/Collection/ResultCollection.php b/lib/Tmdb/Model/Collection/ResultCollection.php new file mode 100644 index 00000000..33b9ea50 --- /dev/null +++ b/lib/Tmdb/Model/Collection/ResultCollection.php @@ -0,0 +1,95 @@ + + * @copyright (c) 2013, Michael Roterman + * @version 0.0.1 + */ +namespace Tmdb\Model\Collection; + +use Tmdb\Model\Common\GenericCollection; + +class ResultCollection extends GenericCollection { + /** + * @var int + */ + private $page = 1; + + /** + * @var int + */ + private $totalPages = 1; + + /** + * @var int + */ + private $totalResults = 0; + + /** + * @var array + */ + public static $_properties = array( + 'page', + 'total_pages', + 'total_results' + ); + + /** + * @param int $page + * @return $this + */ + public function setPage($page) + { + $this->page = $page; + return $this; + } + + /** + * @return int + */ + public function getPage() + { + return $this->page; + } + + /** + * @param int $totalPages + * @return $this + */ + public function setTotalPages($totalPages) + { + $this->totalPages = $totalPages; + return $this; + } + + /** + * @return int + */ + public function getTotalPages() + { + return $this->totalPages; + } + + /** + * @param int $totalResults + * @return $this + */ + public function setTotalResults($totalResults) + { + $this->totalResults = $totalResults; + return $this; + } + + /** + * @return int + */ + public function getTotalResults() + { + return $this->totalResults; + } +} \ No newline at end of file diff --git a/lib/Tmdb/Model/Query/Changes.php b/lib/Tmdb/Model/Query/Changes.php deleted file mode 100644 index 74d1a0fc..00000000 --- a/lib/Tmdb/Model/Query/Changes.php +++ /dev/null @@ -1,85 +0,0 @@ - - * @copyright (c) 2013, Michael Roterman - * @version 0.0.1 - */ -namespace Tmdb\Model; - -use Tmdb\Model\Changes\Change; -use Tmdb\Model\Common\GenericCollection; - -class Changes extends AbstractQuery { - - private $from = null; - private $to = null; - private $page = null; - - /** - * Set the from parameter - * - * @param \DateTime $date - * @return $this - */ - public function from(\DateTime $date) - { - $this->from = $date->format('Y-m-d'); - - return $this; - } - - /** - * Set the to parameter - * - * @param \DateTime $date - * @return $this - */ - public function to(\DateTime $date) - { - $this->to = $date->format('Y-m-d'); - - return $this; - } - - /** - * Set the page parameter - * - * @param int $page - * @return $this - */ - public function page($page = 1) { - $this->page = (int) $page; - - return $this; - } - - /** - * Execute the current state - * - * @return GenericCollection - */ - public function execute() - { - $collection = new GenericCollection(); - - $response = $this->getClient()->getChangesApi()->getMovieChanges(array( - 'from' => $this->from, - 'to' => $this->to, - 'page' => $this->page - )); - - if (!empty($response)) { - foreach($response['results'] as $change) { - $collection->add(null, Change::fromArray($this->getClient(), $change)); - } - } - - return $collection; - } -} diff --git a/lib/Tmdb/Model/Query/ChangesQuery.php b/lib/Tmdb/Model/Query/ChangesQuery.php new file mode 100644 index 00000000..e9d39538 --- /dev/null +++ b/lib/Tmdb/Model/Query/ChangesQuery.php @@ -0,0 +1,56 @@ + + * @copyright (c) 2013, Michael Roterman + * @version 0.0.1 + */ +namespace Tmdb\Model\Query; + +use Tmdb\Model\Collection\QueryParametersCollection; + +class ChangesQuery extends QueryParametersCollection { + + /** + * Set the from parameter + * + * @param \DateTime $date + * @return $this + */ + public function from(\DateTime $date) + { + $this->set('from', $date->format('Y-m-d')); + + return $this; + } + + /** + * Set the to parameter + * + * @param \DateTime $date + * @return $this + */ + public function to(\DateTime $date) + { + $this->set('to', $date->format('Y-m-d')); + + return $this; + } + + /** + * Set the page parameter + * + * @param int $page + * @return $this + */ + public function page($page = 1) { + $this->set('page', (int) $page); + + return $this; + } +} diff --git a/lib/Tmdb/Repository/ChangesRepository.php b/lib/Tmdb/Repository/ChangesRepository.php index 00affd21..2beb7c37 100644 --- a/lib/Tmdb/Repository/ChangesRepository.php +++ b/lib/Tmdb/Repository/ChangesRepository.php @@ -15,8 +15,8 @@ use Tmdb\Factory\MovieFactory; use Tmdb\Factory\People\PeopleFactory; use Tmdb\Model\Common\GenericCollection; -use Tmdb\Model\Company; use Tmdb\Model\Movie; +use Tmdb\Model\Query\ChangesQuery; class ChangesRepository extends AbstractRepository { /** @@ -29,12 +29,12 @@ class ChangesRepository extends AbstractRepository { * Please note that the change log system to support this was changed on October 5, 2012 * and will only show movies that have been edited since. * - * @param array $parameters + * @param ChangesQuery $query * @param array $headers * @return GenericCollection */ - public function getMovieChanges(array $parameters = array(), array $headers = array()) { - $data = $this->getApi()->getMovieChanges($this->parseQueryParameters($parameters), $this->parseHeaders($headers)); + public function getMovieChanges(ChangesQuery $query, array $headers = array()) { + $data = $this->getApi()->getMovieChanges($query->toArray(), $this->parseHeaders($headers)); return MovieFactory::createCollection($data); } @@ -49,12 +49,12 @@ public function getMovieChanges(array $parameters = array(), array $headers = ar * Please note that the change log system to support this was changed on October 5, 2012 * and will only show people that have been edited since. * - * @param array $parameters + * @param ChangesQuery $query * @param array $headers * @return GenericCollection */ - public function getPeopleChanges(array $parameters = array(), array $headers = array()) { - $data = $this->getApi()->getPeopleChanges($this->parseQueryParameters($parameters), $this->parseHeaders($headers)); + public function getPeopleChanges(ChangesQuery $query, array $headers = array()) { + $data = $this->getApi()->getPeopleChanges($query->toArray(), $this->parseHeaders($headers)); return PeopleFactory::createCollection($data); }