1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Nip\Http\Request\Traits; |
4
|
|
|
|
5
|
|
|
use Psr\Http\Message\StreamInterface; |
6
|
|
|
use Psr\Http\Message\UriInterface; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class PsrBridgeTrait |
10
|
|
|
* Add methods to make Symfony Request compliant with PSR |
11
|
|
|
* |
12
|
|
|
* @package Nip\Http\Request\Traits |
13
|
|
|
*/ |
14
|
|
|
trait PsrBridgeTrait |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @inheritDoc |
18
|
|
|
*/ |
19
|
|
|
public function getProtocolVersion(): string |
20
|
|
|
{ |
21
|
|
|
} |
|
|
|
|
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Return an instance with the specified HTTP protocol version. |
25
|
|
|
* |
26
|
|
|
* The version string MUST contain only the HTTP version number (e.g., |
27
|
|
|
* "1.1", "1.0"). |
28
|
|
|
* |
29
|
|
|
* This method MUST be implemented in such a way as to retain the |
30
|
|
|
* immutability of the message, and MUST return an instance that has the |
31
|
|
|
* new protocol version. |
32
|
|
|
* |
33
|
|
|
* @param string $version HTTP protocol version |
34
|
|
|
* @return static |
35
|
|
|
*/ |
36
|
|
|
public function withProtocolVersion($version) |
|
|
|
|
37
|
|
|
{ |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Retrieves all message header values. |
42
|
|
|
* |
43
|
|
|
* The keys represent the header name as it will be sent over the wire, and |
44
|
|
|
* each value is an array of strings associated with the header. |
45
|
|
|
* |
46
|
|
|
* // Represent the headers as a string |
47
|
|
|
* foreach ($message->getHeaders() as $name => $values) { |
48
|
|
|
* echo $name . ": " . implode(", ", $values); |
49
|
|
|
* } |
50
|
|
|
* |
51
|
|
|
* // Emit headers iteratively: |
52
|
|
|
* foreach ($message->getHeaders() as $name => $values) { |
53
|
|
|
* foreach ($values as $value) { |
54
|
|
|
* header(sprintf('%s: %s', $name, $value), false); |
55
|
|
|
* } |
56
|
|
|
* } |
57
|
|
|
* |
58
|
|
|
* While header names are not case-sensitive, getHeaders() will preserve the |
59
|
|
|
* exact case in which headers were originally specified. |
60
|
|
|
* |
61
|
|
|
* @return string[][] Returns an associative array of the message's headers. Each |
62
|
|
|
* key MUST be a header name, and each value MUST be an array of strings |
63
|
|
|
* for that header. |
64
|
|
|
*/ |
65
|
|
|
public function getHeaders() |
66
|
|
|
{ |
67
|
|
|
return $this->headers; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Retrieves a message header value by the given case-insensitive name. |
72
|
|
|
* |
73
|
|
|
* This method returns an array of all the header values of the given |
74
|
|
|
* case-insensitive header name. |
75
|
|
|
* |
76
|
|
|
* If the header does not appear in the message, this method MUST return an |
77
|
|
|
* empty array. |
78
|
|
|
* |
79
|
|
|
* @param string $name Case-insensitive header field name. |
80
|
|
|
* @return string[] An array of string values as provided for the given |
81
|
|
|
* header. If the header does not appear in the message, this method MUST |
82
|
|
|
* return an empty array. |
83
|
|
|
*/ |
84
|
|
|
public function getHeader($name) |
85
|
|
|
{ |
86
|
|
|
return $this->header($name); |
|
|
|
|
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Retrieves a comma-separated string of the values for a single header. |
91
|
|
|
* |
92
|
|
|
* This method returns all of the header values of the given |
93
|
|
|
* case-insensitive header name as a string concatenated together using |
94
|
|
|
* a comma. |
95
|
|
|
* |
96
|
|
|
* NOTE: Not all header values may be appropriately represented using |
97
|
|
|
* comma concatenation. For such headers, use getHeader() instead |
98
|
|
|
* and supply your own delimiter when concatenating. |
99
|
|
|
* |
100
|
|
|
* If the header does not appear in the message, this method MUST return |
101
|
|
|
* an empty string. |
102
|
|
|
* |
103
|
|
|
* @param string $name Case-insensitive header field name. |
104
|
|
|
* @return string A string of values as provided for the given header |
105
|
|
|
* concatenated together using a comma. If the header does not appear in |
106
|
|
|
* the message, this method MUST return an empty string. |
107
|
|
|
*/ |
108
|
|
|
public function getHeaderLine($name) |
|
|
|
|
109
|
|
|
{ |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Return an instance with the specified header appended with the given value. |
114
|
|
|
* |
115
|
|
|
* Existing values for the specified header will be maintained. The new |
116
|
|
|
* value(s) will be appended to the existing list. If the header did not |
117
|
|
|
* exist previously, it will be added. |
118
|
|
|
* |
119
|
|
|
* This method MUST be implemented in such a way as to retain the |
120
|
|
|
* immutability of the message, and MUST return an instance that has the |
121
|
|
|
* new header and/or value. |
122
|
|
|
* |
123
|
|
|
* @param string $name Case-insensitive header field name to add. |
124
|
|
|
* @param string|string[] $value Header value(s). |
125
|
|
|
* @return static |
126
|
|
|
* @throws \InvalidArgumentException for invalid header names or values. |
127
|
|
|
*/ |
128
|
|
|
public function withAddedHeader($name, $value) |
|
|
|
|
129
|
|
|
{ |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Gets the body of the message. |
134
|
|
|
* |
135
|
|
|
* @return StreamInterface Returns the body as a stream. |
136
|
|
|
*/ |
137
|
|
|
public function getBody() |
138
|
|
|
{ |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Return an instance with the specified message body. |
143
|
|
|
* |
144
|
|
|
* The body MUST be a StreamInterface object. |
145
|
|
|
* |
146
|
|
|
* This method MUST be implemented in such a way as to retain the |
147
|
|
|
* immutability of the message, and MUST return a new instance that has the |
148
|
|
|
* new body stream. |
149
|
|
|
* |
150
|
|
|
* @param StreamInterface $body Body. |
151
|
|
|
* @return static |
152
|
|
|
* @throws \InvalidArgumentException When the body is not valid. |
153
|
|
|
*/ |
154
|
|
|
public function withBody(StreamInterface $body) |
|
|
|
|
155
|
|
|
{ |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Retrieves the message's request target. |
160
|
|
|
* |
161
|
|
|
* Retrieves the message's request-target either as it will appear (for |
162
|
|
|
* clients), as it appeared at request (for servers), or as it was |
163
|
|
|
* specified for the instance (see withRequestTarget()). |
164
|
|
|
* |
165
|
|
|
* In most cases, this will be the origin-form of the composed URI, |
166
|
|
|
* unless a value was provided to the concrete implementation (see |
167
|
|
|
* withRequestTarget() below). |
168
|
|
|
* |
169
|
|
|
* If no URI is available, and no request-target has been specifically |
170
|
|
|
* provided, this method MUST return the string "/". |
171
|
|
|
* |
172
|
|
|
* @return string |
173
|
|
|
*/ |
174
|
|
|
public function getRequestTarget() |
175
|
|
|
{ |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* Return an instance with the specific request-target. |
180
|
|
|
* |
181
|
|
|
* If the request needs a non-origin-form request-target — e.g., for |
182
|
|
|
* specifying an absolute-form, authority-form, or asterisk-form — |
183
|
|
|
* this method may be used to create an instance with the specified |
184
|
|
|
* request-target, verbatim. |
185
|
|
|
* |
186
|
|
|
* This method MUST be implemented in such a way as to retain the |
187
|
|
|
* immutability of the message, and MUST return an instance that has the |
188
|
|
|
* changed request target. |
189
|
|
|
* |
190
|
|
|
* @link http://tools.ietf.org/html/rfc7230#section-5.3 (for the various |
191
|
|
|
* request-target forms allowed in request messages) |
192
|
|
|
* @param mixed $requestTarget |
193
|
|
|
* @return static |
194
|
|
|
*/ |
195
|
|
|
public function withRequestTarget($requestTarget) |
|
|
|
|
196
|
|
|
{ |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* Return an instance with the provided HTTP method. |
201
|
|
|
* |
202
|
|
|
* While HTTP method names are typically all uppercase characters, HTTP |
203
|
|
|
* method names are case-sensitive and thus implementations SHOULD NOT |
204
|
|
|
* modify the given string. |
205
|
|
|
* |
206
|
|
|
* This method MUST be implemented in such a way as to retain the |
207
|
|
|
* immutability of the message, and MUST return an instance that has the |
208
|
|
|
* changed request method. |
209
|
|
|
* |
210
|
|
|
* @param string $method Case-sensitive method. |
211
|
|
|
* @return static |
212
|
|
|
* @throws \InvalidArgumentException for invalid HTTP methods. |
213
|
|
|
*/ |
214
|
|
|
public function withMethod($method) |
|
|
|
|
215
|
|
|
{ |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* @param UriInterface $uri |
220
|
|
|
* @param bool $preserveHost |
221
|
|
|
* @return static |
222
|
|
|
*/ |
223
|
|
|
public function withUri(UriInterface $uri, $preserveHost = false) |
|
|
|
|
224
|
|
|
{ |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* @param $name |
229
|
|
|
* @return static |
230
|
|
|
*/ |
231
|
|
|
public function withoutHeader($name) |
|
|
|
|
232
|
|
|
{ |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
/** |
236
|
|
|
* Return an instance with the provided value replacing the specified header. |
237
|
|
|
* |
238
|
|
|
* While header names are case-insensitive, the casing of the header will |
239
|
|
|
* be preserved by this function, and returned from getHeaders(). |
240
|
|
|
* |
241
|
|
|
* This method MUST be implemented in such a way as to retain the |
242
|
|
|
* immutability of the message, and MUST return an instance that has the |
243
|
|
|
* new and/or updated header and value. |
244
|
|
|
* |
245
|
|
|
* @param string $name Case-insensitive header field name. |
246
|
|
|
* @param string|string[] $value Header value(s). |
247
|
|
|
* @return static |
248
|
|
|
* @throws \InvalidArgumentException for invalid header names or values. |
249
|
|
|
*/ |
250
|
|
|
public function withHeader($name, $value) |
251
|
|
|
{ |
252
|
|
|
$new = clone $this; |
253
|
|
|
$new->headers->set($name, $value); |
254
|
|
|
return $new; |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
/** |
258
|
|
|
* Retrieve server parameters. |
259
|
|
|
* |
260
|
|
|
* Retrieves data related to the incoming request environment, |
261
|
|
|
* typically derived from PHP's $_SERVER superglobal. The data IS NOT |
262
|
|
|
* REQUIRED to originate from $_SERVER. |
263
|
|
|
* |
264
|
|
|
* @return array |
265
|
|
|
*/ |
266
|
|
|
public function getServerParams() |
267
|
|
|
{ |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
/** |
271
|
|
|
* Retrieve cookies. |
272
|
|
|
* |
273
|
|
|
* Retrieves cookies sent by the client to the server. |
274
|
|
|
* |
275
|
|
|
* The data MUST be compatible with the structure of the $_COOKIE |
276
|
|
|
* superglobal. |
277
|
|
|
* |
278
|
|
|
* @return array |
279
|
|
|
*/ |
280
|
|
|
public function getCookieParams() |
281
|
|
|
{ |
282
|
|
|
} |
283
|
|
|
|
284
|
|
|
/** |
285
|
|
|
* Return an instance with the specified cookies. |
286
|
|
|
* |
287
|
|
|
* The data IS NOT REQUIRED to come from the $_COOKIE superglobal, but MUST |
288
|
|
|
* be compatible with the structure of $_COOKIE. Typically, this data will |
289
|
|
|
* be injected at instantiation. |
290
|
|
|
* |
291
|
|
|
* This method MUST NOT update the related Cookie header of the request |
292
|
|
|
* instance, nor related values in the server params. |
293
|
|
|
* |
294
|
|
|
* This method MUST be implemented in such a way as to retain the |
295
|
|
|
* immutability of the message, and MUST return an instance that has the |
296
|
|
|
* updated cookie values. |
297
|
|
|
* |
298
|
|
|
* @param array $cookies Array of key/value pairs representing cookies. |
299
|
|
|
* @return static |
300
|
|
|
*/ |
301
|
|
|
public function withCookieParams(array $cookies) |
|
|
|
|
302
|
|
|
{ |
303
|
|
|
} |
304
|
|
|
|
305
|
|
|
/** |
306
|
|
|
* Retrieve query string arguments. |
307
|
|
|
* |
308
|
|
|
* Retrieves the deserialized query string arguments, if any. |
309
|
|
|
* |
310
|
|
|
* Note: the query params might not be in sync with the URI or server |
311
|
|
|
* params. If you need to ensure you are only getting the original |
312
|
|
|
* values, you may need to parse the query string from `getUri()->getQuery()` |
313
|
|
|
* or from the `QUERY_STRING` server param. |
314
|
|
|
* |
315
|
|
|
* @return array |
316
|
|
|
*/ |
317
|
|
|
public function getQueryParams() |
318
|
|
|
{ |
319
|
|
|
} |
320
|
|
|
|
321
|
|
|
/** |
322
|
|
|
* Return an instance with the specified query string arguments. |
323
|
|
|
* |
324
|
|
|
* These values SHOULD remain immutable over the course of the incoming |
325
|
|
|
* request. They MAY be injected during instantiation, such as from PHP's |
326
|
|
|
* $_GET superglobal, or MAY be derived from some other value such as the |
327
|
|
|
* URI. In cases where the arguments are parsed from the URI, the data |
328
|
|
|
* MUST be compatible with what PHP's parse_str() would return for |
329
|
|
|
* purposes of how duplicate query parameters are handled, and how nested |
330
|
|
|
* sets are handled. |
331
|
|
|
* |
332
|
|
|
* Setting query string arguments MUST NOT change the URI stored by the |
333
|
|
|
* request, nor the values in the server params. |
334
|
|
|
* |
335
|
|
|
* This method MUST be implemented in such a way as to retain the |
336
|
|
|
* immutability of the message, and MUST return an instance that has the |
337
|
|
|
* updated query string arguments. |
338
|
|
|
* |
339
|
|
|
* @param array $query Array of query string arguments, typically from |
340
|
|
|
* $_GET. |
341
|
|
|
* @return static |
342
|
|
|
*/ |
343
|
|
|
public function withQueryParams(array $query) |
|
|
|
|
344
|
|
|
{ |
345
|
|
|
} |
346
|
|
|
|
347
|
|
|
|
348
|
|
|
/** |
349
|
|
|
* Retrieve normalized file upload data. |
350
|
|
|
* |
351
|
|
|
* This method returns upload metadata in a normalized tree, with each leaf |
352
|
|
|
* an instance of Psr\Http\Message\UploadedFileInterface. |
353
|
|
|
* |
354
|
|
|
* These values MAY be prepared from $_FILES or the message body during |
355
|
|
|
* instantiation, or MAY be injected via withUploadedFiles(). |
356
|
|
|
* |
357
|
|
|
* @return array An array tree of UploadedFileInterface instances; an empty |
358
|
|
|
* array MUST be returned if no data is present. |
359
|
|
|
*/ |
360
|
|
|
public function getUploadedFiles() |
361
|
|
|
{ |
362
|
|
|
} |
363
|
|
|
|
364
|
|
|
/** |
365
|
|
|
* Create a new instance with the specified uploaded files. |
366
|
|
|
* |
367
|
|
|
* This method MUST be implemented in such a way as to retain the |
368
|
|
|
* immutability of the message, and MUST return an instance that has the |
369
|
|
|
* updated body parameters. |
370
|
|
|
* |
371
|
|
|
* @param array $uploadedFiles An array tree of UploadedFileInterface instances. |
372
|
|
|
* @return static |
373
|
|
|
* @throws \InvalidArgumentException if an invalid structure is provided. |
374
|
|
|
*/ |
375
|
|
|
public function withUploadedFiles(array $uploadedFiles) |
|
|
|
|
376
|
|
|
{ |
377
|
|
|
} |
378
|
|
|
|
379
|
|
|
/** |
380
|
|
|
* Retrieve any parameters provided in the request body. |
381
|
|
|
* |
382
|
|
|
* If the request Content-Type is either application/x-www-form-urlencoded |
383
|
|
|
* or multipart/form-data, and the request method is POST, this method MUST |
384
|
|
|
* return the contents of $_POST. |
385
|
|
|
* |
386
|
|
|
* Otherwise, this method may return any results of deserializing |
387
|
|
|
* the request body content; as parsing returns structured content, the |
388
|
|
|
* potential types MUST be arrays or objects only. A null value indicates |
389
|
|
|
* the absence of body content. |
390
|
|
|
* |
391
|
|
|
* @return null|array|object The deserialized body parameters, if any. |
392
|
|
|
* These will typically be an array or object. |
393
|
|
|
*/ |
394
|
|
|
public function getParsedBody() |
395
|
|
|
{ |
396
|
|
|
} |
397
|
|
|
|
398
|
|
|
/** |
399
|
|
|
* Return an instance with the specified body parameters. |
400
|
|
|
* |
401
|
|
|
* These MAY be injected during instantiation. |
402
|
|
|
* |
403
|
|
|
* If the request Content-Type is either application/x-www-form-urlencoded |
404
|
|
|
* or multipart/form-data, and the request method is POST, use this method |
405
|
|
|
* ONLY to inject the contents of $_POST. |
406
|
|
|
* |
407
|
|
|
* The data IS NOT REQUIRED to come from $_POST, but MUST be the results of |
408
|
|
|
* deserializing the request body content. Deserialization/parsing returns |
409
|
|
|
* structured data, and, as such, this method ONLY accepts arrays or objects, |
410
|
|
|
* or a null value if nothing was available to parse. |
411
|
|
|
* |
412
|
|
|
* As an example, if content negotiation determines that the request data |
413
|
|
|
* is a JSON payload, this method could be used to create a request |
414
|
|
|
* instance with the deserialized parameters. |
415
|
|
|
* |
416
|
|
|
* This method MUST be implemented in such a way as to retain the |
417
|
|
|
* immutability of the message, and MUST return an instance that has the |
418
|
|
|
* updated body parameters. |
419
|
|
|
* |
420
|
|
|
* @param null|array|object $data The deserialized body data. This will |
421
|
|
|
* typically be in an array or object. |
422
|
|
|
* @return static |
423
|
|
|
* @throws \InvalidArgumentException if an unsupported argument type is |
424
|
|
|
* provided. |
425
|
|
|
*/ |
426
|
|
|
public function withParsedBody($data) |
|
|
|
|
427
|
|
|
{ |
428
|
|
|
} |
429
|
|
|
|
430
|
|
|
/** |
431
|
|
|
* Retrieve attributes derived from the request. |
432
|
|
|
* |
433
|
|
|
* The request "attributes" may be used to allow injection of any |
434
|
|
|
* parameters derived from the request: e.g., the results of path |
435
|
|
|
* match operations; the results of decrypting cookies; the results of |
436
|
|
|
* deserializing non-form-encoded message bodies; etc. Attributes |
437
|
|
|
* will be application and request specific, and CAN be mutable. |
438
|
|
|
* |
439
|
|
|
* @return array Attributes derived from the request. |
440
|
|
|
*/ |
441
|
|
|
public function getAttributes() |
442
|
|
|
{ |
443
|
|
|
} |
444
|
|
|
|
445
|
|
|
/** |
446
|
|
|
* Retrieve a single derived request attribute. |
447
|
|
|
* |
448
|
|
|
* Retrieves a single derived request attribute as described in |
449
|
|
|
* getAttributes(). If the attribute has not been previously set, returns |
450
|
|
|
* the default value as provided. |
451
|
|
|
* |
452
|
|
|
* This method obviates the need for a hasAttribute() method, as it allows |
453
|
|
|
* specifying a default value to return if the attribute is not found. |
454
|
|
|
* |
455
|
|
|
* @see getAttributes() |
456
|
|
|
* @param string $name The attribute name. |
457
|
|
|
* @param mixed $default Default value to return if the attribute does not exist. |
458
|
|
|
* @return mixed |
459
|
|
|
*/ |
460
|
|
|
public function getAttribute($name, $default = null) |
|
|
|
|
461
|
|
|
{ |
462
|
|
|
} |
463
|
|
|
|
464
|
|
|
/** |
465
|
|
|
* Return an instance with the specified derived request attribute. |
466
|
|
|
* |
467
|
|
|
* This method allows setting a single derived request attribute as |
468
|
|
|
* described in getAttributes(). |
469
|
|
|
* |
470
|
|
|
* This method MUST be implemented in such a way as to retain the |
471
|
|
|
* immutability of the message, and MUST return an instance that has the |
472
|
|
|
* updated attribute. |
473
|
|
|
* |
474
|
|
|
* @see getAttributes() |
475
|
|
|
* @param string $name The attribute name. |
476
|
|
|
* @param mixed $value The value of the attribute. |
477
|
|
|
* @return static |
478
|
|
|
*/ |
479
|
|
|
public function withAttribute($name, $value) |
|
|
|
|
480
|
|
|
{ |
481
|
|
|
} |
482
|
|
|
|
483
|
|
|
/** |
484
|
|
|
* Return an instance that removes the specified derived request attribute. |
485
|
|
|
* |
486
|
|
|
* This method allows removing a single derived request attribute as |
487
|
|
|
* described in getAttributes(). |
488
|
|
|
* |
489
|
|
|
* This method MUST be implemented in such a way as to retain the |
490
|
|
|
* immutability of the message, and MUST return an instance that removes |
491
|
|
|
* the attribute. |
492
|
|
|
* |
493
|
|
|
* @see getAttributes() |
494
|
|
|
* @param string $name The attribute name. |
495
|
|
|
* @return static |
496
|
|
|
*/ |
497
|
|
|
public function withoutAttribute($name) |
|
|
|
|
498
|
|
|
{ |
499
|
|
|
} |
500
|
|
|
} |
501
|
|
|
|
For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example: