| 1 |
<?php |
| 2 |
|
| 3 |
declare (strict_types=1); |
| 4 |
namespace WCPOS\Vendor\GuzzleHttp\Psr7; |
| 5 |
|
| 6 |
use InvalidArgumentException; |
| 7 |
use WCPOS\Vendor\Psr\Http\Message\ServerRequestInterface; |
| 8 |
use WCPOS\Vendor\Psr\Http\Message\StreamInterface; |
| 9 |
use WCPOS\Vendor\Psr\Http\Message\UploadedFileInterface; |
| 10 |
use WCPOS\Vendor\Psr\Http\Message\UriInterface; |
| 11 |
/** |
| 12 |
* Server-side HTTP request |
| 13 |
* |
| 14 |
* Extends the Request definition to add methods for accessing incoming data, |
| 15 |
* specifically server parameters, cookies, matched path parameters, query |
| 16 |
* string arguments, body parameters, and upload file information. |
| 17 |
* |
| 18 |
* "Attributes" are discovered via decomposing the request (and usually |
| 19 |
* specifically the URI path), and typically will be injected by the application. |
| 20 |
* |
| 21 |
* Requests are considered immutable; all methods that might change state are |
| 22 |
* implemented such that they retain the internal state of the current |
| 23 |
* message and return a new instance that contains the changed state. |
| 24 |
*/ |
| 25 |
class ServerRequest extends Request implements ServerRequestInterface |
| 26 |
{ |
| 27 |
private array $attributes = []; |
| 28 |
private array $cookieParams = []; |
| 29 |
/** |
| 30 |
* @var array|object|null |
| 31 |
*/ |
| 32 |
private $parsedBody; |
| 33 |
private array $queryParams = []; |
| 34 |
private array $serverParams; |
| 35 |
private array $uploadedFiles = []; |
| 36 |
/** |
| 37 |
* @param string $method HTTP method |
| 38 |
* @param string|UriInterface $uri URI |
| 39 |
* @param (string|string[])[] $headers Request headers |
| 40 |
* @param string|resource|StreamInterface|null $body Request body |
| 41 |
* @param string $version Protocol version |
| 42 |
* @param array $serverParams Typically the $_SERVER superglobal |
| 43 |
*/ |
| 44 |
public function __construct(string $method, $uri, array $headers = [], $body = null, string $version = '1.1', #[\SensitiveParameter] array $serverParams = []) |
| 45 |
{ |
| 46 |
$this->serverParams = $serverParams; |
| 47 |
parent::__construct($method, $uri, $headers, $body, $version); |
| 48 |
} |
| 49 |
/** |
| 50 |
* Return an UploadedFile instance array. |
| 51 |
* |
| 52 |
* @param array $files An array which respect $_FILES structure |
| 53 |
* |
| 54 |
* @throws InvalidArgumentException for unrecognized values |
| 55 |
*/ |
| 56 |
public static function normalizeFiles(array $files) : array |
| 57 |
{ |
| 58 |
return UploadedFileNormalizer::normalize($files); |
| 59 |
} |
| 60 |
/** |
| 61 |
* Return a ServerRequest populated with superglobals: |
| 62 |
* $_GET |
| 63 |
* $_POST |
| 64 |
* $_COOKIE |
| 65 |
* $_FILES |
| 66 |
* $_SERVER |
| 67 |
*/ |
| 68 |
public static function fromGlobals() : ServerRequestInterface |
| 69 |
{ |
| 70 |
return ServerRequestGlobalsFactory::fromArrays($_SERVER, $_GET, $_POST, $_COOKIE, $_FILES, static function () { |
| 71 |
if (!\function_exists('apache_request_headers')) { |
| 72 |
return \false; |
| 73 |
} |
| 74 |
return \apache_request_headers(); |
| 75 |
}); |
| 76 |
} |
| 77 |
/** |
| 78 |
* Get a Uri populated with values from $_SERVER. |
| 79 |
*/ |
| 80 |
public static function getUriFromGlobals() : UriInterface |
| 81 |
{ |
| 82 |
return ServerRequestGlobalsFactory::getUriFromServerParams($_SERVER); |
| 83 |
} |
| 84 |
public function getServerParams() : array |
| 85 |
{ |
| 86 |
return $this->serverParams; |
| 87 |
} |
| 88 |
public function getUploadedFiles() : array |
| 89 |
{ |
| 90 |
return $this->uploadedFiles; |
| 91 |
} |
| 92 |
public function withUploadedFiles(array $uploadedFiles) : ServerRequestInterface |
| 93 |
{ |
| 94 |
$stack = [$uploadedFiles]; |
| 95 |
for ($i = 0; $i < \count($stack); ++$i) { |
| 96 |
foreach ($stack[$i] as $uploadedFile) { |
| 97 |
if ($uploadedFile instanceof UploadedFileInterface) { |
| 98 |
continue; |
| 99 |
} |
| 100 |
if (\is_array($uploadedFile)) { |
| 101 |
$stack[] = $uploadedFile; |
| 102 |
continue; |
| 103 |
} |
| 104 |
throw new InvalidArgumentException(\sprintf('Invalid uploaded file tree; expected UploadedFileInterface instances but %s provided.', \get_debug_type($uploadedFile))); |
| 105 |
} |
| 106 |
} |
| 107 |
$new = clone $this; |
| 108 |
$new->uploadedFiles = $uploadedFiles; |
| 109 |
return $new; |
| 110 |
} |
| 111 |
public function getCookieParams() : array |
| 112 |
{ |
| 113 |
return $this->cookieParams; |
| 114 |
} |
| 115 |
public function withCookieParams(#[\SensitiveParameter] array $cookies) : ServerRequestInterface |
| 116 |
{ |
| 117 |
$new = clone $this; |
| 118 |
$new->cookieParams = $cookies; |
| 119 |
return $new; |
| 120 |
} |
| 121 |
public function getQueryParams() : array |
| 122 |
{ |
| 123 |
return $this->queryParams; |
| 124 |
} |
| 125 |
public function withQueryParams(array $query) : ServerRequestInterface |
| 126 |
{ |
| 127 |
$new = clone $this; |
| 128 |
$new->queryParams = $query; |
| 129 |
return $new; |
| 130 |
} |
| 131 |
/** |
| 132 |
* @return array|object|null |
| 133 |
*/ |
| 134 |
public function getParsedBody() |
| 135 |
{ |
| 136 |
return $this->parsedBody; |
| 137 |
} |
| 138 |
public function withParsedBody($data) : ServerRequestInterface |
| 139 |
{ |
| 140 |
if ($data !== null && !\is_array($data) && !\is_object($data)) { |
| 141 |
throw new InvalidArgumentException('Parsed body must be an array, object, or null.'); |
| 142 |
} |
| 143 |
$new = clone $this; |
| 144 |
$new->parsedBody = $data; |
| 145 |
return $new; |
| 146 |
} |
| 147 |
public function getAttributes() : array |
| 148 |
{ |
| 149 |
return $this->attributes; |
| 150 |
} |
| 151 |
/** |
| 152 |
* @return mixed |
| 153 |
*/ |
| 154 |
public function getAttribute(string $name, $default = null) |
| 155 |
{ |
| 156 |
if (\false === \array_key_exists($name, $this->attributes)) { |
| 157 |
return $default; |
| 158 |
} |
| 159 |
return $this->attributes[$name]; |
| 160 |
} |
| 161 |
public function withAttribute(string $name, $value) : ServerRequestInterface |
| 162 |
{ |
| 163 |
$new = clone $this; |
| 164 |
$new->attributes[$name] = $value; |
| 165 |
return $new; |
| 166 |
} |
| 167 |
public function withoutAttribute(string $name) : ServerRequestInterface |
| 168 |
{ |
| 169 |
if (\false === \array_key_exists($name, $this->attributes)) { |
| 170 |
return $this; |
| 171 |
} |
| 172 |
$new = clone $this; |
| 173 |
unset($new->attributes[$name]); |
| 174 |
return $new; |
| 175 |
} |
| 176 |
} |
| 177 |
|