Factory
2 months ago
Interfaces
2 months ago
Cookies.php
2 months ago
Environment.php
2 months ago
Header.php
2 months ago
Headers.php
2 months ago
Message.php
2 months ago
NonBufferedBody.php
2 months ago
Request.php
2 months ago
Response.php
2 months ago
Stream.php
2 months ago
UploadedFile.php
2 months ago
Uri.php
2 months ago
Request.php
385 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Slim Framework (https://slimframework.com) |
| 5 | * |
| 6 | * @license https://github.com/slimphp/Slim-Psr7/blob/master/LICENSE.md (MIT License) |
| 7 | */ |
| 8 | |
| 9 | declare(strict_types=1); |
| 10 | |
| 11 | namespace Slim\Psr7; |
| 12 | |
| 13 | use InvalidArgumentException; |
| 14 | use AmeliaVendor\Psr\Http\Message\ServerRequestInterface; |
| 15 | use AmeliaVendor\Psr\Http\Message\StreamInterface; |
| 16 | use AmeliaVendor\Psr\Http\Message\UploadedFileInterface; |
| 17 | use AmeliaVendor\Psr\Http\Message\UriInterface; |
| 18 | use Slim\Psr7\Interfaces\HeadersInterface; |
| 19 | |
| 20 | use function get_class; |
| 21 | use function gettype; |
| 22 | use function is_array; |
| 23 | use function is_null; |
| 24 | use function is_object; |
| 25 | use function is_string; |
| 26 | use function ltrim; |
| 27 | use function parse_str; |
| 28 | use function preg_match; |
| 29 | use function sprintf; |
| 30 | use function str_replace; |
| 31 | |
| 32 | class Request extends Message implements ServerRequestInterface |
| 33 | { |
| 34 | /** |
| 35 | * @var string |
| 36 | */ |
| 37 | protected $method; |
| 38 | |
| 39 | /** |
| 40 | * @var UriInterface |
| 41 | */ |
| 42 | protected $uri; |
| 43 | |
| 44 | /** |
| 45 | * @var string |
| 46 | */ |
| 47 | protected $requestTarget; |
| 48 | |
| 49 | /** |
| 50 | * @var ?array |
| 51 | */ |
| 52 | protected $queryParams; |
| 53 | |
| 54 | protected array $cookies; |
| 55 | |
| 56 | protected array $serverParams; |
| 57 | |
| 58 | protected array $attributes; |
| 59 | |
| 60 | /** |
| 61 | * @var null|array|object |
| 62 | */ |
| 63 | protected $parsedBody; |
| 64 | |
| 65 | /** |
| 66 | * @var UploadedFileInterface[] |
| 67 | */ |
| 68 | protected array $uploadedFiles; |
| 69 | |
| 70 | /** |
| 71 | * @param string $method The request method |
| 72 | * @param UriInterface $uri The request URI object |
| 73 | * @param HeadersInterface $headers The request headers collection |
| 74 | * @param array $cookies The request cookies collection |
| 75 | * @param array $serverParams The server environment variables |
| 76 | * @param StreamInterface $body The request body object |
| 77 | * @param array $uploadedFiles The request uploadedFiles collection |
| 78 | * @throws InvalidArgumentException on invalid HTTP method |
| 79 | */ |
| 80 | public function __construct( |
| 81 | $method, |
| 82 | UriInterface $uri, |
| 83 | HeadersInterface $headers, |
| 84 | array $cookies, |
| 85 | array $serverParams, |
| 86 | StreamInterface $body, |
| 87 | array $uploadedFiles = [] |
| 88 | ) { |
| 89 | $this->method = $this->filterMethod($method); |
| 90 | $this->uri = $uri; |
| 91 | $this->headers = $headers; |
| 92 | $this->cookies = $cookies; |
| 93 | $this->serverParams = $serverParams; |
| 94 | $this->attributes = []; |
| 95 | $this->body = $body; |
| 96 | $this->uploadedFiles = $uploadedFiles; |
| 97 | |
| 98 | if (isset($serverParams['SERVER_PROTOCOL'])) { |
| 99 | $this->protocolVersion = str_replace('HTTP/', '', $serverParams['SERVER_PROTOCOL']); |
| 100 | } |
| 101 | |
| 102 | if (!$this->headers->hasHeader('Host') || $this->uri->getHost() !== '') { |
| 103 | $this->headers->setHeader('Host', $this->uri->getHost()); |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * This method is applied to the cloned object after PHP performs an initial shallow-copy. |
| 109 | * This method completes a deep-copy by creating new objects for the cloned object's internal reference pointers. |
| 110 | */ |
| 111 | public function __clone() |
| 112 | { |
| 113 | $this->headers = clone $this->headers; |
| 114 | $this->body = clone $this->body; |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * {@inheritdoc} |
| 119 | */ |
| 120 | public function getMethod(): string |
| 121 | { |
| 122 | return $this->method; |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * {@inheritdoc} |
| 127 | * @return static |
| 128 | */ |
| 129 | public function withMethod($method) |
| 130 | { |
| 131 | $method = $this->filterMethod($method); |
| 132 | $clone = clone $this; |
| 133 | $clone->method = $method; |
| 134 | |
| 135 | return $clone; |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * Validate the HTTP method |
| 140 | * |
| 141 | * @param string $method |
| 142 | * |
| 143 | * @return string |
| 144 | * |
| 145 | * @throws InvalidArgumentException on invalid HTTP method. |
| 146 | */ |
| 147 | protected function filterMethod($method): string |
| 148 | { |
| 149 | /** @var mixed $method */ |
| 150 | if (!is_string($method)) { |
| 151 | throw new InvalidArgumentException(sprintf( |
| 152 | 'Unsupported HTTP method; must be a string, received %s', |
| 153 | (is_object($method) ? get_class($method) : gettype($method)) |
| 154 | )); |
| 155 | } |
| 156 | |
| 157 | if (preg_match("/^[!#$%&'*+.^_`|~0-9a-z-]+$/i", $method) !== 1) { |
| 158 | throw new InvalidArgumentException(sprintf( |
| 159 | 'Unsupported HTTP method "%s" provided', |
| 160 | $method |
| 161 | )); |
| 162 | } |
| 163 | |
| 164 | return $method; |
| 165 | } |
| 166 | |
| 167 | /** |
| 168 | * {@inheritdoc} |
| 169 | */ |
| 170 | public function getRequestTarget(): string |
| 171 | { |
| 172 | if ($this->requestTarget) { |
| 173 | return $this->requestTarget; |
| 174 | } |
| 175 | |
| 176 | if ($this->uri === null) { |
| 177 | return '/'; |
| 178 | } |
| 179 | |
| 180 | $path = $this->uri->getPath(); |
| 181 | $path = '/' . ltrim($path, '/'); |
| 182 | |
| 183 | $query = $this->uri->getQuery(); |
| 184 | if ($query) { |
| 185 | $path .= '?' . $query; |
| 186 | } |
| 187 | |
| 188 | return $path; |
| 189 | } |
| 190 | |
| 191 | /** |
| 192 | * {@inheritdoc} |
| 193 | * @return static |
| 194 | */ |
| 195 | public function withRequestTarget($requestTarget) |
| 196 | { |
| 197 | if (!is_string($requestTarget) || preg_match('#\s#', $requestTarget)) { |
| 198 | throw new InvalidArgumentException( |
| 199 | 'Invalid request target provided; must be a string and cannot contain whitespace' |
| 200 | ); |
| 201 | } |
| 202 | |
| 203 | $clone = clone $this; |
| 204 | $clone->requestTarget = $requestTarget; |
| 205 | |
| 206 | return $clone; |
| 207 | } |
| 208 | |
| 209 | /** |
| 210 | * {@inheritdoc} |
| 211 | */ |
| 212 | public function getUri(): UriInterface |
| 213 | { |
| 214 | return $this->uri; |
| 215 | } |
| 216 | |
| 217 | /** |
| 218 | * {@inheritdoc} |
| 219 | * @return static |
| 220 | */ |
| 221 | public function withUri(UriInterface $uri, $preserveHost = false) |
| 222 | { |
| 223 | $clone = clone $this; |
| 224 | $clone->uri = $uri; |
| 225 | |
| 226 | if (!$preserveHost && $uri->getHost() !== '') { |
| 227 | $clone->headers->setHeader('Host', $uri->getHost()); |
| 228 | return $clone; |
| 229 | } |
| 230 | |
| 231 | if (($uri->getHost() !== '' && !$this->hasHeader('Host') || $this->getHeaderLine('Host') === '')) { |
| 232 | $clone->headers->setHeader('Host', $uri->getHost()); |
| 233 | return $clone; |
| 234 | } |
| 235 | |
| 236 | return $clone; |
| 237 | } |
| 238 | |
| 239 | /** |
| 240 | * {@inheritdoc} |
| 241 | */ |
| 242 | public function getCookieParams(): array |
| 243 | { |
| 244 | return $this->cookies; |
| 245 | } |
| 246 | |
| 247 | /** |
| 248 | * {@inheritdoc} |
| 249 | * @return static |
| 250 | */ |
| 251 | public function withCookieParams(array $cookies) |
| 252 | { |
| 253 | $clone = clone $this; |
| 254 | $clone->cookies = $cookies; |
| 255 | |
| 256 | return $clone; |
| 257 | } |
| 258 | |
| 259 | /** |
| 260 | * {@inheritdoc} |
| 261 | */ |
| 262 | public function getQueryParams(): array |
| 263 | { |
| 264 | if (is_array($this->queryParams)) { |
| 265 | return $this->queryParams; |
| 266 | } |
| 267 | |
| 268 | if ($this->uri === null) { |
| 269 | return []; |
| 270 | } |
| 271 | |
| 272 | parse_str($this->uri->getQuery(), $this->queryParams); // <-- URL decodes data |
| 273 | assert(is_array($this->queryParams)); |
| 274 | |
| 275 | return $this->queryParams; |
| 276 | } |
| 277 | |
| 278 | /** |
| 279 | * {@inheritdoc} |
| 280 | * @return static |
| 281 | */ |
| 282 | public function withQueryParams(array $query) |
| 283 | { |
| 284 | $clone = clone $this; |
| 285 | $clone->queryParams = $query; |
| 286 | |
| 287 | return $clone; |
| 288 | } |
| 289 | |
| 290 | /** |
| 291 | * {@inheritdoc} |
| 292 | */ |
| 293 | public function getUploadedFiles(): array |
| 294 | { |
| 295 | return $this->uploadedFiles; |
| 296 | } |
| 297 | |
| 298 | /** |
| 299 | * {@inheritdoc} |
| 300 | * @return static |
| 301 | */ |
| 302 | public function withUploadedFiles(array $uploadedFiles) |
| 303 | { |
| 304 | $clone = clone $this; |
| 305 | $clone->uploadedFiles = $uploadedFiles; |
| 306 | |
| 307 | return $clone; |
| 308 | } |
| 309 | |
| 310 | /** |
| 311 | * {@inheritdoc} |
| 312 | */ |
| 313 | public function getServerParams(): array |
| 314 | { |
| 315 | return $this->serverParams; |
| 316 | } |
| 317 | |
| 318 | /** |
| 319 | * {@inheritdoc} |
| 320 | */ |
| 321 | public function getAttributes(): array |
| 322 | { |
| 323 | return $this->attributes; |
| 324 | } |
| 325 | |
| 326 | /** |
| 327 | * {@inheritdoc} |
| 328 | * @return mixed |
| 329 | */ |
| 330 | public function getAttribute($name, $default = null) |
| 331 | { |
| 332 | return $this->attributes[$name] ?? $default; |
| 333 | } |
| 334 | |
| 335 | /** |
| 336 | * {@inheritdoc} |
| 337 | * @return static |
| 338 | */ |
| 339 | public function withAttribute($name, $value) |
| 340 | { |
| 341 | $clone = clone $this; |
| 342 | $clone->attributes[$name] = $value; |
| 343 | |
| 344 | return $clone; |
| 345 | } |
| 346 | |
| 347 | /** |
| 348 | * {@inheritdoc} |
| 349 | * @return static |
| 350 | */ |
| 351 | public function withoutAttribute($name) |
| 352 | { |
| 353 | $clone = clone $this; |
| 354 | |
| 355 | unset($clone->attributes[$name]); |
| 356 | |
| 357 | return $clone; |
| 358 | } |
| 359 | |
| 360 | /** |
| 361 | * {@inheritdoc} |
| 362 | */ |
| 363 | public function getParsedBody() |
| 364 | { |
| 365 | return $this->parsedBody; |
| 366 | } |
| 367 | |
| 368 | /** |
| 369 | * {@inheritdoc} |
| 370 | * @return static |
| 371 | */ |
| 372 | public function withParsedBody($data) |
| 373 | { |
| 374 | /** @var mixed $data */ |
| 375 | if (!is_null($data) && !is_object($data) && !is_array($data)) { |
| 376 | throw new InvalidArgumentException('Parsed body value must be an array, an object, or null'); |
| 377 | } |
| 378 | |
| 379 | $clone = clone $this; |
| 380 | $clone->parsedBody = $data; |
| 381 | |
| 382 | return $clone; |
| 383 | } |
| 384 | } |
| 385 |