@@ -16,11 +16,11 @@ |
||
16 | 16 | |
17 | 17 | class ResponseFactory implements ResponseFactoryInterface |
18 | 18 | { |
19 | - /** |
|
20 | - * @inheritDoc |
|
21 | - */ |
|
22 | - public function createResponse(int $code = 200, string $reasonPhrase = ''): ResponseInterface |
|
23 | - { |
|
24 | - return new Response($code, $reasonPhrase); |
|
25 | - } |
|
19 | + /** |
|
20 | + * @inheritDoc |
|
21 | + */ |
|
22 | + public function createResponse(int $code = 200, string $reasonPhrase = ''): ResponseInterface |
|
23 | + { |
|
24 | + return new Response($code, $reasonPhrase); |
|
25 | + } |
|
26 | 26 | } |
@@ -22,42 +22,42 @@ |
||
22 | 22 | |
23 | 23 | final class HtmlResponse extends Response |
24 | 24 | { |
25 | - /** |
|
26 | - * @param mixed $html |
|
27 | - * |
|
28 | - * @throws InvalidArgumentException |
|
29 | - */ |
|
30 | - public function __construct(int $statusCode, $html) |
|
31 | - { |
|
32 | - parent::__construct($statusCode); |
|
33 | - |
|
34 | - $this->setBody(self::createBody($html)); |
|
35 | - $this->setHeader('Content-Type', 'text/html; charset=utf-8'); |
|
36 | - } |
|
37 | - |
|
38 | - /** |
|
39 | - * @param mixed $html |
|
40 | - * |
|
41 | - * @throws InvalidArgumentException |
|
42 | - */ |
|
43 | - private static function createBody($html): StreamInterface |
|
44 | - { |
|
45 | - if ($html instanceof StreamInterface) { |
|
46 | - return $html; |
|
47 | - } |
|
48 | - |
|
49 | - if (is_object($html) && method_exists($html, '__toString')) { |
|
50 | - $html = (string) $html; |
|
51 | - } |
|
52 | - |
|
53 | - if (!is_string($html)) { |
|
54 | - throw new InvalidArgumentException('Unable to create the HTML response due to a unexpected HTML type'); |
|
55 | - } |
|
56 | - |
|
57 | - $stream = new PhpTempStream('r+b'); |
|
58 | - $stream->write($html); |
|
59 | - $stream->rewind(); |
|
60 | - |
|
61 | - return $stream; |
|
62 | - } |
|
25 | + /** |
|
26 | + * @param mixed $html |
|
27 | + * |
|
28 | + * @throws InvalidArgumentException |
|
29 | + */ |
|
30 | + public function __construct(int $statusCode, $html) |
|
31 | + { |
|
32 | + parent::__construct($statusCode); |
|
33 | + |
|
34 | + $this->setBody(self::createBody($html)); |
|
35 | + $this->setHeader('Content-Type', 'text/html; charset=utf-8'); |
|
36 | + } |
|
37 | + |
|
38 | + /** |
|
39 | + * @param mixed $html |
|
40 | + * |
|
41 | + * @throws InvalidArgumentException |
|
42 | + */ |
|
43 | + private static function createBody($html): StreamInterface |
|
44 | + { |
|
45 | + if ($html instanceof StreamInterface) { |
|
46 | + return $html; |
|
47 | + } |
|
48 | + |
|
49 | + if (is_object($html) && method_exists($html, '__toString')) { |
|
50 | + $html = (string) $html; |
|
51 | + } |
|
52 | + |
|
53 | + if (!is_string($html)) { |
|
54 | + throw new InvalidArgumentException('Unable to create the HTML response due to a unexpected HTML type'); |
|
55 | + } |
|
56 | + |
|
57 | + $stream = new PhpTempStream('r+b'); |
|
58 | + $stream->write($html); |
|
59 | + $stream->rewind(); |
|
60 | + |
|
61 | + return $stream; |
|
62 | + } |
|
63 | 63 | } |
@@ -31,51 +31,51 @@ |
||
31 | 31 | */ |
32 | 32 | final class UrlEncodedRequest extends Request |
33 | 33 | { |
34 | - public const ENCODING_TYPE_RFC1738 = PHP_QUERY_RFC1738; |
|
35 | - public const ENCODING_TYPE_RFC3986 = PHP_QUERY_RFC3986; |
|
34 | + public const ENCODING_TYPE_RFC1738 = PHP_QUERY_RFC1738; |
|
35 | + public const ENCODING_TYPE_RFC3986 = PHP_QUERY_RFC3986; |
|
36 | 36 | |
37 | - /** |
|
38 | - * @param mixed $uri |
|
39 | - * @param array<array-key, mixed>|object $data |
|
40 | - * @param self::ENCODING_TYPE_* $encodingType |
|
41 | - * |
|
42 | - * @throws InvalidArgumentException |
|
43 | - */ |
|
44 | - public function __construct(string $method, $uri, $data, int $encodingType = self::ENCODING_TYPE_RFC1738) |
|
45 | - { |
|
46 | - /** |
|
47 | - * @psalm-suppress DocblockTypeContradiction |
|
48 | - * @phpstan-ignore-next-line |
|
49 | - */ |
|
50 | - if (!is_array($data) && !is_object($data)) { |
|
51 | - throw new TypeError(sprintf( |
|
52 | - 'Argument #3 ($data) must be of type string, %s given', |
|
53 | - gettype($data), |
|
54 | - )); |
|
55 | - } |
|
37 | + /** |
|
38 | + * @param mixed $uri |
|
39 | + * @param array<array-key, mixed>|object $data |
|
40 | + * @param self::ENCODING_TYPE_* $encodingType |
|
41 | + * |
|
42 | + * @throws InvalidArgumentException |
|
43 | + */ |
|
44 | + public function __construct(string $method, $uri, $data, int $encodingType = self::ENCODING_TYPE_RFC1738) |
|
45 | + { |
|
46 | + /** |
|
47 | + * @psalm-suppress DocblockTypeContradiction |
|
48 | + * @phpstan-ignore-next-line |
|
49 | + */ |
|
50 | + if (!is_array($data) && !is_object($data)) { |
|
51 | + throw new TypeError(sprintf( |
|
52 | + 'Argument #3 ($data) must be of type string, %s given', |
|
53 | + gettype($data), |
|
54 | + )); |
|
55 | + } |
|
56 | 56 | |
57 | - parent::__construct($method, $uri); |
|
57 | + parent::__construct($method, $uri); |
|
58 | 58 | |
59 | - $this->setBody(self::createBody($data, $encodingType)); |
|
60 | - $this->setHeader('Content-Type', 'application/x-www-form-urlencoded; charset=utf-8'); |
|
61 | - } |
|
59 | + $this->setBody(self::createBody($data, $encodingType)); |
|
60 | + $this->setHeader('Content-Type', 'application/x-www-form-urlencoded; charset=utf-8'); |
|
61 | + } |
|
62 | 62 | |
63 | - /** |
|
64 | - * @param array<array-key, mixed>|object $data |
|
65 | - * @param self::ENCODING_TYPE_* $encodingType |
|
66 | - */ |
|
67 | - private static function createBody($data, int $encodingType): StreamInterface |
|
68 | - { |
|
69 | - if ($data instanceof StreamInterface) { |
|
70 | - return $data; |
|
71 | - } |
|
63 | + /** |
|
64 | + * @param array<array-key, mixed>|object $data |
|
65 | + * @param self::ENCODING_TYPE_* $encodingType |
|
66 | + */ |
|
67 | + private static function createBody($data, int $encodingType): StreamInterface |
|
68 | + { |
|
69 | + if ($data instanceof StreamInterface) { |
|
70 | + return $data; |
|
71 | + } |
|
72 | 72 | |
73 | - $encodedData = http_build_query($data, '', '', $encodingType); |
|
73 | + $encodedData = http_build_query($data, '', '', $encodingType); |
|
74 | 74 | |
75 | - $stream = new PhpTempStream('r+b'); |
|
76 | - $stream->write($encodedData); |
|
77 | - $stream->rewind(); |
|
75 | + $stream = new PhpTempStream('r+b'); |
|
76 | + $stream->write($encodedData); |
|
77 | + $stream->rewind(); |
|
78 | 78 | |
79 | - return $stream; |
|
80 | - } |
|
79 | + return $stream; |
|
80 | + } |
|
81 | 81 | } |
@@ -24,296 +24,296 @@ |
||
24 | 24 | |
25 | 25 | abstract class Message implements MessageInterface |
26 | 26 | { |
27 | - /** |
|
28 | - * @deprecated 3.2.0 |
|
29 | - */ |
|
30 | - public const ALLOWED_HTTP_VERSIONS = ['1.0', '1.1', '2.0', '2']; |
|
31 | - |
|
32 | - public const HTTP_VERSION_REGEX = '/^[0-9](?:[.][0-9])?$/'; |
|
33 | - public const DEFAULT_HTTP_VERSION = '1.1'; |
|
34 | - |
|
35 | - private string $protocolVersion = self::DEFAULT_HTTP_VERSION; |
|
36 | - |
|
37 | - /** |
|
38 | - * @var array<string, list<string>> |
|
39 | - */ |
|
40 | - private array $headers = []; |
|
41 | - |
|
42 | - /** |
|
43 | - * @var array<string, string> |
|
44 | - */ |
|
45 | - private array $headerNames = []; |
|
46 | - |
|
47 | - private ?StreamInterface $body = null; |
|
48 | - |
|
49 | - /** |
|
50 | - * @inheritDoc |
|
51 | - */ |
|
52 | - public function getProtocolVersion(): string |
|
53 | - { |
|
54 | - return $this->protocolVersion; |
|
55 | - } |
|
56 | - |
|
57 | - /** |
|
58 | - * @inheritDoc |
|
59 | - * |
|
60 | - * @throws InvalidArgumentException |
|
61 | - */ |
|
62 | - public function withProtocolVersion($version): MessageInterface |
|
63 | - { |
|
64 | - $clone = clone $this; |
|
65 | - $clone->setProtocolVersion($version); |
|
66 | - |
|
67 | - return $clone; |
|
68 | - } |
|
69 | - |
|
70 | - /** |
|
71 | - * @inheritDoc |
|
72 | - */ |
|
73 | - public function getHeaders(): array |
|
74 | - { |
|
75 | - return $this->headers; |
|
76 | - } |
|
77 | - |
|
78 | - /** |
|
79 | - * @inheritDoc |
|
80 | - */ |
|
81 | - public function hasHeader($name): bool |
|
82 | - { |
|
83 | - $key = strtolower($name); |
|
84 | - |
|
85 | - return isset($this->headerNames[$key]); |
|
86 | - } |
|
87 | - |
|
88 | - /** |
|
89 | - * @inheritDoc |
|
90 | - */ |
|
91 | - public function getHeader($name): array |
|
92 | - { |
|
93 | - $key = strtolower($name); |
|
94 | - |
|
95 | - if (!isset($this->headerNames[$key])) { |
|
96 | - return []; |
|
97 | - } |
|
98 | - |
|
99 | - return $this->headers[$this->headerNames[$key]]; |
|
100 | - } |
|
101 | - |
|
102 | - /** |
|
103 | - * @inheritDoc |
|
104 | - */ |
|
105 | - public function getHeaderLine($name): string |
|
106 | - { |
|
107 | - $key = strtolower($name); |
|
108 | - |
|
109 | - if (!isset($this->headerNames[$key])) { |
|
110 | - return ''; |
|
111 | - } |
|
112 | - |
|
113 | - return implode(',', $this->headers[$this->headerNames[$key]]); |
|
114 | - } |
|
115 | - |
|
116 | - /** |
|
117 | - * @inheritDoc |
|
118 | - */ |
|
119 | - public function withHeader($name, $value): MessageInterface |
|
120 | - { |
|
121 | - $clone = clone $this; |
|
122 | - $clone->setHeader($name, $value, true); |
|
123 | - |
|
124 | - return $clone; |
|
125 | - } |
|
126 | - |
|
127 | - /** |
|
128 | - * @inheritDoc |
|
129 | - */ |
|
130 | - public function withAddedHeader($name, $value): MessageInterface |
|
131 | - { |
|
132 | - $clone = clone $this; |
|
133 | - $clone->setHeader($name, $value, false); |
|
134 | - |
|
135 | - return $clone; |
|
136 | - } |
|
137 | - |
|
138 | - /** |
|
139 | - * @inheritDoc |
|
140 | - */ |
|
141 | - public function withoutHeader($name): MessageInterface |
|
142 | - { |
|
143 | - $clone = clone $this; |
|
144 | - $clone->deleteHeader($name); |
|
145 | - |
|
146 | - return $clone; |
|
147 | - } |
|
148 | - |
|
149 | - /** |
|
150 | - * @inheritDoc |
|
151 | - */ |
|
152 | - public function getBody(): StreamInterface |
|
153 | - { |
|
154 | - return $this->body ??= new PhpTempStream(); |
|
155 | - } |
|
156 | - |
|
157 | - /** |
|
158 | - * @inheritDoc |
|
159 | - */ |
|
160 | - public function withBody(StreamInterface $body): MessageInterface |
|
161 | - { |
|
162 | - $clone = clone $this; |
|
163 | - $clone->setBody($body); |
|
164 | - |
|
165 | - return $clone; |
|
166 | - } |
|
167 | - |
|
168 | - /** |
|
169 | - * Sets the given HTTP version to the message |
|
170 | - * |
|
171 | - * @param string $protocolVersion |
|
172 | - * |
|
173 | - * @throws InvalidArgumentException |
|
174 | - */ |
|
175 | - final protected function setProtocolVersion($protocolVersion): void |
|
176 | - { |
|
177 | - $this->validateProtocolVersion($protocolVersion); |
|
178 | - |
|
179 | - $this->protocolVersion = $protocolVersion; |
|
180 | - } |
|
181 | - |
|
182 | - /** |
|
183 | - * Sets a new header to the message with the given name and value(s) |
|
184 | - * |
|
185 | - * @param string $name |
|
186 | - * @param string|string[] $value |
|
187 | - * |
|
188 | - * @throws InvalidArgumentException |
|
189 | - */ |
|
190 | - final protected function setHeader($name, $value, bool $replace = true): void |
|
191 | - { |
|
192 | - if (!is_array($value)) { |
|
193 | - $value = [$value]; |
|
194 | - } |
|
195 | - |
|
196 | - $this->validateHeaderName($name); |
|
197 | - $this->validateHeaderValue($name, $value); |
|
198 | - |
|
199 | - $replace and $this->deleteHeader($name); |
|
200 | - |
|
201 | - $key = strtolower($name); |
|
202 | - |
|
203 | - $this->headerNames[$key] ??= $name; |
|
204 | - $this->headers[$this->headerNames[$key]] ??= []; |
|
205 | - |
|
206 | - foreach ($value as $item) { |
|
207 | - $this->headers[$this->headerNames[$key]][] = $item; |
|
208 | - } |
|
209 | - } |
|
210 | - |
|
211 | - /** |
|
212 | - * Sets the given headers to the message |
|
213 | - * |
|
214 | - * @param array<string, string|string[]> $headers |
|
215 | - * |
|
216 | - * @throws InvalidArgumentException |
|
217 | - */ |
|
218 | - final protected function setHeaders(array $headers): void |
|
219 | - { |
|
220 | - foreach ($headers as $name => $value) { |
|
221 | - $this->setHeader($name, $value, false); |
|
222 | - } |
|
223 | - } |
|
224 | - |
|
225 | - /** |
|
226 | - * Deletes a header from the message by the given name |
|
227 | - * |
|
228 | - * @param string $name |
|
229 | - */ |
|
230 | - final protected function deleteHeader($name): void |
|
231 | - { |
|
232 | - $key = strtolower($name); |
|
233 | - |
|
234 | - if (isset($this->headerNames[$key])) { |
|
235 | - unset($this->headers[$this->headerNames[$key]]); |
|
236 | - unset($this->headerNames[$key]); |
|
237 | - } |
|
238 | - } |
|
239 | - |
|
240 | - /** |
|
241 | - * Sets the given body to the message |
|
242 | - */ |
|
243 | - final protected function setBody(StreamInterface $body): void |
|
244 | - { |
|
245 | - $this->body = $body; |
|
246 | - } |
|
247 | - |
|
248 | - /** |
|
249 | - * Validates the given HTTP version |
|
250 | - * |
|
251 | - * @param mixed $protocolVersion |
|
252 | - * |
|
253 | - * @throws InvalidArgumentException |
|
254 | - */ |
|
255 | - private function validateProtocolVersion($protocolVersion): void |
|
256 | - { |
|
257 | - if ($protocolVersion === '') { |
|
258 | - throw new InvalidArgumentException('HTTP version cannot be an empty'); |
|
259 | - } |
|
260 | - |
|
261 | - if (!is_string($protocolVersion)) { |
|
262 | - throw new InvalidArgumentException('HTTP version must be a string'); |
|
263 | - } |
|
264 | - |
|
265 | - if (!preg_match(self::HTTP_VERSION_REGEX, $protocolVersion)) { |
|
266 | - throw new InvalidArgumentException('HTTP version is invalid'); |
|
267 | - } |
|
268 | - } |
|
269 | - |
|
270 | - /** |
|
271 | - * Validates the given header name |
|
272 | - * |
|
273 | - * @param mixed $name |
|
274 | - * |
|
275 | - * @throws InvalidArgumentException |
|
276 | - */ |
|
277 | - private function validateHeaderName($name): void |
|
278 | - { |
|
279 | - if ($name === '') { |
|
280 | - throw new InvalidArgumentException('HTTP header name cannot be an empty'); |
|
281 | - } |
|
282 | - |
|
283 | - if (!is_string($name)) { |
|
284 | - throw new InvalidArgumentException('HTTP header name must be a string'); |
|
285 | - } |
|
286 | - |
|
287 | - if (!preg_match(HeaderInterface::RFC7230_TOKEN_REGEX, $name)) { |
|
288 | - throw new InvalidArgumentException('HTTP header name is invalid'); |
|
289 | - } |
|
290 | - } |
|
291 | - |
|
292 | - /** |
|
293 | - * Validates the given header value |
|
294 | - * |
|
295 | - * @param array<array-key, mixed> $value |
|
296 | - * |
|
297 | - * @throws InvalidArgumentException |
|
298 | - */ |
|
299 | - private function validateHeaderValue(string $name, array $value): void |
|
300 | - { |
|
301 | - if ($value === []) { |
|
302 | - throw new InvalidArgumentException("The value of the HTTP header $name cannot be an empty array"); |
|
303 | - } |
|
304 | - |
|
305 | - foreach ($value as $key => $item) { |
|
306 | - if ($item === '') { |
|
307 | - continue; |
|
308 | - } |
|
309 | - |
|
310 | - if (!is_string($item)) { |
|
311 | - throw new InvalidArgumentException("The value of the HTTP header $name:$key must be a string"); |
|
312 | - } |
|
313 | - |
|
314 | - if (!preg_match(HeaderInterface::RFC7230_FIELD_VALUE_REGEX, $item)) { |
|
315 | - throw new InvalidArgumentException("The value of the HTTP header $name:$key is invalid"); |
|
316 | - } |
|
317 | - } |
|
318 | - } |
|
27 | + /** |
|
28 | + * @deprecated 3.2.0 |
|
29 | + */ |
|
30 | + public const ALLOWED_HTTP_VERSIONS = ['1.0', '1.1', '2.0', '2']; |
|
31 | + |
|
32 | + public const HTTP_VERSION_REGEX = '/^[0-9](?:[.][0-9])?$/'; |
|
33 | + public const DEFAULT_HTTP_VERSION = '1.1'; |
|
34 | + |
|
35 | + private string $protocolVersion = self::DEFAULT_HTTP_VERSION; |
|
36 | + |
|
37 | + /** |
|
38 | + * @var array<string, list<string>> |
|
39 | + */ |
|
40 | + private array $headers = []; |
|
41 | + |
|
42 | + /** |
|
43 | + * @var array<string, string> |
|
44 | + */ |
|
45 | + private array $headerNames = []; |
|
46 | + |
|
47 | + private ?StreamInterface $body = null; |
|
48 | + |
|
49 | + /** |
|
50 | + * @inheritDoc |
|
51 | + */ |
|
52 | + public function getProtocolVersion(): string |
|
53 | + { |
|
54 | + return $this->protocolVersion; |
|
55 | + } |
|
56 | + |
|
57 | + /** |
|
58 | + * @inheritDoc |
|
59 | + * |
|
60 | + * @throws InvalidArgumentException |
|
61 | + */ |
|
62 | + public function withProtocolVersion($version): MessageInterface |
|
63 | + { |
|
64 | + $clone = clone $this; |
|
65 | + $clone->setProtocolVersion($version); |
|
66 | + |
|
67 | + return $clone; |
|
68 | + } |
|
69 | + |
|
70 | + /** |
|
71 | + * @inheritDoc |
|
72 | + */ |
|
73 | + public function getHeaders(): array |
|
74 | + { |
|
75 | + return $this->headers; |
|
76 | + } |
|
77 | + |
|
78 | + /** |
|
79 | + * @inheritDoc |
|
80 | + */ |
|
81 | + public function hasHeader($name): bool |
|
82 | + { |
|
83 | + $key = strtolower($name); |
|
84 | + |
|
85 | + return isset($this->headerNames[$key]); |
|
86 | + } |
|
87 | + |
|
88 | + /** |
|
89 | + * @inheritDoc |
|
90 | + */ |
|
91 | + public function getHeader($name): array |
|
92 | + { |
|
93 | + $key = strtolower($name); |
|
94 | + |
|
95 | + if (!isset($this->headerNames[$key])) { |
|
96 | + return []; |
|
97 | + } |
|
98 | + |
|
99 | + return $this->headers[$this->headerNames[$key]]; |
|
100 | + } |
|
101 | + |
|
102 | + /** |
|
103 | + * @inheritDoc |
|
104 | + */ |
|
105 | + public function getHeaderLine($name): string |
|
106 | + { |
|
107 | + $key = strtolower($name); |
|
108 | + |
|
109 | + if (!isset($this->headerNames[$key])) { |
|
110 | + return ''; |
|
111 | + } |
|
112 | + |
|
113 | + return implode(',', $this->headers[$this->headerNames[$key]]); |
|
114 | + } |
|
115 | + |
|
116 | + /** |
|
117 | + * @inheritDoc |
|
118 | + */ |
|
119 | + public function withHeader($name, $value): MessageInterface |
|
120 | + { |
|
121 | + $clone = clone $this; |
|
122 | + $clone->setHeader($name, $value, true); |
|
123 | + |
|
124 | + return $clone; |
|
125 | + } |
|
126 | + |
|
127 | + /** |
|
128 | + * @inheritDoc |
|
129 | + */ |
|
130 | + public function withAddedHeader($name, $value): MessageInterface |
|
131 | + { |
|
132 | + $clone = clone $this; |
|
133 | + $clone->setHeader($name, $value, false); |
|
134 | + |
|
135 | + return $clone; |
|
136 | + } |
|
137 | + |
|
138 | + /** |
|
139 | + * @inheritDoc |
|
140 | + */ |
|
141 | + public function withoutHeader($name): MessageInterface |
|
142 | + { |
|
143 | + $clone = clone $this; |
|
144 | + $clone->deleteHeader($name); |
|
145 | + |
|
146 | + return $clone; |
|
147 | + } |
|
148 | + |
|
149 | + /** |
|
150 | + * @inheritDoc |
|
151 | + */ |
|
152 | + public function getBody(): StreamInterface |
|
153 | + { |
|
154 | + return $this->body ??= new PhpTempStream(); |
|
155 | + } |
|
156 | + |
|
157 | + /** |
|
158 | + * @inheritDoc |
|
159 | + */ |
|
160 | + public function withBody(StreamInterface $body): MessageInterface |
|
161 | + { |
|
162 | + $clone = clone $this; |
|
163 | + $clone->setBody($body); |
|
164 | + |
|
165 | + return $clone; |
|
166 | + } |
|
167 | + |
|
168 | + /** |
|
169 | + * Sets the given HTTP version to the message |
|
170 | + * |
|
171 | + * @param string $protocolVersion |
|
172 | + * |
|
173 | + * @throws InvalidArgumentException |
|
174 | + */ |
|
175 | + final protected function setProtocolVersion($protocolVersion): void |
|
176 | + { |
|
177 | + $this->validateProtocolVersion($protocolVersion); |
|
178 | + |
|
179 | + $this->protocolVersion = $protocolVersion; |
|
180 | + } |
|
181 | + |
|
182 | + /** |
|
183 | + * Sets a new header to the message with the given name and value(s) |
|
184 | + * |
|
185 | + * @param string $name |
|
186 | + * @param string|string[] $value |
|
187 | + * |
|
188 | + * @throws InvalidArgumentException |
|
189 | + */ |
|
190 | + final protected function setHeader($name, $value, bool $replace = true): void |
|
191 | + { |
|
192 | + if (!is_array($value)) { |
|
193 | + $value = [$value]; |
|
194 | + } |
|
195 | + |
|
196 | + $this->validateHeaderName($name); |
|
197 | + $this->validateHeaderValue($name, $value); |
|
198 | + |
|
199 | + $replace and $this->deleteHeader($name); |
|
200 | + |
|
201 | + $key = strtolower($name); |
|
202 | + |
|
203 | + $this->headerNames[$key] ??= $name; |
|
204 | + $this->headers[$this->headerNames[$key]] ??= []; |
|
205 | + |
|
206 | + foreach ($value as $item) { |
|
207 | + $this->headers[$this->headerNames[$key]][] = $item; |
|
208 | + } |
|
209 | + } |
|
210 | + |
|
211 | + /** |
|
212 | + * Sets the given headers to the message |
|
213 | + * |
|
214 | + * @param array<string, string|string[]> $headers |
|
215 | + * |
|
216 | + * @throws InvalidArgumentException |
|
217 | + */ |
|
218 | + final protected function setHeaders(array $headers): void |
|
219 | + { |
|
220 | + foreach ($headers as $name => $value) { |
|
221 | + $this->setHeader($name, $value, false); |
|
222 | + } |
|
223 | + } |
|
224 | + |
|
225 | + /** |
|
226 | + * Deletes a header from the message by the given name |
|
227 | + * |
|
228 | + * @param string $name |
|
229 | + */ |
|
230 | + final protected function deleteHeader($name): void |
|
231 | + { |
|
232 | + $key = strtolower($name); |
|
233 | + |
|
234 | + if (isset($this->headerNames[$key])) { |
|
235 | + unset($this->headers[$this->headerNames[$key]]); |
|
236 | + unset($this->headerNames[$key]); |
|
237 | + } |
|
238 | + } |
|
239 | + |
|
240 | + /** |
|
241 | + * Sets the given body to the message |
|
242 | + */ |
|
243 | + final protected function setBody(StreamInterface $body): void |
|
244 | + { |
|
245 | + $this->body = $body; |
|
246 | + } |
|
247 | + |
|
248 | + /** |
|
249 | + * Validates the given HTTP version |
|
250 | + * |
|
251 | + * @param mixed $protocolVersion |
|
252 | + * |
|
253 | + * @throws InvalidArgumentException |
|
254 | + */ |
|
255 | + private function validateProtocolVersion($protocolVersion): void |
|
256 | + { |
|
257 | + if ($protocolVersion === '') { |
|
258 | + throw new InvalidArgumentException('HTTP version cannot be an empty'); |
|
259 | + } |
|
260 | + |
|
261 | + if (!is_string($protocolVersion)) { |
|
262 | + throw new InvalidArgumentException('HTTP version must be a string'); |
|
263 | + } |
|
264 | + |
|
265 | + if (!preg_match(self::HTTP_VERSION_REGEX, $protocolVersion)) { |
|
266 | + throw new InvalidArgumentException('HTTP version is invalid'); |
|
267 | + } |
|
268 | + } |
|
269 | + |
|
270 | + /** |
|
271 | + * Validates the given header name |
|
272 | + * |
|
273 | + * @param mixed $name |
|
274 | + * |
|
275 | + * @throws InvalidArgumentException |
|
276 | + */ |
|
277 | + private function validateHeaderName($name): void |
|
278 | + { |
|
279 | + if ($name === '') { |
|
280 | + throw new InvalidArgumentException('HTTP header name cannot be an empty'); |
|
281 | + } |
|
282 | + |
|
283 | + if (!is_string($name)) { |
|
284 | + throw new InvalidArgumentException('HTTP header name must be a string'); |
|
285 | + } |
|
286 | + |
|
287 | + if (!preg_match(HeaderInterface::RFC7230_TOKEN_REGEX, $name)) { |
|
288 | + throw new InvalidArgumentException('HTTP header name is invalid'); |
|
289 | + } |
|
290 | + } |
|
291 | + |
|
292 | + /** |
|
293 | + * Validates the given header value |
|
294 | + * |
|
295 | + * @param array<array-key, mixed> $value |
|
296 | + * |
|
297 | + * @throws InvalidArgumentException |
|
298 | + */ |
|
299 | + private function validateHeaderValue(string $name, array $value): void |
|
300 | + { |
|
301 | + if ($value === []) { |
|
302 | + throw new InvalidArgumentException("The value of the HTTP header $name cannot be an empty array"); |
|
303 | + } |
|
304 | + |
|
305 | + foreach ($value as $key => $item) { |
|
306 | + if ($item === '') { |
|
307 | + continue; |
|
308 | + } |
|
309 | + |
|
310 | + if (!is_string($item)) { |
|
311 | + throw new InvalidArgumentException("The value of the HTTP header $name:$key must be a string"); |
|
312 | + } |
|
313 | + |
|
314 | + if (!preg_match(HeaderInterface::RFC7230_FIELD_VALUE_REGEX, $item)) { |
|
315 | + throw new InvalidArgumentException("The value of the HTTP header $name:$key is invalid"); |
|
316 | + } |
|
317 | + } |
|
318 | + } |
|
319 | 319 | } |
@@ -23,317 +23,317 @@ |
||
23 | 23 | |
24 | 24 | class ServerRequest extends Request implements ServerRequestInterface |
25 | 25 | { |
26 | - /** |
|
27 | - * @var array<array-key, mixed> |
|
28 | - */ |
|
29 | - private array $serverParams; |
|
30 | - |
|
31 | - /** |
|
32 | - * @var array<array-key, mixed> |
|
33 | - */ |
|
34 | - private array $queryParams; |
|
35 | - |
|
36 | - /** |
|
37 | - * @var array<array-key, mixed> |
|
38 | - */ |
|
39 | - private array $cookieParams; |
|
40 | - |
|
41 | - /** |
|
42 | - * @var array<array-key, mixed> |
|
43 | - */ |
|
44 | - private array $uploadedFiles = []; |
|
45 | - |
|
46 | - /** |
|
47 | - * @var array<array-key, mixed>|object|null |
|
48 | - */ |
|
49 | - private $parsedBody = null; |
|
50 | - |
|
51 | - /** |
|
52 | - * @var array<array-key, mixed> |
|
53 | - */ |
|
54 | - private array $attributes; |
|
55 | - |
|
56 | - /** |
|
57 | - * Constructor of the class |
|
58 | - * |
|
59 | - * @param mixed $uri |
|
60 | - * @param array<string, string|string[]>|null $headers |
|
61 | - * |
|
62 | - * @param array<array-key, mixed> $serverParams |
|
63 | - * @param array<array-key, mixed> $queryParams |
|
64 | - * @param array<array-key, mixed> $cookieParams |
|
65 | - * @param array<array-key, mixed> $uploadedFiles |
|
66 | - * @param array<array-key, mixed>|object|null $parsedBody |
|
67 | - * @param array<array-key, mixed> $attributes |
|
68 | - * |
|
69 | - * @throws InvalidArgumentException |
|
70 | - */ |
|
71 | - public function __construct( |
|
72 | - ?string $protocolVersion = null, |
|
73 | - ?string $method = null, |
|
74 | - $uri = null, |
|
75 | - ?array $headers = null, |
|
76 | - ?StreamInterface $body = null, |
|
77 | - array $serverParams = [], |
|
78 | - array $queryParams = [], |
|
79 | - array $cookieParams = [], |
|
80 | - array $uploadedFiles = [], |
|
81 | - $parsedBody = null, |
|
82 | - array $attributes = [] |
|
83 | - ) { |
|
84 | - parent::__construct($method, $uri, $headers, $body); |
|
85 | - |
|
86 | - if ($protocolVersion !== null) { |
|
87 | - $this->setProtocolVersion($protocolVersion); |
|
88 | - } |
|
89 | - |
|
90 | - if ($uploadedFiles !== []) { |
|
91 | - $this->setUploadedFiles($uploadedFiles); |
|
92 | - } |
|
93 | - |
|
94 | - if ($parsedBody !== null) { |
|
95 | - $this->setParsedBody($parsedBody); |
|
96 | - } |
|
97 | - |
|
98 | - $this->serverParams = $serverParams; |
|
99 | - $this->queryParams = $queryParams; |
|
100 | - $this->cookieParams = $cookieParams; |
|
101 | - $this->attributes = $attributes; |
|
102 | - } |
|
103 | - |
|
104 | - /** |
|
105 | - * {@inheritDoc} |
|
106 | - * |
|
107 | - * @return array<array-key, mixed> |
|
108 | - */ |
|
109 | - public function getServerParams(): array |
|
110 | - { |
|
111 | - return $this->serverParams; |
|
112 | - } |
|
113 | - |
|
114 | - /** |
|
115 | - * {@inheritDoc} |
|
116 | - * |
|
117 | - * @return array<array-key, mixed> |
|
118 | - */ |
|
119 | - public function getQueryParams(): array |
|
120 | - { |
|
121 | - return $this->queryParams; |
|
122 | - } |
|
123 | - |
|
124 | - /** |
|
125 | - * {@inheritDoc} |
|
126 | - * |
|
127 | - * @param array<array-key, mixed> $query |
|
128 | - * |
|
129 | - * @return static |
|
130 | - */ |
|
131 | - public function withQueryParams(array $query): ServerRequestInterface |
|
132 | - { |
|
133 | - $clone = clone $this; |
|
134 | - $clone->queryParams = $query; |
|
135 | - |
|
136 | - return $clone; |
|
137 | - } |
|
138 | - |
|
139 | - /** |
|
140 | - * {@inheritDoc} |
|
141 | - * |
|
142 | - * @return array<array-key, mixed> |
|
143 | - */ |
|
144 | - public function getCookieParams(): array |
|
145 | - { |
|
146 | - return $this->cookieParams; |
|
147 | - } |
|
148 | - |
|
149 | - /** |
|
150 | - * {@inheritDoc} |
|
151 | - * |
|
152 | - * @param array<array-key, mixed> $cookies |
|
153 | - * |
|
154 | - * @return static |
|
155 | - */ |
|
156 | - public function withCookieParams(array $cookies): ServerRequestInterface |
|
157 | - { |
|
158 | - $clone = clone $this; |
|
159 | - $clone->cookieParams = $cookies; |
|
160 | - |
|
161 | - return $clone; |
|
162 | - } |
|
163 | - |
|
164 | - /** |
|
165 | - * {@inheritDoc} |
|
166 | - * |
|
167 | - * @return array<array-key, mixed> |
|
168 | - */ |
|
169 | - public function getUploadedFiles(): array |
|
170 | - { |
|
171 | - return $this->uploadedFiles; |
|
172 | - } |
|
173 | - |
|
174 | - /** |
|
175 | - * {@inheritDoc} |
|
176 | - * |
|
177 | - * @param array<array-key, mixed> $uploadedFiles |
|
178 | - * |
|
179 | - * @return static |
|
180 | - * |
|
181 | - * @throws InvalidArgumentException |
|
182 | - */ |
|
183 | - public function withUploadedFiles(array $uploadedFiles): ServerRequestInterface |
|
184 | - { |
|
185 | - $clone = clone $this; |
|
186 | - $clone->setUploadedFiles($uploadedFiles); |
|
187 | - |
|
188 | - return $clone; |
|
189 | - } |
|
190 | - |
|
191 | - /** |
|
192 | - * {@inheritDoc} |
|
193 | - * |
|
194 | - * @return array<array-key, mixed>|object|null |
|
195 | - */ |
|
196 | - public function getParsedBody() |
|
197 | - { |
|
198 | - return $this->parsedBody; |
|
199 | - } |
|
200 | - |
|
201 | - /** |
|
202 | - * {@inheritDoc} |
|
203 | - * |
|
204 | - * @param array<array-key, mixed>|object|null $data |
|
205 | - * |
|
206 | - * @return static |
|
207 | - * |
|
208 | - * @throws InvalidArgumentException |
|
209 | - */ |
|
210 | - public function withParsedBody($data): ServerRequestInterface |
|
211 | - { |
|
212 | - $clone = clone $this; |
|
213 | - $clone->setParsedBody($data); |
|
214 | - |
|
215 | - return $clone; |
|
216 | - } |
|
217 | - |
|
218 | - /** |
|
219 | - * {@inheritDoc} |
|
220 | - * |
|
221 | - * @return array<array-key, mixed> |
|
222 | - */ |
|
223 | - public function getAttributes(): array |
|
224 | - { |
|
225 | - return $this->attributes; |
|
226 | - } |
|
227 | - |
|
228 | - /** |
|
229 | - * {@inheritDoc} |
|
230 | - * |
|
231 | - * @param array-key $name |
|
232 | - * @param mixed $default |
|
233 | - * |
|
234 | - * @return mixed |
|
235 | - */ |
|
236 | - public function getAttribute($name, $default = null) |
|
237 | - { |
|
238 | - if (!array_key_exists($name, $this->attributes)) { |
|
239 | - return $default; |
|
240 | - } |
|
241 | - |
|
242 | - return $this->attributes[$name]; |
|
243 | - } |
|
244 | - |
|
245 | - /** |
|
246 | - * {@inheritDoc} |
|
247 | - * |
|
248 | - * @param array-key $name |
|
249 | - * @param mixed $value |
|
250 | - * |
|
251 | - * @return static |
|
252 | - */ |
|
253 | - public function withAttribute($name, $value): ServerRequestInterface |
|
254 | - { |
|
255 | - $clone = clone $this; |
|
256 | - $clone->attributes[$name] = $value; |
|
257 | - |
|
258 | - return $clone; |
|
259 | - } |
|
260 | - |
|
261 | - /** |
|
262 | - * {@inheritDoc} |
|
263 | - * |
|
264 | - * @param array-key $name |
|
265 | - * |
|
266 | - * @return static |
|
267 | - */ |
|
268 | - public function withoutAttribute($name): ServerRequestInterface |
|
269 | - { |
|
270 | - $clone = clone $this; |
|
271 | - unset($clone->attributes[$name]); |
|
272 | - |
|
273 | - return $clone; |
|
274 | - } |
|
275 | - |
|
276 | - /** |
|
277 | - * Sets the given uploaded files to the request |
|
278 | - * |
|
279 | - * @param array<array-key, mixed> $files |
|
280 | - * |
|
281 | - * @throws InvalidArgumentException |
|
282 | - */ |
|
283 | - final protected function setUploadedFiles(array $files): void |
|
284 | - { |
|
285 | - $this->validateUploadedFiles($files); |
|
286 | - |
|
287 | - $this->uploadedFiles = $files; |
|
288 | - } |
|
289 | - |
|
290 | - /** |
|
291 | - * Sets the given parsed body to the request |
|
292 | - * |
|
293 | - * @param array<array-key, mixed>|object|null $data |
|
294 | - * |
|
295 | - * @throws InvalidArgumentException |
|
296 | - */ |
|
297 | - final protected function setParsedBody($data): void |
|
298 | - { |
|
299 | - $this->validateParsedBody($data); |
|
300 | - |
|
301 | - $this->parsedBody = $data; |
|
302 | - } |
|
303 | - |
|
304 | - /** |
|
305 | - * Validates the given uploaded files |
|
306 | - * |
|
307 | - * @param array<array-key, mixed> $files |
|
308 | - * |
|
309 | - * @throws InvalidArgumentException |
|
310 | - */ |
|
311 | - private function validateUploadedFiles(array $files): void |
|
312 | - { |
|
313 | - if ($files === []) { |
|
314 | - return; |
|
315 | - } |
|
316 | - |
|
317 | - array_walk_recursive($files, /** @param mixed $file */ static function ($file): void { |
|
318 | - if (!($file instanceof UploadedFileInterface)) { |
|
319 | - throw new InvalidArgumentException('Invalid uploaded file'); |
|
320 | - } |
|
321 | - }); |
|
322 | - } |
|
323 | - |
|
324 | - /** |
|
325 | - * Validates the given parsed body |
|
326 | - * |
|
327 | - * @param mixed $data |
|
328 | - * |
|
329 | - * @throws InvalidArgumentException |
|
330 | - */ |
|
331 | - private function validateParsedBody($data): void |
|
332 | - { |
|
333 | - if ($data === null || is_array($data) || is_object($data)) { |
|
334 | - return; |
|
335 | - } |
|
336 | - |
|
337 | - throw new InvalidArgumentException('Invalid parsed body'); |
|
338 | - } |
|
26 | + /** |
|
27 | + * @var array<array-key, mixed> |
|
28 | + */ |
|
29 | + private array $serverParams; |
|
30 | + |
|
31 | + /** |
|
32 | + * @var array<array-key, mixed> |
|
33 | + */ |
|
34 | + private array $queryParams; |
|
35 | + |
|
36 | + /** |
|
37 | + * @var array<array-key, mixed> |
|
38 | + */ |
|
39 | + private array $cookieParams; |
|
40 | + |
|
41 | + /** |
|
42 | + * @var array<array-key, mixed> |
|
43 | + */ |
|
44 | + private array $uploadedFiles = []; |
|
45 | + |
|
46 | + /** |
|
47 | + * @var array<array-key, mixed>|object|null |
|
48 | + */ |
|
49 | + private $parsedBody = null; |
|
50 | + |
|
51 | + /** |
|
52 | + * @var array<array-key, mixed> |
|
53 | + */ |
|
54 | + private array $attributes; |
|
55 | + |
|
56 | + /** |
|
57 | + * Constructor of the class |
|
58 | + * |
|
59 | + * @param mixed $uri |
|
60 | + * @param array<string, string|string[]>|null $headers |
|
61 | + * |
|
62 | + * @param array<array-key, mixed> $serverParams |
|
63 | + * @param array<array-key, mixed> $queryParams |
|
64 | + * @param array<array-key, mixed> $cookieParams |
|
65 | + * @param array<array-key, mixed> $uploadedFiles |
|
66 | + * @param array<array-key, mixed>|object|null $parsedBody |
|
67 | + * @param array<array-key, mixed> $attributes |
|
68 | + * |
|
69 | + * @throws InvalidArgumentException |
|
70 | + */ |
|
71 | + public function __construct( |
|
72 | + ?string $protocolVersion = null, |
|
73 | + ?string $method = null, |
|
74 | + $uri = null, |
|
75 | + ?array $headers = null, |
|
76 | + ?StreamInterface $body = null, |
|
77 | + array $serverParams = [], |
|
78 | + array $queryParams = [], |
|
79 | + array $cookieParams = [], |
|
80 | + array $uploadedFiles = [], |
|
81 | + $parsedBody = null, |
|
82 | + array $attributes = [] |
|
83 | + ) { |
|
84 | + parent::__construct($method, $uri, $headers, $body); |
|
85 | + |
|
86 | + if ($protocolVersion !== null) { |
|
87 | + $this->setProtocolVersion($protocolVersion); |
|
88 | + } |
|
89 | + |
|
90 | + if ($uploadedFiles !== []) { |
|
91 | + $this->setUploadedFiles($uploadedFiles); |
|
92 | + } |
|
93 | + |
|
94 | + if ($parsedBody !== null) { |
|
95 | + $this->setParsedBody($parsedBody); |
|
96 | + } |
|
97 | + |
|
98 | + $this->serverParams = $serverParams; |
|
99 | + $this->queryParams = $queryParams; |
|
100 | + $this->cookieParams = $cookieParams; |
|
101 | + $this->attributes = $attributes; |
|
102 | + } |
|
103 | + |
|
104 | + /** |
|
105 | + * {@inheritDoc} |
|
106 | + * |
|
107 | + * @return array<array-key, mixed> |
|
108 | + */ |
|
109 | + public function getServerParams(): array |
|
110 | + { |
|
111 | + return $this->serverParams; |
|
112 | + } |
|
113 | + |
|
114 | + /** |
|
115 | + * {@inheritDoc} |
|
116 | + * |
|
117 | + * @return array<array-key, mixed> |
|
118 | + */ |
|
119 | + public function getQueryParams(): array |
|
120 | + { |
|
121 | + return $this->queryParams; |
|
122 | + } |
|
123 | + |
|
124 | + /** |
|
125 | + * {@inheritDoc} |
|
126 | + * |
|
127 | + * @param array<array-key, mixed> $query |
|
128 | + * |
|
129 | + * @return static |
|
130 | + */ |
|
131 | + public function withQueryParams(array $query): ServerRequestInterface |
|
132 | + { |
|
133 | + $clone = clone $this; |
|
134 | + $clone->queryParams = $query; |
|
135 | + |
|
136 | + return $clone; |
|
137 | + } |
|
138 | + |
|
139 | + /** |
|
140 | + * {@inheritDoc} |
|
141 | + * |
|
142 | + * @return array<array-key, mixed> |
|
143 | + */ |
|
144 | + public function getCookieParams(): array |
|
145 | + { |
|
146 | + return $this->cookieParams; |
|
147 | + } |
|
148 | + |
|
149 | + /** |
|
150 | + * {@inheritDoc} |
|
151 | + * |
|
152 | + * @param array<array-key, mixed> $cookies |
|
153 | + * |
|
154 | + * @return static |
|
155 | + */ |
|
156 | + public function withCookieParams(array $cookies): ServerRequestInterface |
|
157 | + { |
|
158 | + $clone = clone $this; |
|
159 | + $clone->cookieParams = $cookies; |
|
160 | + |
|
161 | + return $clone; |
|
162 | + } |
|
163 | + |
|
164 | + /** |
|
165 | + * {@inheritDoc} |
|
166 | + * |
|
167 | + * @return array<array-key, mixed> |
|
168 | + */ |
|
169 | + public function getUploadedFiles(): array |
|
170 | + { |
|
171 | + return $this->uploadedFiles; |
|
172 | + } |
|
173 | + |
|
174 | + /** |
|
175 | + * {@inheritDoc} |
|
176 | + * |
|
177 | + * @param array<array-key, mixed> $uploadedFiles |
|
178 | + * |
|
179 | + * @return static |
|
180 | + * |
|
181 | + * @throws InvalidArgumentException |
|
182 | + */ |
|
183 | + public function withUploadedFiles(array $uploadedFiles): ServerRequestInterface |
|
184 | + { |
|
185 | + $clone = clone $this; |
|
186 | + $clone->setUploadedFiles($uploadedFiles); |
|
187 | + |
|
188 | + return $clone; |
|
189 | + } |
|
190 | + |
|
191 | + /** |
|
192 | + * {@inheritDoc} |
|
193 | + * |
|
194 | + * @return array<array-key, mixed>|object|null |
|
195 | + */ |
|
196 | + public function getParsedBody() |
|
197 | + { |
|
198 | + return $this->parsedBody; |
|
199 | + } |
|
200 | + |
|
201 | + /** |
|
202 | + * {@inheritDoc} |
|
203 | + * |
|
204 | + * @param array<array-key, mixed>|object|null $data |
|
205 | + * |
|
206 | + * @return static |
|
207 | + * |
|
208 | + * @throws InvalidArgumentException |
|
209 | + */ |
|
210 | + public function withParsedBody($data): ServerRequestInterface |
|
211 | + { |
|
212 | + $clone = clone $this; |
|
213 | + $clone->setParsedBody($data); |
|
214 | + |
|
215 | + return $clone; |
|
216 | + } |
|
217 | + |
|
218 | + /** |
|
219 | + * {@inheritDoc} |
|
220 | + * |
|
221 | + * @return array<array-key, mixed> |
|
222 | + */ |
|
223 | + public function getAttributes(): array |
|
224 | + { |
|
225 | + return $this->attributes; |
|
226 | + } |
|
227 | + |
|
228 | + /** |
|
229 | + * {@inheritDoc} |
|
230 | + * |
|
231 | + * @param array-key $name |
|
232 | + * @param mixed $default |
|
233 | + * |
|
234 | + * @return mixed |
|
235 | + */ |
|
236 | + public function getAttribute($name, $default = null) |
|
237 | + { |
|
238 | + if (!array_key_exists($name, $this->attributes)) { |
|
239 | + return $default; |
|
240 | + } |
|
241 | + |
|
242 | + return $this->attributes[$name]; |
|
243 | + } |
|
244 | + |
|
245 | + /** |
|
246 | + * {@inheritDoc} |
|
247 | + * |
|
248 | + * @param array-key $name |
|
249 | + * @param mixed $value |
|
250 | + * |
|
251 | + * @return static |
|
252 | + */ |
|
253 | + public function withAttribute($name, $value): ServerRequestInterface |
|
254 | + { |
|
255 | + $clone = clone $this; |
|
256 | + $clone->attributes[$name] = $value; |
|
257 | + |
|
258 | + return $clone; |
|
259 | + } |
|
260 | + |
|
261 | + /** |
|
262 | + * {@inheritDoc} |
|
263 | + * |
|
264 | + * @param array-key $name |
|
265 | + * |
|
266 | + * @return static |
|
267 | + */ |
|
268 | + public function withoutAttribute($name): ServerRequestInterface |
|
269 | + { |
|
270 | + $clone = clone $this; |
|
271 | + unset($clone->attributes[$name]); |
|
272 | + |
|
273 | + return $clone; |
|
274 | + } |
|
275 | + |
|
276 | + /** |
|
277 | + * Sets the given uploaded files to the request |
|
278 | + * |
|
279 | + * @param array<array-key, mixed> $files |
|
280 | + * |
|
281 | + * @throws InvalidArgumentException |
|
282 | + */ |
|
283 | + final protected function setUploadedFiles(array $files): void |
|
284 | + { |
|
285 | + $this->validateUploadedFiles($files); |
|
286 | + |
|
287 | + $this->uploadedFiles = $files; |
|
288 | + } |
|
289 | + |
|
290 | + /** |
|
291 | + * Sets the given parsed body to the request |
|
292 | + * |
|
293 | + * @param array<array-key, mixed>|object|null $data |
|
294 | + * |
|
295 | + * @throws InvalidArgumentException |
|
296 | + */ |
|
297 | + final protected function setParsedBody($data): void |
|
298 | + { |
|
299 | + $this->validateParsedBody($data); |
|
300 | + |
|
301 | + $this->parsedBody = $data; |
|
302 | + } |
|
303 | + |
|
304 | + /** |
|
305 | + * Validates the given uploaded files |
|
306 | + * |
|
307 | + * @param array<array-key, mixed> $files |
|
308 | + * |
|
309 | + * @throws InvalidArgumentException |
|
310 | + */ |
|
311 | + private function validateUploadedFiles(array $files): void |
|
312 | + { |
|
313 | + if ($files === []) { |
|
314 | + return; |
|
315 | + } |
|
316 | + |
|
317 | + array_walk_recursive($files, /** @param mixed $file */ static function ($file): void { |
|
318 | + if (!($file instanceof UploadedFileInterface)) { |
|
319 | + throw new InvalidArgumentException('Invalid uploaded file'); |
|
320 | + } |
|
321 | + }); |
|
322 | + } |
|
323 | + |
|
324 | + /** |
|
325 | + * Validates the given parsed body |
|
326 | + * |
|
327 | + * @param mixed $data |
|
328 | + * |
|
329 | + * @throws InvalidArgumentException |
|
330 | + */ |
|
331 | + private function validateParsedBody($data): void |
|
332 | + { |
|
333 | + if ($data === null || is_array($data) || is_object($data)) { |
|
334 | + return; |
|
335 | + } |
|
336 | + |
|
337 | + throw new InvalidArgumentException('Invalid parsed body'); |
|
338 | + } |
|
339 | 339 | } |
@@ -314,7 +314,7 @@ |
||
314 | 314 | return; |
315 | 315 | } |
316 | 316 | |
317 | - array_walk_recursive($files, /** @param mixed $file */ static function ($file): void { |
|
317 | + array_walk_recursive($files, /** @param mixed $file */ static function($file): void { |
|
318 | 318 | if (!($file instanceof UploadedFileInterface)) { |
319 | 319 | throw new InvalidArgumentException('Invalid uploaded file'); |
320 | 320 | } |
@@ -18,10 +18,10 @@ |
||
18 | 18 | use function DI\create; |
19 | 19 | |
20 | 20 | return [ |
21 | - RequestFactoryInterface::class => create(RequestFactory::class), |
|
22 | - ResponseFactoryInterface::class => create(ResponseFactory::class), |
|
23 | - ServerRequestFactoryInterface::class => create(ServerRequestFactory::class), |
|
24 | - StreamFactoryInterface::class => create(StreamFactory::class), |
|
25 | - UploadedFileFactoryInterface::class => create(UploadedFileFactory::class), |
|
26 | - UriFactoryInterface::class => create(UriFactory::class), |
|
21 | + RequestFactoryInterface::class => create(RequestFactory::class), |
|
22 | + ResponseFactoryInterface::class => create(ResponseFactory::class), |
|
23 | + ServerRequestFactoryInterface::class => create(ServerRequestFactory::class), |
|
24 | + StreamFactoryInterface::class => create(StreamFactory::class), |
|
25 | + UploadedFileFactoryInterface::class => create(UploadedFileFactory::class), |
|
26 | + UriFactoryInterface::class => create(UriFactory::class), |
|
27 | 27 | ]; |
@@ -40,140 +40,140 @@ |
||
40 | 40 | |
41 | 41 | class UploadedFile implements UploadedFileInterface |
42 | 42 | { |
43 | - /** |
|
44 | - * @link https://www.php.net/manual/en/features.file-upload.errors.php |
|
45 | - * |
|
46 | - * @var array<int, non-empty-string> |
|
47 | - */ |
|
48 | - public const UPLOAD_ERRORS = [ |
|
49 | - UPLOAD_ERR_OK => 'No error', |
|
50 | - UPLOAD_ERR_INI_SIZE => 'Uploaded file exceeds the upload_max_filesize directive in the php.ini', |
|
51 | - UPLOAD_ERR_FORM_SIZE => 'Uploaded file exceeds the MAX_FILE_SIZE directive in the HTML form', |
|
52 | - UPLOAD_ERR_PARTIAL => 'Uploaded file was only partially uploaded', |
|
53 | - UPLOAD_ERR_NO_FILE => 'No file was uploaded', |
|
54 | - UPLOAD_ERR_NO_TMP_DIR => 'Missing temporary directory', |
|
55 | - UPLOAD_ERR_CANT_WRITE => 'Failed to write file to disk', |
|
56 | - UPLOAD_ERR_EXTENSION => 'File upload was stopped by a PHP extension', |
|
57 | - ]; |
|
58 | - |
|
59 | - private ?StreamInterface $stream; |
|
60 | - private ?int $size; |
|
61 | - private int $errorCode; |
|
62 | - private string $errorMessage; |
|
63 | - private ?string $clientFilename; |
|
64 | - private ?string $clientMediaType; |
|
65 | - private bool $isMoved = false; |
|
66 | - |
|
67 | - public function __construct( |
|
68 | - ?StreamInterface $stream, |
|
69 | - ?int $size = null, |
|
70 | - int $error = UPLOAD_ERR_OK, |
|
71 | - ?string $clientFilename = null, |
|
72 | - ?string $clientMediaType = null |
|
73 | - ) { |
|
74 | - $this->stream = $stream; |
|
75 | - $this->size = $size; |
|
76 | - $this->errorCode = $error; |
|
77 | - $this->errorMessage = self::UPLOAD_ERRORS[$error] ?? 'Unknown file upload error'; |
|
78 | - $this->clientFilename = $clientFilename; |
|
79 | - $this->clientMediaType = $clientMediaType; |
|
80 | - } |
|
81 | - |
|
82 | - /** |
|
83 | - * @inheritDoc |
|
84 | - */ |
|
85 | - public function getStream(): StreamInterface |
|
86 | - { |
|
87 | - if ($this->isMoved) { |
|
88 | - throw new RuntimeException('Uploaded file was moved'); |
|
89 | - } |
|
90 | - |
|
91 | - if ($this->errorCode !== UPLOAD_ERR_OK) { |
|
92 | - throw new RuntimeException($this->errorMessage, $this->errorCode); |
|
93 | - } |
|
94 | - |
|
95 | - if ($this->stream === null) { |
|
96 | - throw new RuntimeException('Uploaded file has no stream'); |
|
97 | - } |
|
98 | - |
|
99 | - return $this->stream; |
|
100 | - } |
|
101 | - |
|
102 | - /** |
|
103 | - * @inheritDoc |
|
104 | - */ |
|
105 | - public function moveTo($targetPath): void |
|
106 | - { |
|
107 | - /** @psalm-suppress TypeDoesNotContainType */ |
|
108 | - // @phpstan-ignore function.alreadyNarrowedType |
|
109 | - if (!is_string($targetPath)) { |
|
110 | - throw new TypeError(sprintf( |
|
111 | - 'Argument #1 ($targetPath) must be of type string, %s given', |
|
112 | - gettype($targetPath), |
|
113 | - )); |
|
114 | - } |
|
115 | - |
|
116 | - $sourceStream = $this->getStream(); |
|
117 | - |
|
118 | - $sourcePath = $sourceStream->getMetadata('uri'); |
|
119 | - if (!is_string($sourcePath) || !is_file($sourcePath) || !is_readable($sourcePath)) { |
|
120 | - throw new RuntimeException('Uploaded file does not exist or is not readable'); |
|
121 | - } |
|
122 | - |
|
123 | - $sourceDirname = dirname($sourcePath); |
|
124 | - if (!is_writable($sourceDirname)) { |
|
125 | - throw new RuntimeException('To move the uploaded file, the source directory must be writable'); |
|
126 | - } |
|
127 | - |
|
128 | - $targetDirname = dirname($targetPath); |
|
129 | - if (!is_dir($targetDirname) || !is_writable($targetDirname)) { |
|
130 | - throw new RuntimeException('To move the uploaded file, the target directory must exist and be writable'); |
|
131 | - } |
|
132 | - |
|
133 | - try { |
|
134 | - $this->isMoved = is_uploaded_file($sourcePath) |
|
135 | - ? move_uploaded_file($sourcePath, $targetPath) |
|
136 | - : rename($sourcePath, $targetPath); |
|
137 | - } catch (Throwable $e) { |
|
138 | - } |
|
139 | - |
|
140 | - if (!$this->isMoved) { |
|
141 | - throw new RuntimeException('Failed to move the uploaded file'); |
|
142 | - } |
|
143 | - |
|
144 | - $sourceStream->close(); |
|
145 | - $this->stream = null; |
|
146 | - } |
|
147 | - |
|
148 | - /** |
|
149 | - * @inheritDoc |
|
150 | - */ |
|
151 | - public function getSize(): ?int |
|
152 | - { |
|
153 | - return $this->size; |
|
154 | - } |
|
155 | - |
|
156 | - /** |
|
157 | - * @inheritDoc |
|
158 | - */ |
|
159 | - public function getError(): int |
|
160 | - { |
|
161 | - return $this->errorCode; |
|
162 | - } |
|
163 | - |
|
164 | - /** |
|
165 | - * @inheritDoc |
|
166 | - */ |
|
167 | - public function getClientFilename(): ?string |
|
168 | - { |
|
169 | - return $this->clientFilename; |
|
170 | - } |
|
171 | - |
|
172 | - /** |
|
173 | - * @inheritDoc |
|
174 | - */ |
|
175 | - public function getClientMediaType(): ?string |
|
176 | - { |
|
177 | - return $this->clientMediaType; |
|
178 | - } |
|
43 | + /** |
|
44 | + * @link https://www.php.net/manual/en/features.file-upload.errors.php |
|
45 | + * |
|
46 | + * @var array<int, non-empty-string> |
|
47 | + */ |
|
48 | + public const UPLOAD_ERRORS = [ |
|
49 | + UPLOAD_ERR_OK => 'No error', |
|
50 | + UPLOAD_ERR_INI_SIZE => 'Uploaded file exceeds the upload_max_filesize directive in the php.ini', |
|
51 | + UPLOAD_ERR_FORM_SIZE => 'Uploaded file exceeds the MAX_FILE_SIZE directive in the HTML form', |
|
52 | + UPLOAD_ERR_PARTIAL => 'Uploaded file was only partially uploaded', |
|
53 | + UPLOAD_ERR_NO_FILE => 'No file was uploaded', |
|
54 | + UPLOAD_ERR_NO_TMP_DIR => 'Missing temporary directory', |
|
55 | + UPLOAD_ERR_CANT_WRITE => 'Failed to write file to disk', |
|
56 | + UPLOAD_ERR_EXTENSION => 'File upload was stopped by a PHP extension', |
|
57 | + ]; |
|
58 | + |
|
59 | + private ?StreamInterface $stream; |
|
60 | + private ?int $size; |
|
61 | + private int $errorCode; |
|
62 | + private string $errorMessage; |
|
63 | + private ?string $clientFilename; |
|
64 | + private ?string $clientMediaType; |
|
65 | + private bool $isMoved = false; |
|
66 | + |
|
67 | + public function __construct( |
|
68 | + ?StreamInterface $stream, |
|
69 | + ?int $size = null, |
|
70 | + int $error = UPLOAD_ERR_OK, |
|
71 | + ?string $clientFilename = null, |
|
72 | + ?string $clientMediaType = null |
|
73 | + ) { |
|
74 | + $this->stream = $stream; |
|
75 | + $this->size = $size; |
|
76 | + $this->errorCode = $error; |
|
77 | + $this->errorMessage = self::UPLOAD_ERRORS[$error] ?? 'Unknown file upload error'; |
|
78 | + $this->clientFilename = $clientFilename; |
|
79 | + $this->clientMediaType = $clientMediaType; |
|
80 | + } |
|
81 | + |
|
82 | + /** |
|
83 | + * @inheritDoc |
|
84 | + */ |
|
85 | + public function getStream(): StreamInterface |
|
86 | + { |
|
87 | + if ($this->isMoved) { |
|
88 | + throw new RuntimeException('Uploaded file was moved'); |
|
89 | + } |
|
90 | + |
|
91 | + if ($this->errorCode !== UPLOAD_ERR_OK) { |
|
92 | + throw new RuntimeException($this->errorMessage, $this->errorCode); |
|
93 | + } |
|
94 | + |
|
95 | + if ($this->stream === null) { |
|
96 | + throw new RuntimeException('Uploaded file has no stream'); |
|
97 | + } |
|
98 | + |
|
99 | + return $this->stream; |
|
100 | + } |
|
101 | + |
|
102 | + /** |
|
103 | + * @inheritDoc |
|
104 | + */ |
|
105 | + public function moveTo($targetPath): void |
|
106 | + { |
|
107 | + /** @psalm-suppress TypeDoesNotContainType */ |
|
108 | + // @phpstan-ignore function.alreadyNarrowedType |
|
109 | + if (!is_string($targetPath)) { |
|
110 | + throw new TypeError(sprintf( |
|
111 | + 'Argument #1 ($targetPath) must be of type string, %s given', |
|
112 | + gettype($targetPath), |
|
113 | + )); |
|
114 | + } |
|
115 | + |
|
116 | + $sourceStream = $this->getStream(); |
|
117 | + |
|
118 | + $sourcePath = $sourceStream->getMetadata('uri'); |
|
119 | + if (!is_string($sourcePath) || !is_file($sourcePath) || !is_readable($sourcePath)) { |
|
120 | + throw new RuntimeException('Uploaded file does not exist or is not readable'); |
|
121 | + } |
|
122 | + |
|
123 | + $sourceDirname = dirname($sourcePath); |
|
124 | + if (!is_writable($sourceDirname)) { |
|
125 | + throw new RuntimeException('To move the uploaded file, the source directory must be writable'); |
|
126 | + } |
|
127 | + |
|
128 | + $targetDirname = dirname($targetPath); |
|
129 | + if (!is_dir($targetDirname) || !is_writable($targetDirname)) { |
|
130 | + throw new RuntimeException('To move the uploaded file, the target directory must exist and be writable'); |
|
131 | + } |
|
132 | + |
|
133 | + try { |
|
134 | + $this->isMoved = is_uploaded_file($sourcePath) |
|
135 | + ? move_uploaded_file($sourcePath, $targetPath) |
|
136 | + : rename($sourcePath, $targetPath); |
|
137 | + } catch (Throwable $e) { |
|
138 | + } |
|
139 | + |
|
140 | + if (!$this->isMoved) { |
|
141 | + throw new RuntimeException('Failed to move the uploaded file'); |
|
142 | + } |
|
143 | + |
|
144 | + $sourceStream->close(); |
|
145 | + $this->stream = null; |
|
146 | + } |
|
147 | + |
|
148 | + /** |
|
149 | + * @inheritDoc |
|
150 | + */ |
|
151 | + public function getSize(): ?int |
|
152 | + { |
|
153 | + return $this->size; |
|
154 | + } |
|
155 | + |
|
156 | + /** |
|
157 | + * @inheritDoc |
|
158 | + */ |
|
159 | + public function getError(): int |
|
160 | + { |
|
161 | + return $this->errorCode; |
|
162 | + } |
|
163 | + |
|
164 | + /** |
|
165 | + * @inheritDoc |
|
166 | + */ |
|
167 | + public function getClientFilename(): ?string |
|
168 | + { |
|
169 | + return $this->clientFilename; |
|
170 | + } |
|
171 | + |
|
172 | + /** |
|
173 | + * @inheritDoc |
|
174 | + */ |
|
175 | + public function getClientMediaType(): ?string |
|
176 | + { |
|
177 | + return $this->clientMediaType; |
|
178 | + } |
|
179 | 179 | } |
@@ -24,48 +24,48 @@ |
||
24 | 24 | |
25 | 25 | final class JsonResponse extends Response |
26 | 26 | { |
27 | - /** |
|
28 | - * @param mixed $data |
|
29 | - * @param int<1, max> $depth |
|
30 | - * @psalm-param int<1, 2147483647> $depth |
|
31 | - * |
|
32 | - * @throws InvalidArgumentException |
|
33 | - */ |
|
34 | - public function __construct(int $statusCode, $data, int $flags = 0, int $depth = 512) |
|
35 | - { |
|
36 | - parent::__construct($statusCode); |
|
27 | + /** |
|
28 | + * @param mixed $data |
|
29 | + * @param int<1, max> $depth |
|
30 | + * @psalm-param int<1, 2147483647> $depth |
|
31 | + * |
|
32 | + * @throws InvalidArgumentException |
|
33 | + */ |
|
34 | + public function __construct(int $statusCode, $data, int $flags = 0, int $depth = 512) |
|
35 | + { |
|
36 | + parent::__construct($statusCode); |
|
37 | 37 | |
38 | - $this->setBody(self::createBody($data, $flags, $depth)); |
|
39 | - $this->setHeader('Content-Type', 'application/json; charset=utf-8'); |
|
40 | - } |
|
38 | + $this->setBody(self::createBody($data, $flags, $depth)); |
|
39 | + $this->setHeader('Content-Type', 'application/json; charset=utf-8'); |
|
40 | + } |
|
41 | 41 | |
42 | - /** |
|
43 | - * @param mixed $data |
|
44 | - * @param int<1, max> $depth |
|
45 | - * @psalm-param int<1, 2147483647> $depth |
|
46 | - * |
|
47 | - * @throws InvalidArgumentException |
|
48 | - */ |
|
49 | - private static function createBody($data, int $flags, int $depth): StreamInterface |
|
50 | - { |
|
51 | - if ($data instanceof StreamInterface) { |
|
52 | - return $data; |
|
53 | - } |
|
42 | + /** |
|
43 | + * @param mixed $data |
|
44 | + * @param int<1, max> $depth |
|
45 | + * @psalm-param int<1, 2147483647> $depth |
|
46 | + * |
|
47 | + * @throws InvalidArgumentException |
|
48 | + */ |
|
49 | + private static function createBody($data, int $flags, int $depth): StreamInterface |
|
50 | + { |
|
51 | + if ($data instanceof StreamInterface) { |
|
52 | + return $data; |
|
53 | + } |
|
54 | 54 | |
55 | - try { |
|
56 | - /** @var non-empty-string $json */ |
|
57 | - $json = json_encode($data, $flags | JSON_THROW_ON_ERROR, $depth); |
|
58 | - } catch (JsonException $e) { |
|
59 | - throw new InvalidArgumentException(sprintf( |
|
60 | - 'Unable to create the JSON response due to an invalid data: %s', |
|
61 | - $e->getMessage() |
|
62 | - ), 0, $e); |
|
63 | - } |
|
55 | + try { |
|
56 | + /** @var non-empty-string $json */ |
|
57 | + $json = json_encode($data, $flags | JSON_THROW_ON_ERROR, $depth); |
|
58 | + } catch (JsonException $e) { |
|
59 | + throw new InvalidArgumentException(sprintf( |
|
60 | + 'Unable to create the JSON response due to an invalid data: %s', |
|
61 | + $e->getMessage() |
|
62 | + ), 0, $e); |
|
63 | + } |
|
64 | 64 | |
65 | - $stream = new PhpTempStream('r+b'); |
|
66 | - $stream->write($json); |
|
67 | - $stream->rewind(); |
|
65 | + $stream = new PhpTempStream('r+b'); |
|
66 | + $stream->write($json); |
|
67 | + $stream->rewind(); |
|
68 | 68 | |
69 | - return $stream; |
|
70 | - } |
|
69 | + return $stream; |
|
70 | + } |
|
71 | 71 | } |
@@ -32,289 +32,289 @@ |
||
32 | 32 | |
33 | 33 | class Stream implements StreamInterface |
34 | 34 | { |
35 | - /** |
|
36 | - * @var resource|null |
|
37 | - */ |
|
38 | - private $resource; |
|
39 | - |
|
40 | - private bool $autoClose; |
|
41 | - |
|
42 | - /** |
|
43 | - * @param mixed $resource |
|
44 | - * |
|
45 | - * @throws InvalidArgumentException |
|
46 | - */ |
|
47 | - public function __construct($resource, bool $autoClose = true) |
|
48 | - { |
|
49 | - if (!is_resource($resource)) { |
|
50 | - throw new InvalidArgumentException('Unexpected stream resource'); |
|
51 | - } |
|
52 | - |
|
53 | - $this->resource = $resource; |
|
54 | - $this->autoClose = $autoClose; |
|
55 | - } |
|
56 | - |
|
57 | - /** |
|
58 | - * @param mixed $resource |
|
59 | - * |
|
60 | - * @throws InvalidArgumentException |
|
61 | - */ |
|
62 | - public static function create($resource): StreamInterface |
|
63 | - { |
|
64 | - if ($resource instanceof StreamInterface) { |
|
65 | - return $resource; |
|
66 | - } |
|
67 | - |
|
68 | - return new self($resource); |
|
69 | - } |
|
70 | - |
|
71 | - public function __destruct() |
|
72 | - { |
|
73 | - if ($this->autoClose) { |
|
74 | - $this->close(); |
|
75 | - } |
|
76 | - } |
|
77 | - |
|
78 | - /** |
|
79 | - * @inheritDoc |
|
80 | - */ |
|
81 | - public function detach() |
|
82 | - { |
|
83 | - $resource = $this->resource; |
|
84 | - $this->resource = null; |
|
85 | - |
|
86 | - return $resource; |
|
87 | - } |
|
88 | - |
|
89 | - /** |
|
90 | - * @inheritDoc |
|
91 | - */ |
|
92 | - public function close(): void |
|
93 | - { |
|
94 | - $resource = $this->detach(); |
|
95 | - if (!is_resource($resource)) { |
|
96 | - return; |
|
97 | - } |
|
98 | - |
|
99 | - fclose($resource); |
|
100 | - } |
|
101 | - |
|
102 | - /** |
|
103 | - * @inheritDoc |
|
104 | - */ |
|
105 | - public function eof(): bool |
|
106 | - { |
|
107 | - if (!is_resource($this->resource)) { |
|
108 | - return true; |
|
109 | - } |
|
110 | - |
|
111 | - return feof($this->resource); |
|
112 | - } |
|
113 | - |
|
114 | - /** |
|
115 | - * @inheritDoc |
|
116 | - */ |
|
117 | - public function tell(): int |
|
118 | - { |
|
119 | - if (!is_resource($this->resource)) { |
|
120 | - throw new RuntimeException('Stream has no resource'); |
|
121 | - } |
|
122 | - |
|
123 | - $result = ftell($this->resource); |
|
124 | - if ($result === false) { |
|
125 | - throw new RuntimeException('Unable to get the stream pointer position'); |
|
126 | - } |
|
127 | - |
|
128 | - return $result; |
|
129 | - } |
|
130 | - |
|
131 | - /** |
|
132 | - * @inheritDoc |
|
133 | - */ |
|
134 | - public function isSeekable(): bool |
|
135 | - { |
|
136 | - if (!is_resource($this->resource)) { |
|
137 | - return false; |
|
138 | - } |
|
139 | - |
|
140 | - $metadata = stream_get_meta_data($this->resource); |
|
141 | - |
|
142 | - return $metadata['seekable']; |
|
143 | - } |
|
144 | - |
|
145 | - /** |
|
146 | - * @inheritDoc |
|
147 | - */ |
|
148 | - public function rewind(): void |
|
149 | - { |
|
150 | - $this->seek(0); |
|
151 | - } |
|
152 | - |
|
153 | - /** |
|
154 | - * @inheritDoc |
|
155 | - */ |
|
156 | - public function seek($offset, $whence = SEEK_SET): void |
|
157 | - { |
|
158 | - if (!is_resource($this->resource)) { |
|
159 | - throw new RuntimeException('Stream has no resource'); |
|
160 | - } |
|
161 | - |
|
162 | - if (!$this->isSeekable()) { |
|
163 | - throw new RuntimeException('Stream is not seekable'); |
|
164 | - } |
|
165 | - |
|
166 | - $result = fseek($this->resource, $offset, $whence); |
|
167 | - if ($result !== 0) { |
|
168 | - throw new RuntimeException('Unable to move the stream pointer position'); |
|
169 | - } |
|
170 | - } |
|
171 | - |
|
172 | - /** |
|
173 | - * @inheritDoc |
|
174 | - */ |
|
175 | - public function isWritable(): bool |
|
176 | - { |
|
177 | - if (!is_resource($this->resource)) { |
|
178 | - return false; |
|
179 | - } |
|
180 | - |
|
181 | - $metadata = stream_get_meta_data($this->resource); |
|
182 | - |
|
183 | - return strpbrk($metadata['mode'], '+acwx') !== false; |
|
184 | - } |
|
185 | - |
|
186 | - /** |
|
187 | - * @inheritDoc |
|
188 | - */ |
|
189 | - public function write($string): int |
|
190 | - { |
|
191 | - if (!is_resource($this->resource)) { |
|
192 | - throw new RuntimeException('Stream has no resource'); |
|
193 | - } |
|
194 | - |
|
195 | - if (!$this->isWritable()) { |
|
196 | - throw new RuntimeException('Stream is not writable'); |
|
197 | - } |
|
198 | - |
|
199 | - $result = fwrite($this->resource, $string); |
|
200 | - if ($result === false) { |
|
201 | - throw new RuntimeException('Unable to write to the stream'); |
|
202 | - } |
|
203 | - |
|
204 | - return $result; |
|
205 | - } |
|
206 | - |
|
207 | - /** |
|
208 | - * @inheritDoc |
|
209 | - */ |
|
210 | - public function isReadable(): bool |
|
211 | - { |
|
212 | - if (!is_resource($this->resource)) { |
|
213 | - return false; |
|
214 | - } |
|
215 | - |
|
216 | - $metadata = stream_get_meta_data($this->resource); |
|
217 | - |
|
218 | - return strpbrk($metadata['mode'], '+r') !== false; |
|
219 | - } |
|
220 | - |
|
221 | - /** |
|
222 | - * @inheritDoc |
|
223 | - * |
|
224 | - * @psalm-param int $length |
|
225 | - * @phpstan-param int<1, max> $length |
|
226 | - */ |
|
227 | - public function read($length): string |
|
228 | - { |
|
229 | - if (!is_resource($this->resource)) { |
|
230 | - throw new RuntimeException('Stream has no resource'); |
|
231 | - } |
|
232 | - |
|
233 | - if (!$this->isReadable()) { |
|
234 | - throw new RuntimeException('Stream is not readable'); |
|
235 | - } |
|
236 | - |
|
237 | - $result = fread($this->resource, $length); |
|
238 | - if ($result === false) { |
|
239 | - throw new RuntimeException('Unable to read from the stream'); |
|
240 | - } |
|
241 | - |
|
242 | - return $result; |
|
243 | - } |
|
244 | - |
|
245 | - /** |
|
246 | - * @inheritDoc |
|
247 | - */ |
|
248 | - public function getContents(): string |
|
249 | - { |
|
250 | - if (!is_resource($this->resource)) { |
|
251 | - throw new RuntimeException('Stream has no resource'); |
|
252 | - } |
|
253 | - |
|
254 | - if (!$this->isReadable()) { |
|
255 | - throw new RuntimeException('Stream is not readable'); |
|
256 | - } |
|
257 | - |
|
258 | - $result = stream_get_contents($this->resource); |
|
259 | - if ($result === false) { |
|
260 | - throw new RuntimeException('Unable to read the remainder of the stream'); |
|
261 | - } |
|
262 | - |
|
263 | - return $result; |
|
264 | - } |
|
265 | - |
|
266 | - /** |
|
267 | - * @inheritDoc |
|
268 | - */ |
|
269 | - public function getMetadata($key = null) |
|
270 | - { |
|
271 | - if (!is_resource($this->resource)) { |
|
272 | - return null; |
|
273 | - } |
|
274 | - |
|
275 | - $metadata = stream_get_meta_data($this->resource); |
|
276 | - if ($key === null) { |
|
277 | - return $metadata; |
|
278 | - } |
|
279 | - |
|
280 | - return $metadata[$key] ?? null; |
|
281 | - } |
|
282 | - |
|
283 | - /** |
|
284 | - * @inheritDoc |
|
285 | - */ |
|
286 | - public function getSize(): ?int |
|
287 | - { |
|
288 | - if (!is_resource($this->resource)) { |
|
289 | - return null; |
|
290 | - } |
|
291 | - |
|
292 | - /** @var array{size: int}|false */ |
|
293 | - $stats = fstat($this->resource); |
|
294 | - if ($stats === false) { |
|
295 | - return null; |
|
296 | - } |
|
297 | - |
|
298 | - return $stats['size']; |
|
299 | - } |
|
300 | - |
|
301 | - /** |
|
302 | - * @inheritDoc |
|
303 | - */ |
|
304 | - public function __toString(): string |
|
305 | - { |
|
306 | - if (!$this->isReadable()) { |
|
307 | - return ''; |
|
308 | - } |
|
309 | - |
|
310 | - try { |
|
311 | - if ($this->isSeekable()) { |
|
312 | - $this->rewind(); |
|
313 | - } |
|
314 | - |
|
315 | - return $this->getContents(); |
|
316 | - } catch (Throwable $e) { |
|
317 | - return ''; |
|
318 | - } |
|
319 | - } |
|
35 | + /** |
|
36 | + * @var resource|null |
|
37 | + */ |
|
38 | + private $resource; |
|
39 | + |
|
40 | + private bool $autoClose; |
|
41 | + |
|
42 | + /** |
|
43 | + * @param mixed $resource |
|
44 | + * |
|
45 | + * @throws InvalidArgumentException |
|
46 | + */ |
|
47 | + public function __construct($resource, bool $autoClose = true) |
|
48 | + { |
|
49 | + if (!is_resource($resource)) { |
|
50 | + throw new InvalidArgumentException('Unexpected stream resource'); |
|
51 | + } |
|
52 | + |
|
53 | + $this->resource = $resource; |
|
54 | + $this->autoClose = $autoClose; |
|
55 | + } |
|
56 | + |
|
57 | + /** |
|
58 | + * @param mixed $resource |
|
59 | + * |
|
60 | + * @throws InvalidArgumentException |
|
61 | + */ |
|
62 | + public static function create($resource): StreamInterface |
|
63 | + { |
|
64 | + if ($resource instanceof StreamInterface) { |
|
65 | + return $resource; |
|
66 | + } |
|
67 | + |
|
68 | + return new self($resource); |
|
69 | + } |
|
70 | + |
|
71 | + public function __destruct() |
|
72 | + { |
|
73 | + if ($this->autoClose) { |
|
74 | + $this->close(); |
|
75 | + } |
|
76 | + } |
|
77 | + |
|
78 | + /** |
|
79 | + * @inheritDoc |
|
80 | + */ |
|
81 | + public function detach() |
|
82 | + { |
|
83 | + $resource = $this->resource; |
|
84 | + $this->resource = null; |
|
85 | + |
|
86 | + return $resource; |
|
87 | + } |
|
88 | + |
|
89 | + /** |
|
90 | + * @inheritDoc |
|
91 | + */ |
|
92 | + public function close(): void |
|
93 | + { |
|
94 | + $resource = $this->detach(); |
|
95 | + if (!is_resource($resource)) { |
|
96 | + return; |
|
97 | + } |
|
98 | + |
|
99 | + fclose($resource); |
|
100 | + } |
|
101 | + |
|
102 | + /** |
|
103 | + * @inheritDoc |
|
104 | + */ |
|
105 | + public function eof(): bool |
|
106 | + { |
|
107 | + if (!is_resource($this->resource)) { |
|
108 | + return true; |
|
109 | + } |
|
110 | + |
|
111 | + return feof($this->resource); |
|
112 | + } |
|
113 | + |
|
114 | + /** |
|
115 | + * @inheritDoc |
|
116 | + */ |
|
117 | + public function tell(): int |
|
118 | + { |
|
119 | + if (!is_resource($this->resource)) { |
|
120 | + throw new RuntimeException('Stream has no resource'); |
|
121 | + } |
|
122 | + |
|
123 | + $result = ftell($this->resource); |
|
124 | + if ($result === false) { |
|
125 | + throw new RuntimeException('Unable to get the stream pointer position'); |
|
126 | + } |
|
127 | + |
|
128 | + return $result; |
|
129 | + } |
|
130 | + |
|
131 | + /** |
|
132 | + * @inheritDoc |
|
133 | + */ |
|
134 | + public function isSeekable(): bool |
|
135 | + { |
|
136 | + if (!is_resource($this->resource)) { |
|
137 | + return false; |
|
138 | + } |
|
139 | + |
|
140 | + $metadata = stream_get_meta_data($this->resource); |
|
141 | + |
|
142 | + return $metadata['seekable']; |
|
143 | + } |
|
144 | + |
|
145 | + /** |
|
146 | + * @inheritDoc |
|
147 | + */ |
|
148 | + public function rewind(): void |
|
149 | + { |
|
150 | + $this->seek(0); |
|
151 | + } |
|
152 | + |
|
153 | + /** |
|
154 | + * @inheritDoc |
|
155 | + */ |
|
156 | + public function seek($offset, $whence = SEEK_SET): void |
|
157 | + { |
|
158 | + if (!is_resource($this->resource)) { |
|
159 | + throw new RuntimeException('Stream has no resource'); |
|
160 | + } |
|
161 | + |
|
162 | + if (!$this->isSeekable()) { |
|
163 | + throw new RuntimeException('Stream is not seekable'); |
|
164 | + } |
|
165 | + |
|
166 | + $result = fseek($this->resource, $offset, $whence); |
|
167 | + if ($result !== 0) { |
|
168 | + throw new RuntimeException('Unable to move the stream pointer position'); |
|
169 | + } |
|
170 | + } |
|
171 | + |
|
172 | + /** |
|
173 | + * @inheritDoc |
|
174 | + */ |
|
175 | + public function isWritable(): bool |
|
176 | + { |
|
177 | + if (!is_resource($this->resource)) { |
|
178 | + return false; |
|
179 | + } |
|
180 | + |
|
181 | + $metadata = stream_get_meta_data($this->resource); |
|
182 | + |
|
183 | + return strpbrk($metadata['mode'], '+acwx') !== false; |
|
184 | + } |
|
185 | + |
|
186 | + /** |
|
187 | + * @inheritDoc |
|
188 | + */ |
|
189 | + public function write($string): int |
|
190 | + { |
|
191 | + if (!is_resource($this->resource)) { |
|
192 | + throw new RuntimeException('Stream has no resource'); |
|
193 | + } |
|
194 | + |
|
195 | + if (!$this->isWritable()) { |
|
196 | + throw new RuntimeException('Stream is not writable'); |
|
197 | + } |
|
198 | + |
|
199 | + $result = fwrite($this->resource, $string); |
|
200 | + if ($result === false) { |
|
201 | + throw new RuntimeException('Unable to write to the stream'); |
|
202 | + } |
|
203 | + |
|
204 | + return $result; |
|
205 | + } |
|
206 | + |
|
207 | + /** |
|
208 | + * @inheritDoc |
|
209 | + */ |
|
210 | + public function isReadable(): bool |
|
211 | + { |
|
212 | + if (!is_resource($this->resource)) { |
|
213 | + return false; |
|
214 | + } |
|
215 | + |
|
216 | + $metadata = stream_get_meta_data($this->resource); |
|
217 | + |
|
218 | + return strpbrk($metadata['mode'], '+r') !== false; |
|
219 | + } |
|
220 | + |
|
221 | + /** |
|
222 | + * @inheritDoc |
|
223 | + * |
|
224 | + * @psalm-param int $length |
|
225 | + * @phpstan-param int<1, max> $length |
|
226 | + */ |
|
227 | + public function read($length): string |
|
228 | + { |
|
229 | + if (!is_resource($this->resource)) { |
|
230 | + throw new RuntimeException('Stream has no resource'); |
|
231 | + } |
|
232 | + |
|
233 | + if (!$this->isReadable()) { |
|
234 | + throw new RuntimeException('Stream is not readable'); |
|
235 | + } |
|
236 | + |
|
237 | + $result = fread($this->resource, $length); |
|
238 | + if ($result === false) { |
|
239 | + throw new RuntimeException('Unable to read from the stream'); |
|
240 | + } |
|
241 | + |
|
242 | + return $result; |
|
243 | + } |
|
244 | + |
|
245 | + /** |
|
246 | + * @inheritDoc |
|
247 | + */ |
|
248 | + public function getContents(): string |
|
249 | + { |
|
250 | + if (!is_resource($this->resource)) { |
|
251 | + throw new RuntimeException('Stream has no resource'); |
|
252 | + } |
|
253 | + |
|
254 | + if (!$this->isReadable()) { |
|
255 | + throw new RuntimeException('Stream is not readable'); |
|
256 | + } |
|
257 | + |
|
258 | + $result = stream_get_contents($this->resource); |
|
259 | + if ($result === false) { |
|
260 | + throw new RuntimeException('Unable to read the remainder of the stream'); |
|
261 | + } |
|
262 | + |
|
263 | + return $result; |
|
264 | + } |
|
265 | + |
|
266 | + /** |
|
267 | + * @inheritDoc |
|
268 | + */ |
|
269 | + public function getMetadata($key = null) |
|
270 | + { |
|
271 | + if (!is_resource($this->resource)) { |
|
272 | + return null; |
|
273 | + } |
|
274 | + |
|
275 | + $metadata = stream_get_meta_data($this->resource); |
|
276 | + if ($key === null) { |
|
277 | + return $metadata; |
|
278 | + } |
|
279 | + |
|
280 | + return $metadata[$key] ?? null; |
|
281 | + } |
|
282 | + |
|
283 | + /** |
|
284 | + * @inheritDoc |
|
285 | + */ |
|
286 | + public function getSize(): ?int |
|
287 | + { |
|
288 | + if (!is_resource($this->resource)) { |
|
289 | + return null; |
|
290 | + } |
|
291 | + |
|
292 | + /** @var array{size: int}|false */ |
|
293 | + $stats = fstat($this->resource); |
|
294 | + if ($stats === false) { |
|
295 | + return null; |
|
296 | + } |
|
297 | + |
|
298 | + return $stats['size']; |
|
299 | + } |
|
300 | + |
|
301 | + /** |
|
302 | + * @inheritDoc |
|
303 | + */ |
|
304 | + public function __toString(): string |
|
305 | + { |
|
306 | + if (!$this->isReadable()) { |
|
307 | + return ''; |
|
308 | + } |
|
309 | + |
|
310 | + try { |
|
311 | + if ($this->isSeekable()) { |
|
312 | + $this->rewind(); |
|
313 | + } |
|
314 | + |
|
315 | + return $this->getContents(); |
|
316 | + } catch (Throwable $e) { |
|
317 | + return ''; |
|
318 | + } |
|
319 | + } |
|
320 | 320 | } |