1 | <?php declare(strict_types=1); |
||
30 | class Tmdb implements TmdbInterface |
||
31 | { |
||
32 | |||
33 | /** |
||
34 | * API Key |
||
35 | * @var string |
||
36 | */ |
||
37 | private $api_key = null; |
||
38 | |||
39 | /** |
||
40 | * Default language for API response |
||
41 | * @var string |
||
42 | */ |
||
43 | private $language = 'fr-FR'; |
||
44 | |||
45 | /** |
||
46 | * Include adult content in search result |
||
47 | * @var bool |
||
48 | */ |
||
49 | private $include_adult = false; |
||
50 | |||
51 | /** |
||
52 | * API Page result |
||
53 | * @var int |
||
54 | */ |
||
55 | private $page = 1; |
||
56 | |||
57 | /** |
||
58 | * API configuration |
||
59 | * @var \stdClass |
||
60 | */ |
||
61 | protected $configuration = null; |
||
62 | |||
63 | /** |
||
64 | * API Genres |
||
65 | * @var \stdClass |
||
66 | */ |
||
67 | protected $genres = null; |
||
68 | |||
69 | /** |
||
70 | * Base URL of the API |
||
71 | * @var string |
||
72 | */ |
||
73 | public $base_api_url = 'https://api.themoviedb.org/'; |
||
74 | |||
75 | /** |
||
76 | * Logger |
||
77 | * @var LoggerInterface |
||
78 | */ |
||
79 | protected $logger = null; |
||
80 | |||
81 | /** |
||
82 | * API Version |
||
83 | * @var int |
||
84 | */ |
||
85 | protected $version = 3; |
||
86 | |||
87 | /** |
||
88 | * Http request object |
||
89 | * @var HttpRequestInterface |
||
90 | */ |
||
91 | protected $http_request = null; |
||
92 | /** |
||
93 | * Request object |
||
94 | * @var \stdClass |
||
95 | */ |
||
96 | protected $request; |
||
97 | /** |
||
98 | * Last request url |
||
99 | * @var string |
||
100 | */ |
||
101 | protected $url = null; |
||
102 | |||
103 | /** |
||
104 | * Constructor |
||
105 | * @param string $api_key TMDB API Key |
||
106 | * @param int $version Version of API (Not yet used) |
||
107 | * @param LoggerInterface $logger Logger used in the class |
||
108 | * @param HttpRequestInterface $http_request |
||
109 | */ |
||
110 | 356 | public function __construct(string $api_key, int $version = 3, LoggerInterface $logger, HttpRequestInterface $http_request) |
|
118 | |||
119 | /** |
||
120 | * Send request to TMDB API with GET method |
||
121 | * @param string $action API action to request |
||
122 | * @param array $options Array of options of the request (optional) |
||
123 | * @return \stdClass|null |
||
124 | */ |
||
125 | 317 | public function getRequest(string $action, array $options = array()) : ?\stdClass |
|
131 | |||
132 | /** |
||
133 | * Send request to TMDB API with POST method |
||
134 | * @param string $action API action to request |
||
135 | * @param array $options Array of options of the request (optional) |
||
136 | * @param array $form_params form_params for request options |
||
137 | * @return \stdClass|null |
||
138 | */ |
||
139 | 15 | public function postRequest(string $action, array $options = array(), array $form_params = array()) : ?\stdClass |
|
145 | |||
146 | /** |
||
147 | * Send request to TMDB API with DELETE method |
||
148 | * @param string $action API action to request |
||
149 | * @param array $options Array of options of the request (optional) |
||
150 | * @return \stdClass|null |
||
151 | */ |
||
152 | 5 | public function deleteRequest(string $action, array $options = array()) : ?\stdClass |
|
158 | |||
159 | /** |
||
160 | * Send request to TMDB API with GET method |
||
161 | * @param string $method HTTP method (GET, POST) |
||
162 | * @param string $url API url to request |
||
163 | * @param array $form_params form params request options |
||
164 | * @return \stdClass|null |
||
165 | */ |
||
166 | 6 | protected function sendRequest(string $method, string $url, array $form_params = array()) : ?\stdClass |
|
188 | |||
189 | /** |
||
190 | * Decode request response |
||
191 | * @param mixed $res |
||
192 | * @param string $method |
||
193 | * @param string $url |
||
194 | * @param array $form_params |
||
195 | * @return string |
||
196 | */ |
||
197 | 6 | private function decodeRequest($res, $method, $url, $form_params) |
|
210 | |||
211 | /** |
||
212 | * Build URL for HTTP Call |
||
213 | * @param string $action API action to request |
||
214 | * @param array $options Array of options of the request (optional) |
||
215 | * @return string |
||
216 | */ |
||
217 | 319 | private function buildHTTPUrl(string $action, array $options) : string |
|
233 | |||
234 | /** |
||
235 | * Get API Configuration |
||
236 | * @return \stdClass |
||
237 | * @throws TmdbException |
||
238 | */ |
||
239 | 50 | public function getConfiguration() : \stdClass |
|
252 | |||
253 | /** |
||
254 | * Get logger |
||
255 | * @return LoggerInterface |
||
256 | */ |
||
257 | 324 | public function getLogger() : LoggerInterface |
|
261 | |||
262 | /** |
||
263 | * Magical property getter |
||
264 | * @param string $name Name of the property |
||
265 | * @return string Value of the property |
||
266 | */ |
||
267 | 73 | public function __get(string $name) : string |
|
276 | |||
277 | /** |
||
278 | * Check year option and return correct value |
||
279 | * @param array $options |
||
280 | * @param array &$return Return array to save valid option |
||
281 | * @return void |
||
282 | */ |
||
283 | 29 | public function checkOptionYear(array $options, array &$return) : void |
|
289 | |||
290 | /** |
||
291 | * Check Language string with format ISO 639-1 |
||
292 | * @param array $options |
||
293 | * @param array &$return Return array to save valid option |
||
294 | * @return void |
||
295 | */ |
||
296 | 277 | public function checkOptionLanguage(array $options, array &$return) : void |
|
307 | |||
308 | /** |
||
309 | * Check include adult option |
||
310 | * @param array $options |
||
311 | * @param array &$return Return array to save valid option |
||
312 | * @return void |
||
313 | */ |
||
314 | 29 | public function checkOptionIncludeAdult(array $options, array &$return) : void |
|
320 | |||
321 | /** |
||
322 | * Check page option |
||
323 | * @param array $options |
||
324 | * @param array &$return Return array to save valid option |
||
325 | * @return void |
||
326 | */ |
||
327 | 38 | public function checkOptionPage(array $options, array &$return) : void |
|
333 | |||
334 | /** |
||
335 | * Check sort by option |
||
336 | * @param array $options |
||
337 | * @param array &$return Return array to save valid option |
||
338 | * @return void |
||
339 | */ |
||
340 | 9 | public function checkOptionSortBy(array $options, array &$return) : void |
|
353 | |||
354 | /** |
||
355 | * Check query option |
||
356 | * @param array $options |
||
357 | * @param array &$return Return array to save valid option |
||
358 | * @return void |
||
359 | */ |
||
360 | 28 | public function checkOptionQuery(array $options, array &$return) : void |
|
366 | |||
367 | /** |
||
368 | * Check session_id option |
||
369 | * @param array $options |
||
370 | * @param array &$return Return array to save valid option |
||
371 | * @return void |
||
372 | */ |
||
373 | 7 | public function checkOptionSessionId(array $options, array &$return) : void |
|
379 | } |
||
380 |
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.