1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* Copyright (c) Nate Brunette. |
4
|
|
|
* Distributed under the MIT License (http://opensource.org/licenses/MIT) |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
namespace Tebru\Retrofit\Http; |
8
|
|
|
|
9
|
|
|
use JMS\Serializer\DeserializationContext; |
10
|
|
|
use JMS\Serializer\SerializerInterface; |
11
|
|
|
use Psr\Http\Message\ResponseInterface; |
12
|
|
|
use Psr\Http\Message\StreamInterface; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class Response |
16
|
|
|
* |
17
|
|
|
* @author Nate Brunette <[email protected]> |
18
|
|
|
*/ |
19
|
|
|
class Response implements ResponseInterface |
20
|
|
|
{ |
21
|
|
|
const FORMAT_RAW = 'raw'; |
22
|
|
|
const FORMAT_ARRAY = 'array'; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var ResponseInterface |
26
|
|
|
*/ |
27
|
|
|
private $response; |
28
|
|
|
/** |
29
|
|
|
* @var |
30
|
|
|
*/ |
31
|
|
|
private $returnType; |
32
|
|
|
/** |
33
|
|
|
* @var SerializerInterface |
34
|
|
|
*/ |
35
|
|
|
private $serializer; |
36
|
|
|
/** |
37
|
|
|
* @var array |
38
|
|
|
*/ |
39
|
|
|
private $context; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Constructor |
43
|
|
|
* |
44
|
|
|
* @param ResponseInterface $response |
45
|
|
|
* @param string $returnType |
46
|
|
|
* @param SerializerInterface $serializer |
47
|
|
|
* @param array $context |
48
|
|
|
*/ |
49
|
|
|
public function __construct( |
50
|
|
|
ResponseInterface $response, |
51
|
|
|
$returnType, |
52
|
|
|
SerializerInterface $serializer, |
53
|
|
|
array $context = [] |
54
|
|
|
) { |
55
|
|
|
$this->response = $response; |
56
|
|
|
$this->returnType = $returnType; |
57
|
|
|
$this->serializer = $serializer; |
58
|
|
|
$this->context = $context; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Get the body specified by the Returns annotation |
63
|
|
|
* |
64
|
|
|
* @return mixed |
65
|
|
|
*/ |
66
|
|
|
public function body() |
67
|
|
|
{ |
68
|
|
|
$response = null; |
|
|
|
|
69
|
|
|
$responseBody = (string) $this->response->getBody(); |
70
|
|
|
switch ($this->returnType) { |
71
|
|
|
case self::FORMAT_RAW: |
72
|
|
|
$response = $responseBody; |
73
|
|
|
break; |
74
|
|
|
case self::FORMAT_ARRAY: |
75
|
|
|
$response = json_decode($responseBody, true); |
76
|
|
|
break; |
77
|
|
|
default: |
78
|
|
|
$context = $this->createContext(); |
79
|
|
|
$response = $this->serializer->deserialize($responseBody, $this->returnType, 'json', $context); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
return $response; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Build the deserialization context |
87
|
|
|
*/ |
88
|
|
|
private function createContext() |
89
|
|
|
{ |
90
|
|
|
$context = new DeserializationContext(); |
91
|
|
|
|
92
|
|
|
if (!empty($this->context['groups'])) { |
93
|
|
|
$context->setGroups($this->context['groups']); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
if (!empty($this->context['version'])) { |
97
|
|
|
$context->setVersion((int) $this->context['version']); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
if (!empty($this->context['serializeNull'])) { |
101
|
|
|
$context->setSerializeNull((bool) $this->context['serializeNull']); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
if (!empty($this->context['enableMaxDepthChecks'])) { |
105
|
|
|
$context->enableMaxDepthChecks(); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
View Code Duplication |
if (!empty($this->context['attributes'])) { |
|
|
|
|
109
|
|
|
foreach ($this->context['attributes'] as $key => $value) { |
110
|
|
|
$context->setAttribute($key, $value); |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
if (!empty($this->context['depth'])) { |
115
|
|
|
$contextDepth = (int) $this->context['depth']; |
116
|
|
|
while ($context->getDepth() < $contextDepth) { |
117
|
|
|
$context->increaseDepth(); |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
return $context; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Retrieves the HTTP protocol version as a string. |
126
|
|
|
* |
127
|
|
|
* The string MUST contain only the HTTP version number (e.g., "1.1", "1.0"). |
128
|
|
|
* |
129
|
|
|
* @return string HTTP protocol version. |
130
|
|
|
*/ |
131
|
|
|
public function getProtocolVersion() |
132
|
|
|
{ |
133
|
|
|
return $this->response->getProtocolVersion(); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Return an instance with the specified HTTP protocol version. |
138
|
|
|
* |
139
|
|
|
* The version string MUST contain only the HTTP version number (e.g., |
140
|
|
|
* "1.1", "1.0"). |
141
|
|
|
* |
142
|
|
|
* This method MUST be implemented in such a way as to retain the |
143
|
|
|
* immutability of the message, and MUST return an instance that has the |
144
|
|
|
* new protocol version. |
145
|
|
|
* |
146
|
|
|
* @param string $version HTTP protocol version |
147
|
|
|
* @return self |
148
|
|
|
*/ |
149
|
|
|
public function withProtocolVersion($version) |
150
|
|
|
{ |
151
|
|
|
return $this->response->withProtocolVersion($version); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Retrieves all message header values. |
156
|
|
|
* |
157
|
|
|
* The keys represent the header name as it will be sent over the wire, and |
158
|
|
|
* each value is an array of strings associated with the header. |
159
|
|
|
* |
160
|
|
|
* // Represent the headers as a string |
161
|
|
|
* foreach ($message->getHeaders() as $name => $values) { |
162
|
|
|
* echo $name . ": " . implode(", ", $values); |
163
|
|
|
* } |
164
|
|
|
* |
165
|
|
|
* // Emit headers iteratively: |
166
|
|
|
* foreach ($message->getHeaders() as $name => $values) { |
167
|
|
|
* foreach ($values as $value) { |
168
|
|
|
* header(sprintf('%s: %s', $name, $value), false); |
169
|
|
|
* } |
170
|
|
|
* } |
171
|
|
|
* |
172
|
|
|
* While header names are not case-sensitive, getHeaders() will preserve the |
173
|
|
|
* exact case in which headers were originally specified. |
174
|
|
|
* |
175
|
|
|
* @return array Returns an associative array of the message's headers. Each |
176
|
|
|
* key MUST be a header name, and each value MUST be an array of strings |
177
|
|
|
* for that header. |
178
|
|
|
*/ |
179
|
|
|
public function getHeaders() |
180
|
|
|
{ |
181
|
|
|
return $this->response->getHeaders(); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* Checks if a header exists by the given case-insensitive name. |
186
|
|
|
* |
187
|
|
|
* @param string $name Case-insensitive header field name. |
188
|
|
|
* @return bool Returns true if any header names match the given header |
189
|
|
|
* name using a case-insensitive string comparison. Returns false if |
190
|
|
|
* no matching header name is found in the message. |
191
|
|
|
*/ |
192
|
|
|
public function hasHeader($name) |
193
|
|
|
{ |
194
|
|
|
return $this->response->hasHeader($name); |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* Retrieves a message header value by the given case-insensitive name. |
199
|
|
|
* |
200
|
|
|
* This method returns an array of all the header values of the given |
201
|
|
|
* case-insensitive header name. |
202
|
|
|
* |
203
|
|
|
* If the header does not appear in the message, this method MUST return an |
204
|
|
|
* empty array. |
205
|
|
|
* |
206
|
|
|
* @param string $name Case-insensitive header field name. |
207
|
|
|
* @return string[] An array of string values as provided for the given |
208
|
|
|
* header. If the header does not appear in the message, this method MUST |
209
|
|
|
* return an empty array. |
210
|
|
|
*/ |
211
|
|
|
public function getHeader($name) |
212
|
|
|
{ |
213
|
|
|
return $this->response->getHeader($name); |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* Retrieves a comma-separated string of the values for a single header. |
218
|
|
|
* |
219
|
|
|
* This method returns all of the header values of the given |
220
|
|
|
* case-insensitive header name as a string concatenated together using |
221
|
|
|
* a comma. |
222
|
|
|
* |
223
|
|
|
* NOTE: Not all header values may be appropriately represented using |
224
|
|
|
* comma concatenation. For such headers, use getHeader() instead |
225
|
|
|
* and supply your own delimiter when concatenating. |
226
|
|
|
* |
227
|
|
|
* If the header does not appear in the message, this method MUST return |
228
|
|
|
* an empty string. |
229
|
|
|
* |
230
|
|
|
* @param string $name Case-insensitive header field name. |
231
|
|
|
* @return string A string of values as provided for the given header |
232
|
|
|
* concatenated together using a comma. If the header does not appear in |
233
|
|
|
* the message, this method MUST return an empty string. |
234
|
|
|
*/ |
235
|
|
|
public function getHeaderLine($name) |
236
|
|
|
{ |
237
|
|
|
return $this->response->getHeaderLine($name); |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
/** |
241
|
|
|
* Return an instance with the provided value replacing the specified header. |
242
|
|
|
* |
243
|
|
|
* While header names are case-insensitive, the casing of the header will |
244
|
|
|
* be preserved by this function, and returned from getHeaders(). |
245
|
|
|
* |
246
|
|
|
* This method MUST be implemented in such a way as to retain the |
247
|
|
|
* immutability of the message, and MUST return an instance that has the |
248
|
|
|
* new and/or updated header and value. |
249
|
|
|
* |
250
|
|
|
* @param string $name Case-insensitive header field name. |
251
|
|
|
* @param string|string[] $value Header value(s). |
252
|
|
|
* @return self |
253
|
|
|
* @throws \InvalidArgumentException for invalid header names or values. |
254
|
|
|
*/ |
255
|
|
|
public function withHeader($name, $value) |
256
|
|
|
{ |
257
|
|
|
return $this->response->withHeader($name, $value); |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
/** |
261
|
|
|
* Return an instance with the specified header appended with the given value. |
262
|
|
|
* |
263
|
|
|
* Existing values for the specified header will be maintained. The new |
264
|
|
|
* value(s) will be appended to the existing list. If the header did not |
265
|
|
|
* exist previously, it will be added. |
266
|
|
|
* |
267
|
|
|
* This method MUST be implemented in such a way as to retain the |
268
|
|
|
* immutability of the message, and MUST return an instance that has the |
269
|
|
|
* new header and/or value. |
270
|
|
|
* |
271
|
|
|
* @param string $name Case-insensitive header field name to add. |
272
|
|
|
* @param string|string[] $value Header value(s). |
273
|
|
|
* @return self |
274
|
|
|
* @throws \InvalidArgumentException for invalid header names or values. |
275
|
|
|
*/ |
276
|
|
|
public function withAddedHeader($name, $value) |
277
|
|
|
{ |
278
|
|
|
return $this->response->withAddedHeader($name, $value); |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
/** |
282
|
|
|
* Return an instance without the specified header. |
283
|
|
|
* |
284
|
|
|
* Header resolution MUST be done without case-sensitivity. |
285
|
|
|
* |
286
|
|
|
* This method MUST be implemented in such a way as to retain the |
287
|
|
|
* immutability of the message, and MUST return an instance that removes |
288
|
|
|
* the named header. |
289
|
|
|
* |
290
|
|
|
* @param string $name Case-insensitive header field name to remove. |
291
|
|
|
* @return self |
292
|
|
|
*/ |
293
|
|
|
public function withoutHeader($name) |
294
|
|
|
{ |
295
|
|
|
return $this->response->withoutHeader($name); |
296
|
|
|
} |
297
|
|
|
|
298
|
|
|
/** |
299
|
|
|
* Gets the body of the message. |
300
|
|
|
* |
301
|
|
|
* @return StreamInterface Returns the body as a stream. |
302
|
|
|
*/ |
303
|
|
|
public function getBody() |
304
|
|
|
{ |
305
|
|
|
return $this->response->getBody(); |
306
|
|
|
} |
307
|
|
|
|
308
|
|
|
/** |
309
|
|
|
* Return an instance with the specified message body. |
310
|
|
|
* |
311
|
|
|
* The body MUST be a StreamInterface object. |
312
|
|
|
* |
313
|
|
|
* This method MUST be implemented in such a way as to retain the |
314
|
|
|
* immutability of the message, and MUST return a new instance that has the |
315
|
|
|
* new body stream. |
316
|
|
|
* |
317
|
|
|
* @param StreamInterface $body Body. |
318
|
|
|
* @return self |
319
|
|
|
* @throws \InvalidArgumentException When the body is not valid. |
320
|
|
|
*/ |
321
|
|
|
public function withBody(StreamInterface $body) |
322
|
|
|
{ |
323
|
|
|
return $this->response->withBody($body); |
324
|
|
|
} |
325
|
|
|
|
326
|
|
|
/** |
327
|
|
|
* Gets the response status code. |
328
|
|
|
* |
329
|
|
|
* The status code is a 3-digit integer result code of the server's attempt |
330
|
|
|
* to understand and satisfy the request. |
331
|
|
|
* |
332
|
|
|
* @return int Status code. |
333
|
|
|
*/ |
334
|
|
|
public function getStatusCode() |
335
|
|
|
{ |
336
|
|
|
return $this->response->getStatusCode(); |
337
|
|
|
} |
338
|
|
|
|
339
|
|
|
/** |
340
|
|
|
* Return an instance with the specified status code and, optionally, reason phrase. |
341
|
|
|
* |
342
|
|
|
* If no reason phrase is specified, implementations MAY choose to default |
343
|
|
|
* to the RFC 7231 or IANA recommended reason phrase for the response's |
344
|
|
|
* status code. |
345
|
|
|
* |
346
|
|
|
* This method MUST be implemented in such a way as to retain the |
347
|
|
|
* immutability of the message, and MUST return an instance that has the |
348
|
|
|
* updated status and reason phrase. |
349
|
|
|
* |
350
|
|
|
* @link http://tools.ietf.org/html/rfc7231#section-6 |
351
|
|
|
* @link http://www.iana.org/assignments/http-status-codes/http-status-codes.xhtml |
352
|
|
|
* @param int $code The 3-digit integer result code to set. |
353
|
|
|
* @param string $reasonPhrase The reason phrase to use with the |
354
|
|
|
* provided status code; if none is provided, implementations MAY |
355
|
|
|
* use the defaults as suggested in the HTTP specification. |
356
|
|
|
* @return self |
357
|
|
|
* @throws \InvalidArgumentException For invalid status code arguments. |
358
|
|
|
*/ |
359
|
|
|
public function withStatus($code, $reasonPhrase = '') |
360
|
|
|
{ |
361
|
|
|
return $this->response->withStatus($code, $reasonPhrase); |
362
|
|
|
} |
363
|
|
|
|
364
|
|
|
/** |
365
|
|
|
* Gets the response reason phrase associated with the status code. |
366
|
|
|
* |
367
|
|
|
* Because a reason phrase is not a required element in a response |
368
|
|
|
* status line, the reason phrase value MAY be null. Implementations MAY |
369
|
|
|
* choose to return the default RFC 7231 recommended reason phrase (or those |
370
|
|
|
* listed in the IANA HTTP Status Code Registry) for the response's |
371
|
|
|
* status code. |
372
|
|
|
* |
373
|
|
|
* @link http://tools.ietf.org/html/rfc7231#section-6 |
374
|
|
|
* @link http://www.iana.org/assignments/http-status-codes/http-status-codes.xhtml |
375
|
|
|
* @return string Reason phrase; must return an empty string if none present. |
376
|
|
|
*/ |
377
|
|
|
public function getReasonPhrase() |
378
|
|
|
{ |
379
|
|
|
return $this->response->getReasonPhrase(); |
380
|
|
|
} |
381
|
|
|
} |
382
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.