1 | <?php |
||
13 | class Response implements ResponseInterface |
||
14 | { |
||
15 | /** |
||
16 | * The original response object. |
||
17 | * |
||
18 | * @var ResponseInterface |
||
19 | */ |
||
20 | protected $response; |
||
21 | |||
22 | /** |
||
23 | * The attributes returned from the request. |
||
24 | * |
||
25 | * @var array |
||
26 | */ |
||
27 | protected $attributes = []; |
||
28 | |||
29 | /** |
||
30 | * Store the response object. |
||
31 | * |
||
32 | * @param ResponseInterface $response |
||
33 | */ |
||
34 | 76 | public function __construct(ResponseInterface $response) |
|
40 | |||
41 | /** |
||
42 | * Allow attributes to be retrieved as if properties on the class. |
||
43 | * |
||
44 | * @param string $name |
||
45 | * @return mixed |
||
46 | */ |
||
47 | 70 | public function __get($name) |
|
58 | |||
59 | /** |
||
60 | * Format errors in a unified object format, regardless of whether the supplied errors are a list of objects or a |
||
61 | * single error message string. |
||
62 | * |
||
63 | * @param object $errorBody |
||
64 | * @return array |
||
65 | */ |
||
66 | 5 | protected function formatErrors($errorBody) |
|
88 | |||
89 | /** |
||
90 | * Take in an array of errors with separate ID and description, and format them as an associative array of [$id => |
||
91 | * $description]. |
||
92 | 1 | * |
|
93 | * @param array $inputErrors |
||
94 | 1 | * @return array |
|
95 | */ |
||
96 | protected function errorsToArray($inputErrors) |
||
105 | 10 | ||
106 | /** |
||
107 | * Check if the response contains any error messages. |
||
108 | * |
||
109 | * @return bool |
||
110 | */ |
||
111 | public function hasErrorMessages() |
||
115 | 3 | ||
116 | /** |
||
117 | * Get the specified attribute. |
||
118 | * |
||
119 | * @param string $name |
||
120 | * @return mixed |
||
121 | */ |
||
122 | public function getAttribute($name) |
||
126 | |||
127 | /** |
||
128 | * Get all attributes. |
||
129 | * |
||
130 | * @return array |
||
131 | */ |
||
132 | public function getAttributes() |
||
136 | 31 | ||
137 | 31 | /** |
|
138 | * Decode the JSON body. |
||
139 | * |
||
140 | * @return mixed |
||
141 | */ |
||
142 | public function getBodyAsObject() |
||
146 | 6 | ||
147 | /** |
||
148 | 6 | * If there a 'success' variable contained in the response body, return that. If not, return true if the response |
|
149 | 1 | * has a 2xx status code. |
|
150 | * |
||
151 | * @return bool |
||
152 | 5 | */ |
|
153 | public function wasSuccessful() |
||
158 | 76 | ||
159 | /** |
||
160 | 76 | * Check if the requested resource exists. Throw an exception if the status is not 200 or 404. |
|
161 | 60 | * |
|
162 | 22 | * @return bool |
|
163 | 76 | * @throws UnexpectedStatusException |
|
164 | */ |
||
165 | public function existenceCheck() |
||
173 | |||
174 | /** |
||
175 | * Update the attributes array from the decoded JSON response. |
||
176 | */ |
||
177 | protected function updateAttributesArray() |
||
183 | |||
184 | // Parent Response class should have its own tests for the remaining methods |
||
185 | // @codeCoverageIgnoreStart |
||
186 | |||
187 | /** |
||
188 | * Defer all unknown methods to main response class. |
||
189 | * |
||
190 | * @param $method |
||
191 | * @param $args |
||
192 | * @return mixed |
||
193 | */ |
||
194 | public function __call($method, $args) |
||
198 | |||
199 | /** |
||
200 | * Retrieves the HTTP protocol version as a string. |
||
201 | * |
||
202 | * @return string HTTP protocol version. |
||
203 | */ |
||
204 | public function getProtocolVersion() |
||
208 | |||
209 | /** |
||
210 | * Return an instance with the specified HTTP protocol version. |
||
211 | * |
||
212 | * @param string $version HTTP protocol version |
||
213 | * @return ResponseInterface |
||
214 | */ |
||
215 | public function withProtocolVersion($version) |
||
219 | |||
220 | /** |
||
221 | * Retrieves all message header values. |
||
222 | * |
||
223 | * @return string[][] Returns an associative array of the message's headers. Each |
||
224 | * key MUST be a header name, and each value MUST be an array of strings |
||
225 | * for that header. |
||
226 | */ |
||
227 | public function getHeaders() |
||
231 | |||
232 | /** |
||
233 | * Checks if a header exists by the given case-insensitive name. |
||
234 | * |
||
235 | * @param string $name Case-insensitive header field name. |
||
236 | * @return bool Returns true if any header names match the given header |
||
237 | * name using a case-insensitive string comparison. Returns false if |
||
238 | * no matching header name is found in the message. |
||
239 | */ |
||
240 | public function hasHeader($name) |
||
244 | |||
245 | /** |
||
246 | * Retrieves a message header value by the given case-insensitive name. |
||
247 | * |
||
248 | * @param string $name Case-insensitive header field name. |
||
249 | * @return string[] An array of string values as provided for the given |
||
250 | * header. If the header does not appear in the message, this method MUST |
||
251 | * return an empty array. |
||
252 | */ |
||
253 | public function getHeader($name) |
||
257 | |||
258 | /** |
||
259 | * Retrieves a comma-separated string of the values for a single header. |
||
260 | * |
||
261 | * @param string $name Case-insensitive header field name. |
||
262 | * @return string A string of values as provided for the given header |
||
263 | * concatenated together using a comma. If the header does not appear in |
||
264 | * the message, this method MUST return an empty string. |
||
265 | */ |
||
266 | public function getHeaderLine($name) |
||
270 | |||
271 | /** |
||
272 | * Return an instance with the provided value replacing the specified header. |
||
273 | * |
||
274 | * @param string $name Case-insensitive header field name. |
||
275 | * @param string|string[] $value Header value(s). |
||
276 | * @return ResponseInterface |
||
277 | * @throws \InvalidArgumentException for invalid header names or values. |
||
278 | */ |
||
279 | public function withHeader($name, $value) |
||
283 | |||
284 | /** |
||
285 | * Return an instance with the specified header appended with the given value. |
||
286 | * |
||
287 | * @param string $name Case-insensitive header field name to add. |
||
288 | * @param string|string[] $value Header value(s). |
||
289 | * @return ResponseInterface |
||
290 | * @throws \InvalidArgumentException for invalid header names or values. |
||
291 | */ |
||
292 | public function withAddedHeader($name, $value) |
||
296 | |||
297 | /** |
||
298 | * Return an instance without the specified header. |
||
299 | * |
||
300 | * @param string $name Case-insensitive header field name to remove. |
||
301 | * @return ResponseInterface |
||
302 | */ |
||
303 | public function withoutHeader($name) |
||
307 | |||
308 | /** |
||
309 | * Gets the body of the message. |
||
310 | * |
||
311 | * @return StreamInterface Returns the body as a stream. |
||
312 | */ |
||
313 | public function getBody() |
||
317 | |||
318 | /** |
||
319 | * Return an instance with the specified message body. |
||
320 | * |
||
321 | * @param StreamInterface $body Body. |
||
322 | * @return ResponseInterface |
||
323 | * @throws \InvalidArgumentException When the body is not valid. |
||
324 | */ |
||
325 | public function withBody(StreamInterface $body) |
||
329 | |||
330 | /** |
||
331 | * Gets the response status code. |
||
332 | * |
||
333 | * @return int Status code. |
||
334 | */ |
||
335 | public function getStatusCode() |
||
339 | |||
340 | /** |
||
341 | * Return an instance with the specified status code and, optionally, reason phrase. |
||
342 | * |
||
343 | * @param int $code The 3-digit integer result code to set. |
||
344 | * @param string $reasonPhrase The reason phrase to use with the |
||
345 | * provided status code; if none is provided, implementations MAY |
||
346 | * use the defaults as suggested in the HTTP specification. |
||
347 | * @return ResponseInterface * @throws \InvalidArgumentException For invalid status code arguments. |
||
348 | */ |
||
349 | public function withStatus($code, $reasonPhrase = '') |
||
353 | |||
354 | /** |
||
355 | * Gets the response reason phrase associated with the status code. |
||
356 | * |
||
357 | * @return string Reason phrase; must return an empty string if none present. |
||
358 | */ |
||
359 | public function getReasonPhrase() |
||
363 | |||
364 | // @codeCoverageIgnoreEnd |
||
365 | } |
||
366 |
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArray
is initialized the first time when the foreach loop is entered. You can also see that the value of thebar
key is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.