├── src ├── Factory │ ├── DecoratedUriFactory.php │ ├── DecoratedServerRequestFactory.php │ └── DecoratedResponseFactory.php ├── Interfaces │ └── ResponseInterface.php ├── Uri.php ├── Response.php └── ServerRequest.php ├── LICENSE.md └── composer.json /src/Factory/DecoratedUriFactory.php: -------------------------------------------------------------------------------- 1 | uriFactory = $uriFactory; 27 | } 28 | 29 | /** 30 | * @param string $uri 31 | * @return Uri 32 | */ 33 | public function createUri(string $uri = ''): UriInterface 34 | { 35 | $uri = $this->uriFactory->createUri($uri); 36 | return new Uri($uri); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Slim Framework 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /src/Factory/DecoratedServerRequestFactory.php: -------------------------------------------------------------------------------- 1 | serverRequestFactory = $serverRequestFactory; 28 | } 29 | 30 | /** 31 | * @param string $method 32 | * @param UriInterface|string $uri 33 | * @param array{key: string, value: mixed}|array $serverParams 34 | * @return ServerRequest 35 | */ 36 | public function createServerRequest(string $method, $uri, array $serverParams = []): ServerRequestInterface 37 | { 38 | $serverRequest = $this->serverRequestFactory->createServerRequest($method, $uri, $serverParams); 39 | return new ServerRequest($serverRequest); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /src/Factory/DecoratedResponseFactory.php: -------------------------------------------------------------------------------- 1 | responseFactory = $responseFactory; 31 | $this->streamFactory = $streamFactory; 32 | } 33 | 34 | /** 35 | * @param int $code 36 | * @param string $reasonPhrase 37 | * @return Response 38 | */ 39 | public function createResponse(int $code = 200, string $reasonPhrase = ''): ResponseInterface 40 | { 41 | $response = $this->responseFactory->createResponse($code, $reasonPhrase); 42 | return new Response($response, $this->streamFactory); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "slim/http", 3 | "type": "library", 4 | "description": "Slim PSR-7 Object Decorators", 5 | "keywords": [ 6 | "psr7", 7 | "psr-7", 8 | "http" 9 | ], 10 | "homepage": "http://slimframework.com", 11 | "license": "MIT", 12 | "authors": [ 13 | { 14 | "name": "Josh Lockhart", 15 | "email": "hello@joshlockhart.com", 16 | "homepage": "http://joshlockhart.com" 17 | }, 18 | { 19 | "name": "Andrew Smith", 20 | "email": "a.smith@silentworks.co.uk", 21 | "homepage": "http://silentworks.co.uk" 22 | }, 23 | { 24 | "name": "Rob Allen", 25 | "email": "rob@akrabat.com", 26 | "homepage": "http://akrabat.com" 27 | }, 28 | { 29 | "name": "Pierre Berube", 30 | "email": "pierre@lgse.com", 31 | "homepage": "http://www.lgse.com" 32 | } 33 | ], 34 | "require": { 35 | "php": "^8.0", 36 | "ext-SimpleXML": "*", 37 | "ext-fileinfo": "*", 38 | "ext-json": "*", 39 | "ext-libxml": "*", 40 | "psr/http-factory": "^1.0", 41 | "psr/http-message": "^1.1 || ^2.0" 42 | }, 43 | "require-dev": { 44 | "adriansuter/php-autoload-override": "^1.4", 45 | "laminas/laminas-diactoros": "^3.1.0", 46 | "nyholm/psr7": "^1.8.1", 47 | "php-http/psr7-integration-tests": "^1.3.0", 48 | "phpstan/phpstan": "^2.0", 49 | "phpunit/phpunit": "^9.6", 50 | "doctrine/instantiator": "^1.3.1", 51 | "squizlabs/php_codesniffer": "^3.9" 52 | }, 53 | "autoload": { 54 | "psr-4": { 55 | "Slim\\Http\\": "src/" 56 | } 57 | }, 58 | "autoload-dev": { 59 | "psr-4": { 60 | "Slim\\Tests\\Http\\": "tests/" 61 | } 62 | }, 63 | "scripts": { 64 | "test": [ 65 | "@phpunit", 66 | "@phpcs", 67 | "@phpstan" 68 | ], 69 | "phpunit": "phpunit", 70 | "phpcs": "phpcs", 71 | "phpstan": "phpstan --memory-limit=-1" 72 | }, 73 | "config": { 74 | "sort-packages": true 75 | } 76 | } 77 | -------------------------------------------------------------------------------- /src/Interfaces/ResponseInterface.php: -------------------------------------------------------------------------------- 1 | uri = $uri; 25 | } 26 | 27 | /** 28 | * Disable magic setter to ensure immutability 29 | * @param mixed $name 30 | * @param mixed $value 31 | * @return void 32 | */ 33 | public function __set($name, $value) 34 | { 35 | } 36 | 37 | /** 38 | * {@inheritdoc} 39 | */ 40 | public function getAuthority(): string 41 | { 42 | return $this->uri->getAuthority(); 43 | } 44 | 45 | /** 46 | * {@inheritdoc} 47 | */ 48 | public function getFragment(): string 49 | { 50 | return $this->uri->getFragment(); 51 | } 52 | 53 | /** 54 | * {@inheritdoc} 55 | */ 56 | public function getHost(): string 57 | { 58 | return $this->uri->getHost(); 59 | } 60 | 61 | /** 62 | * {@inheritdoc} 63 | */ 64 | public function getPath(): string 65 | { 66 | return $this->uri->getPath(); 67 | } 68 | 69 | /** 70 | * {@inheritdoc} 71 | */ 72 | public function getPort(): ?int 73 | { 74 | return $this->uri->getPort(); 75 | } 76 | 77 | /** 78 | * {@inheritdoc} 79 | */ 80 | public function getQuery(): string 81 | { 82 | return $this->uri->getQuery(); 83 | } 84 | 85 | /** 86 | * {@inheritdoc} 87 | */ 88 | public function getScheme(): string 89 | { 90 | return $this->uri->getScheme(); 91 | } 92 | 93 | /** 94 | * {@inheritdoc} 95 | */ 96 | public function getUserInfo(): string 97 | { 98 | return $this->uri->getUserInfo(); 99 | } 100 | 101 | /** 102 | * {@inheritdoc} 103 | */ 104 | public function withFragment($fragment): UriInterface 105 | { 106 | $uri = $this->uri->withFragment($fragment); 107 | return new static($uri); 108 | } 109 | 110 | /** 111 | * {@inheritdoc} 112 | */ 113 | public function withHost($host): UriInterface 114 | { 115 | $uri = $this->uri->withHost($host); 116 | return new static($uri); 117 | } 118 | 119 | /** 120 | * {@inheritdoc} 121 | */ 122 | public function withPath($path): UriInterface 123 | { 124 | $uri = $this->uri->withPath($path); 125 | return new static($uri); 126 | } 127 | 128 | /** 129 | * {@inheritdoc} 130 | */ 131 | public function withPort($port): UriInterface 132 | { 133 | $uri = $this->uri->withPort($port); 134 | return new static($uri); 135 | } 136 | 137 | /** 138 | * {@inheritdoc} 139 | */ 140 | public function withQuery($query): UriInterface 141 | { 142 | $uri = $this->uri->withQuery($query); 143 | return new static($uri); 144 | } 145 | 146 | /** 147 | * {@inheritdoc} 148 | */ 149 | public function withScheme($scheme): UriInterface 150 | { 151 | $uri = $this->uri->withScheme($scheme); 152 | return new static($uri); 153 | } 154 | 155 | /** 156 | * {@inheritdoc} 157 | */ 158 | public function withUserInfo($user, $password = null): UriInterface 159 | { 160 | $uri = $this->uri->withUserInfo($user, $password); 161 | return new static($uri); 162 | } 163 | 164 | /** 165 | * {@inheritdoc} 166 | */ 167 | public function __toString(): string 168 | { 169 | return $this->uri->__toString(); 170 | } 171 | 172 | /** 173 | * Return the fully qualified base URL. 174 | * 175 | * Note that this method never includes a trailing slash 176 | * 177 | * This method is not part of PSR-7. 178 | * 179 | * @return string 180 | */ 181 | public function getBaseUrl(): string 182 | { 183 | $scheme = $this->uri->getScheme(); 184 | $authority = $this->uri->getAuthority(); 185 | return ($scheme !== '' ? $scheme . ':' : '') . ($authority !== '' ? '//' . $authority : ''); 186 | } 187 | } 188 | -------------------------------------------------------------------------------- /src/Response.php: -------------------------------------------------------------------------------- 1 | response = $response; 59 | $this->streamFactory = $streamFactory; 60 | } 61 | 62 | /** 63 | * Disable magic setter to ensure immutability 64 | * @param mixed $name 65 | * @param mixed $value 66 | * @return void 67 | */ 68 | public function __set($name, $value) 69 | { 70 | } 71 | 72 | /** 73 | * {@inheritdoc} 74 | */ 75 | public function getBody(): StreamInterface 76 | { 77 | return $this->response->getBody(); 78 | } 79 | 80 | /** 81 | * {@inheritdoc} 82 | */ 83 | public function getHeader($name): array 84 | { 85 | return $this->response->getHeader($name); 86 | } 87 | 88 | /** 89 | * {@inheritdoc} 90 | */ 91 | public function getHeaderLine($name): string 92 | { 93 | return $this->response->getHeaderLine($name); 94 | } 95 | 96 | /** 97 | * {@inheritdoc} 98 | */ 99 | public function getHeaders(): array 100 | { 101 | return $this->response->getHeaders(); 102 | } 103 | 104 | /** 105 | * {@inheritdoc} 106 | */ 107 | public function getProtocolVersion(): string 108 | { 109 | return $this->response->getProtocolVersion(); 110 | } 111 | 112 | /** 113 | * {@inheritdoc} 114 | */ 115 | public function getReasonPhrase(): string 116 | { 117 | return $this->response->getReasonPhrase(); 118 | } 119 | 120 | /** 121 | * {@inheritdoc} 122 | */ 123 | public function getStatusCode(): int 124 | { 125 | return $this->response->getStatusCode(); 126 | } 127 | 128 | /** 129 | * {@inheritdoc} 130 | */ 131 | public function hasHeader($name): bool 132 | { 133 | return $this->response->hasHeader($name); 134 | } 135 | 136 | /** 137 | * {@inheritdoc} 138 | */ 139 | public function withAddedHeader($name, $value): MessageInterface 140 | { 141 | $response = $this->response->withAddedHeader($name, $value); 142 | return new static($response, $this->streamFactory); 143 | } 144 | 145 | /** 146 | * {@inheritdoc} 147 | */ 148 | public function withBody(StreamInterface $body): MessageInterface 149 | { 150 | $response = $this->response->withBody($body); 151 | return new static($response, $this->streamFactory); 152 | } 153 | 154 | /** 155 | * {@inheritdoc} 156 | */ 157 | public function withHeader($name, $value): MessageInterface 158 | { 159 | $response = $this->response->withHeader($name, $value); 160 | return new static($response, $this->streamFactory); 161 | } 162 | 163 | /** 164 | * {@inheritdoc} 165 | */ 166 | public function withoutHeader($name): MessageInterface 167 | { 168 | $response = $this->response->withoutHeader($name); 169 | return new static($response, $this->streamFactory); 170 | } 171 | 172 | /** 173 | * {@inheritdoc} 174 | */ 175 | public function withProtocolVersion($version): MessageInterface 176 | { 177 | $response = $this->response->withProtocolVersion($version); 178 | return new static($response, $this->streamFactory); 179 | } 180 | 181 | /** 182 | * {@inheritdoc} 183 | */ 184 | public function withStatus($code, $reasonPhrase = ''): ResponseInterface 185 | { 186 | $response = $this->response->withStatus($code, $reasonPhrase); 187 | return new static($response, $this->streamFactory); 188 | } 189 | 190 | /** 191 | * Write JSON to Response Body. 192 | * 193 | * Note: This method is not part of the PSR-7 standard. 194 | * 195 | * This method prepares the response object to return an HTTP Json 196 | * response to the client. 197 | * 198 | * @param mixed $data The data 199 | * @param int|null $status The HTTP status code 200 | * @param int $options Json encoding options 201 | * @param int $depth Json encoding max depth 202 | * @return static 203 | */ 204 | public function withJson($data, ?int $status = null, int $options = 0, int $depth = 512): ResponseInterface 205 | { 206 | $json = (string) json_encode($data, $options, $depth > 0 ? $depth : 512); 207 | 208 | if (json_last_error() !== JSON_ERROR_NONE) { 209 | throw new RuntimeException(json_last_error_msg(), json_last_error()); 210 | } 211 | 212 | $response = $this->response 213 | ->withHeader('Content-Type', 'application/json') 214 | ->withBody($this->streamFactory->createStream($json)); 215 | 216 | if ($status !== null) { 217 | $response = $response->withStatus($status); 218 | } 219 | 220 | return new static($response, $this->streamFactory); 221 | } 222 | 223 | /** 224 | * Redirect to specified location 225 | * 226 | * Note: This method is not part of the PSR-7 standard. 227 | * 228 | * This method prepares the response object to return an HTTP Redirect 229 | * response to the client. 230 | * 231 | * @param string $url The redirect destination. 232 | * @param int|null $status The redirect HTTP status code. 233 | * @return static 234 | */ 235 | public function withRedirect(string $url, ?int $status = null): ResponseInterface 236 | { 237 | $response = $this->response->withHeader('Location', $url); 238 | 239 | if ($status === null) { 240 | $status = 302; 241 | } 242 | $response = $response->withStatus($status); 243 | 244 | return new static($response, $this->streamFactory); 245 | } 246 | 247 | /** 248 | * Note: This method is not part of the PSR-7 standard. 249 | * 250 | * This method will trigger the client to download the specified file 251 | * It will append the `Content-Disposition` header to the response object 252 | * 253 | * @param string|resource|StreamInterface $file 254 | * @param string|null $name 255 | * @param bool|string $contentType 256 | * 257 | * @return static 258 | */ 259 | public function withFileDownload($file, ?string $name = null, $contentType = true): ResponseInterface 260 | { 261 | $disposition = 'attachment'; 262 | $fileName = $name; 263 | 264 | if (is_string($file) && $name === null) { 265 | $fileName = basename($file); 266 | } 267 | 268 | if ($name === null && (is_resource($file) || $file instanceof StreamInterface)) { 269 | $metaData = $file instanceof StreamInterface 270 | ? $file->getMetadata() 271 | : stream_get_meta_data($file); 272 | 273 | if (is_array($metaData) && isset($metaData['uri']) && is_string($metaData['uri'])) { 274 | $uri = $metaData['uri']; 275 | if ('php://' !== substr($uri, 0, 6)) { 276 | $fileName = basename($uri); 277 | } 278 | } 279 | } 280 | 281 | if (is_string($fileName) && strlen($fileName)) { 282 | /* 283 | * The regex used below is to ensure that the $fileName contains only 284 | * characters ranging from ASCII 128-255 and ASCII 0-31 and 127 are replaced with an empty string 285 | */ 286 | $disposition .= '; filename="' . preg_replace('/[\x00-\x1F\x7F\"]/', ' ', $fileName) . '"'; 287 | $disposition .= "; filename*=UTF-8''" . rawurlencode($fileName); 288 | } 289 | 290 | return $this 291 | ->withFile($file, $contentType) 292 | ->withHeader('Content-Disposition', $disposition); 293 | } 294 | 295 | /** 296 | * Note: This method is not part of the PSR-7 standard. 297 | * 298 | * This method prepares the response object to return a file response to the 299 | * client without `Content-Disposition` header which defaults to `inline` 300 | * 301 | * You control the behavior of the `Content-Type` header declaration via `$contentType` 302 | * Use a string to override the header to a value of your choice. e.g.: `application/json` 303 | * When set to `true` we attempt to detect the content type using `mime_content_type` 304 | * When set to `false` 305 | * 306 | * @param string|resource|StreamInterface $file 307 | * @param bool|string $contentType 308 | * 309 | * @return static 310 | * 311 | * @throws RuntimeException If the file cannot be opened. 312 | * @throws InvalidArgumentException If the mode is invalid. 313 | */ 314 | public function withFile($file, $contentType = true): ResponseInterface 315 | { 316 | $response = $this->response; 317 | 318 | if (is_resource($file)) { 319 | $response = $response->withBody($this->streamFactory->createStreamFromResource($file)); 320 | } elseif (is_string($file)) { 321 | $response = $response->withBody($this->streamFactory->createStreamFromFile($file)); 322 | } elseif ($file instanceof StreamInterface) { 323 | $response = $response->withBody($file); 324 | } else { 325 | throw new InvalidArgumentException( 326 | 'Parameter 1 of Response::withFile() must be a resource, a string ' . 327 | 'or an instance of Psr\Http\Message\StreamInterface.' 328 | ); 329 | } 330 | 331 | if ($contentType === true) { 332 | $contentType = is_string($file) ? mime_content_type($file) : 'application/octet-stream'; 333 | } 334 | 335 | if (is_string($contentType)) { 336 | $response = $response->withHeader('Content-Type', $contentType); 337 | } 338 | 339 | return new static($response, $this->streamFactory); 340 | } 341 | 342 | /** 343 | * Write data to the response body. 344 | * 345 | * Note: This method is not part of the PSR-7 standard. 346 | * 347 | * Proxies to the underlying stream and writes the provided data to it. 348 | * 349 | * @param string $data 350 | * @return static 351 | */ 352 | public function write(string $data): ResponseInterface 353 | { 354 | $this->response->getBody()->write($data); 355 | return $this; 356 | } 357 | 358 | /** 359 | * Is this response a client error? 360 | * 361 | * Note: This method is not part of the PSR-7 standard. 362 | * 363 | * @return bool 364 | */ 365 | public function isClientError(): bool 366 | { 367 | return $this->response->getStatusCode() >= 400 && $this->response->getStatusCode() < 500; 368 | } 369 | 370 | /** 371 | * Is this response empty? 372 | * 373 | * Note: This method is not part of the PSR-7 standard. 374 | * 375 | * @return bool 376 | */ 377 | public function isEmpty(): bool 378 | { 379 | return in_array($this->response->getStatusCode(), [204, 205, 304]); 380 | } 381 | 382 | /** 383 | * Is this response forbidden? 384 | * 385 | * Note: This method is not part of the PSR-7 standard. 386 | * 387 | * @return bool 388 | * @api 389 | */ 390 | public function isForbidden(): bool 391 | { 392 | return $this->response->getStatusCode() === 403; 393 | } 394 | 395 | /** 396 | * Is this response informational? 397 | * 398 | * Note: This method is not part of the PSR-7 standard. 399 | * 400 | * @return bool 401 | */ 402 | public function isInformational(): bool 403 | { 404 | return $this->response->getStatusCode() >= 100 && $this->response->getStatusCode() < 200; 405 | } 406 | 407 | /** 408 | * Is this response OK? 409 | * 410 | * Note: This method is not part of the PSR-7 standard. 411 | * 412 | * @return bool 413 | */ 414 | public function isOk(): bool 415 | { 416 | return $this->response->getStatusCode() === 200; 417 | } 418 | 419 | /** 420 | * Is this response not Found? 421 | * 422 | * Note: This method is not part of the PSR-7 standard. 423 | * 424 | * @return bool 425 | */ 426 | public function isNotFound(): bool 427 | { 428 | return $this->response->getStatusCode() === 404; 429 | } 430 | 431 | /** 432 | * Is this response a redirect? 433 | * 434 | * Note: This method is not part of the PSR-7 standard. 435 | * 436 | * @return bool 437 | */ 438 | public function isRedirect(): bool 439 | { 440 | return in_array($this->response->getStatusCode(), [301, 302, 303, 307, 308]); 441 | } 442 | 443 | /** 444 | * Is this response a redirection? 445 | * 446 | * Note: This method is not part of the PSR-7 standard. 447 | * 448 | * @return bool 449 | */ 450 | public function isRedirection(): bool 451 | { 452 | return $this->response->getStatusCode() >= 300 && $this->response->getStatusCode() < 400; 453 | } 454 | 455 | /** 456 | * Is this response a server error? 457 | * 458 | * Note: This method is not part of the PSR-7 standard. 459 | * 460 | * @return bool 461 | */ 462 | public function isServerError(): bool 463 | { 464 | return $this->response->getStatusCode() >= 500 && $this->response->getStatusCode() < 600; 465 | } 466 | 467 | /** 468 | * Is this response successful? 469 | * 470 | * Note: This method is not part of the PSR-7 standard. 471 | * 472 | * @return bool 473 | */ 474 | public function isSuccessful(): bool 475 | { 476 | return $this->response->getStatusCode() >= 200 && $this->response->getStatusCode() < 300; 477 | } 478 | 479 | /** 480 | * Convert response to string. 481 | * 482 | * Note: This method is not part of the PSR-7 standard. 483 | * 484 | * @return string 485 | */ 486 | public function __toString(): string 487 | { 488 | $output = sprintf( 489 | 'HTTP/%s %s %s%s', 490 | $this->response->getProtocolVersion(), 491 | $this->response->getStatusCode(), 492 | $this->response->getReasonPhrase(), 493 | self::EOL 494 | ); 495 | 496 | foreach ($this->response->getHeaders() as $name => $values) { 497 | $output .= sprintf('%s: %s', $name, $this->response->getHeaderLine($name)) . self::EOL; 498 | } 499 | 500 | $output .= self::EOL; 501 | $output .= $this->response->getBody(); 502 | 503 | return $output; 504 | } 505 | } 506 | -------------------------------------------------------------------------------- /src/ServerRequest.php: -------------------------------------------------------------------------------- 1 | */ 44 | protected array $bodyParsers; 45 | 46 | /** 47 | * @param ServerRequestInterface $serverRequest 48 | */ 49 | final public function __construct(ServerRequestInterface $serverRequest) 50 | { 51 | $this->serverRequest = $serverRequest; 52 | 53 | $this->registerMediaTypeParser('application/json', function (string $input) { 54 | $result = json_decode($input, true); 55 | 56 | if (!is_array($result)) { 57 | return null; 58 | } 59 | 60 | return $result; 61 | }); 62 | 63 | $xmlParserCallable = function (string $input) { 64 | $backup = self::disableXmlEntityLoader(true); 65 | $backup_errors = libxml_use_internal_errors(true); 66 | $result = simplexml_load_string($input); 67 | 68 | self::disableXmlEntityLoader($backup); 69 | libxml_clear_errors(); 70 | libxml_use_internal_errors($backup_errors); 71 | 72 | if ($result === false) { 73 | return null; 74 | } 75 | 76 | return $result; 77 | }; 78 | 79 | $this->registerMediaTypeParser('application/xml', $xmlParserCallable); 80 | $this->registerMediaTypeParser('text/xml', $xmlParserCallable); 81 | 82 | $this->registerMediaTypeParser('application/x-www-form-urlencoded', function (string $input) { 83 | parse_str($input, $data); 84 | return $data; 85 | }); 86 | } 87 | 88 | /** 89 | * Disable magic setter to ensure immutability 90 | * @param mixed $name 91 | * @param mixed $value 92 | * @return void 93 | */ 94 | public function __set($name, $value) 95 | { 96 | } 97 | 98 | /** 99 | * {@inheritdoc} 100 | */ 101 | public function getAttribute($name, $default = null) 102 | { 103 | return $this->serverRequest->getAttribute($name, $default); 104 | } 105 | 106 | /** 107 | * {@inheritdoc} 108 | */ 109 | public function getAttributes(): array 110 | { 111 | return $this->serverRequest->getAttributes(); 112 | } 113 | 114 | /** 115 | * {@inheritdoc} 116 | */ 117 | public function getBody(): StreamInterface 118 | { 119 | return $this->serverRequest->getBody(); 120 | } 121 | 122 | /** 123 | * {@inheritdoc} 124 | */ 125 | public function getCookieParams(): array 126 | { 127 | return $this->serverRequest->getCookieParams(); 128 | } 129 | 130 | /** 131 | * {@inheritdoc} 132 | */ 133 | public function getHeader($name): array 134 | { 135 | return $this->serverRequest->getHeader($name); 136 | } 137 | 138 | /** 139 | * {@inheritdoc} 140 | */ 141 | public function getHeaderLine($name): string 142 | { 143 | return $this->serverRequest->getHeaderLine($name); 144 | } 145 | 146 | /** 147 | * {@inheritdoc} 148 | */ 149 | public function getHeaders(): array 150 | { 151 | return $this->serverRequest->getHeaders(); 152 | } 153 | 154 | /** 155 | * {@inheritdoc} 156 | */ 157 | public function getMethod(): string 158 | { 159 | return $this->serverRequest->getMethod(); 160 | } 161 | 162 | /** 163 | * {@inheritdoc} 164 | */ 165 | public function getParsedBody() 166 | { 167 | $parsedBody = $this->serverRequest->getParsedBody(); 168 | 169 | if (!empty($parsedBody)) { 170 | return $parsedBody; 171 | } 172 | 173 | $mediaType = $this->getMediaType(); 174 | if ($mediaType === null) { 175 | return $parsedBody; 176 | } 177 | 178 | // Check if this specific media type has a parser registered first 179 | if (!isset($this->bodyParsers[$mediaType])) { 180 | // If not, look for a media type with a structured syntax suffix (RFC 6839) 181 | $parts = explode('+', $mediaType); 182 | if (count($parts) >= 2) { 183 | $mediaType = 'application/' . $parts[count($parts) - 1]; 184 | } 185 | } 186 | 187 | if (isset($this->bodyParsers[$mediaType])) { 188 | $body = (string)$this->getBody(); 189 | $parsed = $this->bodyParsers[$mediaType]($body); 190 | 191 | if (!is_null($parsed) && !is_object($parsed) && !is_array($parsed)) { 192 | throw new RuntimeException( 193 | 'Request body media type parser return value must be an array, an object, or null' 194 | ); 195 | } 196 | 197 | return $parsed; 198 | } 199 | 200 | return null; 201 | } 202 | 203 | /** 204 | * {@inheritdoc} 205 | */ 206 | public function getProtocolVersion(): string 207 | { 208 | return $this->serverRequest->getProtocolVersion(); 209 | } 210 | 211 | /** 212 | * {@inheritdoc} 213 | */ 214 | public function getQueryParams(): array 215 | { 216 | $queryParams = $this->serverRequest->getQueryParams(); 217 | 218 | if (!empty($queryParams)) { 219 | return $queryParams; 220 | } 221 | 222 | $parsedQueryParams = []; 223 | parse_str($this->serverRequest->getUri()->getQuery(), $parsedQueryParams); 224 | 225 | return $parsedQueryParams; 226 | } 227 | 228 | /** 229 | * {@inheritdoc} 230 | */ 231 | public function getRequestTarget(): string 232 | { 233 | return $this->serverRequest->getRequestTarget(); 234 | } 235 | 236 | /** 237 | * {@inheritdoc} 238 | */ 239 | public function getServerParams(): array 240 | { 241 | return $this->serverRequest->getServerParams(); 242 | } 243 | 244 | /** 245 | * {@inheritdoc} 246 | */ 247 | public function getUploadedFiles(): array 248 | { 249 | return $this->serverRequest->getUploadedFiles(); 250 | } 251 | 252 | /** 253 | * {@inheritdoc} 254 | */ 255 | public function getUri(): UriInterface 256 | { 257 | return $this->serverRequest->getUri(); 258 | } 259 | 260 | /** 261 | * {@inheritdoc} 262 | */ 263 | public function hasHeader($name): bool 264 | { 265 | return $this->serverRequest->hasHeader($name); 266 | } 267 | 268 | /** 269 | * {@inheritdoc} 270 | */ 271 | public function withAddedHeader($name, $value): MessageInterface 272 | { 273 | $serverRequest = $this->serverRequest->withAddedHeader($name, $value); 274 | return new static($serverRequest); 275 | } 276 | 277 | /** 278 | * {@inheritdoc} 279 | */ 280 | public function withAttribute($name, $value): ServerRequestInterface 281 | { 282 | $serverRequest = $this->serverRequest->withAttribute($name, $value); 283 | return new static($serverRequest); 284 | } 285 | 286 | /** 287 | * Create a new instance with the specified derived request attributes. 288 | * 289 | * Note: This method is not part of the PSR-7 standard. 290 | * 291 | * This method allows setting all new derived request attributes as 292 | * described in getAttributes(). 293 | * 294 | * This method MUST be implemented in such a way as to retain the 295 | * immutability of the message, and MUST return a new instance that has the 296 | * updated attributes. 297 | * 298 | * @param array $attributes New attributes 299 | * @return static 300 | */ 301 | public function withAttributes(array $attributes): ServerRequestInterface 302 | { 303 | $serverRequest = $this->serverRequest; 304 | 305 | foreach ($attributes as $attribute => $value) { 306 | $serverRequest = $serverRequest->withAttribute($attribute, $value); 307 | } 308 | 309 | return new static($serverRequest); 310 | } 311 | 312 | /** 313 | * {@inheritdoc} 314 | */ 315 | public function withoutAttribute($name): ServerRequestInterface 316 | { 317 | $serverRequest = $this->serverRequest->withoutAttribute($name); 318 | return new static($serverRequest); 319 | } 320 | 321 | /** 322 | * {@inheritdoc} 323 | */ 324 | public function withBody(StreamInterface $body): MessageInterface 325 | { 326 | $serverRequest = $this->serverRequest->withBody($body); 327 | return new static($serverRequest); 328 | } 329 | 330 | /** 331 | * {@inheritdoc} 332 | */ 333 | public function withCookieParams(array $cookies): ServerRequestInterface 334 | { 335 | $serverRequest = $this->serverRequest->withCookieParams($cookies); 336 | return new static($serverRequest); 337 | } 338 | 339 | /** 340 | * {@inheritdoc} 341 | */ 342 | public function withHeader($name, $value): MessageInterface 343 | { 344 | $serverRequest = $this->serverRequest->withHeader($name, $value); 345 | return new static($serverRequest); 346 | } 347 | 348 | /** 349 | * {@inheritdoc} 350 | */ 351 | public function withoutHeader($name): MessageInterface 352 | { 353 | $serverRequest = $this->serverRequest->withoutHeader($name); 354 | return new static($serverRequest); 355 | } 356 | 357 | /** 358 | * {@inheritdoc} 359 | */ 360 | public function withMethod($method): RequestInterface 361 | { 362 | $serverRequest = $this->serverRequest->withMethod($method); 363 | return new static($serverRequest); 364 | } 365 | 366 | /** 367 | * {@inheritdoc} 368 | */ 369 | public function withParsedBody($data): ServerRequestInterface 370 | { 371 | $serverRequest = $this->serverRequest->withParsedBody($data); 372 | return new static($serverRequest); 373 | } 374 | 375 | /** 376 | * {@inheritdoc} 377 | */ 378 | public function withProtocolVersion($version): MessageInterface 379 | { 380 | $serverRequest = $this->serverRequest->withProtocolVersion($version); 381 | return new static($serverRequest); 382 | } 383 | 384 | /** 385 | * {@inheritdoc} 386 | */ 387 | public function withQueryParams(array $query): ServerRequestInterface 388 | { 389 | $serverRequest = $this->serverRequest->withQueryParams($query); 390 | return new static($serverRequest); 391 | } 392 | 393 | /** 394 | * {@inheritdoc} 395 | */ 396 | public function withRequestTarget($requestTarget): RequestInterface 397 | { 398 | $serverRequest = $this->serverRequest->withRequestTarget($requestTarget); 399 | return new static($serverRequest); 400 | } 401 | 402 | /** 403 | * {@inheritdoc} 404 | */ 405 | public function withUploadedFiles(array $uploadedFiles): ServerRequestInterface 406 | { 407 | $serverRequest = $this->serverRequest->withUploadedFiles($uploadedFiles); 408 | return new static($serverRequest); 409 | } 410 | 411 | /** 412 | * {@inheritdoc} 413 | */ 414 | public function withUri(UriInterface $uri, $preserveHost = false): RequestInterface 415 | { 416 | $serverRequest = $this->serverRequest->withUri($uri, $preserveHost); 417 | return new static($serverRequest); 418 | } 419 | 420 | /** 421 | * Get serverRequest content character set, if known. 422 | * 423 | * Note: This method is not part of the PSR-7 standard. 424 | * 425 | * @return string|null 426 | */ 427 | public function getContentCharset(): ?string 428 | { 429 | $mediaTypeParams = $this->getMediaTypeParams(); 430 | 431 | if (isset($mediaTypeParams['charset'])) { 432 | return $mediaTypeParams['charset']; 433 | } 434 | 435 | return null; 436 | } 437 | 438 | /** 439 | * Get serverRequest content type. 440 | * 441 | * Note: This method is not part of the PSR-7 standard. 442 | * 443 | * @return string|null The serverRequest content type, if known 444 | */ 445 | public function getContentType(): ?string 446 | { 447 | $result = $this->serverRequest->getHeader('Content-Type'); 448 | return $result ? $result[0] : null; 449 | } 450 | 451 | /** 452 | * Get serverRequest content length, if known. 453 | * 454 | * Note: This method is not part of the PSR-7 standard. 455 | * 456 | * @return int|null 457 | */ 458 | public function getContentLength(): ?int 459 | { 460 | $result = $this->serverRequest->getHeader('Content-Length'); 461 | return $result ? (int) $result[0] : null; 462 | } 463 | 464 | /** 465 | * Fetch cookie value from cookies sent by the client to the server. 466 | * 467 | * Note: This method is not part of the PSR-7 standard. 468 | * 469 | * @param string $key The attribute name. 470 | * @param mixed $default Default value to return if the attribute does not exist. 471 | * 472 | * @return mixed 473 | */ 474 | public function getCookieParam(string $key, $default = null) 475 | { 476 | $cookies = $this->serverRequest->getCookieParams(); 477 | $result = $default; 478 | 479 | if (isset($cookies[$key])) { 480 | $result = $cookies[$key]; 481 | } 482 | 483 | return $result; 484 | } 485 | 486 | /** 487 | * Get serverRequest media type, if known. 488 | * 489 | * Note: This method is not part of the PSR-7 standard. 490 | * 491 | * @return string|null The serverRequest media type, minus content-type params 492 | */ 493 | public function getMediaType(): ?string 494 | { 495 | $contentType = $this->getContentType(); 496 | 497 | if ($contentType) { 498 | $contentTypeParts = preg_split('/\s*[;,]\s*/', $contentType); 499 | if ($contentTypeParts === false) { 500 | return null; 501 | } 502 | return strtolower($contentTypeParts[0]); 503 | } 504 | 505 | return null; 506 | } 507 | 508 | /** 509 | * Get serverRequest media type params, if known. 510 | * 511 | * Note: This method is not part of the PSR-7 standard. 512 | * 513 | * @return string[] 514 | */ 515 | public function getMediaTypeParams(): array 516 | { 517 | $contentType = $this->getContentType(); 518 | $contentTypeParams = []; 519 | 520 | if ($contentType) { 521 | $contentTypeParts = preg_split('/\s*[;,]\s*/', $contentType); 522 | if ($contentTypeParts !== false) { 523 | $contentTypePartsLength = count($contentTypeParts); 524 | for ($i = 1; $i < $contentTypePartsLength; $i++) { 525 | $paramParts = explode('=', $contentTypeParts[$i]); 526 | /** @var string[] $paramParts */ 527 | $contentTypeParams[strtolower($paramParts[0])] = $paramParts[1]; 528 | } 529 | } 530 | } 531 | 532 | return $contentTypeParams; 533 | } 534 | 535 | /** 536 | * Fetch serverRequest parameter value from body or query string (in that order). 537 | * 538 | * Note: This method is not part of the PSR-7 standard. 539 | * 540 | * @param string $key The parameter key. 541 | * @param mixed $default The default value. 542 | * 543 | * @return mixed The parameter value. 544 | */ 545 | public function getParam(string $key, $default = null) 546 | { 547 | $postParams = $this->getParsedBody(); 548 | $getParams = $this->getQueryParams(); 549 | $result = $default; 550 | 551 | if (is_array($postParams) && isset($postParams[$key])) { 552 | $result = $postParams[$key]; 553 | } elseif (is_object($postParams) && property_exists($postParams, $key)) { 554 | $result = $postParams->$key; 555 | } elseif (isset($getParams[$key])) { 556 | $result = $getParams[$key]; 557 | } 558 | 559 | return $result; 560 | } 561 | 562 | /** 563 | * Fetch associative array of body and query string parameters. 564 | * 565 | * Note: This method is not part of the PSR-7 standard. 566 | * 567 | * @return array 568 | */ 569 | public function getParams(): array 570 | { 571 | $params = $this->getQueryParams(); 572 | $postParams = $this->getParsedBody(); 573 | 574 | if ($postParams) { 575 | $params = array_merge($params, (array)$postParams); 576 | } 577 | 578 | return $params; 579 | } 580 | 581 | /** 582 | * Fetch parameter value from serverRequest body. 583 | * 584 | * Note: This method is not part of the PSR-7 standard. 585 | * 586 | * @param string $key 587 | * @param mixed $default 588 | * 589 | * @return mixed 590 | */ 591 | public function getParsedBodyParam(string $key, $default = null) 592 | { 593 | $postParams = $this->getParsedBody(); 594 | $result = $default; 595 | 596 | if (is_array($postParams) && isset($postParams[$key])) { 597 | $result = $postParams[$key]; 598 | } elseif (is_object($postParams) && property_exists($postParams, $key)) { 599 | $result = $postParams->$key; 600 | } 601 | 602 | return $result; 603 | } 604 | 605 | /** 606 | * Fetch parameter value from query string. 607 | * 608 | * Note: This method is not part of the PSR-7 standard. 609 | * 610 | * @param string $key 611 | * @param mixed $default 612 | * 613 | * @return mixed 614 | */ 615 | public function getQueryParam(string $key, $default = null) 616 | { 617 | $getParams = $this->getQueryParams(); 618 | $result = $default; 619 | 620 | if (isset($getParams[$key])) { 621 | $result = $getParams[$key]; 622 | } 623 | 624 | return $result; 625 | } 626 | 627 | /** 628 | * Retrieve a server parameter. 629 | * 630 | * Note: This method is not part of the PSR-7 standard. 631 | * 632 | * @param string $key 633 | * @param mixed $default 634 | * @return mixed 635 | */ 636 | public function getServerParam(string $key, $default = null) 637 | { 638 | $serverParams = $this->serverRequest->getServerParams(); 639 | return $serverParams[$key] ?? $default; 640 | } 641 | 642 | /** 643 | * Register media type parser. 644 | * 645 | * Note: This method is not part of the PSR-7 standard. 646 | * 647 | * @param string $mediaType A HTTP media type (excluding content-type params). 648 | * @param callable $callable A callable that returns parsed contents for media type. 649 | * @return static 650 | */ 651 | public function registerMediaTypeParser(string $mediaType, callable $callable): ServerRequestInterface 652 | { 653 | if ($callable instanceof Closure) { 654 | $callable = $callable->bindTo($this); 655 | } 656 | 657 | $this->bodyParsers[$mediaType] = $callable; 658 | 659 | return $this; 660 | } 661 | 662 | /** 663 | * Is this a DELETE serverRequest? 664 | * 665 | * Note: This method is not part of the PSR-7 standard. 666 | * 667 | * @return bool 668 | */ 669 | public function isDelete(): bool 670 | { 671 | return $this->isMethod('DELETE'); 672 | } 673 | 674 | /** 675 | * Is this a GET serverRequest? 676 | * 677 | * Note: This method is not part of the PSR-7 standard. 678 | * 679 | * @return bool 680 | */ 681 | public function isGet(): bool 682 | { 683 | return $this->isMethod('GET'); 684 | } 685 | 686 | /** 687 | * Is this a HEAD serverRequest? 688 | * 689 | * Note: This method is not part of the PSR-7 standard. 690 | * 691 | * @return bool 692 | */ 693 | public function isHead(): bool 694 | { 695 | return $this->isMethod('HEAD'); 696 | } 697 | 698 | /** 699 | * Does this serverRequest use a given method? 700 | * 701 | * Note: This method is not part of the PSR-7 standard. 702 | * 703 | * @param string $method HTTP method 704 | * @return bool 705 | */ 706 | public function isMethod(string $method): bool 707 | { 708 | return $this->serverRequest->getMethod() === $method; 709 | } 710 | 711 | /** 712 | * Is this a OPTIONS serverRequest? 713 | * 714 | * Note: This method is not part of the PSR-7 standard. 715 | * 716 | * @return bool 717 | */ 718 | public function isOptions(): bool 719 | { 720 | return $this->isMethod('OPTIONS'); 721 | } 722 | 723 | /** 724 | * Is this a PATCH serverRequest? 725 | * 726 | * Note: This method is not part of the PSR-7 standard. 727 | * 728 | * @return bool 729 | */ 730 | public function isPatch(): bool 731 | { 732 | return $this->isMethod('PATCH'); 733 | } 734 | 735 | /** 736 | * Is this a POST serverRequest? 737 | * 738 | * Note: This method is not part of the PSR-7 standard. 739 | * 740 | * @return bool 741 | */ 742 | public function isPost(): bool 743 | { 744 | return $this->isMethod('POST'); 745 | } 746 | 747 | /** 748 | * Is this a PUT serverRequest? 749 | * 750 | * Note: This method is not part of the PSR-7 standard. 751 | * 752 | * @return bool 753 | */ 754 | public function isPut(): bool 755 | { 756 | return $this->isMethod('PUT'); 757 | } 758 | 759 | /** 760 | * Is this an XHR serverRequest? 761 | * 762 | * Note: This method is not part of the PSR-7 standard. 763 | * 764 | * @return bool 765 | */ 766 | public function isXhr(): bool 767 | { 768 | return $this->serverRequest->getHeaderLine('X-Requested-With') === 'XMLHttpRequest'; 769 | } 770 | 771 | private static function disableXmlEntityLoader(bool $disable): bool 772 | { 773 | if (LIBXML_VERSION >= 20900) { 774 | // libxml >= 2.9.0 disables entity loading by default, so it is 775 | // safe to skip the real call (deprecated in PHP 8). 776 | return true; 777 | } 778 | 779 | // @codeCoverageIgnoreStart 780 | return libxml_disable_entity_loader($disable); 781 | // @codeCoverageIgnoreEnd 782 | } 783 | } 784 | --------------------------------------------------------------------------------