diff --git a/examples/discover/model/movies.php b/examples/discover/model/movies.php new file mode 100644 index 00000000..b792b3cf --- /dev/null +++ b/examples/discover/model/movies.php @@ -0,0 +1,30 @@ + + * @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\Discover\DiscoverMoviesQuery(); + +$query + ->page(1) + ->language('en') +; + +$repository = new \Tmdb\Repository\DiscoverRepository($client); +$response = $repository->discoverMovies($query); + +var_dump($response); + diff --git a/examples/discover/model/tv.php b/examples/discover/model/tv.php new file mode 100644 index 00000000..78b7ec80 --- /dev/null +++ b/examples/discover/model/tv.php @@ -0,0 +1,30 @@ + + * @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\Discover\DiscoverTvQuery(); + +$query + ->page(1) + ->language('en') +; + +$repository = new \Tmdb\Repository\DiscoverRepository($client); +$response = $repository->discoverTv($query); + +var_dump($response); + diff --git a/lib/Tmdb/Api/Discover.php b/lib/Tmdb/Api/Discover.php index d42a63db..d670bd8d 100644 --- a/lib/Tmdb/Api/Discover.php +++ b/lib/Tmdb/Api/Discover.php @@ -26,4 +26,16 @@ public function discoverMovies(array $options = array(), array $headers = array( { return $this->get('discover/movie', $options, $headers); } + + /** + * Discover TV shows by different types of data like average rating, number of votes, genres, the network they aired on and air dates. + * + * @param array $options + * @param array $headers + * @return mixed + */ + public function discoverTv(array $options = array(), array $headers = array()) + { + return $this->get('discover/tv', $options, $headers); + } } \ No newline at end of file diff --git a/lib/Tmdb/Factory/TvFactory.php b/lib/Tmdb/Factory/TvFactory.php index 3ee5f22a..9fe045cc 100644 --- a/lib/Tmdb/Factory/TvFactory.php +++ b/lib/Tmdb/Factory/TvFactory.php @@ -86,6 +86,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/Model/Query/AbstractQuery.php b/lib/Tmdb/Model/Query/AbstractQuery.php deleted file mode 100644 index 99124ef0..00000000 --- a/lib/Tmdb/Model/Query/AbstractQuery.php +++ /dev/null @@ -1,43 +0,0 @@ - - * @copyright (c) 2013, Michael Roterman - * @version 0.0.1 - */ -namespace Tmdb\Model; - -use Tmdb\Client; - -abstract class AbstractQuery extends AbstractModel { - - private $client; - - public function __construct(Client $client) - { - $this->setClient($client); - } - - /** - * @param mixed $client - * @return $this - */ - public function setClient($client) - { - $this->client = $client; - return $this; - } - - /** - * @return Client - */ - public function getClient() - { - return $this->client; - } -} diff --git a/lib/Tmdb/Model/Query/Discover.php b/lib/Tmdb/Model/Query/Discover.php deleted file mode 100644 index 6b2fcc67..00000000 --- a/lib/Tmdb/Model/Query/Discover.php +++ /dev/null @@ -1,86 +0,0 @@ - - * @copyright (c) 2013, Michael Roterman - * @version 0.0.1 - */ -namespace Tmdb\Model\Query; - -use Tmdb\Model\AbstractQuery; -use Tmdb\Model\Changes\Change; -use Tmdb\Model\Common\GenericCollection; - -class Discover 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()->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; - } -} diff --git a/lib/Tmdb/Model/Query/Discover/DiscoverMoviesQuery.php b/lib/Tmdb/Model/Query/Discover/DiscoverMoviesQuery.php new file mode 100644 index 00000000..8bd2dcba --- /dev/null +++ b/lib/Tmdb/Model/Query/Discover/DiscoverMoviesQuery.php @@ -0,0 +1,275 @@ + + * @copyright (c) 2013, Michael Roterman + * @version 0.0.1 + */ +namespace Tmdb\Model\Query\Discover; + +use Tmdb\Model\Collection\QueryParametersCollection; + +class DiscoverMoviesQuery extends QueryParametersCollection { + /** + * Minimum value is 1, expected value is an integer. + * + * @param integer $page + * @return $this + */ + public function page($page = 1) + { + $this->set('page', (int) $page); + + return $this; + } + + /** + * ISO 639-1 code. + * + * @param string $language + * @return $this + */ + public function language($language) + { + $this->set('language', $language); + + return $this; + } + + /** + * Available options are vote_average.desc, vote_average.asc, release_date.desc, release_date.asc, popularity.desc, popularity.asc + * + * @param string $option + * @return $this + */ + public function sortBy($option) + { + $this->set('sort_by', $option); + + return $this; + } + + /** + * Toggle the inclusion of adult titles. Expected value is a boolean, true or false + * + * @param boolean $allow + * @return $this + */ + public function includeAdult($allow = true) + { + $this->set('include_adult', (bool) $allow); + + return $this; + } + + /** + * Filter the results release dates to matches that include this value. Expected value is a year. + * + * @param \DateTime|integer $year + * @return $this + */ + public function year($year) + { + if ($year instanceof \DateTime) { + $year = $year->format('Y'); + } + + $this->set('year', (int) $year); + + return $this; + } + + /** + * Filter the results so that only the primary release date year has this value. Expected value is a year. + * + * @param \DateTime|integer $year + * @return $this + */ + public function primaryReleaseYear($year) + { + if ($year instanceof \DateTime) { + $year = $year->format('Y'); + } + + $this->set('primary_release_year', (int) $year); + + return $this; + } + + /** + * Only include movies that are equal to, or have a vote count higher than this value. Expected value is an integer. + * + * @param integer $count + * @return $this + */ + public function voteCountGte($count) + { + $this->set('vote_count.gte', (int) $count); + + return $this; + } + + /** + * Only include movies that are equal to, or have a higher average rating than this value. Expected value is a float. + * + * @param float $average + * @return $this + */ + public function voteAverageGte($average) + { + $this->set('vote_average.gte', (float) $average); + + return $this; + } + + /** + * Only include movies with the specified genres. Expected value is an integer (the id of a genre). + * Multiple values can be specified. + * + * Comma separated indicates an 'AND' query, while a pipe (|) separated value indicates an 'OR'. + * + * If an array is supplied this defaults to an AND query + * + * @param array|string $genres + * @return $this + */ + public function withGenres($genres) + { + if (is_array($genres)) { + $genres = $this->withGenresAnd($genres); + } + + $this->set('with_genres', $genres); + + return $this; + } + + /** + * Creates an or query for genres + * + * @param array $genres + * @return $this + */ + public function withGenresOr(array $genres = array()) + { + return $this->withGenres( + implode('|', $genres) + ); + } + + /** + * Creates an and query for genres + * + * @param array $genres + * @return $this + */ + public function withGenresAnd(array $genres = array()) + { + return $this->withGenres( + implode(',', $genres) + ); + } + + /** + * The minimum release to include. + * + * @param \DateTime|string $date + * @return $this + */ + public function releaseDateGte($date) + { + if ($date instanceof \DateTime) { + $date = $date->format('Y-m-d'); + } + + $this->set('release_date.gte', $date); + + return $this; + } + + /** + * The maximum release to include. + * + * @param \DateTime|string $date + * @return $this + */ + public function releaseDateLte(\DateTime $date) + { + if ($date instanceof \DateTime) { + $date = $date->format('Y-m-d'); + } + + $this->set('release_date.gte', $date); + + return $this; + } + + /** + * Only include movies with certifications for a specific country. + * + * When this value is specified, 'certification.lte' is required. A ISO 3166-1 is expected. + * + * @param string $country + * @return $this + */ + public function certificationCountry($country) + { + $this->set('certification_country', $country); + + return $this; + } + + /** + * Only include movies with this certification and lower. + * + * Expected value is a valid certification for the specificed 'certification_country'. + * + * @param mixed $value + * @todo fix method parameter name and specification + * @return $this + */ + public function certificationLte($value) + { + $this->set('certification.lte', $value); + + return $this; + } + + /** + * Filter movies to include a specific company. + * + * Expected value is an integer (the id of a company). + * They can be comma separated to indicate an 'AND' query. + * + * @param array|string $companies + * @return $this + */ + public function withCompanies($companies) + { + if (is_array($companies)) { + $companies = $this->withCompaniesAnd($companies); + } + + $this->set('with_companies', $companies); + + return $this; + } + + /** + * Creates an and query for companies + * + * @param array $companies + * @return $this + */ + public function withCompaniesAnd(array $companies = array()) + { + return $this->withGenres( + implode(',', $companies) + ); + } +} diff --git a/lib/Tmdb/Model/Query/Discover/DiscoverTvQuery.php b/lib/Tmdb/Model/Query/Discover/DiscoverTvQuery.php new file mode 100644 index 00000000..544c8161 --- /dev/null +++ b/lib/Tmdb/Model/Query/Discover/DiscoverTvQuery.php @@ -0,0 +1,215 @@ + + * @copyright (c) 2013, Michael Roterman + * @version 0.0.1 + */ +namespace Tmdb\Model\Query\Discover; + +use Tmdb\Model\Collection\QueryParametersCollection; + +class DiscoverTvQuery extends QueryParametersCollection { + /** + * Minimum value is 1, expected value is an integer. + * + * @param integer $page + * @return $this + */ + public function page($page = 1) + { + $this->set('page', (int) $page); + + return $this; + } + + /** + * ISO 639-1 code. + * + * @param string $language + * @return $this + */ + public function language($language) + { + $this->set('language', $language); + + return $this; + } + + /** + * Available options are vote_average.desc, vote_average.asc, first_air_date.desc, first_air_date.asc, popularity.desc, popularity.asc + * + * @param string $option + * @return $this + */ + public function sortBy($option) + { + $this->set('sort_by', $option); + + return $this; + } + + /** + * Filter the results release dates to matches that include this value. Expected value is a year. + * + * @param \DateTime|integer $year + * @return $this + */ + public function firstAirDateYear($year) + { + if ($year instanceof \DateTime) { + $year = $year->format('Y'); + } + + $this->set('first_air_date_year', (int) $year); + + return $this; + } + + /** + * Only include TV shows that are equal to, or have a vote count higher than this value. Expected value is an integer. + * + * @param integer $count + * @return $this + */ + public function voteCountGte($count) + { + $this->set('vote_count.gte', (int) $count); + + return $this; + } + + /** + * Only include TV shows that are equal to, or have a higher average rating than this value. Expected value is a float. + * + * @param float $average + * @return $this + */ + public function voteAverageGte($average) + { + $this->set('vote_average.gte', (float) $average); + + return $this; + } + + /** + * Only include TV shows with the specified genres. Expected value is an integer (the id of a genre). + * + * Multiple values can be specified. + * Comma separated indicates an 'AND' query, while a pipe (|) separated value indicates an 'OR'. + * + * @param array|string $genres + * @return $this + */ + public function withGenres($genres) + { + if (is_array($genres)) { + $genres = $this->withGenresAnd($genres); + } + + $this->set('with_genres', $genres); + + return $this; + } + + /** + * Creates an OR query for genres + * + * @param array $genres + * @return $this + */ + public function withGenresOr(array $genres = array()) + { + return $this->withGenres( + implode('|', $genres) + ); + } + + /** + * Creates an AND query for genres + * + * @param array $genres + * @return $this + */ + public function withGenresAnd(array $genres = array()) + { + return $this->withGenres( + implode(',', $genres) + ); + } + + /** + * The minimum release to include. Expected format is YYYY-MM-DD. + * + * @param \DateTime|string $date + * @return $this + */ + public function firstAirDateGte($date) + { + if ($date instanceof \DateTime) { + $date = $date->format('Y-m-d'); + } + + $this->set('first_air_date.gte', $date); + + return $this; + } + + /** + * The maximum release to include. Expected format is YYYY-MM-DD. + * + * @param \DateTime|string $date + * @return $this + */ + public function firstAirDateLte($date) + { + if ($date instanceof \DateTime) { + $date = $date->format('Y-m-d'); + } + + $this->set('first_air_date.lte', $date); + + return $this; + } + + + /** + * Filter TV shows to include a specific network. + * + * Expected value is an integer (the id of a network). They can be comma separated to indicate an 'AND' query. + * + * Expected value is an integer (the id of a company). + * They can be comma separated to indicate an 'AND' query. + * + * @param array|string $networks + * @return $this + */ + public function withNetworks($networks) + { + if (is_array($networks)) { + $networks = $this->withNetworksAnd($networks); + } + + $this->set('with_networks', $networks); + + return $this; + } + + /** + * Creates an and query for networks + * + * @param array $networks + * @return $this + */ + public function withNetworksAnd(array $networks = array()) + { + return $this->withGenres( + implode(',', $networks) + ); + } +} diff --git a/lib/Tmdb/Repository/DiscoverRepository.php b/lib/Tmdb/Repository/DiscoverRepository.php new file mode 100644 index 00000000..facb43b9 --- /dev/null +++ b/lib/Tmdb/Repository/DiscoverRepository.php @@ -0,0 +1,57 @@ + + * @copyright (c) 2013, Michael Roterman + * @version 0.0.1 + */ +namespace Tmdb\Repository; + +use Tmdb\Factory\MovieFactory; +use Tmdb\Factory\TvFactory; +use Tmdb\Model\Common\GenericCollection; +use Tmdb\Model\Query\Discover\DiscoverMoviesQuery; +use Tmdb\Model\Query\Discover\DiscoverTvQuery; + +class DiscoverRepository extends AbstractRepository { + /** + * Discover movies by different types of data like average rating, number of votes, genres and certifications. + * + * @param DiscoverMoviesQuery $query + * @param array $headers + * @return GenericCollection + */ + public function discoverMovies(DiscoverMoviesQuery $query, array $headers = array()) { + $data = $this->getApi()->discoverMovies($query->toArray(), $this->parseHeaders($headers)); + + return MovieFactory::createCollection($data); + } + + /** + * Discover TV shows by different types of data like average rating, number of votes, genres, the network they aired on and air dates. + * + * @param DiscoverTvQuery $query + * @param array $headers + * @return GenericCollection + */ + public function discoverTv(DiscoverTvQuery $query, array $headers = array()) { + $data = $this->getApi()->discoverTv($query->toArray(), $this->parseHeaders($headers)); + + return TvFactory::createCollection($data); + } + + /** + * Return the related API class + * + * @return \Tmdb\Api\Discover + */ + public function getApi() + { + return $this->getClient()->getDiscoverApi(); + } +} \ No newline at end of file