1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Muzzle\Messages; |
4
|
|
|
|
5
|
|
|
use Psr\Http\Message\RequestInterface; |
6
|
|
|
use Psr\Http\Message\StreamInterface; |
7
|
|
|
use Psr\Http\Message\UriInterface; |
8
|
|
|
|
9
|
|
|
trait RequestDecorator |
10
|
|
|
{ |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @var RequestInterface |
14
|
|
|
*/ |
15
|
|
|
private $request; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* AssertableRequest constructor. |
19
|
|
|
* @param $request |
20
|
|
|
*/ |
21
|
|
|
public function __construct(RequestInterface $request) |
22
|
|
|
{ |
23
|
|
|
|
24
|
|
|
$this->request = $request; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Create a new AssertableRequest from another response. |
29
|
|
|
* |
30
|
|
|
* @param RequestInterface $request |
31
|
|
|
* @return self |
32
|
|
|
*/ |
33
|
|
|
public static function fromBaseRequest(RequestInterface $request) |
34
|
|
|
{ |
35
|
|
|
|
36
|
|
|
return new static($request); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Retrieves the HTTP protocol version as a string. |
41
|
|
|
* |
42
|
|
|
* The string MUST contain only the HTTP version number (e.g., "1.1", "1.0"). |
43
|
|
|
* |
44
|
|
|
* @return string HTTP protocol version. |
45
|
|
|
*/ |
46
|
|
|
public function getProtocolVersion() |
47
|
|
|
{ |
48
|
|
|
|
49
|
|
|
return $this->request->getProtocolVersion(); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Return an instance with the specified HTTP protocol version. |
54
|
|
|
* |
55
|
|
|
* The version string MUST contain only the HTTP version number (e.g., |
56
|
|
|
* "1.1", "1.0"). |
57
|
|
|
* |
58
|
|
|
* This method MUST be implemented in such a way as to retain the |
59
|
|
|
* immutability of the message, and MUST return an instance that has the |
60
|
|
|
* new protocol version. |
61
|
|
|
* |
62
|
|
|
* @param string $version HTTP protocol version |
63
|
|
|
* @return static |
64
|
|
|
*/ |
65
|
|
|
public function withProtocolVersion($version) |
66
|
|
|
{ |
67
|
|
|
|
68
|
|
|
return static::fromBaseRequest($this->request->withProtocolVersion($version)); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Retrieves all message header values. |
73
|
|
|
* |
74
|
|
|
* The keys represent the header name as it will be sent over the wire, and |
75
|
|
|
* each value is an array of strings associated with the header. |
76
|
|
|
* |
77
|
|
|
* // Represent the headers as a string |
78
|
|
|
* foreach ($message->getHeaders() as $name => $values) { |
79
|
|
|
* echo $name . ": " . implode(", ", $values); |
80
|
|
|
* } |
81
|
|
|
* |
82
|
|
|
* // Emit headers iteratively: |
83
|
|
|
* foreach ($message->getHeaders() as $name => $values) { |
84
|
|
|
* foreach ($values as $value) { |
85
|
|
|
* header(sprintf('%s: %s', $name, $value), false); |
86
|
|
|
* } |
87
|
|
|
* } |
88
|
|
|
* |
89
|
|
|
* While header names are not case-sensitive, getHeaders() will preserve the |
90
|
|
|
* exact case in which headers were originally specified. |
91
|
|
|
* |
92
|
|
|
* @return string[][] Returns an associative array of the message's headers. Each |
93
|
|
|
* key MUST be a header name, and each value MUST be an array of strings |
94
|
|
|
* for that header. |
95
|
|
|
*/ |
96
|
|
|
public function getHeaders() |
97
|
|
|
{ |
98
|
|
|
|
99
|
|
|
return $this->request->getHeaders(); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Checks if a header exists by the given case-insensitive name. |
104
|
|
|
* |
105
|
|
|
* @param string $name Case-insensitive header field name. |
106
|
|
|
* @return bool Returns true if any header names match the given header |
107
|
|
|
* name using a case-insensitive string comparison. Returns false if |
108
|
|
|
* no matching header name is found in the message. |
109
|
|
|
*/ |
110
|
|
|
public function hasHeader($name) |
111
|
|
|
{ |
112
|
|
|
|
113
|
|
|
return $this->request->hasHeader($name); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Retrieves a message header value by the given case-insensitive name. |
118
|
|
|
* |
119
|
|
|
* This method returns an array of all the header values of the given |
120
|
|
|
* case-insensitive header name. |
121
|
|
|
* |
122
|
|
|
* If the header does not appear in the message, this method MUST return an |
123
|
|
|
* empty array. |
124
|
|
|
* |
125
|
|
|
* @param string $name Case-insensitive header field name. |
126
|
|
|
* @return string[] An array of string values as provided for the given |
127
|
|
|
* header. If the header does not appear in the message, this method MUST |
128
|
|
|
* return an empty array. |
129
|
|
|
*/ |
130
|
|
|
public function getHeader($name) |
131
|
|
|
{ |
132
|
|
|
|
133
|
|
|
return $this->request->getHeader($name); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Retrieves a comma-separated string of the values for a single header. |
138
|
|
|
* |
139
|
|
|
* This method returns all of the header values of the given |
140
|
|
|
* case-insensitive header name as a string concatenated together using |
141
|
|
|
* a comma. |
142
|
|
|
* |
143
|
|
|
* NOTE: Not all header values may be appropriately represented using |
144
|
|
|
* comma concatenation. For such headers, use getHeader() instead |
145
|
|
|
* and supply your own delimiter when concatenating. |
146
|
|
|
* |
147
|
|
|
* If the header does not appear in the message, this method MUST return |
148
|
|
|
* an empty string. |
149
|
|
|
* |
150
|
|
|
* @param string $name Case-insensitive header field name. |
151
|
|
|
* @return string A string of values as provided for the given header |
152
|
|
|
* concatenated together using a comma. If the header does not appear in |
153
|
|
|
* the message, this method MUST return an empty string. |
154
|
|
|
*/ |
155
|
|
|
public function getHeaderLine($name) |
156
|
|
|
{ |
157
|
|
|
|
158
|
|
|
return $this->request->getHeaderLine($name); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Return an instance with the provided value replacing the specified header. |
163
|
|
|
* |
164
|
|
|
* While header names are case-insensitive, the casing of the header will |
165
|
|
|
* be preserved by this function, and returned from getHeaders(). |
166
|
|
|
* |
167
|
|
|
* This method MUST be implemented in such a way as to retain the |
168
|
|
|
* immutability of the message, and MUST return an instance that has the |
169
|
|
|
* new and/or updated header and value. |
170
|
|
|
* |
171
|
|
|
* @param string $name Case-insensitive header field name. |
172
|
|
|
* @param string|string[] $value Header value(s). |
173
|
|
|
* @return static |
174
|
|
|
* @throws \InvalidArgumentException for invalid header names or values. |
175
|
|
|
*/ |
176
|
|
|
public function withHeader($name, $value) |
177
|
|
|
{ |
178
|
|
|
|
179
|
|
|
return static::fromBaseRequest($this->request->withHeader($name, $value)); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* Return an instance with the specified header appended with the given value. |
184
|
|
|
* |
185
|
|
|
* Existing values for the specified header will be maintained. The new |
186
|
|
|
* value(s) will be appended to the existing list. If the header did not |
187
|
|
|
* exist previously, it will be added. |
188
|
|
|
* |
189
|
|
|
* This method MUST be implemented in such a way as to retain the |
190
|
|
|
* immutability of the message, and MUST return an instance that has the |
191
|
|
|
* new header and/or value. |
192
|
|
|
* |
193
|
|
|
* @param string $name Case-insensitive header field name to add. |
194
|
|
|
* @param string|string[] $value Header value(s). |
195
|
|
|
* @return static |
196
|
|
|
* @throws \InvalidArgumentException for invalid header names or values. |
197
|
|
|
*/ |
198
|
|
|
public function withAddedHeader($name, $value) |
199
|
|
|
{ |
200
|
|
|
|
201
|
|
|
return static::fromBaseRequest($this->request->withAddedHeader($name, $value)); |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* Return an instance without the specified header. |
206
|
|
|
* |
207
|
|
|
* Header resolution MUST be done without case-sensitivity. |
208
|
|
|
* |
209
|
|
|
* This method MUST be implemented in such a way as to retain the |
210
|
|
|
* immutability of the message, and MUST return an instance that removes |
211
|
|
|
* the named header. |
212
|
|
|
* |
213
|
|
|
* @param string $name Case-insensitive header field name to remove. |
214
|
|
|
* @return static |
215
|
|
|
*/ |
216
|
|
|
public function withoutHeader($name) |
217
|
|
|
{ |
218
|
|
|
|
219
|
|
|
return static::fromBaseRequest($this->request->withoutHeader($name)); |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* Gets the body of the message. |
224
|
|
|
* |
225
|
|
|
* @return StreamInterface Returns the body as a stream. |
226
|
|
|
*/ |
227
|
|
|
public function getBody() |
228
|
|
|
{ |
229
|
|
|
|
230
|
|
|
return $this->request->getBody(); |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* Return an instance with the specified message body. |
235
|
|
|
* |
236
|
|
|
* The body MUST be a StreamInterface object. |
237
|
|
|
* |
238
|
|
|
* This method MUST be implemented in such a way as to retain the |
239
|
|
|
* immutability of the message, and MUST return a new instance that has the |
240
|
|
|
* new body stream. |
241
|
|
|
* |
242
|
|
|
* @param StreamInterface $body Body. |
243
|
|
|
* @return static |
244
|
|
|
* @throws \InvalidArgumentException When the body is not valid. |
245
|
|
|
*/ |
246
|
|
|
public function withBody(StreamInterface $body) |
247
|
|
|
{ |
248
|
|
|
|
249
|
|
|
return static::fromBaseRequest($this->request->withBody($body)); |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
/** |
253
|
|
|
* Retrieves the message's request target. |
254
|
|
|
* |
255
|
|
|
* Retrieves the message's request-target either as it will appear (for |
256
|
|
|
* clients), as it appeared at request (for servers), or as it was |
257
|
|
|
* specified for the instance (see withRequestTarget()). |
258
|
|
|
* |
259
|
|
|
* In most cases, this will be the origin-form of the composed URI, |
260
|
|
|
* unless a value was provided to the concrete implementation (see |
261
|
|
|
* withRequestTarget() below). |
262
|
|
|
* |
263
|
|
|
* If no URI is available, and no request-target has been specifically |
264
|
|
|
* provided, this method MUST return the string "/". |
265
|
|
|
* |
266
|
|
|
* @return string |
267
|
|
|
*/ |
268
|
|
|
public function getRequestTarget() |
269
|
|
|
{ |
270
|
|
|
|
271
|
|
|
return $this->request->getRequestTarget(); |
272
|
|
|
} |
273
|
|
|
|
274
|
|
|
/** |
275
|
|
|
* Return an instance with the specific request-target. |
276
|
|
|
* |
277
|
|
|
* If the request needs a non-origin-form request-target — e.g., for |
278
|
|
|
* specifying an absolute-form, authority-form, or asterisk-form — |
279
|
|
|
* this method may be used to create an instance with the specified |
280
|
|
|
* request-target, verbatim. |
281
|
|
|
* |
282
|
|
|
* This method MUST be implemented in such a way as to retain the |
283
|
|
|
* immutability of the message, and MUST return an instance that has the |
284
|
|
|
* changed request target. |
285
|
|
|
* |
286
|
|
|
* @link http://tools.ietf.org/html/rfc7230#section-5.3 (for the various |
287
|
|
|
* request-target forms allowed in request messages) |
288
|
|
|
* @param mixed $requestTarget |
289
|
|
|
* @return static |
290
|
|
|
*/ |
291
|
|
|
public function withRequestTarget($requestTarget) |
292
|
|
|
{ |
293
|
|
|
|
294
|
|
|
return static::fromBaseRequest($this->request->withRequestTarget($requestTarget)); |
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
/** |
298
|
|
|
* Retrieves the HTTP method of the request. |
299
|
|
|
* |
300
|
|
|
* @return string Returns the request method. |
301
|
|
|
*/ |
302
|
|
|
public function getMethod() |
303
|
|
|
{ |
304
|
|
|
|
305
|
|
|
return $this->request->getMethod(); |
306
|
|
|
} |
307
|
|
|
|
308
|
|
|
/** |
309
|
|
|
* Return an instance with the provided HTTP method. |
310
|
|
|
* |
311
|
|
|
* While HTTP method names are typically all uppercase characters, HTTP |
312
|
|
|
* method names are case-sensitive and thus implementations SHOULD NOT |
313
|
|
|
* modify the given string. |
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
|
|
|
* changed request method. |
318
|
|
|
* |
319
|
|
|
* @param string $method Case-sensitive method. |
320
|
|
|
* @return static |
321
|
|
|
* @throws \InvalidArgumentException for invalid HTTP methods. |
322
|
|
|
*/ |
323
|
|
|
public function withMethod($method) |
324
|
|
|
{ |
325
|
|
|
|
326
|
|
|
return static::fromBaseRequest($this->request->withMethod($method)); |
327
|
|
|
} |
328
|
|
|
|
329
|
|
|
/** |
330
|
|
|
* Retrieves the URI instance. |
331
|
|
|
* |
332
|
|
|
* This method MUST return a UriInterface instance. |
333
|
|
|
* |
334
|
|
|
* @link http://tools.ietf.org/html/rfc3986#section-4.3 |
335
|
|
|
* @return UriInterface Returns a UriInterface instance |
336
|
|
|
* representing the URI of the request. |
337
|
|
|
*/ |
338
|
|
|
public function getUri() |
339
|
|
|
{ |
340
|
|
|
|
341
|
|
|
return $this->request->getUri(); |
342
|
|
|
} |
343
|
|
|
|
344
|
|
|
/** |
345
|
|
|
* Returns an instance with the provided URI. |
346
|
|
|
* |
347
|
|
|
* This method MUST update the Host header of the returned request by |
348
|
|
|
* default if the URI contains a host component. If the URI does not |
349
|
|
|
* contain a host component, any pre-existing Host header MUST be carried |
350
|
|
|
* over to the returned request. |
351
|
|
|
* |
352
|
|
|
* You can opt-in to preserving the original state of the Host header by |
353
|
|
|
* setting `$preserveHost` to `true`. When `$preserveHost` is set to |
354
|
|
|
* `true`, this method interacts with the Host header in the following ways: |
355
|
|
|
* |
356
|
|
|
* - If the Host header is missing or empty, and the new URI contains |
357
|
|
|
* a host component, this method MUST update the Host header in the returned |
358
|
|
|
* request. |
359
|
|
|
* - If the Host header is missing or empty, and the new URI does not contain a |
360
|
|
|
* host component, this method MUST NOT update the Host header in the returned |
361
|
|
|
* request. |
362
|
|
|
* - If a Host header is present and non-empty, this method MUST NOT update |
363
|
|
|
* the Host header in the returned request. |
364
|
|
|
* |
365
|
|
|
* This method MUST be implemented in such a way as to retain the |
366
|
|
|
* immutability of the message, and MUST return an instance that has the |
367
|
|
|
* new UriInterface instance. |
368
|
|
|
* |
369
|
|
|
* @link http://tools.ietf.org/html/rfc3986#section-4.3 |
370
|
|
|
* @param UriInterface $uri New request URI to use. |
371
|
|
|
* @param bool $preserveHost Preserve the original state of the Host header. |
372
|
|
|
* @return static |
373
|
|
|
*/ |
374
|
|
|
public function withUri(UriInterface $uri, $preserveHost = false) |
375
|
|
|
{ |
376
|
|
|
|
377
|
|
|
return static::fromBaseRequest($this->request->withUri($uri, $preserveHost)); |
378
|
|
|
} |
379
|
|
|
} |
380
|
|
|
|