@@ -27,13 +27,13 @@ |
||
27 | 27 | final class PhpMemoryStream extends Stream |
28 | 28 | { |
29 | 29 | |
30 | - /** |
|
31 | - * Constructor of the class |
|
32 | - * |
|
33 | - * @param string $mode |
|
34 | - */ |
|
35 | - public function __construct(string $mode = 'r+b') |
|
36 | - { |
|
37 | - parent::__construct(fopen('php://memory', $mode)); |
|
38 | - } |
|
30 | + /** |
|
31 | + * Constructor of the class |
|
32 | + * |
|
33 | + * @param string $mode |
|
34 | + */ |
|
35 | + public function __construct(string $mode = 'r+b') |
|
36 | + { |
|
37 | + parent::__construct(fopen('php://memory', $mode)); |
|
38 | + } |
|
39 | 39 | } |
@@ -35,23 +35,23 @@ |
||
35 | 35 | final class TmpfileStream extends Stream |
36 | 36 | { |
37 | 37 | |
38 | - /** |
|
39 | - * Constructor of the class |
|
40 | - * |
|
41 | - * @throws RuntimeException |
|
42 | - */ |
|
43 | - public function __construct() |
|
44 | - { |
|
45 | - $dirname = sys_get_temp_dir(); |
|
46 | - if (!is_writable($dirname)) { |
|
47 | - throw new RuntimeException('Temporary files directory is not writable'); |
|
48 | - } |
|
49 | - |
|
50 | - $resource = tmpfile(); |
|
51 | - if (!is_resource($resource)) { |
|
52 | - throw new RuntimeException('Temporary file cannot be created or opened'); |
|
53 | - } |
|
54 | - |
|
55 | - parent::__construct($resource); |
|
56 | - } |
|
38 | + /** |
|
39 | + * Constructor of the class |
|
40 | + * |
|
41 | + * @throws RuntimeException |
|
42 | + */ |
|
43 | + public function __construct() |
|
44 | + { |
|
45 | + $dirname = sys_get_temp_dir(); |
|
46 | + if (!is_writable($dirname)) { |
|
47 | + throw new RuntimeException('Temporary files directory is not writable'); |
|
48 | + } |
|
49 | + |
|
50 | + $resource = tmpfile(); |
|
51 | + if (!is_resource($resource)) { |
|
52 | + throw new RuntimeException('Temporary file cannot be created or opened'); |
|
53 | + } |
|
54 | + |
|
55 | + parent::__construct($resource); |
|
56 | + } |
|
57 | 57 | } |
@@ -22,52 +22,52 @@ |
||
22 | 22 | interface HeaderInterface extends IteratorAggregate |
23 | 23 | { |
24 | 24 | |
25 | - /** |
|
26 | - * Date format according to RFC-822 |
|
27 | - * |
|
28 | - * @var string |
|
29 | - */ |
|
30 | - public const RFC822_DATE_FORMAT = 'D, d M y H:i:s O'; |
|
25 | + /** |
|
26 | + * Date format according to RFC-822 |
|
27 | + * |
|
28 | + * @var string |
|
29 | + */ |
|
30 | + public const RFC822_DATE_FORMAT = 'D, d M y H:i:s O'; |
|
31 | 31 | |
32 | - /** |
|
33 | - * Regular Expression used for a token validation according to RFC-7230 |
|
34 | - * |
|
35 | - * @var string |
|
36 | - */ |
|
37 | - public const RFC7230_TOKEN_REGEX = '/^[\x21\x23-\x27\x2A\x2B\x2D\x2E\x30-\x39\x41-\x5A\x5E-\x7A\x7C\x7E]+$/'; |
|
32 | + /** |
|
33 | + * Regular Expression used for a token validation according to RFC-7230 |
|
34 | + * |
|
35 | + * @var string |
|
36 | + */ |
|
37 | + public const RFC7230_TOKEN_REGEX = '/^[\x21\x23-\x27\x2A\x2B\x2D\x2E\x30-\x39\x41-\x5A\x5E-\x7A\x7C\x7E]+$/'; |
|
38 | 38 | |
39 | - /** |
|
40 | - * Regular Expression used for a field-value validation according to RFC-7230 |
|
41 | - * |
|
42 | - * @var string |
|
43 | - */ |
|
44 | - public const RFC7230_FIELD_VALUE_REGEX = '/^[\x09\x20-\x7E\x80-\xFF]*$/'; |
|
39 | + /** |
|
40 | + * Regular Expression used for a field-value validation according to RFC-7230 |
|
41 | + * |
|
42 | + * @var string |
|
43 | + */ |
|
44 | + public const RFC7230_FIELD_VALUE_REGEX = '/^[\x09\x20-\x7E\x80-\xFF]*$/'; |
|
45 | 45 | |
46 | - /** |
|
47 | - * Regular Expression used for a quoted-string validation according to RFC-7230 |
|
48 | - * |
|
49 | - * @var string |
|
50 | - */ |
|
51 | - public const RFC7230_QUOTED_STRING_REGEX = '/^(?:[\x5C][\x22]|[\x09\x20\x21\x23-\x5B\x5D-\x7E\x80-\xFF])*$/'; |
|
46 | + /** |
|
47 | + * Regular Expression used for a quoted-string validation according to RFC-7230 |
|
48 | + * |
|
49 | + * @var string |
|
50 | + */ |
|
51 | + public const RFC7230_QUOTED_STRING_REGEX = '/^(?:[\x5C][\x22]|[\x09\x20\x21\x23-\x5B\x5D-\x7E\x80-\xFF])*$/'; |
|
52 | 52 | |
53 | - /** |
|
54 | - * Gets the header field name |
|
55 | - * |
|
56 | - * @return string |
|
57 | - */ |
|
58 | - public function getFieldName(): string; |
|
53 | + /** |
|
54 | + * Gets the header field name |
|
55 | + * |
|
56 | + * @return string |
|
57 | + */ |
|
58 | + public function getFieldName(): string; |
|
59 | 59 | |
60 | - /** |
|
61 | - * Gets the header field value |
|
62 | - * |
|
63 | - * @return string |
|
64 | - */ |
|
65 | - public function getFieldValue(): string; |
|
60 | + /** |
|
61 | + * Gets the header field value |
|
62 | + * |
|
63 | + * @return string |
|
64 | + */ |
|
65 | + public function getFieldValue(): string; |
|
66 | 66 | |
67 | - /** |
|
68 | - * Converts the header to a field |
|
69 | - * |
|
70 | - * @return string |
|
71 | - */ |
|
72 | - public function __toString(): string; |
|
67 | + /** |
|
68 | + * Converts the header to a field |
|
69 | + * |
|
70 | + * @return string |
|
71 | + */ |
|
72 | + public function __toString(): string; |
|
73 | 73 | } |
@@ -28,14 +28,14 @@ |
||
28 | 28 | final class PhpTempStream extends Stream |
29 | 29 | { |
30 | 30 | |
31 | - /** |
|
32 | - * Constructor of the class |
|
33 | - * |
|
34 | - * @param string $mode |
|
35 | - * @param int<0, max> $maxmemory |
|
36 | - */ |
|
37 | - public function __construct(string $mode = 'r+b', int $maxmemory = 2097152) |
|
38 | - { |
|
39 | - parent::__construct(fopen(sprintf('php://temp/maxmemory:%d', $maxmemory), $mode)); |
|
40 | - } |
|
31 | + /** |
|
32 | + * Constructor of the class |
|
33 | + * |
|
34 | + * @param string $mode |
|
35 | + * @param int<0, max> $maxmemory |
|
36 | + */ |
|
37 | + public function __construct(string $mode = 'r+b', int $maxmemory = 2097152) |
|
38 | + { |
|
39 | + parent::__construct(fopen(sprintf('php://temp/maxmemory:%d', $maxmemory), $mode)); |
|
40 | + } |
|
41 | 41 | } |
@@ -32,30 +32,30 @@ |
||
32 | 32 | final class TempFileStream extends Stream |
33 | 33 | { |
34 | 34 | |
35 | - /** |
|
36 | - * Constructor of the class |
|
37 | - * |
|
38 | - * @param string $prefix |
|
39 | - * |
|
40 | - * @throws RuntimeException |
|
41 | - */ |
|
42 | - public function __construct(string $prefix = '') |
|
43 | - { |
|
44 | - $dirname = sys_get_temp_dir(); |
|
45 | - if (!is_writable($dirname)) { |
|
46 | - throw new RuntimeException('Temporary files directory is not writable'); |
|
47 | - } |
|
48 | - |
|
49 | - $filename = tempnam($dirname, $prefix); |
|
50 | - if ($filename === false) { |
|
51 | - throw new RuntimeException('Temporary file name cannot be generated'); |
|
52 | - } |
|
53 | - |
|
54 | - $resource = fopen($filename, 'w+b'); |
|
55 | - if (!is_resource($resource)) { |
|
56 | - throw new RuntimeException('Temporary file cannot be created or opened'); |
|
57 | - } |
|
58 | - |
|
59 | - parent::__construct($resource); |
|
60 | - } |
|
35 | + /** |
|
36 | + * Constructor of the class |
|
37 | + * |
|
38 | + * @param string $prefix |
|
39 | + * |
|
40 | + * @throws RuntimeException |
|
41 | + */ |
|
42 | + public function __construct(string $prefix = '') |
|
43 | + { |
|
44 | + $dirname = sys_get_temp_dir(); |
|
45 | + if (!is_writable($dirname)) { |
|
46 | + throw new RuntimeException('Temporary files directory is not writable'); |
|
47 | + } |
|
48 | + |
|
49 | + $filename = tempnam($dirname, $prefix); |
|
50 | + if ($filename === false) { |
|
51 | + throw new RuntimeException('Temporary file name cannot be generated'); |
|
52 | + } |
|
53 | + |
|
54 | + $resource = fopen($filename, 'w+b'); |
|
55 | + if (!is_resource($resource)) { |
|
56 | + throw new RuntimeException('Temporary file cannot be created or opened'); |
|
57 | + } |
|
58 | + |
|
59 | + parent::__construct($resource); |
|
60 | + } |
|
61 | 61 | } |
@@ -32,51 +32,51 @@ |
||
32 | 32 | final class HtmlResponse extends Response |
33 | 33 | { |
34 | 34 | |
35 | - /** |
|
36 | - * Constructor of the class |
|
37 | - * |
|
38 | - * @param int $statusCode |
|
39 | - * @param mixed $html |
|
40 | - * |
|
41 | - * @throws InvalidArgumentException |
|
42 | - */ |
|
43 | - public function __construct(int $statusCode, $html) |
|
44 | - { |
|
45 | - parent::__construct($statusCode); |
|
35 | + /** |
|
36 | + * Constructor of the class |
|
37 | + * |
|
38 | + * @param int $statusCode |
|
39 | + * @param mixed $html |
|
40 | + * |
|
41 | + * @throws InvalidArgumentException |
|
42 | + */ |
|
43 | + public function __construct(int $statusCode, $html) |
|
44 | + { |
|
45 | + parent::__construct($statusCode); |
|
46 | 46 | |
47 | - $this->setBody($this->createBody($html)); |
|
47 | + $this->setBody($this->createBody($html)); |
|
48 | 48 | |
49 | - $this->setHeader('Content-Type', 'text/html; charset=utf-8'); |
|
50 | - } |
|
49 | + $this->setHeader('Content-Type', 'text/html; charset=utf-8'); |
|
50 | + } |
|
51 | 51 | |
52 | - /** |
|
53 | - * Creates the response body from the given HTML data |
|
54 | - * |
|
55 | - * @param mixed $html |
|
56 | - * |
|
57 | - * @return StreamInterface |
|
58 | - * |
|
59 | - * @throws InvalidArgumentException |
|
60 | - */ |
|
61 | - private function createBody($html): StreamInterface |
|
62 | - { |
|
63 | - if ($html instanceof StreamInterface) { |
|
64 | - return $html; |
|
65 | - } |
|
52 | + /** |
|
53 | + * Creates the response body from the given HTML data |
|
54 | + * |
|
55 | + * @param mixed $html |
|
56 | + * |
|
57 | + * @return StreamInterface |
|
58 | + * |
|
59 | + * @throws InvalidArgumentException |
|
60 | + */ |
|
61 | + private function createBody($html): StreamInterface |
|
62 | + { |
|
63 | + if ($html instanceof StreamInterface) { |
|
64 | + return $html; |
|
65 | + } |
|
66 | 66 | |
67 | - if (is_object($html) && method_exists($html, '__toString')) { |
|
68 | - /** @var string */ |
|
69 | - $html = $html->__toString(); |
|
70 | - } |
|
67 | + if (is_object($html) && method_exists($html, '__toString')) { |
|
68 | + /** @var string */ |
|
69 | + $html = $html->__toString(); |
|
70 | + } |
|
71 | 71 | |
72 | - if (!is_string($html)) { |
|
73 | - throw new InvalidArgumentException('Unable to create HTML response due to unexpected HTML data'); |
|
74 | - } |
|
72 | + if (!is_string($html)) { |
|
73 | + throw new InvalidArgumentException('Unable to create HTML response due to unexpected HTML data'); |
|
74 | + } |
|
75 | 75 | |
76 | - $stream = new PhpTempStream('r+b'); |
|
77 | - $stream->write($html); |
|
78 | - $stream->rewind(); |
|
76 | + $stream = new PhpTempStream('r+b'); |
|
77 | + $stream->write($html); |
|
78 | + $stream->rewind(); |
|
79 | 79 | |
80 | - return $stream; |
|
81 | - } |
|
80 | + return $stream; |
|
81 | + } |
|
82 | 82 | } |
@@ -28,7 +28,7 @@ |
||
28 | 28 | */ |
29 | 29 | function server_request_method(?array $serverParams = null): string |
30 | 30 | { |
31 | - $serverParams ??= $_SERVER; |
|
31 | + $serverParams ??= $_SERVER; |
|
32 | 32 | |
33 | - return $serverParams['REQUEST_METHOD'] ?? RequestMethodInterface::METHOD_GET; |
|
33 | + return $serverParams['REQUEST_METHOD'] ?? RequestMethodInterface::METHOD_GET; |
|
34 | 34 | } |
@@ -358,8 +358,8 @@ |
||
358 | 358 | /** |
359 | 359 | * @psalm-suppress MissingClosureParamType |
360 | 360 | */ |
361 | - array_walk_recursive($files, static function ($file): void { |
|
362 | - if (! ($file instanceof UploadedFileInterface)) { |
|
361 | + array_walk_recursive($files, static function($file): void { |
|
362 | + if (!($file instanceof UploadedFileInterface)) { |
|
363 | 363 | throw new InvalidArgumentException('Invalid uploaded file'); |
364 | 364 | } |
365 | 365 | }); |
@@ -35,354 +35,354 @@ |
||
35 | 35 | class ServerRequest extends Request implements ServerRequestInterface |
36 | 36 | { |
37 | 37 | |
38 | - /** |
|
39 | - * The server parameters |
|
40 | - * |
|
41 | - * @var array<array-key, mixed> |
|
42 | - */ |
|
43 | - private array $serverParams; |
|
44 | - |
|
45 | - /** |
|
46 | - * The request's query parameters |
|
47 | - * |
|
48 | - * @var array<array-key, mixed> |
|
49 | - */ |
|
50 | - private array $queryParams; |
|
51 | - |
|
52 | - /** |
|
53 | - * The request's cookie parameters |
|
54 | - * |
|
55 | - * @var array<array-key, mixed> |
|
56 | - */ |
|
57 | - private array $cookieParams; |
|
58 | - |
|
59 | - /** |
|
60 | - * The request's uploaded files |
|
61 | - * |
|
62 | - * @var array<array-key, mixed> |
|
63 | - */ |
|
64 | - private array $uploadedFiles = []; |
|
65 | - |
|
66 | - /** |
|
67 | - * The request's parsed body |
|
68 | - * |
|
69 | - * @var array<array-key, mixed>|object|null |
|
70 | - */ |
|
71 | - private $parsedBody = null; |
|
72 | - |
|
73 | - /** |
|
74 | - * The request attributes |
|
75 | - * |
|
76 | - * @var array<array-key, mixed> |
|
77 | - */ |
|
78 | - private array $attributes; |
|
79 | - |
|
80 | - /** |
|
81 | - * Constructor of the class |
|
82 | - * |
|
83 | - * @param string|null $protocolVersion |
|
84 | - * @param string|null $method |
|
85 | - * @param mixed $uri |
|
86 | - * @param array<string, string|string[]>|null $headers |
|
87 | - * @param StreamInterface|null $body |
|
88 | - * |
|
89 | - * @param array<array-key, mixed> $serverParams |
|
90 | - * @param array<array-key, mixed> $queryParams |
|
91 | - * @param array<array-key, mixed> $cookieParams |
|
92 | - * @param array<array-key, mixed> $uploadedFiles |
|
93 | - * @param array<array-key, mixed>|object|null $parsedBody |
|
94 | - * @param array<array-key, mixed> $attributes |
|
95 | - * |
|
96 | - * @throws InvalidArgumentException |
|
97 | - * If one of the arguments isn't valid. |
|
98 | - */ |
|
99 | - public function __construct( |
|
100 | - ?string $protocolVersion = null, |
|
101 | - ?string $method = null, |
|
102 | - $uri = null, |
|
103 | - ?array $headers = null, |
|
104 | - ?StreamInterface $body = null, |
|
105 | - array $serverParams = [], |
|
106 | - array $queryParams = [], |
|
107 | - array $cookieParams = [], |
|
108 | - array $uploadedFiles = [], |
|
109 | - $parsedBody = null, |
|
110 | - array $attributes = [] |
|
111 | - ) { |
|
112 | - parent::__construct($method, $uri, $headers, $body); |
|
113 | - |
|
114 | - if (isset($protocolVersion)) { |
|
115 | - $this->setProtocolVersion($protocolVersion); |
|
116 | - } |
|
117 | - |
|
118 | - if (!empty($uploadedFiles)) { |
|
119 | - $this->setUploadedFiles($uploadedFiles); |
|
120 | - } |
|
121 | - |
|
122 | - if (isset($parsedBody)) { |
|
123 | - $this->setParsedBody($parsedBody); |
|
124 | - } |
|
125 | - |
|
126 | - $this->serverParams = $serverParams; |
|
127 | - $this->queryParams = $queryParams; |
|
128 | - $this->cookieParams = $cookieParams; |
|
129 | - $this->attributes = $attributes; |
|
130 | - } |
|
131 | - |
|
132 | - /** |
|
133 | - * Gets the server parameters |
|
134 | - * |
|
135 | - * @return array<array-key, mixed> |
|
136 | - */ |
|
137 | - public function getServerParams(): array |
|
138 | - { |
|
139 | - return $this->serverParams; |
|
140 | - } |
|
141 | - |
|
142 | - /** |
|
143 | - * Gets the request's query parameters |
|
144 | - * |
|
145 | - * @return array<array-key, mixed> |
|
146 | - */ |
|
147 | - public function getQueryParams(): array |
|
148 | - { |
|
149 | - return $this->queryParams; |
|
150 | - } |
|
151 | - |
|
152 | - /** |
|
153 | - * Creates a new instance of the request with the given query parameters |
|
154 | - * |
|
155 | - * @param array<array-key, mixed> $query |
|
156 | - * |
|
157 | - * @return static |
|
158 | - */ |
|
159 | - public function withQueryParams(array $query): ServerRequestInterface |
|
160 | - { |
|
161 | - $clone = clone $this; |
|
162 | - $clone->queryParams = $query; |
|
163 | - |
|
164 | - return $clone; |
|
165 | - } |
|
166 | - |
|
167 | - /** |
|
168 | - * Gets the request's cookie parameters |
|
169 | - * |
|
170 | - * @return array<array-key, mixed> |
|
171 | - */ |
|
172 | - public function getCookieParams(): array |
|
173 | - { |
|
174 | - return $this->cookieParams; |
|
175 | - } |
|
176 | - |
|
177 | - /** |
|
178 | - * Creates a new instance of the request with the given cookie parameters |
|
179 | - * |
|
180 | - * @param array<array-key, mixed> $cookies |
|
181 | - * |
|
182 | - * @return static |
|
183 | - */ |
|
184 | - public function withCookieParams(array $cookies): ServerRequestInterface |
|
185 | - { |
|
186 | - $clone = clone $this; |
|
187 | - $clone->cookieParams = $cookies; |
|
188 | - |
|
189 | - return $clone; |
|
190 | - } |
|
191 | - |
|
192 | - /** |
|
193 | - * Gets the request's uploaded files |
|
194 | - * |
|
195 | - * @return array<array-key, mixed> |
|
196 | - */ |
|
197 | - public function getUploadedFiles(): array |
|
198 | - { |
|
199 | - return $this->uploadedFiles; |
|
200 | - } |
|
201 | - |
|
202 | - /** |
|
203 | - * Creates a new instance of the request with the given uploaded files |
|
204 | - * |
|
205 | - * @param array<array-key, mixed> $uploadedFiles |
|
206 | - * |
|
207 | - * @return static |
|
208 | - * |
|
209 | - * @throws InvalidArgumentException |
|
210 | - * If one of the files isn't valid. |
|
211 | - */ |
|
212 | - public function withUploadedFiles(array $uploadedFiles): ServerRequestInterface |
|
213 | - { |
|
214 | - $clone = clone $this; |
|
215 | - $clone->setUploadedFiles($uploadedFiles); |
|
216 | - |
|
217 | - return $clone; |
|
218 | - } |
|
219 | - |
|
220 | - /** |
|
221 | - * Gets the request's parsed body |
|
222 | - * |
|
223 | - * @return array<array-key, mixed>|object|null |
|
224 | - */ |
|
225 | - public function getParsedBody() |
|
226 | - { |
|
227 | - return $this->parsedBody; |
|
228 | - } |
|
229 | - |
|
230 | - /** |
|
231 | - * Creates a new instance of the request with the given parsed body |
|
232 | - * |
|
233 | - * @param array<array-key, mixed>|object|null $data |
|
234 | - * |
|
235 | - * @return static |
|
236 | - * |
|
237 | - * @throws InvalidArgumentException |
|
238 | - * If the data isn't valid. |
|
239 | - */ |
|
240 | - public function withParsedBody($data): ServerRequestInterface |
|
241 | - { |
|
242 | - $clone = clone $this; |
|
243 | - $clone->setParsedBody($data); |
|
244 | - |
|
245 | - return $clone; |
|
246 | - } |
|
247 | - |
|
248 | - /** |
|
249 | - * Gets the request attributes |
|
250 | - * |
|
251 | - * @return array<array-key, mixed> |
|
252 | - */ |
|
253 | - public function getAttributes(): array |
|
254 | - { |
|
255 | - return $this->attributes; |
|
256 | - } |
|
257 | - |
|
258 | - /** |
|
259 | - * Gets the request's attribute value by the given name |
|
260 | - * |
|
261 | - * Returns the default value if the attribute wasn't found. |
|
262 | - * |
|
263 | - * @param array-key $name |
|
264 | - * @param mixed $default |
|
265 | - * |
|
266 | - * @return mixed |
|
267 | - */ |
|
268 | - public function getAttribute($name, $default = null) |
|
269 | - { |
|
270 | - if (!array_key_exists($name, $this->attributes)) { |
|
271 | - return $default; |
|
272 | - } |
|
273 | - |
|
274 | - return $this->attributes[$name]; |
|
275 | - } |
|
276 | - |
|
277 | - /** |
|
278 | - * Creates a new instance of the request with the given attribute |
|
279 | - * |
|
280 | - * @param array-key $name |
|
281 | - * @param mixed $value |
|
282 | - * |
|
283 | - * @return static |
|
284 | - */ |
|
285 | - public function withAttribute($name, $value): ServerRequestInterface |
|
286 | - { |
|
287 | - $clone = clone $this; |
|
288 | - $clone->attributes[$name] = $value; |
|
289 | - |
|
290 | - return $clone; |
|
291 | - } |
|
292 | - |
|
293 | - /** |
|
294 | - * Creates a new instance of the request without an attribute with the given name |
|
295 | - * |
|
296 | - * @param array-key $name |
|
297 | - * |
|
298 | - * @return static |
|
299 | - */ |
|
300 | - public function withoutAttribute($name): ServerRequestInterface |
|
301 | - { |
|
302 | - $clone = clone $this; |
|
303 | - unset($clone->attributes[$name]); |
|
304 | - |
|
305 | - return $clone; |
|
306 | - } |
|
307 | - |
|
308 | - /** |
|
309 | - * Sets the given uploaded files to the request |
|
310 | - * |
|
311 | - * @param array<array-key, mixed> $files |
|
312 | - * |
|
313 | - * @return void |
|
314 | - * |
|
315 | - * @throws InvalidArgumentException |
|
316 | - * If one of the files isn't valid. |
|
317 | - */ |
|
318 | - final protected function setUploadedFiles(array $files): void |
|
319 | - { |
|
320 | - $this->validateUploadedFiles($files); |
|
321 | - |
|
322 | - $this->uploadedFiles = $files; |
|
323 | - } |
|
324 | - |
|
325 | - /** |
|
326 | - * Sets the given parsed body to the request |
|
327 | - * |
|
328 | - * @param array<array-key, mixed>|object|null $data |
|
329 | - * |
|
330 | - * @return void |
|
331 | - * |
|
332 | - * @throws InvalidArgumentException |
|
333 | - * If the parsed body isn't valid. |
|
334 | - */ |
|
335 | - final protected function setParsedBody($data): void |
|
336 | - { |
|
337 | - $this->validateParsedBody($data); |
|
338 | - |
|
339 | - $this->parsedBody = $data; |
|
340 | - } |
|
341 | - |
|
342 | - /** |
|
343 | - * Validates the given uploaded files |
|
344 | - * |
|
345 | - * @param array<array-key, mixed> $files |
|
346 | - * |
|
347 | - * @return void |
|
348 | - * |
|
349 | - * @throws InvalidArgumentException |
|
350 | - * If one of the files isn't valid. |
|
351 | - */ |
|
352 | - private function validateUploadedFiles(array $files): void |
|
353 | - { |
|
354 | - if ([] === $files) { |
|
355 | - return; |
|
356 | - } |
|
357 | - |
|
358 | - /** |
|
359 | - * @psalm-suppress MissingClosureParamType |
|
360 | - */ |
|
361 | - array_walk_recursive($files, static function ($file): void { |
|
362 | - if (! ($file instanceof UploadedFileInterface)) { |
|
363 | - throw new InvalidArgumentException('Invalid uploaded file'); |
|
364 | - } |
|
365 | - }); |
|
366 | - } |
|
367 | - |
|
368 | - /** |
|
369 | - * Validates the given parsed body |
|
370 | - * |
|
371 | - * @param mixed $data |
|
372 | - * |
|
373 | - * @return void |
|
374 | - * |
|
375 | - * @throws InvalidArgumentException |
|
376 | - * If the parsed body isn't valid. |
|
377 | - */ |
|
378 | - private function validateParsedBody($data): void |
|
379 | - { |
|
380 | - if (null === $data) { |
|
381 | - return; |
|
382 | - } |
|
383 | - |
|
384 | - if (!is_array($data) && !is_object($data)) { |
|
385 | - throw new InvalidArgumentException('Invalid parsed body'); |
|
386 | - } |
|
387 | - } |
|
38 | + /** |
|
39 | + * The server parameters |
|
40 | + * |
|
41 | + * @var array<array-key, mixed> |
|
42 | + */ |
|
43 | + private array $serverParams; |
|
44 | + |
|
45 | + /** |
|
46 | + * The request's query parameters |
|
47 | + * |
|
48 | + * @var array<array-key, mixed> |
|
49 | + */ |
|
50 | + private array $queryParams; |
|
51 | + |
|
52 | + /** |
|
53 | + * The request's cookie parameters |
|
54 | + * |
|
55 | + * @var array<array-key, mixed> |
|
56 | + */ |
|
57 | + private array $cookieParams; |
|
58 | + |
|
59 | + /** |
|
60 | + * The request's uploaded files |
|
61 | + * |
|
62 | + * @var array<array-key, mixed> |
|
63 | + */ |
|
64 | + private array $uploadedFiles = []; |
|
65 | + |
|
66 | + /** |
|
67 | + * The request's parsed body |
|
68 | + * |
|
69 | + * @var array<array-key, mixed>|object|null |
|
70 | + */ |
|
71 | + private $parsedBody = null; |
|
72 | + |
|
73 | + /** |
|
74 | + * The request attributes |
|
75 | + * |
|
76 | + * @var array<array-key, mixed> |
|
77 | + */ |
|
78 | + private array $attributes; |
|
79 | + |
|
80 | + /** |
|
81 | + * Constructor of the class |
|
82 | + * |
|
83 | + * @param string|null $protocolVersion |
|
84 | + * @param string|null $method |
|
85 | + * @param mixed $uri |
|
86 | + * @param array<string, string|string[]>|null $headers |
|
87 | + * @param StreamInterface|null $body |
|
88 | + * |
|
89 | + * @param array<array-key, mixed> $serverParams |
|
90 | + * @param array<array-key, mixed> $queryParams |
|
91 | + * @param array<array-key, mixed> $cookieParams |
|
92 | + * @param array<array-key, mixed> $uploadedFiles |
|
93 | + * @param array<array-key, mixed>|object|null $parsedBody |
|
94 | + * @param array<array-key, mixed> $attributes |
|
95 | + * |
|
96 | + * @throws InvalidArgumentException |
|
97 | + * If one of the arguments isn't valid. |
|
98 | + */ |
|
99 | + public function __construct( |
|
100 | + ?string $protocolVersion = null, |
|
101 | + ?string $method = null, |
|
102 | + $uri = null, |
|
103 | + ?array $headers = null, |
|
104 | + ?StreamInterface $body = null, |
|
105 | + array $serverParams = [], |
|
106 | + array $queryParams = [], |
|
107 | + array $cookieParams = [], |
|
108 | + array $uploadedFiles = [], |
|
109 | + $parsedBody = null, |
|
110 | + array $attributes = [] |
|
111 | + ) { |
|
112 | + parent::__construct($method, $uri, $headers, $body); |
|
113 | + |
|
114 | + if (isset($protocolVersion)) { |
|
115 | + $this->setProtocolVersion($protocolVersion); |
|
116 | + } |
|
117 | + |
|
118 | + if (!empty($uploadedFiles)) { |
|
119 | + $this->setUploadedFiles($uploadedFiles); |
|
120 | + } |
|
121 | + |
|
122 | + if (isset($parsedBody)) { |
|
123 | + $this->setParsedBody($parsedBody); |
|
124 | + } |
|
125 | + |
|
126 | + $this->serverParams = $serverParams; |
|
127 | + $this->queryParams = $queryParams; |
|
128 | + $this->cookieParams = $cookieParams; |
|
129 | + $this->attributes = $attributes; |
|
130 | + } |
|
131 | + |
|
132 | + /** |
|
133 | + * Gets the server parameters |
|
134 | + * |
|
135 | + * @return array<array-key, mixed> |
|
136 | + */ |
|
137 | + public function getServerParams(): array |
|
138 | + { |
|
139 | + return $this->serverParams; |
|
140 | + } |
|
141 | + |
|
142 | + /** |
|
143 | + * Gets the request's query parameters |
|
144 | + * |
|
145 | + * @return array<array-key, mixed> |
|
146 | + */ |
|
147 | + public function getQueryParams(): array |
|
148 | + { |
|
149 | + return $this->queryParams; |
|
150 | + } |
|
151 | + |
|
152 | + /** |
|
153 | + * Creates a new instance of the request with the given query parameters |
|
154 | + * |
|
155 | + * @param array<array-key, mixed> $query |
|
156 | + * |
|
157 | + * @return static |
|
158 | + */ |
|
159 | + public function withQueryParams(array $query): ServerRequestInterface |
|
160 | + { |
|
161 | + $clone = clone $this; |
|
162 | + $clone->queryParams = $query; |
|
163 | + |
|
164 | + return $clone; |
|
165 | + } |
|
166 | + |
|
167 | + /** |
|
168 | + * Gets the request's cookie parameters |
|
169 | + * |
|
170 | + * @return array<array-key, mixed> |
|
171 | + */ |
|
172 | + public function getCookieParams(): array |
|
173 | + { |
|
174 | + return $this->cookieParams; |
|
175 | + } |
|
176 | + |
|
177 | + /** |
|
178 | + * Creates a new instance of the request with the given cookie parameters |
|
179 | + * |
|
180 | + * @param array<array-key, mixed> $cookies |
|
181 | + * |
|
182 | + * @return static |
|
183 | + */ |
|
184 | + public function withCookieParams(array $cookies): ServerRequestInterface |
|
185 | + { |
|
186 | + $clone = clone $this; |
|
187 | + $clone->cookieParams = $cookies; |
|
188 | + |
|
189 | + return $clone; |
|
190 | + } |
|
191 | + |
|
192 | + /** |
|
193 | + * Gets the request's uploaded files |
|
194 | + * |
|
195 | + * @return array<array-key, mixed> |
|
196 | + */ |
|
197 | + public function getUploadedFiles(): array |
|
198 | + { |
|
199 | + return $this->uploadedFiles; |
|
200 | + } |
|
201 | + |
|
202 | + /** |
|
203 | + * Creates a new instance of the request with the given uploaded files |
|
204 | + * |
|
205 | + * @param array<array-key, mixed> $uploadedFiles |
|
206 | + * |
|
207 | + * @return static |
|
208 | + * |
|
209 | + * @throws InvalidArgumentException |
|
210 | + * If one of the files isn't valid. |
|
211 | + */ |
|
212 | + public function withUploadedFiles(array $uploadedFiles): ServerRequestInterface |
|
213 | + { |
|
214 | + $clone = clone $this; |
|
215 | + $clone->setUploadedFiles($uploadedFiles); |
|
216 | + |
|
217 | + return $clone; |
|
218 | + } |
|
219 | + |
|
220 | + /** |
|
221 | + * Gets the request's parsed body |
|
222 | + * |
|
223 | + * @return array<array-key, mixed>|object|null |
|
224 | + */ |
|
225 | + public function getParsedBody() |
|
226 | + { |
|
227 | + return $this->parsedBody; |
|
228 | + } |
|
229 | + |
|
230 | + /** |
|
231 | + * Creates a new instance of the request with the given parsed body |
|
232 | + * |
|
233 | + * @param array<array-key, mixed>|object|null $data |
|
234 | + * |
|
235 | + * @return static |
|
236 | + * |
|
237 | + * @throws InvalidArgumentException |
|
238 | + * If the data isn't valid. |
|
239 | + */ |
|
240 | + public function withParsedBody($data): ServerRequestInterface |
|
241 | + { |
|
242 | + $clone = clone $this; |
|
243 | + $clone->setParsedBody($data); |
|
244 | + |
|
245 | + return $clone; |
|
246 | + } |
|
247 | + |
|
248 | + /** |
|
249 | + * Gets the request attributes |
|
250 | + * |
|
251 | + * @return array<array-key, mixed> |
|
252 | + */ |
|
253 | + public function getAttributes(): array |
|
254 | + { |
|
255 | + return $this->attributes; |
|
256 | + } |
|
257 | + |
|
258 | + /** |
|
259 | + * Gets the request's attribute value by the given name |
|
260 | + * |
|
261 | + * Returns the default value if the attribute wasn't found. |
|
262 | + * |
|
263 | + * @param array-key $name |
|
264 | + * @param mixed $default |
|
265 | + * |
|
266 | + * @return mixed |
|
267 | + */ |
|
268 | + public function getAttribute($name, $default = null) |
|
269 | + { |
|
270 | + if (!array_key_exists($name, $this->attributes)) { |
|
271 | + return $default; |
|
272 | + } |
|
273 | + |
|
274 | + return $this->attributes[$name]; |
|
275 | + } |
|
276 | + |
|
277 | + /** |
|
278 | + * Creates a new instance of the request with the given attribute |
|
279 | + * |
|
280 | + * @param array-key $name |
|
281 | + * @param mixed $value |
|
282 | + * |
|
283 | + * @return static |
|
284 | + */ |
|
285 | + public function withAttribute($name, $value): ServerRequestInterface |
|
286 | + { |
|
287 | + $clone = clone $this; |
|
288 | + $clone->attributes[$name] = $value; |
|
289 | + |
|
290 | + return $clone; |
|
291 | + } |
|
292 | + |
|
293 | + /** |
|
294 | + * Creates a new instance of the request without an attribute with the given name |
|
295 | + * |
|
296 | + * @param array-key $name |
|
297 | + * |
|
298 | + * @return static |
|
299 | + */ |
|
300 | + public function withoutAttribute($name): ServerRequestInterface |
|
301 | + { |
|
302 | + $clone = clone $this; |
|
303 | + unset($clone->attributes[$name]); |
|
304 | + |
|
305 | + return $clone; |
|
306 | + } |
|
307 | + |
|
308 | + /** |
|
309 | + * Sets the given uploaded files to the request |
|
310 | + * |
|
311 | + * @param array<array-key, mixed> $files |
|
312 | + * |
|
313 | + * @return void |
|
314 | + * |
|
315 | + * @throws InvalidArgumentException |
|
316 | + * If one of the files isn't valid. |
|
317 | + */ |
|
318 | + final protected function setUploadedFiles(array $files): void |
|
319 | + { |
|
320 | + $this->validateUploadedFiles($files); |
|
321 | + |
|
322 | + $this->uploadedFiles = $files; |
|
323 | + } |
|
324 | + |
|
325 | + /** |
|
326 | + * Sets the given parsed body to the request |
|
327 | + * |
|
328 | + * @param array<array-key, mixed>|object|null $data |
|
329 | + * |
|
330 | + * @return void |
|
331 | + * |
|
332 | + * @throws InvalidArgumentException |
|
333 | + * If the parsed body isn't valid. |
|
334 | + */ |
|
335 | + final protected function setParsedBody($data): void |
|
336 | + { |
|
337 | + $this->validateParsedBody($data); |
|
338 | + |
|
339 | + $this->parsedBody = $data; |
|
340 | + } |
|
341 | + |
|
342 | + /** |
|
343 | + * Validates the given uploaded files |
|
344 | + * |
|
345 | + * @param array<array-key, mixed> $files |
|
346 | + * |
|
347 | + * @return void |
|
348 | + * |
|
349 | + * @throws InvalidArgumentException |
|
350 | + * If one of the files isn't valid. |
|
351 | + */ |
|
352 | + private function validateUploadedFiles(array $files): void |
|
353 | + { |
|
354 | + if ([] === $files) { |
|
355 | + return; |
|
356 | + } |
|
357 | + |
|
358 | + /** |
|
359 | + * @psalm-suppress MissingClosureParamType |
|
360 | + */ |
|
361 | + array_walk_recursive($files, static function ($file): void { |
|
362 | + if (! ($file instanceof UploadedFileInterface)) { |
|
363 | + throw new InvalidArgumentException('Invalid uploaded file'); |
|
364 | + } |
|
365 | + }); |
|
366 | + } |
|
367 | + |
|
368 | + /** |
|
369 | + * Validates the given parsed body |
|
370 | + * |
|
371 | + * @param mixed $data |
|
372 | + * |
|
373 | + * @return void |
|
374 | + * |
|
375 | + * @throws InvalidArgumentException |
|
376 | + * If the parsed body isn't valid. |
|
377 | + */ |
|
378 | + private function validateParsedBody($data): void |
|
379 | + { |
|
380 | + if (null === $data) { |
|
381 | + return; |
|
382 | + } |
|
383 | + |
|
384 | + if (!is_array($data) && !is_object($data)) { |
|
385 | + throw new InvalidArgumentException('Invalid parsed body'); |
|
386 | + } |
|
387 | + } |
|
388 | 388 | } |
@@ -36,55 +36,55 @@ |
||
36 | 36 | final class JsonResponse extends Response |
37 | 37 | { |
38 | 38 | |
39 | - /** |
|
40 | - * Constructor of the class |
|
41 | - * |
|
42 | - * @param int $statusCode |
|
43 | - * @param mixed $data |
|
44 | - * @param int $flags |
|
45 | - * @param int<1, max> $depth |
|
46 | - * |
|
47 | - * @throws InvalidArgumentException |
|
48 | - */ |
|
49 | - public function __construct(int $statusCode, $data, int $flags = 0, int $depth = 512) |
|
50 | - { |
|
51 | - parent::__construct($statusCode); |
|
39 | + /** |
|
40 | + * Constructor of the class |
|
41 | + * |
|
42 | + * @param int $statusCode |
|
43 | + * @param mixed $data |
|
44 | + * @param int $flags |
|
45 | + * @param int<1, max> $depth |
|
46 | + * |
|
47 | + * @throws InvalidArgumentException |
|
48 | + */ |
|
49 | + public function __construct(int $statusCode, $data, int $flags = 0, int $depth = 512) |
|
50 | + { |
|
51 | + parent::__construct($statusCode); |
|
52 | 52 | |
53 | - $this->setBody($this->createBody($data, $flags, $depth)); |
|
53 | + $this->setBody($this->createBody($data, $flags, $depth)); |
|
54 | 54 | |
55 | - $this->setHeader('Content-Type', 'application/json; charset=utf-8'); |
|
56 | - } |
|
55 | + $this->setHeader('Content-Type', 'application/json; charset=utf-8'); |
|
56 | + } |
|
57 | 57 | |
58 | - /** |
|
59 | - * Creates the response body from the given JSON data |
|
60 | - * |
|
61 | - * @param mixed $data |
|
62 | - * @param int $flags |
|
63 | - * @param int<1, max> $depth |
|
64 | - * |
|
65 | - * @return StreamInterface |
|
66 | - * |
|
67 | - * @throws InvalidArgumentException |
|
68 | - */ |
|
69 | - private function createBody($data, int $flags, int $depth): StreamInterface |
|
70 | - { |
|
71 | - if ($data instanceof StreamInterface) { |
|
72 | - return $data; |
|
73 | - } |
|
58 | + /** |
|
59 | + * Creates the response body from the given JSON data |
|
60 | + * |
|
61 | + * @param mixed $data |
|
62 | + * @param int $flags |
|
63 | + * @param int<1, max> $depth |
|
64 | + * |
|
65 | + * @return StreamInterface |
|
66 | + * |
|
67 | + * @throws InvalidArgumentException |
|
68 | + */ |
|
69 | + private function createBody($data, int $flags, int $depth): StreamInterface |
|
70 | + { |
|
71 | + if ($data instanceof StreamInterface) { |
|
72 | + return $data; |
|
73 | + } |
|
74 | 74 | |
75 | - try { |
|
76 | - $payload = json_encode($data, $flags | JSON_THROW_ON_ERROR, $depth); |
|
77 | - } catch (JsonException $e) { |
|
78 | - throw new InvalidArgumentException(sprintf( |
|
79 | - 'Unable to create JSON response due to invalid JSON data: %s', |
|
80 | - $e->getMessage() |
|
81 | - ), 0, $e); |
|
82 | - } |
|
75 | + try { |
|
76 | + $payload = json_encode($data, $flags | JSON_THROW_ON_ERROR, $depth); |
|
77 | + } catch (JsonException $e) { |
|
78 | + throw new InvalidArgumentException(sprintf( |
|
79 | + 'Unable to create JSON response due to invalid JSON data: %s', |
|
80 | + $e->getMessage() |
|
81 | + ), 0, $e); |
|
82 | + } |
|
83 | 83 | |
84 | - $stream = new PhpTempStream('r+b'); |
|
85 | - $stream->write($payload); |
|
86 | - $stream->rewind(); |
|
84 | + $stream = new PhpTempStream('r+b'); |
|
85 | + $stream->write($payload); |
|
86 | + $stream->rewind(); |
|
87 | 87 | |
88 | - return $stream; |
|
89 | - } |
|
88 | + return $stream; |
|
89 | + } |
|
90 | 90 | } |