1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224
|
<?php
/**
* Slim Framework (https://slimframework.com)
*
* @license https://github.com/slimphp/Slim/blob/3.x/LICENSE.md (MIT License)
*/
namespace Slim\Http;
use Closure;
use InvalidArgumentException;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\StreamInterface;
use Psr\Http\Message\UploadedFileInterface;
use Psr\Http\Message\UriInterface;
use RuntimeException;
use Slim\Collection;
use Slim\Exception\InvalidMethodException;
use Slim\Interfaces\Http\HeadersInterface;
/**
* This class represents an HTTP request.
* It manages the request method, URI, headers, cookies, and body according to the PSR-7 standard.
*
* @link https://github.com/php-fig/http-message/blob/master/src/MessageInterface.php
* @link https://github.com/php-fig/http-message/blob/master/src/RequestInterface.php
* @link https://github.com/php-fig/http-message/blob/master/src/ServerRequestInterface.php
*/
class Request extends Message implements ServerRequestInterface
{
/**
* The request method
*
* @var string
*/
protected $method;
/**
* The original request method (ignoring override)
*
* @var string
*/
protected $originalMethod;
/**
* The request URI object
*
* @var UriInterface
*/
protected $uri;
/**
* The request URI target (path + query string)
*
* @var string
*/
protected $requestTarget;
/**
* The request query string params
*
* @var array
*/
protected $queryParams;
/**
* The request cookies
*
* @var array
*/
protected $cookies;
/**
* The server environment variables at the time the request was created.
*
* @var array
*/
protected $serverParams;
/**
* The request attributes (route segment names and values)
*
* @var Collection
*/
protected $attributes;
/**
* The request body parsed (if possible) into a PHP array or object
*
* @var null|array|object
*/
protected $bodyParsed = false;
/**
* List of request body parsers (e.g., url-encoded, JSON, XML, multipart)
*
* @var callable[]
*/
protected $bodyParsers = [];
/**
* List of uploaded files
*
* @var UploadedFileInterface[]
*/
protected $uploadedFiles;
/**
* Valid request methods
*
* @var string[]
* @deprecated
*/
protected $validMethods = [
'CONNECT' => 1,
'DELETE' => 1,
'GET' => 1,
'HEAD' => 1,
'OPTIONS' => 1,
'PATCH' => 1,
'POST' => 1,
'PUT' => 1,
'TRACE' => 1,
];
/**
* Create new HTTP request with data extracted from the application
* Environment object
*
* @param Environment $environment The Slim application Environment
*
* @return static
*/
public static function createFromEnvironment(Environment $environment)
{
$method = $environment['REQUEST_METHOD'];
$uri = Uri::createFromEnvironment($environment);
$headers = Headers::createFromEnvironment($environment);
$cookies = Cookies::parseHeader($headers->get('Cookie', []));
$serverParams = $environment->all();
$body = new RequestBody();
$uploadedFiles = UploadedFile::createFromEnvironment($environment);
$request = new static($method, $uri, $headers, $cookies, $serverParams, $body, $uploadedFiles);
if ($method === 'POST' &&
in_array($request->getMediaType(), ['application/x-www-form-urlencoded', 'multipart/form-data'])
) {
// parsed body must be $_POST
$request = $request->withParsedBody($_POST);
}
return $request;
}
/**
* @param string $method The request method
* @param UriInterface $uri The request URI object
* @param HeadersInterface $headers The request headers collection
* @param array $cookies The request cookies collection
* @param array $serverParams The server environment variables
* @param StreamInterface $body The request body object
* @param array $uploadedFiles The request uploadedFiles collection
*
* @throws InvalidMethodException on invalid HTTP method
*/
public function __construct(
$method,
UriInterface $uri,
HeadersInterface $headers,
array $cookies,
array $serverParams,
StreamInterface $body,
array $uploadedFiles = []
) {
try {
$this->originalMethod = $this->filterMethod($method);
} catch (InvalidMethodException $e) {
$this->originalMethod = $method;
}
$this->uri = $uri;
$this->headers = $headers;
$this->cookies = $cookies;
$this->serverParams = $serverParams;
$this->attributes = new Collection();
$this->body = $body;
$this->uploadedFiles = $uploadedFiles;
if (isset($serverParams['SERVER_PROTOCOL'])) {
$this->protocolVersion = str_replace('HTTP/', '', $serverParams['SERVER_PROTOCOL']);
}
if (!$this->headers->has('Host') && $this->uri->getHost() !== '') {
$port = $this->uri->getPort() ? ":{$this->uri->getPort()}" : '';
$this->headers->set('Host', $this->uri->getHost() . $port);
}
$this->registerMediaTypeParser('application/json', function ($input) {
$result = json_decode($input, true);
if (!is_array($result)) {
return null;
}
return $result;
});
$this->registerMediaTypeParser('application/xml', function ($input) {
$backup = self::disableXmlEntityLoader(true);
$backup_errors = libxml_use_internal_errors(true);
$result = simplexml_load_string($input);
self::disableXmlEntityLoader($backup);
libxml_clear_errors();
libxml_use_internal_errors($backup_errors);
if ($result === false) {
return null;
}
return $result;
});
$this->registerMediaTypeParser('text/xml', function ($input) {
$backup = self::disableXmlEntityLoader(true);
$backup_errors = libxml_use_internal_errors(true);
$result = simplexml_load_string($input);
self::disableXmlEntityLoader($backup);
libxml_clear_errors();
libxml_use_internal_errors($backup_errors);
if ($result === false) {
return null;
}
return $result;
});
$this->registerMediaTypeParser('application/x-www-form-urlencoded', function ($input) {
parse_str($input, $data);
return $data;
});
// if the request had an invalid method, we can throw it now
if (isset($e) && $e instanceof InvalidMethodException) {
throw $e;
}
}
/**
* This method is applied to the cloned object after PHP performs an initial shallow-copy.
* This method completes a deep-copy by creating new objects for the cloned object's internal reference pointers.
*/
public function __clone()
{
$this->headers = clone $this->headers;
$this->attributes = clone $this->attributes;
$this->body = clone $this->body;
}
/**
* Retrieves the HTTP method of the request.
*
* @return string
*/
public function getMethod()
{
if ($this->method === null) {
$this->method = $this->originalMethod;
$customMethod = $this->getHeaderLine('X-Http-Method-Override');
if ($customMethod) {
$this->method = $this->filterMethod($customMethod);
} elseif ($this->originalMethod === 'POST') {
$overrideMethod = $this->filterMethod($this->getParsedBodyParam('_METHOD'));
if ($overrideMethod !== null) {
$this->method = $overrideMethod;
}
if ($this->getBody()->eof()) {
$this->getBody()->rewind();
}
}
}
return $this->method;
}
/**
* Get the original HTTP method (ignore override).
*
* Note: This method is not part of the PSR-7 standard.
*
* @return string
*/
public function getOriginalMethod()
{
return $this->originalMethod;
}
/**
* Return an instance with the provided HTTP method.
*
* While HTTP method names are typically all uppercase characters, HTTP
* method names are case-sensitive and thus implementations SHOULD NOT
* modify the given string.
*
* This method MUST be implemented in such a way as to retain the
* immutability of the message, and MUST return an instance that has the
* changed request method.
*
* @param string $method Case-sensitive method.
*
* @return static
*
* @throws InvalidArgumentException for invalid HTTP methods.
*/
public function withMethod($method)
{
$method = $this->filterMethod($method);
$clone = clone $this;
$clone->originalMethod = $method;
$clone->method = $method;
return $clone;
}
/**
* Validate the HTTP method
*
* @param null|string $method
*
* @return null|string
*
* @throws InvalidArgumentException on invalid HTTP method.
*/
protected function filterMethod($method)
{
if ($method === null) {
return $method;
}
if (!is_string($method)) {
throw new InvalidArgumentException(sprintf(
'Unsupported HTTP method; must be a string, received %s',
(is_object($method) ? get_class($method) : gettype($method))
));
}
$method = strtoupper($method);
if (preg_match("/^[!#$%&'*+.^_`|~0-9a-z-]+$/i", $method) !== 1) {
throw new InvalidMethodException($this, $method);
}
return $method;
}
/**
* Does this request use a given method?
*
* Note: This method is not part of the PSR-7 standard.
*
* @param string $method HTTP method
*
* @return bool
*/
public function isMethod($method)
{
return $this->getMethod() === $method;
}
/**
* Is this a GET request?
*
* Note: This method is not part of the PSR-7 standard.
*
* @return bool
*/
public function isGet()
{
return $this->isMethod('GET');
}
/**
* Is this a POST request?
*
* Note: This method is not part of the PSR-7 standard.
*
* @return bool
*/
public function isPost()
{
return $this->isMethod('POST');
}
/**
* Is this a PUT request?
*
* Note: This method is not part of the PSR-7 standard.
*
* @return bool
*/
public function isPut()
{
return $this->isMethod('PUT');
}
/**
* Is this a PATCH request?
*
* Note: This method is not part of the PSR-7 standard.
*
* @return bool
*/
public function isPatch()
{
return $this->isMethod('PATCH');
}
/**
* Is this a DELETE request?
*
* Note: This method is not part of the PSR-7 standard.
*
* @return bool
*/
public function isDelete()
{
return $this->isMethod('DELETE');
}
/**
* Is this a HEAD request?
*
* Note: This method is not part of the PSR-7 standard.
*
* @return bool
*/
public function isHead()
{
return $this->isMethod('HEAD');
}
/**
* Is this a OPTIONS request?
*
* Note: This method is not part of the PSR-7 standard.
*
* @return bool
*/
public function isOptions()
{
return $this->isMethod('OPTIONS');
}
/**
* Is this an XHR request?
*
* Note: This method is not part of the PSR-7 standard.
*
* @return bool
*/
public function isXhr()
{
return $this->getHeaderLine('X-Requested-With') === 'XMLHttpRequest';
}
/**
* Retrieves the message's request target.
*
* Retrieves the message's request-target either as it will appear (for
* clients), as it appeared at request (for servers), or as it was
* specified for the instance (see withRequestTarget()).
*
* In most cases, this will be the origin-form of the composed URI,
* unless a value was provided to the concrete implementation (see
* withRequestTarget() below).
*
* If no URI is available, and no request-target has been specifically
* provided, this method MUST return the string "/".
*
* @return string
*/
public function getRequestTarget()
{
if ($this->requestTarget) {
return $this->requestTarget;
}
if ($this->uri === null) {
return '/';
}
if ($this->uri instanceof Uri) {
$basePath = $this->uri->getBasePath();
} else {
$basePath = '';
}
$path = $this->uri->getPath();
$path = $basePath . '/' . ltrim($path, '/');
$query = $this->uri->getQuery();
if ($query) {
$path .= '?' . $query;
}
$this->requestTarget = $path;
return $this->requestTarget;
}
/**
* Return an instance with the specific request-target.
*
* If the request needs a non-origin-form request-target — e.g., for
* specifying an absolute-form, authority-form, or asterisk-form —
* this method may be used to create an instance with the specified
* request-target, verbatim.
*
* This method MUST be implemented in such a way as to retain the
* immutability of the message, and MUST return an instance that has the
* changed request target.
*
* @link http://tools.ietf.org/html/rfc7230#section-2.7
* (for the various request-target forms allowed in request messages)
*
* @param string $requestTarget
*
* @return static
*
* @throws InvalidArgumentException if the request target is invalid
*/
public function withRequestTarget($requestTarget)
{
if (preg_match('#\s#', $requestTarget)) {
throw new InvalidArgumentException(
'Invalid request target provided; must be a string and cannot contain whitespace'
);
}
$clone = clone $this;
$clone->requestTarget = $requestTarget;
return $clone;
}
/**
* Retrieves the URI instance.
*
* This method MUST return a UriInterface instance.
*
* @link http://tools.ietf.org/html/rfc3986#section-4.3
*
* @return UriInterface
*/
public function getUri()
{
return $this->uri;
}
/**
* Returns an instance with the provided URI.
*
* This method MUST update the Host header of the returned request by
* default if the URI contains a host component. If the URI does not
* contain a host component, any pre-existing Host header MUST be carried
* over to the returned request.
*
* You can opt-in to preserving the original state of the Host header by
* setting `$preserveHost` to `true`. When `$preserveHost` is set to
* `true`, this method interacts with the Host header in the following ways:
*
* - If the the Host header is missing or empty, and the new URI contains
* a host component, this method MUST update the Host header in the returned
* request.
* - If the Host header is missing or empty, and the new URI does not contain a
* host component, this method MUST NOT update the Host header in the returned
* request.
* - If a Host header is present and non-empty, this method MUST NOT update
* the Host header in the returned request.
*
* This method MUST be implemented in such a way as to retain the
* immutability of the message, and MUST return an instance that has the
* new UriInterface instance.
*
* @link http://tools.ietf.org/html/rfc3986#section-4.3
*
* @param UriInterface $uri New request URI to use.
* @param bool $preserveHost Preserve the original state of the Host header.
*
* @return static
*/
public function withUri(UriInterface $uri, $preserveHost = false)
{
$clone = clone $this;
$clone->uri = $uri;
if (!$preserveHost) {
if ($uri->getHost() !== '') {
$clone->headers->set('Host', $uri->getHost());
}
} else {
if ($uri->getHost() !== '' && (!$this->hasHeader('Host') || $this->getHeaderLine('Host') === '')) {
$clone->headers->set('Host', $uri->getHost());
}
}
return $clone;
}
/**
* Get request content type.
*
* Note: This method is not part of the PSR-7 standard.
*
* @return string|null
*/
public function getContentType()
{
$result = $this->getHeader('Content-Type');
return $result ? $result[0] : null;
}
/**
* Get request media type, if known.
*
* Note: This method is not part of the PSR-7 standard.
*
* @return string|null
*/
public function getMediaType()
{
$contentType = $this->getContentType();
if ($contentType) {
$contentTypeParts = preg_split('/\s*[;,]\s*/', $contentType);
return strtolower($contentTypeParts[0]);
}
return null;
}
/**
* Get request media type params, if known.
*
* Note: This method is not part of the PSR-7 standard.
*
* @return string[]
*/
public function getMediaTypeParams()
{
$contentType = $this->getContentType();
$contentTypeParams = [];
if ($contentType) {
$contentTypeParts = preg_split('/\s*[;,]\s*/', $contentType);
$contentTypePartsLength = count($contentTypeParts);
for ($i = 1; $i < $contentTypePartsLength; $i++) {
$paramParts = explode('=', $contentTypeParts[$i]);
$contentTypeParams[strtolower($paramParts[0])] = $paramParts[1];
}
}
return $contentTypeParams;
}
/**
* Get request content character set, if known.
*
* Note: This method is not part of the PSR-7 standard.
*
* @return string|null
*/
public function getContentCharset()
{
$mediaTypeParams = $this->getMediaTypeParams();
if (isset($mediaTypeParams['charset'])) {
return $mediaTypeParams['charset'];
}
return null;
}
/**
* Get request content length, if known.
*
* Note: This method is not part of the PSR-7 standard.
*
* @return int|null
*/
public function getContentLength()
{
$result = $this->headers->get('Content-Length');
return $result ? (int)$result[0] : null;
}
/**
* Retrieve cookies.
*
* Retrieves cookies sent by the client to the server.
*
* The data MUST be compatible with the structure of the $_COOKIE superglobal.
*
* @return array
*/
public function getCookieParams()
{
return $this->cookies;
}
/**
* Fetch cookie value from cookies sent by the client to the server.
*
* Note: This method is not part of the PSR-7 standard.
*
* @param string $key The attribute name.
* @param mixed $default Default value to return if the attribute does not exist.
*
* @return mixed
*/
public function getCookieParam($key, $default = null)
{
$cookies = $this->getCookieParams();
$result = $default;
if (isset($cookies[$key])) {
$result = $cookies[$key];
}
return $result;
}
/**
* Return an instance with the specified cookies.
*
* The data IS NOT REQUIRED to come from the $_COOKIE superglobal, but MUST
* be compatible with the structure of $_COOKIE. Typically, this data will
* be injected at instantiation.
*
* This method MUST NOT update the related Cookie header of the request
* instance, nor related values in the server params.
*
* This method MUST be implemented in such a way as to retain the
* immutability of the message, and MUST return an instance that has the
* updated cookie values.
*
* @param array $cookies Array of key/value pairs representing cookies.
*
* @return static
*/
public function withCookieParams(array $cookies)
{
$clone = clone $this;
$clone->cookies = $cookies;
return $clone;
}
/**
* Retrieve query string arguments.
*
* Retrieves the deserialized query string arguments, if any.
*
* Note: the query params might not be in sync with the URI or server
* params. If you need to ensure you are only getting the original
* values, you may need to parse the query string from `getUri()->getQuery()`
* or from the `QUERY_STRING` server param.
*
* @return array
*/
public function getQueryParams()
{
if (is_array($this->queryParams)) {
return $this->queryParams;
}
if ($this->uri === null) {
return [];
}
parse_str($this->uri->getQuery(), $this->queryParams); // <-- URL decodes data
return $this->queryParams;
}
/**
* Return an instance with the specified query string arguments.
*
* These values SHOULD remain immutable over the course of the incoming
* request. They MAY be injected during instantiation, such as from PHP's
* $_GET superglobal, or MAY be derived from some other value such as the
* URI. In cases where the arguments are parsed from the URI, the data
* MUST be compatible with what PHP's parse_str() would return for
* purposes of how duplicate query parameters are handled, and how nested
* sets are handled.
*
* Setting query string arguments MUST NOT change the URI stored by the
* request, nor the values in the server params.
*
* This method MUST be implemented in such a way as to retain the
* immutability of the message, and MUST return an instance that has the
* updated query string arguments.
*
* @param array $query Array of query string arguments, typically from
* $_GET.
*
* @return static
*/
public function withQueryParams(array $query)
{
$clone = clone $this;
$clone->queryParams = $query;
return $clone;
}
/**
* Retrieve normalized file upload data.
*
* This method returns upload metadata in a normalized tree, with each leaf
* an instance of Psr\Http\Message\UploadedFileInterface.
*
* These values MAY be prepared from $_FILES or the message body during
* instantiation, or MAY be injected via withUploadedFiles().
*
* @return array
*/
public function getUploadedFiles()
{
return $this->uploadedFiles;
}
/**
* Create a new instance with the specified uploaded files.
*
* This method MUST be implemented in such a way as to retain the
* immutability of the message, and MUST return an instance that has the
* updated body parameters.
*
* @param array $uploadedFiles An array tree of UploadedFileInterface instances.
*
* @return static
*
* @throws InvalidArgumentException if an invalid structure is provided.
*/
public function withUploadedFiles(array $uploadedFiles)
{
$clone = clone $this;
$clone->uploadedFiles = $uploadedFiles;
return $clone;
}
/**
* Retrieve server parameters.
*
* Retrieves data related to the incoming request environment,
* typically derived from PHP's $_SERVER superglobal. The data IS NOT
* REQUIRED to originate from $_SERVER.
*
* @return array
*/
public function getServerParams()
{
return $this->serverParams;
}
/**
* Retrieve a server parameter.
*
* Note: This method is not part of the PSR-7 standard.
*
* @param string $key
* @param mixed $default
*
* @return mixed
*/
public function getServerParam($key, $default = null)
{
$serverParams = $this->getServerParams();
return isset($serverParams[$key]) ? $serverParams[$key] : $default;
}
/**
* Retrieve attributes derived from the request.
*
* The request "attributes" may be used to allow injection of any
* parameters derived from the request: e.g., the results of path
* match operations; the results of decrypting cookies; the results of
* deserializing non-form-encoded message bodies; etc. Attributes
* will be application and request specific, and CAN be mutable.
*
* @return array
*/
public function getAttributes()
{
return $this->attributes->all();
}
/**
* Retrieve a single derived request attribute.
*
* Retrieves a single derived request attribute as described in
* getAttributes(). If the attribute has not been previously set, returns
* the default value as provided.
*
* This method obviates the need for a hasAttribute() method, as it allows
* specifying a default value to return if the attribute is not found.
*
* @see getAttributes()
*
* @param string $name The attribute name.
* @param mixed $default Default value to return if the attribute does not exist.
*
* @return mixed
*/
public function getAttribute($name, $default = null)
{
return $this->attributes->get($name, $default);
}
/**
* Return an instance with the specified derived request attribute.
*
* This method allows setting a single derived request attribute as
* described in getAttributes().
*
* This method MUST be implemented in such a way as to retain the
* immutability of the message, and MUST return an instance that has the
* updated attribute.
*
* @see getAttributes()
*
* @param string $name The attribute name.
* @param mixed $value The value of the attribute.
*
* @return static
*/
public function withAttribute($name, $value)
{
$clone = clone $this;
$clone->attributes->set($name, $value);
return $clone;
}
/**
* Create a new instance with the specified derived request attributes.
*
* Note: This method is not part of the PSR-7 standard.
*
* This method allows setting all new derived request attributes as
* described in getAttributes().
*
* This method MUST be implemented in such a way as to retain the
* immutability of the message, and MUST return a new instance that has the
* updated attributes.
*
* @param array $attributes New attributes
*
* @return static
*/
public function withAttributes(array $attributes)
{
$clone = clone $this;
$clone->attributes = new Collection($attributes);
return $clone;
}
/**
* Return an instance that removes the specified derived request attribute.
*
* This method allows removing a single derived request attribute as
* described in getAttributes().
*
* This method MUST be implemented in such a way as to retain the
* immutability of the message, and MUST return an instance that removes
* the attribute.
*
* @see getAttributes()
*
* @param string $name The attribute name.
*
* @return static
*/
public function withoutAttribute($name)
{
$clone = clone $this;
$clone->attributes->remove($name);
return $clone;
}
/**
* Retrieve any parameters provided in the request body.
*
* If the request Content-Type is either application/x-www-form-urlencoded
* or multipart/form-data, and the request method is POST, this method MUST
* return the contents of $_POST.
*
* Otherwise, this method may return any results of deserializing
* the request body content; as parsing returns structured content, the
* potential types MUST be arrays or objects only. A null value indicates
* the absence of body content.
*
* @return null|array|object
*
* @throws RuntimeException if the request body media type parser returns an invalid value
*/
public function getParsedBody()
{
if ($this->bodyParsed !== false) {
return $this->bodyParsed;
}
if (!$this->body) {
return null;
}
$mediaType = $this->getMediaType();
// Check if this specific media type has a parser registered first
if (!isset($this->bodyParsers[$mediaType])) {
// If not, look for a media type with a structured syntax suffix (RFC 6839)
$parts = explode('+', $mediaType);
if (count($parts) >= 2) {
$mediaType = 'application/' . $parts[count($parts)-1];
}
}
if (isset($this->bodyParsers[$mediaType])) {
$body = (string)$this->getBody();
$parsed = $this->bodyParsers[$mediaType]($body);
if (!is_null($parsed) && !is_object($parsed) && !is_array($parsed)) {
throw new RuntimeException(
'Request body media type parser return value must be an array, an object, or null'
);
}
$this->bodyParsed = $parsed;
return $this->bodyParsed;
}
return null;
}
/**
* Return an instance with the specified body parameters.
*
* These MAY be injected during instantiation.
*
* If the request Content-Type is either application/x-www-form-urlencoded
* or multipart/form-data, and the request method is POST, use this method
* ONLY to inject the contents of $_POST.
*
* The data IS NOT REQUIRED to come from $_POST, but MUST be the results of
* deserializing the request body content. Deserialization/parsing returns
* structured data, and, as such, this method ONLY accepts arrays or objects,
* or a null value if nothing was available to parse.
*
* As an example, if content negotiation determines that the request data
* is a JSON payload, this method could be used to create a request
* instance with the deserialized parameters.
*
* This method MUST be implemented in such a way as to retain the
* immutability of the message, and MUST return an instance that has the
* updated body parameters.
*
* @param null|array|object $data The deserialized body data. This will typically be in an array or object.
*
* @return static
*
* @throws InvalidArgumentException if an unsupported argument type is
* provided.
*/
public function withParsedBody($data)
{
if (!is_null($data) && !is_object($data) && !is_array($data)) {
throw new InvalidArgumentException('Parsed body value must be an array, an object, or null');
}
$clone = clone $this;
$clone->bodyParsed = $data;
return $clone;
}
/**
* Force Body to be parsed again.
*
* Note: This method is not part of the PSR-7 standard.
*
* @return $this
*/
public function reparseBody()
{
$this->bodyParsed = false;
return $this;
}
/**
* Register media type parser.
*
* Note: This method is not part of the PSR-7 standard.
*
* @param string $mediaType A HTTP media type (excluding content-type params).
* @param callable $callable A callable that returns parsed contents for media type.
*/
public function registerMediaTypeParser($mediaType, callable $callable)
{
if ($callable instanceof Closure) {
$callable = $callable->bindTo($this);
}
$this->bodyParsers[(string)$mediaType] = $callable;
}
/**
* Fetch request parameter value from body or query string (in that order).
*
* Note: This method is not part of the PSR-7 standard.
*
* @param string $key The parameter key.
* @param mixed $default The default value.
*
* @return mixed
*/
public function getParam($key, $default = null)
{
$postParams = $this->getParsedBody();
$getParams = $this->getQueryParams();
$result = $default;
if (is_array($postParams) && isset($postParams[$key])) {
$result = $postParams[$key];
} elseif (is_object($postParams) && property_exists($postParams, $key)) {
$result = $postParams->$key;
} elseif (isset($getParams[$key])) {
$result = $getParams[$key];
}
return $result;
}
/**
* Fetch parameter value from request body.
*
* Note: This method is not part of the PSR-7 standard.
*
* @param string $key
* @param mixed $default
*
* @return mixed
*/
public function getParsedBodyParam($key, $default = null)
{
$postParams = $this->getParsedBody();
$result = $default;
if (is_array($postParams) && isset($postParams[$key])) {
$result = $postParams[$key];
} elseif (is_object($postParams) && property_exists($postParams, $key)) {
$result = $postParams->$key;
}
return $result;
}
/**
* Fetch parameter value from query string.
*
* Note: This method is not part of the PSR-7 standard.
*
* @param string $key
* @param mixed $default
*
* @return mixed
*/
public function getQueryParam($key, $default = null)
{
$getParams = $this->getQueryParams();
$result = $default;
if (isset($getParams[$key])) {
$result = $getParams[$key];
}
return $result;
}
/**
* Fetch associative array of body and query string parameters.
*
* Note: This method is not part of the PSR-7 standard.
*
* @param array|null $only list the keys to retrieve.
*
* @return mixed[]
*/
public function getParams(array $only = null)
{
$params = $this->getQueryParams();
$postParams = $this->getParsedBody();
if ($postParams) {
$params = array_replace($params, (array)$postParams);
}
if ($only) {
$onlyParams = [];
foreach ($only as $key) {
if (array_key_exists($key, $params)) {
$onlyParams[$key] = $params[$key];
}
}
return $onlyParams;
}
return $params;
}
private static function disableXmlEntityLoader($disable)
{
if (\LIBXML_VERSION >= 20900) {
// libxml >= 2.9.0 disables entity loading by default, so it is
// safe to skip the real call (deprecated in PHP 8).
return true;
}
// @codeCoverageIgnoreStart
return libxml_disable_entity_loader($disable);
// @codeCoverageIgnoreEnd
}
}
|