1 | <?php |
||
11 | class Client implements ClientInterface |
||
12 | { |
||
13 | /** |
||
14 | * The default ttl for cached responses (24 hours). |
||
15 | * |
||
16 | * @link http://developer.marvel.com/documentation/attribution Marvel's rules for caching. |
||
17 | * |
||
18 | * @const integer |
||
19 | */ |
||
20 | const MAX_TTL = 86400; |
||
21 | |||
22 | /** |
||
23 | * The public API key issued by Marvel. |
||
24 | * |
||
25 | * @var string |
||
26 | */ |
||
27 | private $publicApiKey; |
||
28 | |||
29 | /** |
||
30 | * The private API key issued by Marvel. |
||
31 | * |
||
32 | * @var string |
||
33 | */ |
||
34 | private $privateApiKey; |
||
35 | |||
36 | /** |
||
37 | * Guzzle HTTP Client implementation. |
||
38 | * |
||
39 | * @var GuzzleHttp\ClientInterface |
||
40 | */ |
||
41 | private $guzzleClient; |
||
42 | |||
43 | /** |
||
44 | * Cache implementation. |
||
45 | * |
||
46 | * @var CacheInterface |
||
47 | */ |
||
48 | private $cache; |
||
49 | |||
50 | /** |
||
51 | * Default Guzzle request options. |
||
52 | * |
||
53 | * @var array |
||
54 | */ |
||
55 | private static $guzzleRequestOptions = [ |
||
56 | 'http_errors' => false, |
||
57 | 'headers' => [ |
||
58 | 'Accept' => 'application/json', |
||
59 | 'Accept-Encoding' => 'gzip,deflate', |
||
60 | ], |
||
61 | ]; |
||
62 | |||
63 | /** |
||
64 | * The Marvel API URL. |
||
65 | * |
||
66 | * @const string |
||
67 | */ |
||
68 | const BASE_URL = 'http://gateway.marvel.com/v1/public/'; |
||
69 | |||
70 | /** |
||
71 | * Construct a new Client. |
||
72 | * |
||
73 | * @param string $privateApiKey The private API key issued by Marvel. |
||
74 | * @param string $publicApiKey The public API key issued by Marvel. |
||
75 | * @param GuzzleHttp\ClientInterface $guzzleClient Implementation of a Guzzle HTTP client. |
||
76 | * @param CacheInterface $cache Implementation of Cache. |
||
77 | */ |
||
78 | final public function __construct( |
||
89 | |||
90 | /** |
||
91 | * Execute a search request against the Marvel API. |
||
92 | * |
||
93 | * @param string $resource The API resource to search for. |
||
94 | * @param array $filters Array of search criteria to use in request. |
||
95 | * |
||
96 | * @return null|DataWrapper |
||
97 | */ |
||
98 | final public function search(string $resource, array $filters = []) |
||
102 | |||
103 | /** |
||
104 | * Execute a GET request against the Marvel API for a single resource. |
||
105 | * |
||
106 | * @param string $resource The API resource to search for. |
||
107 | * @param integer $id The id of the API resource. |
||
108 | * |
||
109 | * @return null|DataWrapper |
||
110 | */ |
||
111 | final public function get(string $resource, int $id) |
||
115 | |||
116 | /** |
||
117 | * Send the given API URL request. |
||
118 | * |
||
119 | * @param string $resource The API resource to search for. |
||
120 | * @param integer $id The id of a specific API resource. |
||
121 | * @param array $query Array of search criteria to use in request. |
||
122 | * |
||
123 | * @return null|DataWrapperInterface |
||
124 | */ |
||
125 | final private function send(string $resource, int $id = null, array $query = []) |
||
145 | |||
146 | /** |
||
147 | * Allow calls such as $client->characters(); |
||
148 | * |
||
149 | * @param string $name The name of the API resource. |
||
150 | * @param array $arguments The parameters to pass to get() or search(). |
||
151 | * |
||
152 | * @return Collection|EntityInterface|null |
||
153 | */ |
||
154 | final public function __call(string $name, array $arguments) |
||
171 | } |
||
172 |