11<?php
22
3+ declare (strict_types=1 );
4+
35/**
46 * This file is part of the Tmdb PHP API created by Michael Roterman.
57 *
68 * For the full copyright and license information, please view the LICENSE
79 * file that was distributed with this source code.
810 *
9- * @package Tmdb
1011 * @author Michael Roterman <michael@wtfz.net>
1112 * @copyright (c) 2013, Michael Roterman
13+ *
1214 * @version 4.0.0
1315 */
1416
2325use Tmdb \HttpClient \HttpClient ;
2426
2527/**
26- * Class AbstractApi
27- * @package Tmdb\Api
28+ * Class AbstractApi.
2829 */
2930abstract class AbstractApi implements ApiInterface
3031{
3132 /**
32- * The client
33- *
34- * @var Client
35- */
36- protected $ client ;
37-
38- /**
39- * Constructor
40- *
41- * @param Client $client
33+ * Constructor.
4234 */
43- public function __construct (Client $ client )
35+ public function __construct (
36+ /**
37+ * The client.
38+ */
39+ protected \Tmdb \Client $ client
40+ )
4441 {
45- $ this ->client = $ client ;
4642 }
4743
4844 /**
49- * Send a GET request
50- *
51- * @param string $path
52- * @param array $parameters
53- * @param array $headers
54- * @return array
45+ * Send a GET request.
5546 */
5647 public function get (string $ path , array $ parameters = [], array $ headers = []): array
5748 {
5849 return $ this ->decodeResponse (
59- $ this ->getHttpClient ()->send ($ path , 'GET ' , $ parameters , $ headers )
50+ $ this ->getHttpClient ()->send ($ path , 'GET ' , $ parameters , $ headers ),
6051 );
6152 }
6253
6354 /**
64- * Send a POST request
65- *
66- * @param string $path
67- * @param null $postBody
68- * @param array $parameters
69- * @param array $headers
70- * @return array
55+ * Send a POST request.
7156 */
7257 public function post (string $ path , $ postBody = null , array $ parameters = [], array $ headers = []): array
7358 {
7459 return $ this ->decodeResponse (
75- $ this ->getHttpClient ()->send ($ path , 'POST ' , $ parameters , $ headers , $ postBody )
60+ $ this ->getHttpClient ()->send ($ path , 'POST ' , $ parameters , $ headers , $ postBody ),
7661 );
7762 }
7863
7964 /**
80- * Send a POST request but json_encode the post body in the request
65+ * Send a POST request but json_encode the post body in the request.
8166 *
82- * @param string $path
83- * @param mixed $postBody
84- * @param array $parameters
85- * @param array $headers
86- * @return array
8767 * @throws InvalidArgumentException
8868 */
8969 public function postJson (string $ path , $ postBody = null , array $ parameters = [], array $ headers = []): array
9070 {
9171 try {
92- if (is_array ($ postBody )) {
72+ if (\ is_array ($ postBody )) {
9373 $ postBody = json_encode ($ postBody , JSON_THROW_ON_ERROR );
9474 }
9575
9676 return $ this ->post ($ path , $ postBody , $ parameters , $ headers );
9777 } catch (JsonException $ e ) {
98- throw new InvalidArgumentException (
99- 'Unable to json_encode the data provided. ' ,
100- 0 ,
101- $ e
102- );
78+ throw new InvalidArgumentException ('Unable to json_encode the data provided. ' , 0 , $ e );
10379 }
10480 }
10581
10682 /**
107- * Send a HEAD request
108- *
109- * @param string $path
110- * @param array $parameters
111- * @param array $headers
112- * @return array
83+ * Send a HEAD request.
11384 */
11485 public function head (string $ path , array $ parameters = [], array $ headers = []): array
11586 {
11687 return $ this ->decodeResponse (
117- $ this ->getHttpClient ()->send ($ path , 'HEAD ' , $ parameters , $ headers )
88+ $ this ->getHttpClient ()->send ($ path , 'HEAD ' , $ parameters , $ headers ),
11889 );
11990 }
12091
12192 /**
122- * Send a PUT request
123- *
124- * @param string $path
125- * @param null $body
126- * @param array $parameters
127- * @param array $headers
128- * @return array
93+ * Send a PUT request.
12994 */
13095 public function put (string $ path , $ body = null , array $ parameters = [], array $ headers = []): array
13196 {
13297 return $ this ->decodeResponse (
133- $ this ->getHttpClient ()->send ($ path , 'PUT ' , $ parameters , $ headers , $ body )
98+ $ this ->getHttpClient ()->send ($ path , 'PUT ' , $ parameters , $ headers , $ body ),
13499 );
135100 }
136101
137102 /**
138- * Send a DELETE request
139- *
140- * @param string $path
141- * @param null $body
142- * @param array $parameters
143- * @param array $headers
144- * @return array
103+ * Send a DELETE request.
145104 */
146105 public function delete (string $ path , $ body = null , array $ parameters = [], array $ headers = []): array
147106 {
148107 return $ this ->decodeResponse (
149- $ this ->getHttpClient ()->send ($ path , 'DELETE ' , $ parameters , $ headers , $ body )
108+ $ this ->getHttpClient ()->send ($ path , 'DELETE ' , $ parameters , $ headers , $ body ),
150109 );
151110 }
152111
153112 /**
154- * Send a PATCH request
113+ * Send a PATCH request.
155114 *
156- * @param string $path
157115 * @param string|null $body
158- * @param array $parameters
159- * @param array $headers
160- * @return array
161116 */
162117 public function patch (string $ path , $ body = null , array $ parameters = [], array $ headers = []): array
163118 {
164119 return $ this ->decodeResponse (
165- $ this ->getHttpClient ()->send ($ path , 'PATCH ' , $ parameters , $ headers , $ body )
120+ $ this ->getHttpClient ()->send ($ path , 'PATCH ' , $ parameters , $ headers , $ body ),
166121 );
167122 }
168123
169124 /**
170- * Retrieve the client
125+ * Retrieve the client.
171126 *
172127 * @return Client
173128 */
@@ -177,7 +132,7 @@ public function getClient()
177132 }
178133
179134 /**
180- * Retrieve the http client
135+ * Retrieve the http client.
181136 *
182137 * @return HttpClient
183138 */
@@ -187,53 +142,36 @@ public function getHttpClient()
187142 }
188143
189144 /**
190- * Decode the response
145+ * Decode the response.
191146 *
192- * @param ResponseInterface $response
193147 * @return array
148+ *
194149 * @throws UnexpectedResponseException
195150 */
196151 private function decodeResponse (ResponseInterface $ response )
197152 {
198153 if (!$ response ->getBody () instanceof StreamInterface) {
199- throw new UnexpectedResponseException (
200- 'Response body is not a valid StreamInterface instance ' ,
201- $ response ->getStatusCode ()
202- );
154+ throw new UnexpectedResponseException ('Response body is not a valid StreamInterface instance ' , $ response ->getStatusCode ());
203155 }
204156
205- $ body = (string )$ response ->getBody ();
206-
157+ $ body = (string ) $ response ->getBody ();
158+
207159 // If the body is empty, we should still throw an exception
208160 // Empty responses are only acceptable for 204 No Content responses
209- if (empty ( $ body) ) {
210- if ($ response ->getStatusCode () === 204 ) {
161+ if ($ body === '' || $ body === ' 0 ' ) {
162+ if (204 === $ response ->getStatusCode ()) {
211163 return [];
212164 }
213-
214- throw new UnexpectedResponseException (
215- sprintf (
216- 'Empty response body with status code %d ' ,
217- $ response ->getStatusCode ()
218- ),
219- $ response ->getStatusCode ()
220- );
165+
166+ throw new UnexpectedResponseException (\sprintf ('Empty response body with status code %d ' , $ response ->getStatusCode ()), $ response ->getStatusCode ());
221167 }
222-
168+
223169 try {
224170 // Decode any response with a valid JSON body, regardless of status code
225171 // This ensures we capture error details from 4xx/5xx responses
226172 return json_decode ($ body , true , 512 , JSON_THROW_ON_ERROR );
227173 } catch (JsonException $ e ) {
228- throw new UnexpectedResponseException (
229- sprintf (
230- 'Unable to decode response with body "%s", %s. ' ,
231- $ body ,
232- json_last_error_msg ()
233- ),
234- $ response ->getStatusCode (),
235- $ e
236- );
174+ throw new UnexpectedResponseException (\sprintf ('Unable to decode response with body "%s", %s. ' , $ body , json_last_error_msg ()), $ response ->getStatusCode (), $ e );
237175 }
238176 }
239177}
0 commit comments