diff --git a/TODO b/TODO new file mode 100644 index 00000000..23bf34e1 --- /dev/null +++ b/TODO @@ -0,0 +1,2 @@ +- Add event subscribers to supply language and adult filters +- Figure out a way to slim down the seemingly dependency on Guzzle \ No newline at end of file diff --git a/examples/movies/model/all.php b/examples/movies/model/all.php index 13bed762..d1e6c50b 100644 --- a/examples/movies/model/all.php +++ b/examples/movies/model/all.php @@ -10,6 +10,8 @@ * @copyright (c) 2013, Michael Roterman * @version 0.0.1 */ +header('Content-Type: text/html; charset=utf-8'); + require_once('../../../vendor/autoload.php'); require_once('../../../apikey.php'); @@ -88,4 +90,12 @@ foreach($movie->getTrailers() as $trailer) { printf(" - %s\n", $trailer->getUrl()); +} + +$popular = $repository->getPopular(); + +echo "Popular titles\n"; + +foreach($popular as $p) { + printf(" - %s\n", $p->getTitle()); } \ No newline at end of file diff --git a/examples/people/model/all.php b/examples/people/model/all.php new file mode 100644 index 00000000..c922b2e9 --- /dev/null +++ b/examples/people/model/all.php @@ -0,0 +1,33 @@ + + * @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); + +// This is optional, but if you want lots of data this is the way. +$append = new \Tmdb\Model\Person\QueryParameter\AppendToResponse(array( + \Tmdb\Model\Person\QueryParameter\AppendToResponse::IMAGES, + \Tmdb\Model\Person\QueryParameter\AppendToResponse::CHANGES, + \Tmdb\Model\Person\QueryParameter\AppendToResponse::COMBINED_CREDITS, + \Tmdb\Model\Person\QueryParameter\AppendToResponse::LATEST, + \Tmdb\Model\Person\QueryParameter\AppendToResponse::MOVIE_CREDITS, + \Tmdb\Model\Person\QueryParameter\AppendToResponse::TV_CREDITS, + \Tmdb\Model\Person\QueryParameter\AppendToResponse::POPULAR +)); + +$repository = new \Tmdb\Repository\PersonRepository($client); +$person = $repository->load(33, array($append)); + +var_dump($person); \ No newline at end of file diff --git a/lib/Tmdb/Client.php b/lib/Tmdb/Client.php index d5d256ac..3fac2cf7 100644 --- a/lib/Tmdb/Client.php +++ b/lib/Tmdb/Client.php @@ -87,6 +87,14 @@ public function getMovieApi() return new Api\Movies($this); } + /** + * @return Api\People + */ + public function getPersonApi() + { + return new Api\People($this); + } + /** * Return the relevant API object * diff --git a/lib/Tmdb/Factory/Common/ImageFactory.php b/lib/Tmdb/Factory/Common/ImageFactory.php index 77c8dbd5..452846e4 100644 --- a/lib/Tmdb/Factory/Common/ImageFactory.php +++ b/lib/Tmdb/Factory/Common/ImageFactory.php @@ -43,7 +43,7 @@ public static function createCollection(array $data = array()) /** * {@inheritdoc} */ - public static function createCollectionFromMovie(array $data = array()) + public static function createImageCollection(array $data = array()) { $collection = array(); @@ -60,4 +60,20 @@ public static function createCollectionFromMovie(array $data = array()) return self::createCollection($collection); } + /** + * {@inheritdoc} + */ + public static function createCollectionFromMovie(array $data = array()) + { + return self::createImageCollection($data); + } + + /** + * {@inheritdoc} + */ + public static function createCollectionFromPeople(array $data = array()) + { + return self::createImageCollection($data); + } + } \ No newline at end of file diff --git a/lib/Tmdb/Factory/People/PeopleFactory.php b/lib/Tmdb/Factory/People/PeopleFactory.php index 06111a36..4d5e290d 100644 --- a/lib/Tmdb/Factory/People/PeopleFactory.php +++ b/lib/Tmdb/Factory/People/PeopleFactory.php @@ -12,12 +12,13 @@ */ namespace Tmdb\Factory\People; -use Symfony\Component\Yaml\Exception\RuntimeException; -use Tmdb\Client; use Tmdb\Factory\AbstractFactory; use Tmdb\Model\Common\Collection; use Tmdb\Model\Person\CastMember; use Tmdb\Model\Person\CrewMember; +use Tmdb\Model\Person; + +use Tmdb\Factory\Common\ImageFactory; class PeopleFactory extends AbstractFactory { /** @@ -36,9 +37,12 @@ public static function create(array $data = array()) } if (null === $person) { - throw new RuntimeException(sprintf( - 'Was unable to determine the type of person by the data provided for #%d', $data['id'] - )); + $person = new Person(); + } + + /** Images */ + if (array_key_exists('images', $data)) { + $person->setImages(ImageFactory::createCollectionFromPeople($data['images'])); } return parent::hydrate($person, $data); diff --git a/lib/Tmdb/HttpClient/HttpClientInterface.php b/lib/Tmdb/HttpClient/HttpClientInterface.php index c5fac92b..e4a260e5 100644 --- a/lib/Tmdb/HttpClient/HttpClientInterface.php +++ b/lib/Tmdb/HttpClient/HttpClientInterface.php @@ -18,7 +18,7 @@ interface HttpClientInterface { /** - * Send a GET request + * Compose a GET request * * @param string $path Request path * @param array $parameters GET Parameters @@ -29,7 +29,7 @@ interface HttpClientInterface public function get($path, array $parameters = array(), array $headers = array()); /** - * Send a POST request + * Compose a POST request * * @param string $path Request path * @param string $postBody The post BODY @@ -41,7 +41,7 @@ public function get($path, array $parameters = array(), array $headers = array() public function post($path, $postBody, array $parameters = array(), array $headers = array()); /** - * Send a PATCH request + * Compose a PATCH request * * @param string $path Request path * @param string $body The body @@ -53,7 +53,7 @@ public function post($path, $postBody, array $parameters = array(), array $heade public function patch($path, $body = null, array $parameters = array(), array $headers = array()); /** - * Send a PUT request + * Compose a PUT request * * @param string $path Request path * @param string $body The body @@ -65,7 +65,7 @@ public function patch($path, $body = null, array $parameters = array(), array $h public function put($path, $body = null, array $parameters = array(), array $headers = array()); /** - * Send a DELETE request + * Compose a DELETE request * * @param string $path Request path * @param string $body The body diff --git a/lib/Tmdb/Model/Person.php b/lib/Tmdb/Model/Person.php index a4adf13c..ba70859a 100644 --- a/lib/Tmdb/Model/Person.php +++ b/lib/Tmdb/Model/Person.php @@ -13,6 +13,12 @@ namespace Tmdb\Model; use Tmdb\Client; + +use Tmdb\Model\Collection\Credits; + +use Tmdb\Model\Common\Collection; +use Tmdb\Model\Common\Collection\Images; + use Tmdb\Model\Collection\People\PersonInterface; class Person extends AbstractModel implements PersonInterface { @@ -34,7 +40,7 @@ class Person extends AbstractModel implements PersonInterface { protected $images; protected $changes; - protected static $_properties = array( + public static $_properties = array( 'adult', 'also_known_as', 'biography', @@ -47,6 +53,18 @@ class Person extends AbstractModel implements PersonInterface { 'profile_path', ); + /** + * Constructor + * + * Set all default collections + */ + public function __construct() + { + $this->credits = new Credits(); + $this->images = new Images(); + $this->changes = new Collection(); + } + /** * Convert an array to an hydrated object * diff --git a/lib/Tmdb/Model/Person/QueryParameter/AppendToResponse.php b/lib/Tmdb/Model/Person/QueryParameter/AppendToResponse.php new file mode 100644 index 00000000..4b80b5e7 --- /dev/null +++ b/lib/Tmdb/Model/Person/QueryParameter/AppendToResponse.php @@ -0,0 +1,23 @@ + + * @copyright (c) 2013, Michael Roterman + * @version 0.0.1 + */ +namespace Tmdb\Model\Person\QueryParameter; + +use Tmdb\Model\Common\QueryParameter\AppendToResponse as BaseAppendToResponse; + +final class AppendToResponse extends BaseAppendToResponse { + const MOVIE_CREDITS = 'movie_credits'; + const TV_CREDITS = 'tv_credits'; + const COMBINED_CREDITS = 'combined_credits'; + const IMAGES = 'images'; + const CHANGES = 'changes'; +} \ No newline at end of file diff --git a/lib/Tmdb/Repository/AbstractRepository.php b/lib/Tmdb/Repository/AbstractRepository.php index 610763be..72346b9a 100644 --- a/lib/Tmdb/Repository/AbstractRepository.php +++ b/lib/Tmdb/Repository/AbstractRepository.php @@ -12,6 +12,7 @@ */ namespace Tmdb\Repository; +use Tmdb\Api\ApiInterface; use Tmdb\Client; use Tmdb\Model\Common\QueryParameter\QueryParameterInterface; @@ -19,6 +20,8 @@ abstract class AbstractRepository { private static $client = null; + protected $api = null; + /** * Constructor * @@ -57,4 +60,20 @@ protected function parseQueryParameters(array $parameters = array()) return $parameters; } + + /** + * Load the given identifier + * + * @param $id + * @param array $parameters + * @return mixed + */ + abstract public function load($id, array $parameters = array()); + + /** + * Return the API Class + * + * @return ApiInterface + */ + abstract public function getApi(); } \ No newline at end of file diff --git a/lib/Tmdb/Repository/MovieRepository.php b/lib/Tmdb/Repository/MovieRepository.php index 855c9b7e..e058c0b7 100644 --- a/lib/Tmdb/Repository/MovieRepository.php +++ b/lib/Tmdb/Repository/MovieRepository.php @@ -13,9 +13,11 @@ namespace Tmdb\Repository; use Tmdb\Factory\MovieFactory; +use Tmdb\Model\Common\Collection; use Tmdb\Model\Movie; class MovieRepository extends AbstractRepository { + /** * Load a movie with the given identifier * @@ -23,25 +25,130 @@ class MovieRepository extends AbstractRepository { * @param $parameters * @return Movie */ - public static function load($id, array $parameters = array()) { - $data = parent::getClient()->getMovieApi()->getMovie($id, parent::parseQueryParameters($parameters)); + public function load($id, array $parameters = array()) { + + if (empty($parameters) && $parameters !== false) { + // Load a no-nonsense default set + $parameters = array( + new \Tmdb\Model\Movie\QueryParameter\AppendToResponse(array( + \Tmdb\Model\Movie\QueryParameter\AppendToResponse::CREDITS, + \Tmdb\Model\Movie\QueryParameter\AppendToResponse::IMAGES, + \Tmdb\Model\Movie\QueryParameter\AppendToResponse::KEYWORDS, + \Tmdb\Model\Movie\QueryParameter\AppendToResponse::RELEASES, + \Tmdb\Model\Movie\QueryParameter\AppendToResponse::TRAILERS, + \Tmdb\Model\Movie\QueryParameter\AppendToResponse::TRANSLATIONS, + )) + ); + } + + $data = $this->getApi()->getMovie($id, $this->parseQueryParameters($parameters)); return MovieFactory::create($data); } /** - * If you obtained an movie model which is not completely hydrated, you can use this function. ( E.g. similar_movies ) + * If you obtained an movie model which is not completely hydrated, you can use this function. * - * Do note that you will have to provide the AppendToResponse object into the parameters array of which data - * you would like to obtain. - * - * array(AppendToResponse(array(...))) + * @todo store the previous given parameters so the same conditions apply to a refresh, and merge the new set * * @param Movie $movie * @param array $parameters * @return Movie */ public function refresh(Movie $movie, array $parameters = array()) { - return self::load($movie->getId(), $parameters); + return $this->load($movie->getId(), $parameters); + } + + /** + * Return the Movies API Class + * + * @return \Tmdb\Api\Movies + */ + public function getApi() + { + return $this->getClient()->getMovieApi(); } + + /** + * Get the latest movie id. + * + * @return Movie + */ + public function getLatest() + { + return MovieFactory::create( + $this->getApi()->getLatest() + ); + } + + /** + * Get the list of upcoming movies. This list refreshes every day. The maximum number of items this list will include is 100. + * + * @return Collection + */ + public function getUpcoming() + { + return $this->createCollection( + $this->getApi()->getUpcoming() + ); + } + + /** + * Get the list of movies playing in theatres. This list refreshes every day. The maximum number of items this list will include is 100. + * + * @return Collection + */ + public function getNowPlaying() + { + return $this->createCollection( + $this->getApi()->getNowPlaying() + ); + } + + /** + * Get the list of popular movies on The Movie Database. This list refreshes every day. + * + * @return Collection + */ + public function getPopular() + { + return $this->createCollection( + $this->getApi()->getPopular() + ); + } + + /** + * Get the list of top rated movies. By default, this list will only include movies that have 10 or more votes. This list refreshes every day. + * + * @return Collection + */ + public function getTopRated() + { + return $this->createCollection( + $this->getApi()->getTopRated() + ); + } + + /** + * Create an collection of an array + * + * @todo Allow an array of Movie objects to pass ( custom collection ) + * + * @param $data + * @return Collection + */ + private function createCollection($data){ + $collection = new Collection(); + + if (array_key_exists('results', $data)) { + $data = $data['results']; + } + + foreach($data as $item) { + $collection->add(null, MovieFactory::create($item)); + } + + return $collection; + } + } \ No newline at end of file diff --git a/lib/Tmdb/Repository/PersonRepository.php b/lib/Tmdb/Repository/PersonRepository.php new file mode 100644 index 00000000..4423e545 --- /dev/null +++ b/lib/Tmdb/Repository/PersonRepository.php @@ -0,0 +1,60 @@ + + * @copyright (c) 2013, Michael Roterman + * @version 0.0.1 + */ +namespace Tmdb\Repository; + +use Tmdb\Factory\People\PeopleFactory; +use Tmdb\Model\Person; + +class PersonRepository extends AbstractRepository { + /** + * Load a person with the given identifier + * + * @param $id + * @param $parameters + * @return Person + */ + public function load($id, array $parameters = array()) { + + if (empty($parameters) && $parameters !== false) { + // Load a no-nonsense default set + $parameters = array( + new \Tmdb\Model\Person\QueryParameter\AppendToResponse(array( + \Tmdb\Model\Person\QueryParameter\AppendToResponse::IMAGES, + )) + ); + } + + $data = $this->getApi()->getPerson($id, $this->parseQueryParameters($parameters)); + + return PeopleFactory::create($data); + } + + /** + * If you obtained an person model which is not completely hydrated, you can use this function. + * + * @param Person $person + * @param array $parameters + * @return Person + */ + public function refresh(Person $person, array $parameters = array()) { + return $this->load($person->getId(), $parameters); + } + + /** + * @return \Tmdb\Api\People + */ + public function getApi() + { + return $this->getClient()->getPersonApi(); + } +} \ No newline at end of file