diff --git a/LICENSE b/LICENSE new file mode 100644 index 00000000..7d5068d1 --- /dev/null +++ b/LICENSE @@ -0,0 +1,24 @@ +The MIT License + +Copyright (c) 2013 Michael Roterman +Copyright (c) 2013 Matt Humphrey +Copyright (c) 2012 KnpLabs +Copyright (c) 2010 Thibault Duplessis + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. diff --git a/composer.json b/composer.json new file mode 100644 index 00000000..b54adbb8 --- /dev/null +++ b/composer.json @@ -0,0 +1,22 @@ +{ + "name": "wtfzdotnet/php-tmdb-api", + "type": "library", + "description": "TMDB API client", + "homepage": "https://github.com/wtfzdotnet/php-tmdb-api", + "keywords": ["tmdb", "api", "php"], + "license": "MIT", + "authors": [ + { + "name": "Michael Roterman", + "homepage": "http://wtfz.net" + } + ], + "require": { + "php": ">=5.3.2", + "ext-curl": "*", + "guzzle/guzzle": ">=0.7" + }, + "autoload": { + "psr-0": { "Tmdb\\": "lib/" } + } +} \ No newline at end of file diff --git a/lib/Tmdb/Api/AbstractApi.php b/lib/Tmdb/Api/AbstractApi.php new file mode 100644 index 00000000..82559529 --- /dev/null +++ b/lib/Tmdb/Api/AbstractApi.php @@ -0,0 +1,124 @@ + + * @copyright (c) 2013, Michael Roterman + * @version 0.0.1 + */ +namespace Tmdb\Api; + +use Tmdb\Client; + +abstract class AbstractApi + implements ApiInterface +{ + /** + * The client + * + * @var Client + */ + protected $client; + + /** + * Constructor + * + * @param Client $client + */ + public function __construct(Client $client) + { + $this->client = $client; + } + + /** + * Send a GET request + * + * @param $path + * @param array $parameters + * @param array $headers + * @return mixed + */ + public function get($path, array $parameters = array(), $headers = array()) + { + $response = $this->client->getHttpClient()->get($path, $parameters, $headers); + return $response->json(); + } + + /** + * Send a HEAD request + * + * @param $path + * @param array $parameters + * @param array $headers + * @return mixed + */ + public function head($path, array $parameters = array(), $headers = array()) + { + $response = $this->client->getHttpClient()->head($path, $parameters, $headers); + return $response->json(); + } + + /** + * Send a POST request + * + * @param $path + * @param null $postBody + * @param array $parameters + * @param array $headers + * @return mixed + */ + public function post($path, $postBody = null, array $parameters = array(), $headers = array()) + { + $response = $this->client->getHttpClient()->post($path, $postBody, $parameters, $headers); + return $response->json(); + } + + /** + * Send a PUT request + * + * @param $path + * @param null $body + * @param array $parameters + * @param array $headers + * @return mixed + */ + public function put($path, $body = null, array $parameters = array(), $headers = array()) + { + $response = $this->client->getHttpClient()->put($path, $body, $parameters, $headers); + return $response->json(); + } + + /** + * Send a DELETE request + * + * @param $path + * @param null $body + * @param array $parameters + * @param array $headers + * @return mixed + */ + public function delete($path, $body = null, array $parameters = array(), $headers = array()) + { + $response = $this->client->getHttpClient()->delete($path, $body, $parameters, $headers); + return $response->json(); + } + + /** + * Send a PATCH request + * + * @param $path + * @param null $body + * @param array $parameters + * @param array $headers + * @return mixed + */ + public function patch($path, $body = null, array $parameters = array(), $headers = array()) + { + $response = $this->client->getHttpClient()->patch($path, $body, $parameters, $headers); + return $response->json(); + } +} \ No newline at end of file diff --git a/lib/Tmdb/Api/ApiInterface.php b/lib/Tmdb/Api/ApiInterface.php index 7552511a..9d29e712 100644 --- a/lib/Tmdb/Api/ApiInterface.php +++ b/lib/Tmdb/Api/ApiInterface.php @@ -1,19 +1,17 @@ - * @copyright (c) 2013, B-Found Internet Marketing & Services + * @package Tmdb + * @author Michael Roterman + * @copyright (c) 2013, Michael Roterman * @version 0.0.1 */ namespace Tmdb\Api; - interface ApiInterface { - -} \ No newline at end of file +} \ No newline at end of file diff --git a/lib/Tmdb/Api/Movies.php b/lib/Tmdb/Api/Movies.php index 5df2808b..ab16d0ca 100644 --- a/lib/Tmdb/Api/Movies.php +++ b/lib/Tmdb/Api/Movies.php @@ -1,19 +1,237 @@ - * @copyright (c) 2013, B-Found Internet Marketing & Services + * + * @package Tmdb + * @author Michael Roterman + * @copyright (c) 2013, Michael Roterman * @version 0.0.1 */ - namespace Tmdb\Api; +use Tmdb\Exception\NotImplementedException; + +class Movies + extends AbstractApi +{ + /** + * Get the basic movie information for a specific movie id. + * + * @param $movie_id + * @param array $options + * @return mixed + */ + public function getMovie($movie_id, array $options = array()) + { + return $this->get('movie/' . $movie_id, $options); + } + + /** + * Get the alternative titles for a specific movie id. + * + * @param $movie_id + * @param array $options + * @return mixed + */ + public function getAlternativeTitles($movie_id, array $options = array()) + { + return $this->get('movie/' . $movie_id . '/alternative_titles', $options); + } + + /** + * Get the cast information for a specific movie id. + * + * @param $movie_id + * @param array $options + * @return mixed + */ + public function getCast($movie_id, array $options = array()) + { + return $this->get('movie/' . $movie_id . '/casts', $options); + } + + /** + * Get the images (posters and backdrops) for a specific movie id. + * + * @param $movie_id + * @param array $options + * @return mixed + */ + public function getImages($movie_id, array $options = array()) + { + return $this->get('movie/' . $movie_id . '/images', $options); + } + + /** + * Get the plot keywords for a specific movie id. + * + * @param $movie_id + * @param array $options + * @return mixed + */ + public function getKeywords($movie_id, array $options = array()) + { + return $this->get('movie/' . $movie_id . '/keywords', $options); + } + + /** + * Get the release date by country for a specific movie id. + * + * @param $movie_id + * @param array $options + * @return mixed + */ + public function getReleases($movie_id, array $options = array()) + { + return $this->get('movie/' . $movie_id . '/releases', $options); + } + + /** + * Get the trailers for a specific movie id. + * + * @param $movie_id + * @param array $options + * @return mixed + */ + public function getTrailers($movie_id, array $options = array()) + { + return $this->get('movie/' . $movie_id . '/trailers', $options); + } + + /** + * Get the translations for a specific movie id. + * + * @param $movie_id + * @param array $options + * @return mixed + */ + public function getTranslations($movie_id, array $options = array()) + { + return $this->get('movie/' . $movie_id . '/translations', $options); + } + + /** + * Get the similar movies for a specific movie id. + * + * @param $movie_id + * @param array $options + * @return mixed + */ + public function getSimilarMovies($movie_id, array $options = array()) + { + return $this->get('movie/' . $movie_id . '/similar_movies', $options); + } + + /** + * Get the reviews for a particular movie id. + * + * @param $movie_id + * @param array $options + * @return mixed + */ + public function getReviews($movie_id, array $options = array()) + { + return $this->get('movie/' . $movie_id . '/reviews', $options); + } + + /** + * Get the lists that the movie belongs to. + * + * @param $movie_id + * @param array $options + * @return mixed + */ + public function getLists($movie_id, array $options = array()) + { + return $this->get('movie/' . $movie_id . '/lists', $options); + } + + /** + * Get the changes for a specific movie id. + * + * @param $movie_id + * @param array $options + * @return mixed + */ + public function getChanges($movie_id, array $options = array()) + { + return $this->get('movie/' . $movie_id . '/changes', $options); + } + + /** + * Get the latest movie id. + * + * @return mixed + */ + public function getLatest() + { + return $this->get('movie/latest'); + } + + /** + * Get the list of upcoming movies. This list refreshes every day. The maximum number of items this list will include is 100. + * + * @return mixed + */ + public function getUpcoming() + { + return $this->get('movie/upcoming'); + } + + /** + * 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 mixed + */ + public function getNowPlaying() + { + return $this->get('movie/now_playing'); + } + + /** + * Get the list of popular movies on The Movie Database. This list refreshes every day. + * + * @return mixed + */ + public function getPopular() + { + return $this->get('movie/popular'); + } + + /** + * 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 mixed + */ + public function getTopRated() + { + return $this->get('movie/top_rated'); + } -class Movies { + /** + * This method lets users get the status of whether or not the movie has been rated or added to their favourite or watch lists. A valid session id is required. + * + * @param $movie_id + * @param array $options + * @throws \Tmdb\Exception\NotImplementedException + */ + public function getAccountStates($movie_id, array $options = array()) + { + throw new NotImplementedException('TMDB account sessions have not been implemented yet!'); + } -} \ No newline at end of file + /** + * TThis method lets users rate a movie. A valid session id or guest session id is required. + * + * @param $movie_id + * @param array $options + * @throws \Tmdb\Exception\NotImplementedException + */ + public function rateMovie($movie_id, array $options = array()) + { + throw new NotImplementedException('TMDB account sessions have not been implemented yet!'); + } +} \ No newline at end of file diff --git a/lib/Tmdb/ApiToken.php b/lib/Tmdb/ApiToken.php new file mode 100644 index 00000000..c71bcd20 --- /dev/null +++ b/lib/Tmdb/ApiToken.php @@ -0,0 +1,46 @@ + + * @copyright (c) 2013, Michael Roterman + * @version 0.0.1 + */ +namespace Tmdb; + +class ApiToken { + private $apiToken = null; + + /** + * Token bag + * + * @param $api_token + */ + public function __construct($api_token = null) + { + $this->apiToken = $api_token; + } + + /** + * @param null $apiToken + * @return $this + */ + public function setApiToken($apiToken) + { + $this->apiToken = $apiToken; + return $this; + } + + /** + * @return null + */ + public function getApiToken() + { + return $this->apiToken; + } + +} \ No newline at end of file diff --git a/lib/Tmdb/Client.php b/lib/Tmdb/Client.php new file mode 100644 index 00000000..8d8551fe --- /dev/null +++ b/lib/Tmdb/Client.php @@ -0,0 +1,161 @@ + + * @copyright (c) 2013, Michael Roterman + * @version 0.0.1 + */ +namespace Tmdb; + +use Guzzle\Http\Client as GuzzleClient; +use Guzzle\Http\ClientInterface; +use Guzzle\Http\Message\RequestInterface; + +use Tmdb\Exception\InvalidArgumentException; +use Tmdb\HttpClient\HttpClient; +use Tmdb\HttpClient\HttpClientInterface; + +use Tmdb\ApiToken as Token; +use Tmdb\HttpClient\Plugin\AcceptJsonHeader; +use Tmdb\HttpClient\Plugin\ApiTokenPlugin; + +/** + * Simple wrapper for the Tmdb API + * + * @package Tmdb + */ +class Client { + const TMDB_URI = 'http://private-a868-themoviedb.apiary.io/3/'; + + /** + * @var Token + */ + private $token; + + /** + * @var HttpClientInterface + */ + private $httpClient; + + private $options = array(); + + /** + * Construct our client + * + * @param ClientInterface $httpClient + * @param Token $token + */ + public function __construct(Token $token, ClientInterface $httpClient = null) + { + $httpClient = $httpClient ?: new GuzzleClient(self::TMDB_URI); + + $plugin = new ApiTokenPlugin($token); + $httpClient->addSubscriber($plugin); + + $plugin = new AcceptJsonHeader(); + $httpClient->addSubscriber($plugin); + + $this->httpClient = new HttpClient(self::TMDB_URI, array(), $httpClient); + $this->setToken($token); + } + + /** + * Add the token subscriber + * + * @param Token $token + * @return $this + */ + public function setToken(Token $token) + { + $this->token = $token; + return $this; + } + + /** + * @param $name + * @throws Exception\InvalidArgumentException + */ + public function api($name) + { + switch($name) { + case 'movies': + /** @return Api\Movies */ + $api = new Api\Movies($this); + break; + + default: + throw new InvalidArgumentException(); + } + + return $api; + } + + /** + * @return HttpClientInterface + */ + public function getHttpClient() + { + return $this->httpClient; + } + + /** + * @param HttpClientInterface $httpClient + */ + public function setHttpClient(HttpClientInterface $httpClient) + { + $this->httpClient = $httpClient; + } + + /** + * Clears used headers + */ + public function clearHeaders() + { + $this->httpClient->clearHeaders(); + } + + /** + * @param array $headers + */ + public function setHeaders(array $headers) + { + $this->httpClient->setHeaders($headers); + } + + /** + * @param string $name + * + * @return mixed + * + * @throws InvalidArgumentException + */ + public function getOption($name) + { + if (!array_key_exists($name, $this->options)) { + throw new InvalidArgumentException(sprintf('Undefined option called: "%s"', $name)); + } + + return $this->options[$name]; + } + + /** + * @param string $name + * @param mixed $value + * + * @throws InvalidArgumentException + * @throws InvalidArgumentException + */ + public function setOption($name, $value) + { + if (!array_key_exists($name, $this->options)) { + throw new InvalidArgumentException(sprintf('Undefined option called: "%s"', $name)); + } + + $this->options[$name] = $value; + } +} \ No newline at end of file diff --git a/lib/Tmdb/Exception/InvalidArgumentException.php b/lib/Tmdb/Exception/InvalidArgumentException.php new file mode 100644 index 00000000..ad304508 --- /dev/null +++ b/lib/Tmdb/Exception/InvalidArgumentException.php @@ -0,0 +1,17 @@ + + * @copyright (c) 2013, Michael Roterman + * @version 0.0.1 + */ +namespace Tmdb\Exception; + +class InvalidArgumentException extends \InvalidArgumentException { + +} \ No newline at end of file diff --git a/lib/Tmdb/Exception/MissingArgumentException.php b/lib/Tmdb/Exception/MissingArgumentException.php new file mode 100644 index 00000000..4920e916 --- /dev/null +++ b/lib/Tmdb/Exception/MissingArgumentException.php @@ -0,0 +1,17 @@ + + * @copyright (c) 2013, Michael Roterman + * @version 0.0.1 + */ +namespace Tmdb\Exception; + +class MissingArgumentException extends \InvalidArgumentException { + +} \ No newline at end of file diff --git a/lib/Tmdb/Exception/NotImplementedException.php b/lib/Tmdb/Exception/NotImplementedException.php new file mode 100644 index 00000000..2ce02fcd --- /dev/null +++ b/lib/Tmdb/Exception/NotImplementedException.php @@ -0,0 +1,17 @@ + + * @copyright (c) 2013, Michael Roterman + * @version 0.0.1 + */ +namespace Tmdb\Exception; + +class NotImplementedException extends \Exception { + +} \ No newline at end of file diff --git a/lib/Tmdb/Exception/RuntimeException.php b/lib/Tmdb/Exception/RuntimeException.php new file mode 100644 index 00000000..3962054b --- /dev/null +++ b/lib/Tmdb/Exception/RuntimeException.php @@ -0,0 +1,17 @@ + + * @copyright (c) 2013, Michael Roterman + * @version 0.0.1 + */ +namespace Tmdb\Exception; + +class RuntimeException extends \RuntimeException { + +} \ No newline at end of file diff --git a/lib/Tmdb/HttpClient/HttpClient.php b/lib/Tmdb/HttpClient/HttpClient.php new file mode 100644 index 00000000..dd7bd684 --- /dev/null +++ b/lib/Tmdb/HttpClient/HttpClient.php @@ -0,0 +1,211 @@ + + * @copyright (c) 2013, Michael Roterman + * @version 0.0.1 + */ +namespace Tmdb\HttpClient; + +use Guzzle\Http\ClientInterface; +use Guzzle\Http\Message\Request; +use Guzzle\Http\Message\RequestInterface; +use Guzzle\Http\Message\Response; +use Symfony\Component\EventDispatcher\EventSubscriberInterface; + +class HttpClient + implements HttpClientInterface +{ + /** + * @var \Guzzle\Http\ClientInterface + */ + private $client; + + protected $options = array(); + protected $base_url = null; + protected $headers = array(); + + /** + * @var Response + */ + private $lastResponse; + + /** + * @var Request + */ + private $lastRequest; + + /** + * Constructor + * + * @param $baseUrl + * @param array $options + * @param ClientInterface $client + */ + public function __construct($baseUrl, array $options, ClientInterface $client) + { + $this->base_url = $baseUrl; + $this->options = $options; + $this->client = $client; + + $this->clearHeaders(); + } + + /** + * Add a subscriber + * + * @param EventSubscriberInterface $subscriber + */ + public function addSubscriber(EventSubscriberInterface $subscriber) + { + $this->client->addSubscriber($subscriber); + } + + /** + * Clear up the headers + * @return void + */ + public function clearHeaders() + { + $this->headers = array(); + } + + /** + * @param array $headers + */ + public function setHeaders(array $headers = array()) + { + $this->headers = $headers; + } + + /** + * Set options + * + * @param string $key + * @param mixed $value + */ + public function setOption($key, $value) + { + $this->options[$key] = $value; + } + + /** + * Get an option + * + * @param $key + * @return mixed + */ + public function getOption($key) + { + return $this->options[$key]; + } + + /** + * Set the query parameters + * + * @param $queryParameters + * @return array + */ + protected function buildQueryParameters($queryParameters) + { + return array_merge($this->options, array('query' => $queryParameters)); + } + + /** + * {@inheritDoc} + */ + public function get($path, array $parameters = array(), array $headers = array()) + { + $parameters = $this->buildQueryParameters($parameters); + + return $this->request( + $this->client->get($path, $headers, $parameters) + ); + } + + /** + * {@inheritDoc} + */ + public function head($path, array $parameters = array(), array $headers = array()) + { + $parameters = $this->buildQueryParameters($parameters); + + return $this->request( + $this->client->head($path, $headers, $parameters) + ); + } + + /** + * {@inheritDoc} + */ + public function post($path, $postBody, array $parameters = array(), array $headers = array()) + { + $parameters = $this->buildQueryParameters($parameters); + + return $this->request( + $this->client->post($path, $headers, $postBody, $parameters) + ); + } + + /** + * {@inheritDoc} + */ + public function patch($path, $body = null, array $parameters = array(), array $headers = array()) + { + $parameters = $this->buildQueryParameters($parameters); + + return $this->request( + $this->client->patch($path, $headers, $body, $parameters) + ); + } + + /** + * {@inheritDoc} + */ + public function delete($path, $body = null, array $parameters = array(), array $headers = array()) + { + $parameters = $this->buildQueryParameters($parameters); + + return $this->request( + $this->client->delete($path, $headers, $body, $parameters) + ); + } + + /** + * {@inheritDoc} + */ + public function put($path, $body = null, array $parameters = array(), array $headers = array()) + { + $parameters = $this->buildQueryParameters($parameters); + + return $this->request( + $this->client->put($path, $headers, $body, $parameters) + ); + } + + /** + * @{inheritDoc} + */ + public function request(RequestInterface $request) + { + $response = null; + + try { + $response = $request->send(); + } + catch(\Exception $e) + { + throw $e; + } + + $this->lastRequest = $request; + $this->lastResponse = $response; + + return $response; + } +} \ No newline at end of file diff --git a/lib/Tmdb/HttpClient/HttpClientInterface.php b/lib/Tmdb/HttpClient/HttpClientInterface.php new file mode 100644 index 00000000..c5fac92b --- /dev/null +++ b/lib/Tmdb/HttpClient/HttpClientInterface.php @@ -0,0 +1,103 @@ + + * @copyright (c) 2013, Michael Roterman + * @version 0.0.1 + */ +namespace Tmdb\HttpClient; + +use Guzzle\Http\Message\RequestInterface; +use Guzzle\Http\Message\Response; + +interface HttpClientInterface +{ + /** + * Send a GET request + * + * @param string $path Request path + * @param array $parameters GET Parameters + * @param array $headers Reconfigure the request headers for this call only + * + * @return array Data + */ + public function get($path, array $parameters = array(), array $headers = array()); + + /** + * Send a POST request + * + * @param string $path Request path + * @param string $postBody The post BODY + * @param array $parameters POST Parameters + * @param array $headers Reconfigure the request headers for this call only + * + * @return array Data + */ + public function post($path, $postBody, array $parameters = array(), array $headers = array()); + + /** + * Send a PATCH request + * + * @param string $path Request path + * @param string $body The body + * @param array $parameters PATCH Parameters + * @param array $headers Reconfigure the request headers for this call only + * + * @return array Data + */ + public function patch($path, $body = null, array $parameters = array(), array $headers = array()); + + /** + * Send a PUT request + * + * @param string $path Request path + * @param string $body The body + * @param array $parameters PUT Parameters + * @param array $headers Reconfigure the request headers for this call only + * + * @return array Data + */ + public function put($path, $body = null, array $parameters = array(), array $headers = array()); + + /** + * Send a DELETE request + * + * @param string $path Request path + * @param string $body The body + * @param array $parameters DELETE Parameters + * @param array $headers Reconfigure the request headers for this call only + * + * @return array Data + */ + public function delete($path, $body = null, array $parameters = array(), array $headers = array()); + + /** + * Send a request to the server, receive a response, + * decode the response and returns an associative array + * @param $request RequestInterface + * @return Response + */ + public function request(RequestInterface $request); + + /** + * Change an option value. + * + * @param string $name The option name + * @param mixed $value The value + * + * @throws InvalidArgumentException + */ + public function setOption($name, $value); + + /** + * Set HTTP headers + * + * @param array $headers + */ + public function setHeaders(array $headers); +} \ No newline at end of file diff --git a/lib/Tmdb/HttpClient/Plugin/AcceptJsonHeader.php b/lib/Tmdb/HttpClient/Plugin/AcceptJsonHeader.php index 3400c0c0..d2a96ee6 100644 --- a/lib/Tmdb/HttpClient/Plugin/AcceptJsonHeader.php +++ b/lib/Tmdb/HttpClient/Plugin/AcceptJsonHeader.php @@ -1,34 +1,22 @@ - * @copyright (c) 2013, B-Found Internet Marketing & Services + * + * @package Tmdb + * @author Michael Roterman + * @copyright (c) 2013, Michael Roterman * @version 0.0.1 */ - namespace Tmdb\HttpClient\Plugin; use Guzzle\Common\Event; use Symfony\Component\EventDispatcher\EventSubscriberInterface; -use Tmdb\ApiToken; -class ApiTokenPlugin implements EventSubscriberInterface +class AcceptJsonHeader implements EventSubscriberInterface { - /** - * @var \Tmdb\ApiToken - */ - private $token; - - public function __construct(ApiToken $token) - { - $this->token = $token; - } - public static function getSubscribedEvents() { return array('request.before_send' => 'onBeforeSend'); @@ -36,13 +24,6 @@ public static function getSubscribedEvents() public function onBeforeSend(Event $event) { - $url = $event['request']->getUrl(true); - - $origionalQuery = $url->getQuery(); - $newQuery = clone $origionalQuery; - - $newQuery->set('api_key', $this->token->getApiToken()); - - $event['request']->setUrl($url->setQuery($newQuery)); + $event['request']->setHeader('accept', 'application/json'); } } \ No newline at end of file diff --git a/lib/Tmdb/HttpClient/Plugin/ApiTokenPlugin.php b/lib/Tmdb/HttpClient/Plugin/ApiTokenPlugin.php index cbbd5fb6..bb271074 100644 --- a/lib/Tmdb/HttpClient/Plugin/ApiTokenPlugin.php +++ b/lib/Tmdb/HttpClient/Plugin/ApiTokenPlugin.php @@ -1,19 +1,47 @@ - * @copyright (c) 2013, B-Found Internet Marketing & Services + * + * @package Tmdb + * @author Michael Roterman + * @copyright (c) 2013, Michael Roterman * @version 0.0.1 */ - namespace Tmdb\HttpClient\Plugin; +use Guzzle\Common\Event; +use Symfony\Component\EventDispatcher\EventSubscriberInterface; +use Tmdb\ApiToken; + +class ApiTokenPlugin implements EventSubscriberInterface +{ + /** + * @var \Tmdb\ApiToken + */ + private $token; + + public function __construct(ApiToken $token) + { + $this->token = $token; + } + + public static function getSubscribedEvents() + { + return array('request.before_send' => 'onBeforeSend'); + } + + public function onBeforeSend(Event $event) + { + $url = $event['request']->getUrl(true); + + $origionalQuery = $url->getQuery(); + $newQuery = clone $origionalQuery; -class ApiTokenPlugin { + $newQuery->set('api_key', $this->token->getApiToken()); -} \ No newline at end of file + $event['request']->setUrl($url->setQuery($newQuery)); + } +} \ No newline at end of file diff --git a/lib/Tmdb/Model/AbstractModel.php b/lib/Tmdb/Model/AbstractModel.php new file mode 100644 index 00000000..0863bf38 --- /dev/null +++ b/lib/Tmdb/Model/AbstractModel.php @@ -0,0 +1,89 @@ + + * @copyright (c) 2013, Michael Roterman + * @version 0.0.1 + */ +namespace Tmdb\Model; + +use Tmdb\Client; +use Tmdb\Exception\RuntimeException; + +class AbstractModel { + protected static $_properties; + + protected $_data = array(); + protected $_client = null; + + /** + * Retrieve the client + * + * @return Client + */ + public function getClient() + { + return $this->_client; + } + + /** + * Set the client + * + * @param Client $client + * @return $this + */ + public function setClient(Client $client = null) + { + if (null !== $client) { + $this->_client = $client; + } + + return $this; + } + + /** + * Call a part of the API + * + * @param $api + * @return mixed + */ + 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 = sprintf('set%s', ucfirst($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; + } +} \ No newline at end of file