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