1 | <?php |
||
12 | class ServerRequest implements ServerRequestInterface |
||
13 | { |
||
14 | /** |
||
15 | * @var string Represent the HTTP version number (e.g., "1.1", "1.0") |
||
16 | */ |
||
17 | private $version; |
||
18 | |||
19 | public function __construct(string $version = '1.1', $headers = []) |
||
27 | |||
28 | /** |
||
29 | * Retrieves the HTTP protocol version as a string. |
||
30 | * |
||
31 | * The string MUST contain only the HTTP version number (e.g., "1.1", "1.0"). |
||
32 | * |
||
33 | * @return string HTTP protocol version. |
||
34 | */ |
||
35 | public function getProtocolVersion() |
||
39 | |||
40 | /** |
||
41 | * Return an instance with the specified HTTP protocol version. |
||
42 | * |
||
43 | * The version string MUST contain only the HTTP version number (e.g., |
||
44 | * "1.1", "1.0"). |
||
45 | * |
||
46 | * This method MUST be implemented in such a way as to retain the |
||
47 | * immutability of the message, and MUST return an instance that has the |
||
48 | * new protocol version. |
||
49 | * |
||
50 | * @param string $version HTTP protocol version |
||
51 | * @return static |
||
52 | */ |
||
53 | public function withProtocolVersion($version) : self |
||
59 | |||
60 | /** |
||
61 | * Verifies the protocol version |
||
62 | * |
||
63 | * @throws InvalidArgumentException |
||
64 | * @param string $version The version string MUST contain only the HTTP |
||
65 | * version number (e.g., "1.1", "1.0"). |
||
66 | */ |
||
67 | private function validateProtocolVersion(string $version) : void |
||
73 | |||
74 | /** |
||
75 | * Retrieves all message header values. |
||
76 | * |
||
77 | * The keys represent the header name as it will be sent over the wire, and |
||
78 | * each value is an array of strings associated with the header. |
||
79 | * |
||
80 | * // Represent the headers as a string |
||
81 | * foreach ($message->getHeaders() as $name => $values) { |
||
82 | * echo $name . ": " . implode(", ", $values); |
||
83 | * } |
||
84 | * |
||
85 | * // Emit headers iteratively: |
||
86 | * foreach ($message->getHeaders() as $name => $values) { |
||
87 | * foreach ($values as $value) { |
||
88 | * header(sprintf('%s: %s', $name, $value), false); |
||
89 | * } |
||
90 | * } |
||
91 | * |
||
92 | * While header names are not case-sensitive, getHeaders() will preserve the |
||
93 | * exact case in which headers were originally specified. |
||
94 | * |
||
95 | * @return string[][] Returns an associative array of the message's headers. Each |
||
96 | * key MUST be a header name, and each value MUST be an array of strings |
||
97 | * for that header. |
||
98 | */ |
||
99 | public function getHeaders() |
||
103 | |||
104 | /** |
||
105 | * Checks if a header exists by the given case-insensitive name. |
||
106 | * |
||
107 | * @param string $name Case-insensitive header field name. |
||
108 | * @return bool Returns true if any header names match the given header |
||
109 | * name using a case-insensitive string comparison. Returns false if |
||
110 | * no matching header name is found in the message. |
||
111 | */ |
||
112 | public function hasHeader($name) |
||
119 | |||
120 | /** |
||
121 | * Retrieves a message header value by the given case-insensitive name. |
||
122 | * |
||
123 | * This method returns an array of all the header values of the given |
||
124 | * case-insensitive header name. |
||
125 | * |
||
126 | * If the header does not appear in the message, this method MUST return an |
||
127 | * empty array. |
||
128 | * |
||
129 | * @param string $name Case-insensitive header field name. |
||
130 | * @return string[] An array of string values as provided for the given |
||
131 | * header. If the header does not appear in the message, this method MUST |
||
132 | * return an empty array. |
||
133 | */ |
||
134 | public function getHeader($name) |
||
145 | |||
146 | /** |
||
147 | * Retrieves a comma-separated string of the values for a single header. |
||
148 | * |
||
149 | * This method returns all of the header values of the given |
||
150 | * case-insensitive header name as a string concatenated together using |
||
151 | * a comma. |
||
152 | * |
||
153 | * NOTE: Not all header values may be appropriately represented using |
||
154 | * comma concatenation. For such headers, use getHeader() instead |
||
155 | * and supply your own delimiter when concatenating. |
||
156 | * |
||
157 | * If the header does not appear in the message, this method MUST return |
||
158 | * an empty string. |
||
159 | * |
||
160 | * @param string $name Case-insensitive header field name. |
||
161 | * @return string A string of values as provided for the given header |
||
162 | * concatenated together using a comma. If the header does not appear in |
||
163 | * the message, this method MUST return an empty string. |
||
164 | */ |
||
165 | public function getHeaderLine($name) |
||
169 | |||
170 | /** |
||
171 | * Return an instance with the provided value replacing the specified header. |
||
172 | * |
||
173 | * While header names are case-insensitive, the casing of the header will |
||
174 | * be preserved by this function, and returned from getHeaders(). |
||
175 | * |
||
176 | * This method MUST be implemented in such a way as to retain the |
||
177 | * immutability of the message, and MUST return an instance that has the |
||
178 | * new and/or updated header and value. |
||
179 | * |
||
180 | * @param string $name Case-insensitive header field name. |
||
181 | * @param string|string[] $value Header value(s). |
||
182 | * @return static |
||
183 | * @throws \InvalidArgumentException for invalid header names or values. |
||
184 | */ |
||
185 | public function withHeader($name, $value) |
||
189 | |||
190 | /** |
||
191 | * Return an instance with the specified header appended with the given value. |
||
192 | * |
||
193 | * Existing values for the specified header will be maintained. The new |
||
194 | * value(s) will be appended to the existing list. If the header did not |
||
195 | * exist previously, it will be added. |
||
196 | * |
||
197 | * This method MUST be implemented in such a way as to retain the |
||
198 | * immutability of the message, and MUST return an instance that has the |
||
199 | * new header and/or value. |
||
200 | * |
||
201 | * @param string $name Case-insensitive header field name to add. |
||
202 | * @param string|string[] $value Header value(s). |
||
203 | * @return static |
||
204 | * @throws \InvalidArgumentException for invalid header names or values. |
||
205 | */ |
||
206 | public function withAddedHeader($name, $value) |
||
210 | |||
211 | /** |
||
212 | * Return an instance without the specified header. |
||
213 | * |
||
214 | * Header resolution MUST be done without case-sensitivity. |
||
215 | * |
||
216 | * This method MUST be implemented in such a way as to retain the |
||
217 | * immutability of the message, and MUST return an instance that removes |
||
218 | * the named header. |
||
219 | * |
||
220 | * @param string $name Case-insensitive header field name to remove. |
||
221 | * @return static |
||
222 | */ |
||
223 | public function withoutHeader($name) |
||
227 | |||
228 | /** |
||
229 | * Verifies the headers are valid |
||
230 | * |
||
231 | * @throws InvalidArgumentException |
||
232 | * @param array $headers Headers for the incoming request |
||
233 | */ |
||
234 | private function validateHeaders(array $headers) : void |
||
262 | |||
263 | /** |
||
264 | * Gets the body of the message. |
||
265 | * |
||
266 | * @return StreamInterface Returns the body as a stream. |
||
267 | */ |
||
268 | public function getBody() |
||
272 | |||
273 | /** |
||
274 | * Return an instance with the specified message body. |
||
275 | * |
||
276 | * The body MUST be a StreamInterface object. |
||
277 | * |
||
278 | * This method MUST be implemented in such a way as to retain the |
||
279 | * immutability of the message, and MUST return a new instance that has the |
||
280 | * new body stream. |
||
281 | * |
||
282 | * @param StreamInterface $body Body. |
||
283 | * @return static |
||
284 | * @throws \InvalidArgumentException When the body is not valid. |
||
285 | */ |
||
286 | public function withBody(StreamInterface $body) |
||
290 | |||
291 | /** |
||
292 | * Retrieves the message's request target. |
||
293 | * |
||
294 | * Retrieves the message's request-target either as it will appear (for |
||
295 | * clients), as it appeared at request (for servers), or as it was |
||
296 | * specified for the instance (see withRequestTarget()). |
||
297 | * |
||
298 | * In most cases, this will be the origin-form of the composed URI, |
||
299 | * unless a value was provided to the concrete implementation (see |
||
300 | * withRequestTarget() below). |
||
301 | * |
||
302 | * If no URI is available, and no request-target has been specifically |
||
303 | * provided, this method MUST return the string "/". |
||
304 | * |
||
305 | * @return string |
||
306 | */ |
||
307 | public function getRequestTarget() |
||
311 | |||
312 | /** |
||
313 | * Return an instance with the specific request-target. |
||
314 | * |
||
315 | * If the request needs a non-origin-form request-target — e.g., for |
||
316 | * specifying an absolute-form, authority-form, or asterisk-form — |
||
317 | * this method may be used to create an instance with the specified |
||
318 | * request-target, verbatim. |
||
319 | * |
||
320 | * This method MUST be implemented in such a way as to retain the |
||
321 | * immutability of the message, and MUST return an instance that has the |
||
322 | * changed request target. |
||
323 | * |
||
324 | * @link http://tools.ietf.org/html/rfc7230#section-5.3 (for the various |
||
325 | * request-target forms allowed in request messages) |
||
326 | * @param mixed $requestTarget |
||
327 | * @return static |
||
328 | */ |
||
329 | public function withRequestTarget($requestTarget) |
||
333 | |||
334 | /** |
||
335 | * Retrieves the HTTP method of the request. |
||
336 | * |
||
337 | * @return string Returns the request method. |
||
338 | */ |
||
339 | public function getMethod() |
||
343 | |||
344 | /** |
||
345 | * Return an instance with the provided HTTP method. |
||
346 | * |
||
347 | * While HTTP method names are typically all uppercase characters, HTTP |
||
348 | * method names are case-sensitive and thus implementations SHOULD NOT |
||
349 | * modify the given string. |
||
350 | * |
||
351 | * This method MUST be implemented in such a way as to retain the |
||
352 | * immutability of the message, and MUST return an instance that has the |
||
353 | * changed request method. |
||
354 | * |
||
355 | * @param string $method Case-sensitive method. |
||
356 | * @return static |
||
357 | * @throws \InvalidArgumentException for invalid HTTP methods. |
||
358 | */ |
||
359 | public function withMethod($method) |
||
363 | |||
364 | /** |
||
365 | * Retrieves the URI instance. |
||
366 | * |
||
367 | * This method MUST return a UriInterface instance. |
||
368 | * |
||
369 | * @link http://tools.ietf.org/html/rfc3986#section-4.3 |
||
370 | * @return UriInterface Returns a UriInterface instance |
||
371 | * representing the URI of the request. |
||
372 | */ |
||
373 | public function getUri() |
||
377 | |||
378 | /** |
||
379 | * Returns an instance with the provided URI. |
||
380 | * |
||
381 | * This method MUST update the Host header of the returned request by |
||
382 | * default if the URI contains a host component. If the URI does not |
||
383 | * contain a host component, any pre-existing Host header MUST be carried |
||
384 | * over to the returned request. |
||
385 | * |
||
386 | * You can opt-in to preserving the original state of the Host header by |
||
387 | * setting `$preserveHost` to `true`. When `$preserveHost` is set to |
||
388 | * `true`, this method interacts with the Host header in the following ways: |
||
389 | * |
||
390 | * - If the Host header is missing or empty, and the new URI contains |
||
391 | * a host component, this method MUST update the Host header in the returned |
||
392 | * request. |
||
393 | * - If the Host header is missing or empty, and the new URI does not contain a |
||
394 | * host component, this method MUST NOT update the Host header in the returned |
||
395 | * request. |
||
396 | * - If a Host header is present and non-empty, this method MUST NOT update |
||
397 | * the Host header in the returned request. |
||
398 | * |
||
399 | * This method MUST be implemented in such a way as to retain the |
||
400 | * immutability of the message, and MUST return an instance that has the |
||
401 | * new UriInterface instance. |
||
402 | * |
||
403 | * @link http://tools.ietf.org/html/rfc3986#section-4.3 |
||
404 | * @param UriInterface $uri New request URI to use. |
||
405 | * @param bool $preserveHost Preserve the original state of the Host header. |
||
406 | * @return static |
||
407 | */ |
||
408 | public function withUri(UriInterface $uri, $preserveHost = false) |
||
412 | |||
413 | /** |
||
414 | * Retrieve server parameters. |
||
415 | * |
||
416 | * Retrieves data related to the incoming request environment, |
||
417 | * typically derived from PHP's $_SERVER superglobal. The data IS NOT |
||
418 | * REQUIRED to originate from $_SERVER. |
||
419 | * |
||
420 | * @return array |
||
421 | */ |
||
422 | public function getServerParams() |
||
426 | |||
427 | /** |
||
428 | * Retrieve cookies. |
||
429 | * |
||
430 | * Retrieves cookies sent by the client to the server. |
||
431 | * |
||
432 | * The data MUST be compatible with the structure of the $_COOKIE |
||
433 | * superglobal. |
||
434 | * |
||
435 | * @return array |
||
436 | */ |
||
437 | public function getCookieParams() |
||
441 | |||
442 | /** |
||
443 | * Return an instance with the specified cookies. |
||
444 | * |
||
445 | * The data IS NOT REQUIRED to come from the $_COOKIE superglobal, but MUST |
||
446 | * be compatible with the structure of $_COOKIE. Typically, this data will |
||
447 | * be injected at instantiation. |
||
448 | * |
||
449 | * This method MUST NOT update the related Cookie header of the request |
||
450 | * instance, nor related values in the server params. |
||
451 | * |
||
452 | * This method MUST be implemented in such a way as to retain the |
||
453 | * immutability of the message, and MUST return an instance that has the |
||
454 | * updated cookie values. |
||
455 | * |
||
456 | * @param array $cookies Array of key/value pairs representing cookies. |
||
457 | * @return static |
||
458 | */ |
||
459 | public function withCookieParams(array $cookies) |
||
463 | |||
464 | /** |
||
465 | * Retrieve query string arguments. |
||
466 | * |
||
467 | * Retrieves the deserialized query string arguments, if any. |
||
468 | * |
||
469 | * Note: the query params might not be in sync with the URI or server |
||
470 | * params. If you need to ensure you are only getting the original |
||
471 | * values, you may need to parse the query string from `getUri()->getQuery()` |
||
472 | * or from the `QUERY_STRING` server param. |
||
473 | * |
||
474 | * @return array |
||
475 | */ |
||
476 | public function getQueryParams() |
||
480 | |||
481 | /** |
||
482 | * Return an instance with the specified query string arguments. |
||
483 | * |
||
484 | * These values SHOULD remain immutable over the course of the incoming |
||
485 | * request. They MAY be injected during instantiation, such as from PHP's |
||
486 | * $_GET superglobal, or MAY be derived from some other value such as the |
||
487 | * URI. In cases where the arguments are parsed from the URI, the data |
||
488 | * MUST be compatible with what PHP's parse_str() would return for |
||
489 | * purposes of how duplicate query parameters are handled, and how nested |
||
490 | * sets are handled. |
||
491 | * |
||
492 | * Setting query string arguments MUST NOT change the URI stored by the |
||
493 | * request, nor the values in the server params. |
||
494 | * |
||
495 | * This method MUST be implemented in such a way as to retain the |
||
496 | * immutability of the message, and MUST return an instance that has the |
||
497 | * updated query string arguments. |
||
498 | * |
||
499 | * @param array $query Array of query string arguments, typically from |
||
500 | * $_GET. |
||
501 | * @return static |
||
502 | */ |
||
503 | public function withQueryParams(array $query) |
||
507 | |||
508 | /** |
||
509 | * Retrieve normalized file upload data. |
||
510 | * |
||
511 | * This method returns upload metadata in a normalized tree, with each leaf |
||
512 | * an instance of Psr\Http\Message\UploadedFileInterface. |
||
513 | * |
||
514 | * These values MAY be prepared from $_FILES or the message body during |
||
515 | * instantiation, or MAY be injected via withUploadedFiles(). |
||
516 | * |
||
517 | * @return array An array tree of UploadedFileInterface instances; an empty |
||
518 | * array MUST be returned if no data is present. |
||
519 | */ |
||
520 | public function getUploadedFiles() |
||
524 | |||
525 | /** |
||
526 | * Create a new instance with the specified uploaded files. |
||
527 | * |
||
528 | * This method MUST be implemented in such a way as to retain the |
||
529 | * immutability of the message, and MUST return an instance that has the |
||
530 | * updated body parameters. |
||
531 | * |
||
532 | * @param array $uploadedFiles An array tree of UploadedFileInterface instances. |
||
533 | * @return static |
||
534 | * @throws \InvalidArgumentException if an invalid structure is provided. |
||
535 | */ |
||
536 | public function withUploadedFiles(array $uploadedFiles) |
||
540 | |||
541 | /** |
||
542 | * Retrieve any parameters provided in the request body. |
||
543 | * |
||
544 | * If the request Content-Type is either application/x-www-form-urlencoded |
||
545 | * or multipart/form-data, and the request method is POST, this method MUST |
||
546 | * return the contents of $_POST. |
||
547 | * |
||
548 | * Otherwise, this method may return any results of deserializing |
||
549 | * the request body content; as parsing returns structured content, the |
||
550 | * potential types MUST be arrays or objects only. A null value indicates |
||
551 | * the absence of body content. |
||
552 | * |
||
553 | * @return null|array|object The deserialized body parameters, if any. |
||
554 | * These will typically be an array or object. |
||
555 | */ |
||
556 | public function getParsedBody() |
||
560 | |||
561 | /** |
||
562 | * Return an instance with the specified body parameters. |
||
563 | * |
||
564 | * These MAY be injected during instantiation. |
||
565 | * |
||
566 | * If the request Content-Type is either application/x-www-form-urlencoded |
||
567 | * or multipart/form-data, and the request method is POST, use this method |
||
568 | * ONLY to inject the contents of $_POST. |
||
569 | * |
||
570 | * The data IS NOT REQUIRED to come from $_POST, but MUST be the results of |
||
571 | * deserializing the request body content. Deserialization/parsing returns |
||
572 | * structured data, and, as such, this method ONLY accepts arrays or objects, |
||
573 | * or a null value if nothing was available to parse. |
||
574 | * |
||
575 | * As an example, if content negotiation determines that the request data |
||
576 | * is a JSON payload, this method could be used to create a request |
||
577 | * instance with the deserialized parameters. |
||
578 | * |
||
579 | * This method MUST be implemented in such a way as to retain the |
||
580 | * immutability of the message, and MUST return an instance that has the |
||
581 | * updated body parameters. |
||
582 | * |
||
583 | * @param null|array|object $data The deserialized body data. This will |
||
584 | * typically be in an array or object. |
||
585 | * @return static |
||
586 | * @throws \InvalidArgumentException if an unsupported argument type is |
||
587 | * provided. |
||
588 | */ |
||
589 | public function withParsedBody($data) |
||
593 | |||
594 | /** |
||
595 | * Retrieve attributes derived from the request. |
||
596 | * |
||
597 | * The request "attributes" may be used to allow injection of any |
||
598 | * parameters derived from the request: e.g., the results of path |
||
599 | * match operations; the results of decrypting cookies; the results of |
||
600 | * deserializing non-form-encoded message bodies; etc. Attributes |
||
601 | * will be application and request specific, and CAN be mutable. |
||
602 | * |
||
603 | * @return array Attributes derived from the request. |
||
604 | */ |
||
605 | public function getAttributes() |
||
609 | * Retrieve a single derived request attribute. |
||
610 | * |
||
611 | * Retrieves a single derived request attribute as described in |
||
612 | * getAttributes(). If the attribute has not been previously set, returns |
||
613 | * the default value as provided. |
||
614 | * |
||
615 | * This method obviates the need for a hasAttribute() method, as it allows |
||
616 | * specifying a default value to return if the attribute is not found. |
||
617 | * |
||
618 | * @param string $name The attribute name. |
||
619 | * @param mixed $default Default value to return if the attribute does not exist. |
||
620 | * @return mixed |
||
621 | * @see getAttributes() |
||
622 | */ |
||
623 | public function getAttribute($name, $default = null) |
||
627 | |||
628 | /** |
||
629 | * Return an instance with the specified derived request attribute. |
||
630 | * |
||
631 | * This method allows setting a single derived request attribute as |
||
632 | * described in getAttributes(). |
||
633 | * |
||
634 | * This method MUST be implemented in such a way as to retain the |
||
635 | * immutability of the message, and MUST return an instance that has the |
||
636 | * updated attribute. |
||
637 | * |
||
638 | * @param string $name The attribute name. |
||
639 | * @param mixed $value The value of the attribute. |
||
640 | * @return static |
||
641 | * @see getAttributes() |
||
642 | */ |
||
643 | public function withAttribute($name, $value) |
||
647 | |||
648 | /** |
||
649 | * Return an instance that removes the specified derived request attribute. |
||
650 | * |
||
651 | * This method allows removing a single derived request attribute as |
||
652 | * described in getAttributes(). |
||
653 | * |
||
654 | * This method MUST be implemented in such a way as to retain the |
||
655 | * immutability of the message, and MUST return an instance that removes |
||
656 | * the attribute. |
||
657 | * |
||
658 | * @param string $name The attribute name. |
||
659 | * @return static |
||
660 | * @see getAttributes() |
||
661 | */ |
||
662 | public function withoutAttribute($name) |
||
666 | } |
||
667 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: