@@ -36,57 +36,57 @@ |
||
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 $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 $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 $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 $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 | - $flags |= JSON_THROW_ON_ERROR; |
|
75 | + $flags |= JSON_THROW_ON_ERROR; |
|
76 | 76 | |
77 | - try { |
|
78 | - $payload = json_encode($data, $flags, $depth); |
|
79 | - } catch (JsonException $e) { |
|
80 | - throw new InvalidArgumentException(sprintf( |
|
81 | - 'Unable to create JSON response due to invalid JSON data: %s', |
|
82 | - $e->getMessage() |
|
83 | - ), 0, $e); |
|
84 | - } |
|
77 | + try { |
|
78 | + $payload = json_encode($data, $flags, $depth); |
|
79 | + } catch (JsonException $e) { |
|
80 | + throw new InvalidArgumentException(sprintf( |
|
81 | + 'Unable to create JSON response due to invalid JSON data: %s', |
|
82 | + $e->getMessage() |
|
83 | + ), 0, $e); |
|
84 | + } |
|
85 | 85 | |
86 | - $stream = new PhpTempStream('r+b'); |
|
87 | - $stream->write($payload); |
|
88 | - $stream->rewind(); |
|
86 | + $stream = new PhpTempStream('r+b'); |
|
87 | + $stream->write($payload); |
|
88 | + $stream->rewind(); |
|
89 | 89 | |
90 | - return $stream; |
|
91 | - } |
|
90 | + return $stream; |
|
91 | + } |
|
92 | 92 | } |
@@ -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 | } |
@@ -28,18 +28,18 @@ |
||
28 | 28 | final class PhpInputStream extends Stream |
29 | 29 | { |
30 | 30 | |
31 | - /** |
|
32 | - * Constructor of the class |
|
33 | - */ |
|
34 | - public function __construct() |
|
35 | - { |
|
36 | - $input = fopen('php://input', 'rb'); |
|
37 | - $resource = fopen('php://temp', 'r+b'); |
|
31 | + /** |
|
32 | + * Constructor of the class |
|
33 | + */ |
|
34 | + public function __construct() |
|
35 | + { |
|
36 | + $input = fopen('php://input', 'rb'); |
|
37 | + $resource = fopen('php://temp', 'r+b'); |
|
38 | 38 | |
39 | - stream_copy_to_stream($input, $resource); |
|
39 | + stream_copy_to_stream($input, $resource); |
|
40 | 40 | |
41 | - parent::__construct($resource); |
|
41 | + parent::__construct($resource); |
|
42 | 42 | |
43 | - $this->rewind(); |
|
44 | - } |
|
43 | + $this->rewind(); |
|
44 | + } |
|
45 | 45 | } |
@@ -31,30 +31,30 @@ |
||
31 | 31 | final class FileStream extends Stream |
32 | 32 | { |
33 | 33 | |
34 | - /** |
|
35 | - * Constructor of the class |
|
36 | - * |
|
37 | - * @param string $filename |
|
38 | - * @param string $mode |
|
39 | - * |
|
40 | - * @throws RuntimeException |
|
41 | - */ |
|
42 | - public function __construct(string $filename, string $mode) |
|
43 | - { |
|
44 | - try { |
|
45 | - $resource = fopen($filename, $mode); |
|
46 | - } catch (Throwable $e) { |
|
47 | - $resource = false; |
|
48 | - } |
|
49 | - |
|
50 | - if (!is_resource($resource)) { |
|
51 | - throw new RuntimeException(sprintf( |
|
52 | - 'Unable to open the file "%s" in the mode "%s"', |
|
53 | - $filename, |
|
54 | - $mode |
|
55 | - )); |
|
56 | - } |
|
57 | - |
|
58 | - parent::__construct($resource); |
|
59 | - } |
|
34 | + /** |
|
35 | + * Constructor of the class |
|
36 | + * |
|
37 | + * @param string $filename |
|
38 | + * @param string $mode |
|
39 | + * |
|
40 | + * @throws RuntimeException |
|
41 | + */ |
|
42 | + public function __construct(string $filename, string $mode) |
|
43 | + { |
|
44 | + try { |
|
45 | + $resource = fopen($filename, $mode); |
|
46 | + } catch (Throwable $e) { |
|
47 | + $resource = false; |
|
48 | + } |
|
49 | + |
|
50 | + if (!is_resource($resource)) { |
|
51 | + throw new RuntimeException(sprintf( |
|
52 | + 'Unable to open the file "%s" in the mode "%s"', |
|
53 | + $filename, |
|
54 | + $mode |
|
55 | + )); |
|
56 | + } |
|
57 | + |
|
58 | + parent::__construct($resource); |
|
59 | + } |
|
60 | 60 | } |
@@ -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 | } |
@@ -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 |
|
42 | - */ |
|
43 | - private array $serverParams; |
|
44 | - |
|
45 | - /** |
|
46 | - * The request's query parameters |
|
47 | - * |
|
48 | - * @var array |
|
49 | - */ |
|
50 | - private array $queryParams; |
|
51 | - |
|
52 | - /** |
|
53 | - * The request's cookie parameters |
|
54 | - * |
|
55 | - * @var array |
|
56 | - */ |
|
57 | - private array $cookieParams; |
|
58 | - |
|
59 | - /** |
|
60 | - * The request's uploaded files |
|
61 | - * |
|
62 | - * @var array |
|
63 | - */ |
|
64 | - private array $uploadedFiles; |
|
65 | - |
|
66 | - /** |
|
67 | - * The request's parsed body |
|
68 | - * |
|
69 | - * @var array|object|null |
|
70 | - */ |
|
71 | - private $parsedBody; |
|
72 | - |
|
73 | - /** |
|
74 | - * The request attributes |
|
75 | - * |
|
76 | - * @var array |
|
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 $serverParams |
|
90 | - * @param array $queryParams |
|
91 | - * @param array $cookieParams |
|
92 | - * @param array $uploadedFiles |
|
93 | - * @param array|object|null $parsedBody |
|
94 | - * @param array $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 | - if (isset($protocolVersion)) { |
|
113 | - $this->setProtocolVersion($protocolVersion); |
|
114 | - } |
|
115 | - |
|
116 | - parent::__construct($method, $uri, $headers, $body); |
|
117 | - |
|
118 | - $this->serverParams = $serverParams; |
|
119 | - $this->queryParams = $queryParams; |
|
120 | - $this->cookieParams = $cookieParams; |
|
121 | - $this->setUploadedFiles($uploadedFiles); |
|
122 | - $this->setParsedBody($parsedBody); |
|
123 | - $this->attributes = $attributes; |
|
124 | - } |
|
125 | - |
|
126 | - /** |
|
127 | - * Gets the server parameters |
|
128 | - * |
|
129 | - * @return array |
|
130 | - */ |
|
131 | - public function getServerParams(): array |
|
132 | - { |
|
133 | - return $this->serverParams; |
|
134 | - } |
|
135 | - |
|
136 | - /** |
|
137 | - * Gets the request's query parameters |
|
138 | - * |
|
139 | - * @return array |
|
140 | - */ |
|
141 | - public function getQueryParams(): array |
|
142 | - { |
|
143 | - return $this->queryParams; |
|
144 | - } |
|
145 | - |
|
146 | - /** |
|
147 | - * Creates a new instance of the request with the given query parameters |
|
148 | - * |
|
149 | - * @param array $query |
|
150 | - * |
|
151 | - * @return static |
|
152 | - */ |
|
153 | - public function withQueryParams(array $query): ServerRequestInterface |
|
154 | - { |
|
155 | - $clone = clone $this; |
|
156 | - $clone->queryParams = $query; |
|
157 | - |
|
158 | - return $clone; |
|
159 | - } |
|
160 | - |
|
161 | - /** |
|
162 | - * Gets the request's cookie parameters |
|
163 | - * |
|
164 | - * @return array |
|
165 | - */ |
|
166 | - public function getCookieParams(): array |
|
167 | - { |
|
168 | - return $this->cookieParams; |
|
169 | - } |
|
170 | - |
|
171 | - /** |
|
172 | - * Creates a new instance of the request with the given cookie parameters |
|
173 | - * |
|
174 | - * @param array $cookies |
|
175 | - * |
|
176 | - * @return static |
|
177 | - */ |
|
178 | - public function withCookieParams(array $cookies): ServerRequestInterface |
|
179 | - { |
|
180 | - $clone = clone $this; |
|
181 | - $clone->cookieParams = $cookies; |
|
182 | - |
|
183 | - return $clone; |
|
184 | - } |
|
185 | - |
|
186 | - /** |
|
187 | - * Gets the request's uploaded files |
|
188 | - * |
|
189 | - * @return array |
|
190 | - */ |
|
191 | - public function getUploadedFiles(): array |
|
192 | - { |
|
193 | - return $this->uploadedFiles; |
|
194 | - } |
|
195 | - |
|
196 | - /** |
|
197 | - * Creates a new instance of the request with the given uploaded files |
|
198 | - * |
|
199 | - * @param array $uploadedFiles |
|
200 | - * |
|
201 | - * @return static |
|
202 | - * |
|
203 | - * @throws InvalidArgumentException |
|
204 | - * If one of the files isn't valid. |
|
205 | - */ |
|
206 | - public function withUploadedFiles(array $uploadedFiles): ServerRequestInterface |
|
207 | - { |
|
208 | - $clone = clone $this; |
|
209 | - $clone->setUploadedFiles($uploadedFiles); |
|
210 | - |
|
211 | - return $clone; |
|
212 | - } |
|
213 | - |
|
214 | - /** |
|
215 | - * Gets the request's parsed body |
|
216 | - * |
|
217 | - * @return array|object|null |
|
218 | - */ |
|
219 | - public function getParsedBody() |
|
220 | - { |
|
221 | - return $this->parsedBody; |
|
222 | - } |
|
223 | - |
|
224 | - /** |
|
225 | - * Creates a new instance of the request with the given parsed body |
|
226 | - * |
|
227 | - * @param array|object|null $data |
|
228 | - * |
|
229 | - * @return static |
|
230 | - * |
|
231 | - * @throws InvalidArgumentException |
|
232 | - * If the data isn't valid. |
|
233 | - */ |
|
234 | - public function withParsedBody($data): ServerRequestInterface |
|
235 | - { |
|
236 | - $clone = clone $this; |
|
237 | - $clone->setParsedBody($data); |
|
238 | - |
|
239 | - return $clone; |
|
240 | - } |
|
241 | - |
|
242 | - /** |
|
243 | - * Gets the request attributes |
|
244 | - * |
|
245 | - * @return array |
|
246 | - */ |
|
247 | - public function getAttributes(): array |
|
248 | - { |
|
249 | - return $this->attributes; |
|
250 | - } |
|
251 | - |
|
252 | - /** |
|
253 | - * Gets the request's attribute value by the given name |
|
254 | - * |
|
255 | - * Returns the default value if the attribute wasn't found. |
|
256 | - * |
|
257 | - * @param array-key $name |
|
258 | - * @param mixed $default |
|
259 | - * |
|
260 | - * @return mixed |
|
261 | - */ |
|
262 | - public function getAttribute($name, $default = null) |
|
263 | - { |
|
264 | - if (!array_key_exists($name, $this->attributes)) { |
|
265 | - return $default; |
|
266 | - } |
|
267 | - |
|
268 | - return $this->attributes[$name]; |
|
269 | - } |
|
270 | - |
|
271 | - /** |
|
272 | - * Creates a new instance of the request with the given attribute |
|
273 | - * |
|
274 | - * @param array-key $name |
|
275 | - * @param mixed $value |
|
276 | - * |
|
277 | - * @return static |
|
278 | - */ |
|
279 | - public function withAttribute($name, $value): ServerRequestInterface |
|
280 | - { |
|
281 | - $clone = clone $this; |
|
282 | - $clone->attributes[$name] = $value; |
|
283 | - |
|
284 | - return $clone; |
|
285 | - } |
|
286 | - |
|
287 | - /** |
|
288 | - * Creates a new instance of the request without an attribute with the given name |
|
289 | - * |
|
290 | - * @param array-key $name |
|
291 | - * |
|
292 | - * @return static |
|
293 | - */ |
|
294 | - public function withoutAttribute($name): ServerRequestInterface |
|
295 | - { |
|
296 | - $clone = clone $this; |
|
297 | - unset($clone->attributes[$name]); |
|
298 | - |
|
299 | - return $clone; |
|
300 | - } |
|
301 | - |
|
302 | - /** |
|
303 | - * Sets the given uploaded files to the request |
|
304 | - * |
|
305 | - * @param array $files |
|
306 | - * |
|
307 | - * @return void |
|
308 | - * |
|
309 | - * @throws InvalidArgumentException |
|
310 | - * If one of the files isn't valid. |
|
311 | - */ |
|
312 | - final protected function setUploadedFiles(array $files): void |
|
313 | - { |
|
314 | - $this->validateUploadedFiles($files); |
|
315 | - |
|
316 | - $this->uploadedFiles = $files; |
|
317 | - } |
|
318 | - |
|
319 | - /** |
|
320 | - * Sets the given parsed body to the request |
|
321 | - * |
|
322 | - * @param array|object|null $data |
|
323 | - * |
|
324 | - * @return void |
|
325 | - * |
|
326 | - * @throws InvalidArgumentException |
|
327 | - * If the parsed body isn't valid. |
|
328 | - */ |
|
329 | - final protected function setParsedBody($data): void |
|
330 | - { |
|
331 | - $this->validateParsedBody($data); |
|
332 | - |
|
333 | - $this->parsedBody = $data; |
|
334 | - } |
|
335 | - |
|
336 | - /** |
|
337 | - * Validates the given uploaded files |
|
338 | - * |
|
339 | - * @param array $files |
|
340 | - * |
|
341 | - * @return void |
|
342 | - * |
|
343 | - * @throws InvalidArgumentException |
|
344 | - * If one of the files isn't valid. |
|
345 | - */ |
|
346 | - private function validateUploadedFiles(array $files): void |
|
347 | - { |
|
348 | - if ([] === $files) { |
|
349 | - return; |
|
350 | - } |
|
351 | - |
|
352 | - /** |
|
353 | - * @param mixed $file |
|
354 | - * |
|
355 | - * @return void |
|
356 | - * |
|
357 | - * @throws InvalidArgumentException |
|
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 files'); |
|
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 |
|
42 | + */ |
|
43 | + private array $serverParams; |
|
44 | + |
|
45 | + /** |
|
46 | + * The request's query parameters |
|
47 | + * |
|
48 | + * @var array |
|
49 | + */ |
|
50 | + private array $queryParams; |
|
51 | + |
|
52 | + /** |
|
53 | + * The request's cookie parameters |
|
54 | + * |
|
55 | + * @var array |
|
56 | + */ |
|
57 | + private array $cookieParams; |
|
58 | + |
|
59 | + /** |
|
60 | + * The request's uploaded files |
|
61 | + * |
|
62 | + * @var array |
|
63 | + */ |
|
64 | + private array $uploadedFiles; |
|
65 | + |
|
66 | + /** |
|
67 | + * The request's parsed body |
|
68 | + * |
|
69 | + * @var array|object|null |
|
70 | + */ |
|
71 | + private $parsedBody; |
|
72 | + |
|
73 | + /** |
|
74 | + * The request attributes |
|
75 | + * |
|
76 | + * @var array |
|
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 $serverParams |
|
90 | + * @param array $queryParams |
|
91 | + * @param array $cookieParams |
|
92 | + * @param array $uploadedFiles |
|
93 | + * @param array|object|null $parsedBody |
|
94 | + * @param array $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 | + if (isset($protocolVersion)) { |
|
113 | + $this->setProtocolVersion($protocolVersion); |
|
114 | + } |
|
115 | + |
|
116 | + parent::__construct($method, $uri, $headers, $body); |
|
117 | + |
|
118 | + $this->serverParams = $serverParams; |
|
119 | + $this->queryParams = $queryParams; |
|
120 | + $this->cookieParams = $cookieParams; |
|
121 | + $this->setUploadedFiles($uploadedFiles); |
|
122 | + $this->setParsedBody($parsedBody); |
|
123 | + $this->attributes = $attributes; |
|
124 | + } |
|
125 | + |
|
126 | + /** |
|
127 | + * Gets the server parameters |
|
128 | + * |
|
129 | + * @return array |
|
130 | + */ |
|
131 | + public function getServerParams(): array |
|
132 | + { |
|
133 | + return $this->serverParams; |
|
134 | + } |
|
135 | + |
|
136 | + /** |
|
137 | + * Gets the request's query parameters |
|
138 | + * |
|
139 | + * @return array |
|
140 | + */ |
|
141 | + public function getQueryParams(): array |
|
142 | + { |
|
143 | + return $this->queryParams; |
|
144 | + } |
|
145 | + |
|
146 | + /** |
|
147 | + * Creates a new instance of the request with the given query parameters |
|
148 | + * |
|
149 | + * @param array $query |
|
150 | + * |
|
151 | + * @return static |
|
152 | + */ |
|
153 | + public function withQueryParams(array $query): ServerRequestInterface |
|
154 | + { |
|
155 | + $clone = clone $this; |
|
156 | + $clone->queryParams = $query; |
|
157 | + |
|
158 | + return $clone; |
|
159 | + } |
|
160 | + |
|
161 | + /** |
|
162 | + * Gets the request's cookie parameters |
|
163 | + * |
|
164 | + * @return array |
|
165 | + */ |
|
166 | + public function getCookieParams(): array |
|
167 | + { |
|
168 | + return $this->cookieParams; |
|
169 | + } |
|
170 | + |
|
171 | + /** |
|
172 | + * Creates a new instance of the request with the given cookie parameters |
|
173 | + * |
|
174 | + * @param array $cookies |
|
175 | + * |
|
176 | + * @return static |
|
177 | + */ |
|
178 | + public function withCookieParams(array $cookies): ServerRequestInterface |
|
179 | + { |
|
180 | + $clone = clone $this; |
|
181 | + $clone->cookieParams = $cookies; |
|
182 | + |
|
183 | + return $clone; |
|
184 | + } |
|
185 | + |
|
186 | + /** |
|
187 | + * Gets the request's uploaded files |
|
188 | + * |
|
189 | + * @return array |
|
190 | + */ |
|
191 | + public function getUploadedFiles(): array |
|
192 | + { |
|
193 | + return $this->uploadedFiles; |
|
194 | + } |
|
195 | + |
|
196 | + /** |
|
197 | + * Creates a new instance of the request with the given uploaded files |
|
198 | + * |
|
199 | + * @param array $uploadedFiles |
|
200 | + * |
|
201 | + * @return static |
|
202 | + * |
|
203 | + * @throws InvalidArgumentException |
|
204 | + * If one of the files isn't valid. |
|
205 | + */ |
|
206 | + public function withUploadedFiles(array $uploadedFiles): ServerRequestInterface |
|
207 | + { |
|
208 | + $clone = clone $this; |
|
209 | + $clone->setUploadedFiles($uploadedFiles); |
|
210 | + |
|
211 | + return $clone; |
|
212 | + } |
|
213 | + |
|
214 | + /** |
|
215 | + * Gets the request's parsed body |
|
216 | + * |
|
217 | + * @return array|object|null |
|
218 | + */ |
|
219 | + public function getParsedBody() |
|
220 | + { |
|
221 | + return $this->parsedBody; |
|
222 | + } |
|
223 | + |
|
224 | + /** |
|
225 | + * Creates a new instance of the request with the given parsed body |
|
226 | + * |
|
227 | + * @param array|object|null $data |
|
228 | + * |
|
229 | + * @return static |
|
230 | + * |
|
231 | + * @throws InvalidArgumentException |
|
232 | + * If the data isn't valid. |
|
233 | + */ |
|
234 | + public function withParsedBody($data): ServerRequestInterface |
|
235 | + { |
|
236 | + $clone = clone $this; |
|
237 | + $clone->setParsedBody($data); |
|
238 | + |
|
239 | + return $clone; |
|
240 | + } |
|
241 | + |
|
242 | + /** |
|
243 | + * Gets the request attributes |
|
244 | + * |
|
245 | + * @return array |
|
246 | + */ |
|
247 | + public function getAttributes(): array |
|
248 | + { |
|
249 | + return $this->attributes; |
|
250 | + } |
|
251 | + |
|
252 | + /** |
|
253 | + * Gets the request's attribute value by the given name |
|
254 | + * |
|
255 | + * Returns the default value if the attribute wasn't found. |
|
256 | + * |
|
257 | + * @param array-key $name |
|
258 | + * @param mixed $default |
|
259 | + * |
|
260 | + * @return mixed |
|
261 | + */ |
|
262 | + public function getAttribute($name, $default = null) |
|
263 | + { |
|
264 | + if (!array_key_exists($name, $this->attributes)) { |
|
265 | + return $default; |
|
266 | + } |
|
267 | + |
|
268 | + return $this->attributes[$name]; |
|
269 | + } |
|
270 | + |
|
271 | + /** |
|
272 | + * Creates a new instance of the request with the given attribute |
|
273 | + * |
|
274 | + * @param array-key $name |
|
275 | + * @param mixed $value |
|
276 | + * |
|
277 | + * @return static |
|
278 | + */ |
|
279 | + public function withAttribute($name, $value): ServerRequestInterface |
|
280 | + { |
|
281 | + $clone = clone $this; |
|
282 | + $clone->attributes[$name] = $value; |
|
283 | + |
|
284 | + return $clone; |
|
285 | + } |
|
286 | + |
|
287 | + /** |
|
288 | + * Creates a new instance of the request without an attribute with the given name |
|
289 | + * |
|
290 | + * @param array-key $name |
|
291 | + * |
|
292 | + * @return static |
|
293 | + */ |
|
294 | + public function withoutAttribute($name): ServerRequestInterface |
|
295 | + { |
|
296 | + $clone = clone $this; |
|
297 | + unset($clone->attributes[$name]); |
|
298 | + |
|
299 | + return $clone; |
|
300 | + } |
|
301 | + |
|
302 | + /** |
|
303 | + * Sets the given uploaded files to the request |
|
304 | + * |
|
305 | + * @param array $files |
|
306 | + * |
|
307 | + * @return void |
|
308 | + * |
|
309 | + * @throws InvalidArgumentException |
|
310 | + * If one of the files isn't valid. |
|
311 | + */ |
|
312 | + final protected function setUploadedFiles(array $files): void |
|
313 | + { |
|
314 | + $this->validateUploadedFiles($files); |
|
315 | + |
|
316 | + $this->uploadedFiles = $files; |
|
317 | + } |
|
318 | + |
|
319 | + /** |
|
320 | + * Sets the given parsed body to the request |
|
321 | + * |
|
322 | + * @param array|object|null $data |
|
323 | + * |
|
324 | + * @return void |
|
325 | + * |
|
326 | + * @throws InvalidArgumentException |
|
327 | + * If the parsed body isn't valid. |
|
328 | + */ |
|
329 | + final protected function setParsedBody($data): void |
|
330 | + { |
|
331 | + $this->validateParsedBody($data); |
|
332 | + |
|
333 | + $this->parsedBody = $data; |
|
334 | + } |
|
335 | + |
|
336 | + /** |
|
337 | + * Validates the given uploaded files |
|
338 | + * |
|
339 | + * @param array $files |
|
340 | + * |
|
341 | + * @return void |
|
342 | + * |
|
343 | + * @throws InvalidArgumentException |
|
344 | + * If one of the files isn't valid. |
|
345 | + */ |
|
346 | + private function validateUploadedFiles(array $files): void |
|
347 | + { |
|
348 | + if ([] === $files) { |
|
349 | + return; |
|
350 | + } |
|
351 | + |
|
352 | + /** |
|
353 | + * @param mixed $file |
|
354 | + * |
|
355 | + * @return void |
|
356 | + * |
|
357 | + * @throws InvalidArgumentException |
|
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 files'); |
|
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 | } |
@@ -47,386 +47,386 @@ |
||
47 | 47 | class Stream implements StreamInterface |
48 | 48 | { |
49 | 49 | |
50 | - /** |
|
51 | - * The stream resource |
|
52 | - * |
|
53 | - * @var resource|null |
|
54 | - */ |
|
55 | - private $resource; |
|
56 | - |
|
57 | - /** |
|
58 | - * Signals to close the stream on destruction |
|
59 | - * |
|
60 | - * @var bool |
|
61 | - */ |
|
62 | - private $autoClose; |
|
63 | - |
|
64 | - /** |
|
65 | - * Constructor of the class |
|
66 | - * |
|
67 | - * @param mixed $resource |
|
68 | - * @param bool $autoClose |
|
69 | - * |
|
70 | - * @throws InvalidArgumentException |
|
71 | - * If the stream cannot be initialized with the resource. |
|
72 | - */ |
|
73 | - public function __construct($resource, bool $autoClose = true) |
|
74 | - { |
|
75 | - if (!is_resource($resource)) { |
|
76 | - throw new InvalidArgumentException('Unexpected stream resource'); |
|
77 | - } |
|
78 | - |
|
79 | - $this->resource = $resource; |
|
80 | - $this->autoClose = $autoClose; |
|
81 | - } |
|
82 | - |
|
83 | - /** |
|
84 | - * Creates a stream |
|
85 | - * |
|
86 | - * @param mixed $resource |
|
87 | - * |
|
88 | - * @return StreamInterface |
|
89 | - * |
|
90 | - * @throws InvalidArgumentException |
|
91 | - * If the stream cannot be initialized with the resource. |
|
92 | - */ |
|
93 | - public static function create($resource): StreamInterface |
|
94 | - { |
|
95 | - if ($resource instanceof StreamInterface) { |
|
96 | - return $resource; |
|
97 | - } |
|
98 | - |
|
99 | - return new self($resource); |
|
100 | - } |
|
101 | - |
|
102 | - /** |
|
103 | - * Destructor of the class |
|
104 | - */ |
|
105 | - public function __destruct() |
|
106 | - { |
|
107 | - if ($this->autoClose) { |
|
108 | - $this->close(); |
|
109 | - } |
|
110 | - } |
|
111 | - |
|
112 | - /** |
|
113 | - * Detaches a resource from the stream |
|
114 | - * |
|
115 | - * Returns NULL if the stream already without a resource. |
|
116 | - * |
|
117 | - * @return resource|null |
|
118 | - */ |
|
119 | - public function detach() |
|
120 | - { |
|
121 | - $resource = $this->resource; |
|
122 | - $this->resource = null; |
|
123 | - |
|
124 | - return $resource; |
|
125 | - } |
|
126 | - |
|
127 | - /** |
|
128 | - * Closes the stream |
|
129 | - * |
|
130 | - * @link http://php.net/manual/en/function.fclose.php |
|
131 | - * |
|
132 | - * @return void |
|
133 | - */ |
|
134 | - public function close(): void |
|
135 | - { |
|
136 | - $resource = $this->detach(); |
|
137 | - if (!is_resource($resource)) { |
|
138 | - return; |
|
139 | - } |
|
140 | - |
|
141 | - fclose($resource); |
|
142 | - } |
|
143 | - |
|
144 | - /** |
|
145 | - * Checks if the end of the stream is reached |
|
146 | - * |
|
147 | - * @link http://php.net/manual/en/function.feof.php |
|
148 | - * |
|
149 | - * @return bool |
|
150 | - */ |
|
151 | - public function eof(): bool |
|
152 | - { |
|
153 | - if (!is_resource($this->resource)) { |
|
154 | - return true; |
|
155 | - } |
|
156 | - |
|
157 | - return feof($this->resource); |
|
158 | - } |
|
159 | - |
|
160 | - /** |
|
161 | - * Gets the stream pointer position |
|
162 | - * |
|
163 | - * @link http://php.net/manual/en/function.ftell.php |
|
164 | - * |
|
165 | - * @return int |
|
166 | - * |
|
167 | - * @throws RuntimeException |
|
168 | - */ |
|
169 | - public function tell(): int |
|
170 | - { |
|
171 | - if (!is_resource($this->resource)) { |
|
172 | - throw new RuntimeException('The stream without a resource so the operation is not possible'); |
|
173 | - } |
|
174 | - |
|
175 | - $result = ftell($this->resource); |
|
176 | - if ($result === false) { |
|
177 | - throw new RuntimeException('Unable to get the stream pointer position'); |
|
178 | - } |
|
179 | - |
|
180 | - return $result; |
|
181 | - } |
|
182 | - |
|
183 | - /** |
|
184 | - * Checks if the stream is seekable |
|
185 | - * |
|
186 | - * @return bool |
|
187 | - */ |
|
188 | - public function isSeekable(): bool |
|
189 | - { |
|
190 | - if (!is_resource($this->resource)) { |
|
191 | - return false; |
|
192 | - } |
|
193 | - |
|
194 | - /** @var array{seekable: bool} */ |
|
195 | - $metadata = stream_get_meta_data($this->resource); |
|
196 | - |
|
197 | - return $metadata['seekable']; |
|
198 | - } |
|
199 | - |
|
200 | - /** |
|
201 | - * Moves the stream pointer to the beginning |
|
202 | - * |
|
203 | - * @return void |
|
204 | - * |
|
205 | - * @throws RuntimeException |
|
206 | - */ |
|
207 | - public function rewind(): void |
|
208 | - { |
|
209 | - $this->seek(0); |
|
210 | - } |
|
211 | - |
|
212 | - /** |
|
213 | - * Moves the stream pointer to the given position |
|
214 | - * |
|
215 | - * @link http://php.net/manual/en/function.fseek.php |
|
216 | - * |
|
217 | - * @param int $offset |
|
218 | - * @param int $whence |
|
219 | - * |
|
220 | - * @return void |
|
221 | - * |
|
222 | - * @throws RuntimeException |
|
223 | - */ |
|
224 | - public function seek($offset, $whence = SEEK_SET): void |
|
225 | - { |
|
226 | - if (!is_resource($this->resource)) { |
|
227 | - throw new RuntimeException('The stream without a resource so the operation is not possible'); |
|
228 | - } |
|
229 | - |
|
230 | - if (!$this->isSeekable()) { |
|
231 | - throw new RuntimeException('Stream is not seekable'); |
|
232 | - } |
|
233 | - |
|
234 | - $result = fseek($this->resource, $offset, $whence); |
|
235 | - if ($result !== 0) { |
|
236 | - throw new RuntimeException('Unable to move the stream pointer position'); |
|
237 | - } |
|
238 | - } |
|
239 | - |
|
240 | - /** |
|
241 | - * Checks if the stream is writable |
|
242 | - * |
|
243 | - * @return bool |
|
244 | - */ |
|
245 | - public function isWritable(): bool |
|
246 | - { |
|
247 | - if (!is_resource($this->resource)) { |
|
248 | - return false; |
|
249 | - } |
|
250 | - |
|
251 | - /** @var array{mode: string} */ |
|
252 | - $metadata = stream_get_meta_data($this->resource); |
|
253 | - |
|
254 | - return strpbrk($metadata['mode'], '+acwx') !== false; |
|
255 | - } |
|
256 | - |
|
257 | - /** |
|
258 | - * Writes the given string to the stream |
|
259 | - * |
|
260 | - * Returns the number of bytes written to the stream. |
|
261 | - * |
|
262 | - * @link http://php.net/manual/en/function.fwrite.php |
|
263 | - * |
|
264 | - * @param string $string |
|
265 | - * |
|
266 | - * @return int |
|
267 | - * |
|
268 | - * @throws RuntimeException |
|
269 | - */ |
|
270 | - public function write($string): int |
|
271 | - { |
|
272 | - if (!is_resource($this->resource)) { |
|
273 | - throw new RuntimeException('The stream without a resource so the operation is not possible'); |
|
274 | - } |
|
275 | - |
|
276 | - if (!$this->isWritable()) { |
|
277 | - throw new RuntimeException('Stream is not writable'); |
|
278 | - } |
|
279 | - |
|
280 | - $result = fwrite($this->resource, $string); |
|
281 | - if ($result === false) { |
|
282 | - throw new RuntimeException('Unable to write to the stream'); |
|
283 | - } |
|
284 | - |
|
285 | - return $result; |
|
286 | - } |
|
287 | - |
|
288 | - /** |
|
289 | - * Checks if the stream is readable |
|
290 | - * |
|
291 | - * @return bool |
|
292 | - */ |
|
293 | - public function isReadable(): bool |
|
294 | - { |
|
295 | - if (!is_resource($this->resource)) { |
|
296 | - return false; |
|
297 | - } |
|
298 | - |
|
299 | - /** @var array{mode: string} */ |
|
300 | - $metadata = stream_get_meta_data($this->resource); |
|
301 | - |
|
302 | - return strpbrk($metadata['mode'], '+r') !== false; |
|
303 | - } |
|
304 | - |
|
305 | - /** |
|
306 | - * Reads the given number of bytes from the stream |
|
307 | - * |
|
308 | - * @link http://php.net/manual/en/function.fread.php |
|
309 | - * |
|
310 | - * @param int $length |
|
311 | - * |
|
312 | - * @return string |
|
313 | - * |
|
314 | - * @throws RuntimeException |
|
315 | - */ |
|
316 | - public function read($length): string |
|
317 | - { |
|
318 | - if (!is_resource($this->resource)) { |
|
319 | - throw new RuntimeException('The stream without a resource so the operation is not possible'); |
|
320 | - } |
|
321 | - |
|
322 | - if (!$this->isReadable()) { |
|
323 | - throw new RuntimeException('Stream is not readable'); |
|
324 | - } |
|
325 | - |
|
326 | - $result = fread($this->resource, $length); |
|
327 | - if ($result === false) { |
|
328 | - throw new RuntimeException('Unable to read from the stream'); |
|
329 | - } |
|
330 | - |
|
331 | - return $result; |
|
332 | - } |
|
333 | - |
|
334 | - /** |
|
335 | - * Reads the remainder of the stream |
|
336 | - * |
|
337 | - * @link http://php.net/manual/en/function.stream-get-contents.php |
|
338 | - * |
|
339 | - * @return string |
|
340 | - * |
|
341 | - * @throws RuntimeException |
|
342 | - */ |
|
343 | - public function getContents(): string |
|
344 | - { |
|
345 | - if (!is_resource($this->resource)) { |
|
346 | - throw new RuntimeException('The stream without a resource so the operation is not possible'); |
|
347 | - } |
|
348 | - |
|
349 | - if (!$this->isReadable()) { |
|
350 | - throw new RuntimeException('Stream is not readable'); |
|
351 | - } |
|
352 | - |
|
353 | - $result = stream_get_contents($this->resource); |
|
354 | - if ($result === false) { |
|
355 | - throw new RuntimeException('Unable to read the remainder of the stream'); |
|
356 | - } |
|
357 | - |
|
358 | - return $result; |
|
359 | - } |
|
360 | - |
|
361 | - /** |
|
362 | - * Gets the stream metadata |
|
363 | - * |
|
364 | - * @link http://php.net/manual/en/function.stream-get-meta-data.php |
|
365 | - * |
|
366 | - * @param string|null $key |
|
367 | - * |
|
368 | - * @return mixed |
|
369 | - */ |
|
370 | - public function getMetadata($key = null) |
|
371 | - { |
|
372 | - if (!is_resource($this->resource)) { |
|
373 | - return null; |
|
374 | - } |
|
375 | - |
|
376 | - $metadata = stream_get_meta_data($this->resource); |
|
377 | - if ($key === null) { |
|
378 | - return $metadata; |
|
379 | - } |
|
380 | - |
|
381 | - return $metadata[$key] ?? null; |
|
382 | - } |
|
383 | - |
|
384 | - /** |
|
385 | - * Gets the stream size |
|
386 | - * |
|
387 | - * Returns NULL if the stream without a resource, |
|
388 | - * or if the stream size cannot be determined. |
|
389 | - * |
|
390 | - * @link http://php.net/manual/en/function.fstat.php |
|
391 | - * |
|
392 | - * @return int|null |
|
393 | - */ |
|
394 | - public function getSize(): ?int |
|
395 | - { |
|
396 | - if (!is_resource($this->resource)) { |
|
397 | - return null; |
|
398 | - } |
|
399 | - |
|
400 | - /** @var array{size: int}|false */ |
|
401 | - $stats = fstat($this->resource); |
|
402 | - if ($stats === false) { |
|
403 | - return null; |
|
404 | - } |
|
405 | - |
|
406 | - return $stats['size']; |
|
407 | - } |
|
408 | - |
|
409 | - /** |
|
410 | - * Converts the stream to a string |
|
411 | - * |
|
412 | - * @link http://php.net/manual/en/language.oop5.magic.php#object.tostring |
|
413 | - * |
|
414 | - * @return string |
|
415 | - */ |
|
416 | - public function __toString(): string |
|
417 | - { |
|
418 | - if (!$this->isReadable()) { |
|
419 | - return ''; |
|
420 | - } |
|
421 | - |
|
422 | - try { |
|
423 | - if ($this->isSeekable()) { |
|
424 | - $this->rewind(); |
|
425 | - } |
|
426 | - |
|
427 | - return $this->getContents(); |
|
428 | - } catch (Throwable $e) { |
|
429 | - return ''; |
|
430 | - } |
|
431 | - } |
|
50 | + /** |
|
51 | + * The stream resource |
|
52 | + * |
|
53 | + * @var resource|null |
|
54 | + */ |
|
55 | + private $resource; |
|
56 | + |
|
57 | + /** |
|
58 | + * Signals to close the stream on destruction |
|
59 | + * |
|
60 | + * @var bool |
|
61 | + */ |
|
62 | + private $autoClose; |
|
63 | + |
|
64 | + /** |
|
65 | + * Constructor of the class |
|
66 | + * |
|
67 | + * @param mixed $resource |
|
68 | + * @param bool $autoClose |
|
69 | + * |
|
70 | + * @throws InvalidArgumentException |
|
71 | + * If the stream cannot be initialized with the resource. |
|
72 | + */ |
|
73 | + public function __construct($resource, bool $autoClose = true) |
|
74 | + { |
|
75 | + if (!is_resource($resource)) { |
|
76 | + throw new InvalidArgumentException('Unexpected stream resource'); |
|
77 | + } |
|
78 | + |
|
79 | + $this->resource = $resource; |
|
80 | + $this->autoClose = $autoClose; |
|
81 | + } |
|
82 | + |
|
83 | + /** |
|
84 | + * Creates a stream |
|
85 | + * |
|
86 | + * @param mixed $resource |
|
87 | + * |
|
88 | + * @return StreamInterface |
|
89 | + * |
|
90 | + * @throws InvalidArgumentException |
|
91 | + * If the stream cannot be initialized with the resource. |
|
92 | + */ |
|
93 | + public static function create($resource): StreamInterface |
|
94 | + { |
|
95 | + if ($resource instanceof StreamInterface) { |
|
96 | + return $resource; |
|
97 | + } |
|
98 | + |
|
99 | + return new self($resource); |
|
100 | + } |
|
101 | + |
|
102 | + /** |
|
103 | + * Destructor of the class |
|
104 | + */ |
|
105 | + public function __destruct() |
|
106 | + { |
|
107 | + if ($this->autoClose) { |
|
108 | + $this->close(); |
|
109 | + } |
|
110 | + } |
|
111 | + |
|
112 | + /** |
|
113 | + * Detaches a resource from the stream |
|
114 | + * |
|
115 | + * Returns NULL if the stream already without a resource. |
|
116 | + * |
|
117 | + * @return resource|null |
|
118 | + */ |
|
119 | + public function detach() |
|
120 | + { |
|
121 | + $resource = $this->resource; |
|
122 | + $this->resource = null; |
|
123 | + |
|
124 | + return $resource; |
|
125 | + } |
|
126 | + |
|
127 | + /** |
|
128 | + * Closes the stream |
|
129 | + * |
|
130 | + * @link http://php.net/manual/en/function.fclose.php |
|
131 | + * |
|
132 | + * @return void |
|
133 | + */ |
|
134 | + public function close(): void |
|
135 | + { |
|
136 | + $resource = $this->detach(); |
|
137 | + if (!is_resource($resource)) { |
|
138 | + return; |
|
139 | + } |
|
140 | + |
|
141 | + fclose($resource); |
|
142 | + } |
|
143 | + |
|
144 | + /** |
|
145 | + * Checks if the end of the stream is reached |
|
146 | + * |
|
147 | + * @link http://php.net/manual/en/function.feof.php |
|
148 | + * |
|
149 | + * @return bool |
|
150 | + */ |
|
151 | + public function eof(): bool |
|
152 | + { |
|
153 | + if (!is_resource($this->resource)) { |
|
154 | + return true; |
|
155 | + } |
|
156 | + |
|
157 | + return feof($this->resource); |
|
158 | + } |
|
159 | + |
|
160 | + /** |
|
161 | + * Gets the stream pointer position |
|
162 | + * |
|
163 | + * @link http://php.net/manual/en/function.ftell.php |
|
164 | + * |
|
165 | + * @return int |
|
166 | + * |
|
167 | + * @throws RuntimeException |
|
168 | + */ |
|
169 | + public function tell(): int |
|
170 | + { |
|
171 | + if (!is_resource($this->resource)) { |
|
172 | + throw new RuntimeException('The stream without a resource so the operation is not possible'); |
|
173 | + } |
|
174 | + |
|
175 | + $result = ftell($this->resource); |
|
176 | + if ($result === false) { |
|
177 | + throw new RuntimeException('Unable to get the stream pointer position'); |
|
178 | + } |
|
179 | + |
|
180 | + return $result; |
|
181 | + } |
|
182 | + |
|
183 | + /** |
|
184 | + * Checks if the stream is seekable |
|
185 | + * |
|
186 | + * @return bool |
|
187 | + */ |
|
188 | + public function isSeekable(): bool |
|
189 | + { |
|
190 | + if (!is_resource($this->resource)) { |
|
191 | + return false; |
|
192 | + } |
|
193 | + |
|
194 | + /** @var array{seekable: bool} */ |
|
195 | + $metadata = stream_get_meta_data($this->resource); |
|
196 | + |
|
197 | + return $metadata['seekable']; |
|
198 | + } |
|
199 | + |
|
200 | + /** |
|
201 | + * Moves the stream pointer to the beginning |
|
202 | + * |
|
203 | + * @return void |
|
204 | + * |
|
205 | + * @throws RuntimeException |
|
206 | + */ |
|
207 | + public function rewind(): void |
|
208 | + { |
|
209 | + $this->seek(0); |
|
210 | + } |
|
211 | + |
|
212 | + /** |
|
213 | + * Moves the stream pointer to the given position |
|
214 | + * |
|
215 | + * @link http://php.net/manual/en/function.fseek.php |
|
216 | + * |
|
217 | + * @param int $offset |
|
218 | + * @param int $whence |
|
219 | + * |
|
220 | + * @return void |
|
221 | + * |
|
222 | + * @throws RuntimeException |
|
223 | + */ |
|
224 | + public function seek($offset, $whence = SEEK_SET): void |
|
225 | + { |
|
226 | + if (!is_resource($this->resource)) { |
|
227 | + throw new RuntimeException('The stream without a resource so the operation is not possible'); |
|
228 | + } |
|
229 | + |
|
230 | + if (!$this->isSeekable()) { |
|
231 | + throw new RuntimeException('Stream is not seekable'); |
|
232 | + } |
|
233 | + |
|
234 | + $result = fseek($this->resource, $offset, $whence); |
|
235 | + if ($result !== 0) { |
|
236 | + throw new RuntimeException('Unable to move the stream pointer position'); |
|
237 | + } |
|
238 | + } |
|
239 | + |
|
240 | + /** |
|
241 | + * Checks if the stream is writable |
|
242 | + * |
|
243 | + * @return bool |
|
244 | + */ |
|
245 | + public function isWritable(): bool |
|
246 | + { |
|
247 | + if (!is_resource($this->resource)) { |
|
248 | + return false; |
|
249 | + } |
|
250 | + |
|
251 | + /** @var array{mode: string} */ |
|
252 | + $metadata = stream_get_meta_data($this->resource); |
|
253 | + |
|
254 | + return strpbrk($metadata['mode'], '+acwx') !== false; |
|
255 | + } |
|
256 | + |
|
257 | + /** |
|
258 | + * Writes the given string to the stream |
|
259 | + * |
|
260 | + * Returns the number of bytes written to the stream. |
|
261 | + * |
|
262 | + * @link http://php.net/manual/en/function.fwrite.php |
|
263 | + * |
|
264 | + * @param string $string |
|
265 | + * |
|
266 | + * @return int |
|
267 | + * |
|
268 | + * @throws RuntimeException |
|
269 | + */ |
|
270 | + public function write($string): int |
|
271 | + { |
|
272 | + if (!is_resource($this->resource)) { |
|
273 | + throw new RuntimeException('The stream without a resource so the operation is not possible'); |
|
274 | + } |
|
275 | + |
|
276 | + if (!$this->isWritable()) { |
|
277 | + throw new RuntimeException('Stream is not writable'); |
|
278 | + } |
|
279 | + |
|
280 | + $result = fwrite($this->resource, $string); |
|
281 | + if ($result === false) { |
|
282 | + throw new RuntimeException('Unable to write to the stream'); |
|
283 | + } |
|
284 | + |
|
285 | + return $result; |
|
286 | + } |
|
287 | + |
|
288 | + /** |
|
289 | + * Checks if the stream is readable |
|
290 | + * |
|
291 | + * @return bool |
|
292 | + */ |
|
293 | + public function isReadable(): bool |
|
294 | + { |
|
295 | + if (!is_resource($this->resource)) { |
|
296 | + return false; |
|
297 | + } |
|
298 | + |
|
299 | + /** @var array{mode: string} */ |
|
300 | + $metadata = stream_get_meta_data($this->resource); |
|
301 | + |
|
302 | + return strpbrk($metadata['mode'], '+r') !== false; |
|
303 | + } |
|
304 | + |
|
305 | + /** |
|
306 | + * Reads the given number of bytes from the stream |
|
307 | + * |
|
308 | + * @link http://php.net/manual/en/function.fread.php |
|
309 | + * |
|
310 | + * @param int $length |
|
311 | + * |
|
312 | + * @return string |
|
313 | + * |
|
314 | + * @throws RuntimeException |
|
315 | + */ |
|
316 | + public function read($length): string |
|
317 | + { |
|
318 | + if (!is_resource($this->resource)) { |
|
319 | + throw new RuntimeException('The stream without a resource so the operation is not possible'); |
|
320 | + } |
|
321 | + |
|
322 | + if (!$this->isReadable()) { |
|
323 | + throw new RuntimeException('Stream is not readable'); |
|
324 | + } |
|
325 | + |
|
326 | + $result = fread($this->resource, $length); |
|
327 | + if ($result === false) { |
|
328 | + throw new RuntimeException('Unable to read from the stream'); |
|
329 | + } |
|
330 | + |
|
331 | + return $result; |
|
332 | + } |
|
333 | + |
|
334 | + /** |
|
335 | + * Reads the remainder of the stream |
|
336 | + * |
|
337 | + * @link http://php.net/manual/en/function.stream-get-contents.php |
|
338 | + * |
|
339 | + * @return string |
|
340 | + * |
|
341 | + * @throws RuntimeException |
|
342 | + */ |
|
343 | + public function getContents(): string |
|
344 | + { |
|
345 | + if (!is_resource($this->resource)) { |
|
346 | + throw new RuntimeException('The stream without a resource so the operation is not possible'); |
|
347 | + } |
|
348 | + |
|
349 | + if (!$this->isReadable()) { |
|
350 | + throw new RuntimeException('Stream is not readable'); |
|
351 | + } |
|
352 | + |
|
353 | + $result = stream_get_contents($this->resource); |
|
354 | + if ($result === false) { |
|
355 | + throw new RuntimeException('Unable to read the remainder of the stream'); |
|
356 | + } |
|
357 | + |
|
358 | + return $result; |
|
359 | + } |
|
360 | + |
|
361 | + /** |
|
362 | + * Gets the stream metadata |
|
363 | + * |
|
364 | + * @link http://php.net/manual/en/function.stream-get-meta-data.php |
|
365 | + * |
|
366 | + * @param string|null $key |
|
367 | + * |
|
368 | + * @return mixed |
|
369 | + */ |
|
370 | + public function getMetadata($key = null) |
|
371 | + { |
|
372 | + if (!is_resource($this->resource)) { |
|
373 | + return null; |
|
374 | + } |
|
375 | + |
|
376 | + $metadata = stream_get_meta_data($this->resource); |
|
377 | + if ($key === null) { |
|
378 | + return $metadata; |
|
379 | + } |
|
380 | + |
|
381 | + return $metadata[$key] ?? null; |
|
382 | + } |
|
383 | + |
|
384 | + /** |
|
385 | + * Gets the stream size |
|
386 | + * |
|
387 | + * Returns NULL if the stream without a resource, |
|
388 | + * or if the stream size cannot be determined. |
|
389 | + * |
|
390 | + * @link http://php.net/manual/en/function.fstat.php |
|
391 | + * |
|
392 | + * @return int|null |
|
393 | + */ |
|
394 | + public function getSize(): ?int |
|
395 | + { |
|
396 | + if (!is_resource($this->resource)) { |
|
397 | + return null; |
|
398 | + } |
|
399 | + |
|
400 | + /** @var array{size: int}|false */ |
|
401 | + $stats = fstat($this->resource); |
|
402 | + if ($stats === false) { |
|
403 | + return null; |
|
404 | + } |
|
405 | + |
|
406 | + return $stats['size']; |
|
407 | + } |
|
408 | + |
|
409 | + /** |
|
410 | + * Converts the stream to a string |
|
411 | + * |
|
412 | + * @link http://php.net/manual/en/language.oop5.magic.php#object.tostring |
|
413 | + * |
|
414 | + * @return string |
|
415 | + */ |
|
416 | + public function __toString(): string |
|
417 | + { |
|
418 | + if (!$this->isReadable()) { |
|
419 | + return ''; |
|
420 | + } |
|
421 | + |
|
422 | + try { |
|
423 | + if ($this->isSeekable()) { |
|
424 | + $this->rewind(); |
|
425 | + } |
|
426 | + |
|
427 | + return $this->getContents(); |
|
428 | + } catch (Throwable $e) { |
|
429 | + return ''; |
|
430 | + } |
|
431 | + } |
|
432 | 432 | } |
@@ -43,478 +43,478 @@ |
||
43 | 43 | class Uri implements UriInterface |
44 | 44 | { |
45 | 45 | |
46 | - /** |
|
47 | - * Scheme of the URI |
|
48 | - * |
|
49 | - * @var string |
|
50 | - */ |
|
51 | - private string $scheme = ''; |
|
52 | - |
|
53 | - /** |
|
54 | - * User Information of the URI |
|
55 | - * |
|
56 | - * @var string |
|
57 | - */ |
|
58 | - private string $userInfo = ''; |
|
59 | - |
|
60 | - /** |
|
61 | - * Host of the URI |
|
62 | - * |
|
63 | - * @var string |
|
64 | - */ |
|
65 | - private string $host = ''; |
|
66 | - |
|
67 | - /** |
|
68 | - * Port of the URI |
|
69 | - * |
|
70 | - * @var int|null |
|
71 | - */ |
|
72 | - private ?int $port = null; |
|
73 | - |
|
74 | - /** |
|
75 | - * Path of the URI |
|
76 | - * |
|
77 | - * @var string |
|
78 | - */ |
|
79 | - private string $path = ''; |
|
80 | - |
|
81 | - /** |
|
82 | - * Query of the URI |
|
83 | - * |
|
84 | - * @var string |
|
85 | - */ |
|
86 | - private string $query = ''; |
|
87 | - |
|
88 | - /** |
|
89 | - * Fragment of the URI |
|
90 | - * |
|
91 | - * @var string |
|
92 | - */ |
|
93 | - private string $fragment = ''; |
|
94 | - |
|
95 | - /** |
|
96 | - * Constructor of the class |
|
97 | - * |
|
98 | - * @param string $uri |
|
99 | - * |
|
100 | - * @throws InvalidUriException |
|
101 | - * If the URI isn't valid. |
|
102 | - */ |
|
103 | - public function __construct(string $uri = '') |
|
104 | - { |
|
105 | - if ($uri === '') { |
|
106 | - return; |
|
107 | - } |
|
108 | - |
|
109 | - $components = parse_url($uri); |
|
110 | - if ($components === false) { |
|
111 | - throw new InvalidUriException('Unable to parse URI'); |
|
112 | - } |
|
113 | - |
|
114 | - if (isset($components['scheme'])) { |
|
115 | - $this->setScheme($components['scheme']); |
|
116 | - } |
|
117 | - |
|
118 | - if (isset($components['user'])) { |
|
119 | - $this->setUserInfo( |
|
120 | - $components['user'], |
|
121 | - $components['pass'] ?? null |
|
122 | - ); |
|
123 | - } |
|
124 | - |
|
125 | - if (isset($components['host'])) { |
|
126 | - $this->setHost($components['host']); |
|
127 | - } |
|
128 | - |
|
129 | - if (isset($components['port'])) { |
|
130 | - $this->setPort($components['port']); |
|
131 | - } |
|
132 | - |
|
133 | - if (isset($components['path'])) { |
|
134 | - $this->setPath($components['path']); |
|
135 | - } |
|
136 | - |
|
137 | - if (isset($components['query'])) { |
|
138 | - $this->setQuery($components['query']); |
|
139 | - } |
|
140 | - |
|
141 | - if (isset($components['fragment'])) { |
|
142 | - $this->setFragment($components['fragment']); |
|
143 | - } |
|
144 | - } |
|
145 | - |
|
146 | - /** |
|
147 | - * Creates a URI |
|
148 | - * |
|
149 | - * @param mixed $uri |
|
150 | - * |
|
151 | - * @return UriInterface |
|
152 | - * |
|
153 | - * @throws InvalidArgumentException |
|
154 | - * If the URI isn't valid. |
|
155 | - */ |
|
156 | - public static function create($uri): UriInterface |
|
157 | - { |
|
158 | - if ($uri instanceof UriInterface) { |
|
159 | - return $uri; |
|
160 | - } |
|
161 | - |
|
162 | - if (!is_string($uri)) { |
|
163 | - throw new InvalidArgumentException('URI should be a string'); |
|
164 | - } |
|
165 | - |
|
166 | - return new self($uri); |
|
167 | - } |
|
168 | - |
|
169 | - /** |
|
170 | - * {@inheritdoc} |
|
171 | - * |
|
172 | - * @throws InvalidUriComponentException |
|
173 | - * If the scheme isn't valid. |
|
174 | - */ |
|
175 | - public function withScheme($scheme): UriInterface |
|
176 | - { |
|
177 | - $clone = clone $this; |
|
178 | - $clone->setScheme($scheme); |
|
179 | - |
|
180 | - return $clone; |
|
181 | - } |
|
182 | - |
|
183 | - /** |
|
184 | - * {@inheritdoc} |
|
185 | - * |
|
186 | - * @throws InvalidUriComponentException |
|
187 | - * If the user information isn't valid. |
|
188 | - */ |
|
189 | - public function withUserInfo($user, $password = null): UriInterface |
|
190 | - { |
|
191 | - $clone = clone $this; |
|
192 | - $clone->setUserInfo($user, $password); |
|
193 | - |
|
194 | - return $clone; |
|
195 | - } |
|
196 | - |
|
197 | - /** |
|
198 | - * {@inheritdoc} |
|
199 | - * |
|
200 | - * @throws InvalidUriComponentException |
|
201 | - * If the host isn't valid. |
|
202 | - */ |
|
203 | - public function withHost($host): UriInterface |
|
204 | - { |
|
205 | - $clone = clone $this; |
|
206 | - $clone->setHost($host); |
|
207 | - |
|
208 | - return $clone; |
|
209 | - } |
|
210 | - |
|
211 | - /** |
|
212 | - * {@inheritdoc} |
|
213 | - * |
|
214 | - * @throws InvalidUriComponentException |
|
215 | - * If the port isn't valid. |
|
216 | - */ |
|
217 | - public function withPort($port): UriInterface |
|
218 | - { |
|
219 | - $clone = clone $this; |
|
220 | - $clone->setPort($port); |
|
221 | - |
|
222 | - return $clone; |
|
223 | - } |
|
224 | - |
|
225 | - /** |
|
226 | - * {@inheritdoc} |
|
227 | - * |
|
228 | - * @throws InvalidUriComponentException |
|
229 | - * If the path isn't valid. |
|
230 | - */ |
|
231 | - public function withPath($path): UriInterface |
|
232 | - { |
|
233 | - $clone = clone $this; |
|
234 | - $clone->setPath($path); |
|
235 | - |
|
236 | - return $clone; |
|
237 | - } |
|
238 | - |
|
239 | - /** |
|
240 | - * {@inheritdoc} |
|
241 | - * |
|
242 | - * @throws InvalidUriComponentException |
|
243 | - * If the query isn't valid. |
|
244 | - */ |
|
245 | - public function withQuery($query): UriInterface |
|
246 | - { |
|
247 | - $clone = clone $this; |
|
248 | - $clone->setQuery($query); |
|
249 | - |
|
250 | - return $clone; |
|
251 | - } |
|
252 | - |
|
253 | - /** |
|
254 | - * {@inheritdoc} |
|
255 | - * |
|
256 | - * @throws InvalidUriComponentException |
|
257 | - * If the fragment isn't valid. |
|
258 | - */ |
|
259 | - public function withFragment($fragment): UriInterface |
|
260 | - { |
|
261 | - $clone = clone $this; |
|
262 | - $clone->setFragment($fragment); |
|
263 | - |
|
264 | - return $clone; |
|
265 | - } |
|
266 | - |
|
267 | - /** |
|
268 | - * {@inheritdoc} |
|
269 | - */ |
|
270 | - public function getScheme(): string |
|
271 | - { |
|
272 | - return $this->scheme; |
|
273 | - } |
|
274 | - |
|
275 | - /** |
|
276 | - * {@inheritdoc} |
|
277 | - */ |
|
278 | - public function getUserInfo(): string |
|
279 | - { |
|
280 | - return $this->userInfo; |
|
281 | - } |
|
282 | - |
|
283 | - /** |
|
284 | - * {@inheritdoc} |
|
285 | - */ |
|
286 | - public function getHost(): string |
|
287 | - { |
|
288 | - return $this->host; |
|
289 | - } |
|
290 | - |
|
291 | - /** |
|
292 | - * {@inheritdoc} |
|
293 | - */ |
|
294 | - public function getPort(): ?int |
|
295 | - { |
|
296 | - // The 80 is the default port number for the HTTP protocol. |
|
297 | - if ($this->port === 80 && $this->scheme === 'http') { |
|
298 | - return null; |
|
299 | - } |
|
300 | - |
|
301 | - // The 443 is the default port number for the HTTPS protocol. |
|
302 | - if ($this->port === 443 && $this->scheme === 'https') { |
|
303 | - return null; |
|
304 | - } |
|
305 | - |
|
306 | - return $this->port; |
|
307 | - } |
|
308 | - |
|
309 | - /** |
|
310 | - * {@inheritdoc} |
|
311 | - */ |
|
312 | - public function getPath(): string |
|
313 | - { |
|
314 | - // CVE-2015-3257 |
|
315 | - if (strncmp($this->path, '//', 2) === 0) { |
|
316 | - return '/' . ltrim($this->path, '/'); |
|
317 | - } |
|
318 | - |
|
319 | - return $this->path; |
|
320 | - } |
|
321 | - |
|
322 | - /** |
|
323 | - * {@inheritdoc} |
|
324 | - */ |
|
325 | - public function getQuery(): string |
|
326 | - { |
|
327 | - return $this->query; |
|
328 | - } |
|
329 | - |
|
330 | - /** |
|
331 | - * {@inheritdoc} |
|
332 | - */ |
|
333 | - public function getFragment(): string |
|
334 | - { |
|
335 | - return $this->fragment; |
|
336 | - } |
|
337 | - |
|
338 | - /** |
|
339 | - * {@inheritdoc} |
|
340 | - */ |
|
341 | - public function getAuthority(): string |
|
342 | - { |
|
343 | - // The host is the basic subcomponent. |
|
344 | - if ($this->host === '') { |
|
345 | - return ''; |
|
346 | - } |
|
347 | - |
|
348 | - $authority = $this->host; |
|
349 | - if ($this->userInfo !== '') { |
|
350 | - $authority = $this->userInfo . '@' . $authority; |
|
351 | - } |
|
352 | - |
|
353 | - $port = $this->getPort(); |
|
354 | - if ($port !== null) { |
|
355 | - $authority = $authority . ':' . $port; |
|
356 | - } |
|
357 | - |
|
358 | - return $authority; |
|
359 | - } |
|
360 | - |
|
361 | - /** |
|
362 | - * {@inheritdoc} |
|
363 | - */ |
|
364 | - public function __toString(): string |
|
365 | - { |
|
366 | - $uri = ''; |
|
367 | - |
|
368 | - $scheme = $this->scheme; |
|
369 | - if ($scheme !== '') { |
|
370 | - $uri .= $scheme . ':'; |
|
371 | - } |
|
372 | - |
|
373 | - $authority = $this->getAuthority(); |
|
374 | - if ($authority !== '') { |
|
375 | - $uri .= '//' . $authority; |
|
376 | - } |
|
377 | - |
|
378 | - $path = $this->path; |
|
379 | - if ($path !== '') { |
|
380 | - // https://github.com/sunrise-php/uri/issues/31 |
|
381 | - // https://datatracker.ietf.org/doc/html/rfc3986#section-3.3 |
|
382 | - // |
|
383 | - // If a URI contains an authority component, |
|
384 | - // then the path component must either be empty |
|
385 | - // or begin with a slash ("/") character. |
|
386 | - if ($authority !== '' && strncmp($path, '/', 1) !== 0) { |
|
387 | - $path = '/' . $path; |
|
388 | - } |
|
389 | - |
|
390 | - // https://github.com/sunrise-php/uri/issues/31 |
|
391 | - // https://datatracker.ietf.org/doc/html/rfc3986#section-3.3 |
|
392 | - // |
|
393 | - // If a URI does not contain an authority component, |
|
394 | - // then the path cannot begin with two slash characters ("//"). |
|
395 | - if ($authority === '' && strncmp($path, '//', 2) === 0) { |
|
396 | - $path = '/' . ltrim($path, '/'); |
|
397 | - } |
|
398 | - |
|
399 | - $uri .= $path; |
|
400 | - } |
|
401 | - |
|
402 | - $query = $this->query; |
|
403 | - if ($query !== '') { |
|
404 | - $uri .= '?' . $query; |
|
405 | - } |
|
406 | - |
|
407 | - $fragment = $this->fragment; |
|
408 | - if ($fragment !== '') { |
|
409 | - $uri .= '#' . $fragment; |
|
410 | - } |
|
411 | - |
|
412 | - return $uri; |
|
413 | - } |
|
414 | - |
|
415 | - /** |
|
416 | - * Sets the given scheme to the URI |
|
417 | - * |
|
418 | - * @param mixed $scheme |
|
419 | - * |
|
420 | - * @return void |
|
421 | - * |
|
422 | - * @throws InvalidUriComponentException |
|
423 | - * If the scheme isn't valid. |
|
424 | - */ |
|
425 | - final protected function setScheme($scheme): void |
|
426 | - { |
|
427 | - $this->scheme = (new Scheme($scheme))->getValue(); |
|
428 | - } |
|
429 | - |
|
430 | - /** |
|
431 | - * Sets the given user information to the URI |
|
432 | - * |
|
433 | - * @param mixed $user |
|
434 | - * @param mixed $password |
|
435 | - * |
|
436 | - * @return void |
|
437 | - * |
|
438 | - * @throws InvalidUriComponentException |
|
439 | - * If the user information isn't valid. |
|
440 | - */ |
|
441 | - final protected function setUserInfo($user, $password): void |
|
442 | - { |
|
443 | - $this->userInfo = (new UserInfo($user, $password))->getValue(); |
|
444 | - } |
|
445 | - |
|
446 | - /** |
|
447 | - * Sets the given host to the URI |
|
448 | - * |
|
449 | - * @param mixed $host |
|
450 | - * |
|
451 | - * @return void |
|
452 | - * |
|
453 | - * @throws InvalidUriComponentException |
|
454 | - * If the host isn't valid. |
|
455 | - */ |
|
456 | - final protected function setHost($host): void |
|
457 | - { |
|
458 | - $this->host = (new Host($host))->getValue(); |
|
459 | - } |
|
460 | - |
|
461 | - /** |
|
462 | - * Sets the given port to the URI |
|
463 | - * |
|
464 | - * @param mixed $port |
|
465 | - * |
|
466 | - * @return void |
|
467 | - * |
|
468 | - * @throws InvalidUriComponentException |
|
469 | - * If the port isn't valid. |
|
470 | - */ |
|
471 | - final protected function setPort($port): void |
|
472 | - { |
|
473 | - $this->port = (new Port($port))->getValue(); |
|
474 | - } |
|
475 | - |
|
476 | - /** |
|
477 | - * Sets the given path to the URI |
|
478 | - * |
|
479 | - * @param mixed $path |
|
480 | - * |
|
481 | - * @return void |
|
482 | - * |
|
483 | - * @throws InvalidUriComponentException |
|
484 | - * If the path isn't valid. |
|
485 | - */ |
|
486 | - final protected function setPath($path): void |
|
487 | - { |
|
488 | - $this->path = (new Path($path))->getValue(); |
|
489 | - } |
|
490 | - |
|
491 | - /** |
|
492 | - * Sets the given query to the URI |
|
493 | - * |
|
494 | - * @param mixed $query |
|
495 | - * |
|
496 | - * @return void |
|
497 | - * |
|
498 | - * @throws InvalidUriComponentException |
|
499 | - * If the query isn't valid. |
|
500 | - */ |
|
501 | - final protected function setQuery($query): void |
|
502 | - { |
|
503 | - $this->query = (new Query($query))->getValue(); |
|
504 | - } |
|
505 | - |
|
506 | - /** |
|
507 | - * Sets the given fragment to the URI |
|
508 | - * |
|
509 | - * @param mixed $fragment |
|
510 | - * |
|
511 | - * @return void |
|
512 | - * |
|
513 | - * @throws InvalidUriComponentException |
|
514 | - * If the fragment isn't valid. |
|
515 | - */ |
|
516 | - final protected function setFragment($fragment): void |
|
517 | - { |
|
518 | - $this->fragment = (new Fragment($fragment))->getValue(); |
|
519 | - } |
|
46 | + /** |
|
47 | + * Scheme of the URI |
|
48 | + * |
|
49 | + * @var string |
|
50 | + */ |
|
51 | + private string $scheme = ''; |
|
52 | + |
|
53 | + /** |
|
54 | + * User Information of the URI |
|
55 | + * |
|
56 | + * @var string |
|
57 | + */ |
|
58 | + private string $userInfo = ''; |
|
59 | + |
|
60 | + /** |
|
61 | + * Host of the URI |
|
62 | + * |
|
63 | + * @var string |
|
64 | + */ |
|
65 | + private string $host = ''; |
|
66 | + |
|
67 | + /** |
|
68 | + * Port of the URI |
|
69 | + * |
|
70 | + * @var int|null |
|
71 | + */ |
|
72 | + private ?int $port = null; |
|
73 | + |
|
74 | + /** |
|
75 | + * Path of the URI |
|
76 | + * |
|
77 | + * @var string |
|
78 | + */ |
|
79 | + private string $path = ''; |
|
80 | + |
|
81 | + /** |
|
82 | + * Query of the URI |
|
83 | + * |
|
84 | + * @var string |
|
85 | + */ |
|
86 | + private string $query = ''; |
|
87 | + |
|
88 | + /** |
|
89 | + * Fragment of the URI |
|
90 | + * |
|
91 | + * @var string |
|
92 | + */ |
|
93 | + private string $fragment = ''; |
|
94 | + |
|
95 | + /** |
|
96 | + * Constructor of the class |
|
97 | + * |
|
98 | + * @param string $uri |
|
99 | + * |
|
100 | + * @throws InvalidUriException |
|
101 | + * If the URI isn't valid. |
|
102 | + */ |
|
103 | + public function __construct(string $uri = '') |
|
104 | + { |
|
105 | + if ($uri === '') { |
|
106 | + return; |
|
107 | + } |
|
108 | + |
|
109 | + $components = parse_url($uri); |
|
110 | + if ($components === false) { |
|
111 | + throw new InvalidUriException('Unable to parse URI'); |
|
112 | + } |
|
113 | + |
|
114 | + if (isset($components['scheme'])) { |
|
115 | + $this->setScheme($components['scheme']); |
|
116 | + } |
|
117 | + |
|
118 | + if (isset($components['user'])) { |
|
119 | + $this->setUserInfo( |
|
120 | + $components['user'], |
|
121 | + $components['pass'] ?? null |
|
122 | + ); |
|
123 | + } |
|
124 | + |
|
125 | + if (isset($components['host'])) { |
|
126 | + $this->setHost($components['host']); |
|
127 | + } |
|
128 | + |
|
129 | + if (isset($components['port'])) { |
|
130 | + $this->setPort($components['port']); |
|
131 | + } |
|
132 | + |
|
133 | + if (isset($components['path'])) { |
|
134 | + $this->setPath($components['path']); |
|
135 | + } |
|
136 | + |
|
137 | + if (isset($components['query'])) { |
|
138 | + $this->setQuery($components['query']); |
|
139 | + } |
|
140 | + |
|
141 | + if (isset($components['fragment'])) { |
|
142 | + $this->setFragment($components['fragment']); |
|
143 | + } |
|
144 | + } |
|
145 | + |
|
146 | + /** |
|
147 | + * Creates a URI |
|
148 | + * |
|
149 | + * @param mixed $uri |
|
150 | + * |
|
151 | + * @return UriInterface |
|
152 | + * |
|
153 | + * @throws InvalidArgumentException |
|
154 | + * If the URI isn't valid. |
|
155 | + */ |
|
156 | + public static function create($uri): UriInterface |
|
157 | + { |
|
158 | + if ($uri instanceof UriInterface) { |
|
159 | + return $uri; |
|
160 | + } |
|
161 | + |
|
162 | + if (!is_string($uri)) { |
|
163 | + throw new InvalidArgumentException('URI should be a string'); |
|
164 | + } |
|
165 | + |
|
166 | + return new self($uri); |
|
167 | + } |
|
168 | + |
|
169 | + /** |
|
170 | + * {@inheritdoc} |
|
171 | + * |
|
172 | + * @throws InvalidUriComponentException |
|
173 | + * If the scheme isn't valid. |
|
174 | + */ |
|
175 | + public function withScheme($scheme): UriInterface |
|
176 | + { |
|
177 | + $clone = clone $this; |
|
178 | + $clone->setScheme($scheme); |
|
179 | + |
|
180 | + return $clone; |
|
181 | + } |
|
182 | + |
|
183 | + /** |
|
184 | + * {@inheritdoc} |
|
185 | + * |
|
186 | + * @throws InvalidUriComponentException |
|
187 | + * If the user information isn't valid. |
|
188 | + */ |
|
189 | + public function withUserInfo($user, $password = null): UriInterface |
|
190 | + { |
|
191 | + $clone = clone $this; |
|
192 | + $clone->setUserInfo($user, $password); |
|
193 | + |
|
194 | + return $clone; |
|
195 | + } |
|
196 | + |
|
197 | + /** |
|
198 | + * {@inheritdoc} |
|
199 | + * |
|
200 | + * @throws InvalidUriComponentException |
|
201 | + * If the host isn't valid. |
|
202 | + */ |
|
203 | + public function withHost($host): UriInterface |
|
204 | + { |
|
205 | + $clone = clone $this; |
|
206 | + $clone->setHost($host); |
|
207 | + |
|
208 | + return $clone; |
|
209 | + } |
|
210 | + |
|
211 | + /** |
|
212 | + * {@inheritdoc} |
|
213 | + * |
|
214 | + * @throws InvalidUriComponentException |
|
215 | + * If the port isn't valid. |
|
216 | + */ |
|
217 | + public function withPort($port): UriInterface |
|
218 | + { |
|
219 | + $clone = clone $this; |
|
220 | + $clone->setPort($port); |
|
221 | + |
|
222 | + return $clone; |
|
223 | + } |
|
224 | + |
|
225 | + /** |
|
226 | + * {@inheritdoc} |
|
227 | + * |
|
228 | + * @throws InvalidUriComponentException |
|
229 | + * If the path isn't valid. |
|
230 | + */ |
|
231 | + public function withPath($path): UriInterface |
|
232 | + { |
|
233 | + $clone = clone $this; |
|
234 | + $clone->setPath($path); |
|
235 | + |
|
236 | + return $clone; |
|
237 | + } |
|
238 | + |
|
239 | + /** |
|
240 | + * {@inheritdoc} |
|
241 | + * |
|
242 | + * @throws InvalidUriComponentException |
|
243 | + * If the query isn't valid. |
|
244 | + */ |
|
245 | + public function withQuery($query): UriInterface |
|
246 | + { |
|
247 | + $clone = clone $this; |
|
248 | + $clone->setQuery($query); |
|
249 | + |
|
250 | + return $clone; |
|
251 | + } |
|
252 | + |
|
253 | + /** |
|
254 | + * {@inheritdoc} |
|
255 | + * |
|
256 | + * @throws InvalidUriComponentException |
|
257 | + * If the fragment isn't valid. |
|
258 | + */ |
|
259 | + public function withFragment($fragment): UriInterface |
|
260 | + { |
|
261 | + $clone = clone $this; |
|
262 | + $clone->setFragment($fragment); |
|
263 | + |
|
264 | + return $clone; |
|
265 | + } |
|
266 | + |
|
267 | + /** |
|
268 | + * {@inheritdoc} |
|
269 | + */ |
|
270 | + public function getScheme(): string |
|
271 | + { |
|
272 | + return $this->scheme; |
|
273 | + } |
|
274 | + |
|
275 | + /** |
|
276 | + * {@inheritdoc} |
|
277 | + */ |
|
278 | + public function getUserInfo(): string |
|
279 | + { |
|
280 | + return $this->userInfo; |
|
281 | + } |
|
282 | + |
|
283 | + /** |
|
284 | + * {@inheritdoc} |
|
285 | + */ |
|
286 | + public function getHost(): string |
|
287 | + { |
|
288 | + return $this->host; |
|
289 | + } |
|
290 | + |
|
291 | + /** |
|
292 | + * {@inheritdoc} |
|
293 | + */ |
|
294 | + public function getPort(): ?int |
|
295 | + { |
|
296 | + // The 80 is the default port number for the HTTP protocol. |
|
297 | + if ($this->port === 80 && $this->scheme === 'http') { |
|
298 | + return null; |
|
299 | + } |
|
300 | + |
|
301 | + // The 443 is the default port number for the HTTPS protocol. |
|
302 | + if ($this->port === 443 && $this->scheme === 'https') { |
|
303 | + return null; |
|
304 | + } |
|
305 | + |
|
306 | + return $this->port; |
|
307 | + } |
|
308 | + |
|
309 | + /** |
|
310 | + * {@inheritdoc} |
|
311 | + */ |
|
312 | + public function getPath(): string |
|
313 | + { |
|
314 | + // CVE-2015-3257 |
|
315 | + if (strncmp($this->path, '//', 2) === 0) { |
|
316 | + return '/' . ltrim($this->path, '/'); |
|
317 | + } |
|
318 | + |
|
319 | + return $this->path; |
|
320 | + } |
|
321 | + |
|
322 | + /** |
|
323 | + * {@inheritdoc} |
|
324 | + */ |
|
325 | + public function getQuery(): string |
|
326 | + { |
|
327 | + return $this->query; |
|
328 | + } |
|
329 | + |
|
330 | + /** |
|
331 | + * {@inheritdoc} |
|
332 | + */ |
|
333 | + public function getFragment(): string |
|
334 | + { |
|
335 | + return $this->fragment; |
|
336 | + } |
|
337 | + |
|
338 | + /** |
|
339 | + * {@inheritdoc} |
|
340 | + */ |
|
341 | + public function getAuthority(): string |
|
342 | + { |
|
343 | + // The host is the basic subcomponent. |
|
344 | + if ($this->host === '') { |
|
345 | + return ''; |
|
346 | + } |
|
347 | + |
|
348 | + $authority = $this->host; |
|
349 | + if ($this->userInfo !== '') { |
|
350 | + $authority = $this->userInfo . '@' . $authority; |
|
351 | + } |
|
352 | + |
|
353 | + $port = $this->getPort(); |
|
354 | + if ($port !== null) { |
|
355 | + $authority = $authority . ':' . $port; |
|
356 | + } |
|
357 | + |
|
358 | + return $authority; |
|
359 | + } |
|
360 | + |
|
361 | + /** |
|
362 | + * {@inheritdoc} |
|
363 | + */ |
|
364 | + public function __toString(): string |
|
365 | + { |
|
366 | + $uri = ''; |
|
367 | + |
|
368 | + $scheme = $this->scheme; |
|
369 | + if ($scheme !== '') { |
|
370 | + $uri .= $scheme . ':'; |
|
371 | + } |
|
372 | + |
|
373 | + $authority = $this->getAuthority(); |
|
374 | + if ($authority !== '') { |
|
375 | + $uri .= '//' . $authority; |
|
376 | + } |
|
377 | + |
|
378 | + $path = $this->path; |
|
379 | + if ($path !== '') { |
|
380 | + // https://github.com/sunrise-php/uri/issues/31 |
|
381 | + // https://datatracker.ietf.org/doc/html/rfc3986#section-3.3 |
|
382 | + // |
|
383 | + // If a URI contains an authority component, |
|
384 | + // then the path component must either be empty |
|
385 | + // or begin with a slash ("/") character. |
|
386 | + if ($authority !== '' && strncmp($path, '/', 1) !== 0) { |
|
387 | + $path = '/' . $path; |
|
388 | + } |
|
389 | + |
|
390 | + // https://github.com/sunrise-php/uri/issues/31 |
|
391 | + // https://datatracker.ietf.org/doc/html/rfc3986#section-3.3 |
|
392 | + // |
|
393 | + // If a URI does not contain an authority component, |
|
394 | + // then the path cannot begin with two slash characters ("//"). |
|
395 | + if ($authority === '' && strncmp($path, '//', 2) === 0) { |
|
396 | + $path = '/' . ltrim($path, '/'); |
|
397 | + } |
|
398 | + |
|
399 | + $uri .= $path; |
|
400 | + } |
|
401 | + |
|
402 | + $query = $this->query; |
|
403 | + if ($query !== '') { |
|
404 | + $uri .= '?' . $query; |
|
405 | + } |
|
406 | + |
|
407 | + $fragment = $this->fragment; |
|
408 | + if ($fragment !== '') { |
|
409 | + $uri .= '#' . $fragment; |
|
410 | + } |
|
411 | + |
|
412 | + return $uri; |
|
413 | + } |
|
414 | + |
|
415 | + /** |
|
416 | + * Sets the given scheme to the URI |
|
417 | + * |
|
418 | + * @param mixed $scheme |
|
419 | + * |
|
420 | + * @return void |
|
421 | + * |
|
422 | + * @throws InvalidUriComponentException |
|
423 | + * If the scheme isn't valid. |
|
424 | + */ |
|
425 | + final protected function setScheme($scheme): void |
|
426 | + { |
|
427 | + $this->scheme = (new Scheme($scheme))->getValue(); |
|
428 | + } |
|
429 | + |
|
430 | + /** |
|
431 | + * Sets the given user information to the URI |
|
432 | + * |
|
433 | + * @param mixed $user |
|
434 | + * @param mixed $password |
|
435 | + * |
|
436 | + * @return void |
|
437 | + * |
|
438 | + * @throws InvalidUriComponentException |
|
439 | + * If the user information isn't valid. |
|
440 | + */ |
|
441 | + final protected function setUserInfo($user, $password): void |
|
442 | + { |
|
443 | + $this->userInfo = (new UserInfo($user, $password))->getValue(); |
|
444 | + } |
|
445 | + |
|
446 | + /** |
|
447 | + * Sets the given host to the URI |
|
448 | + * |
|
449 | + * @param mixed $host |
|
450 | + * |
|
451 | + * @return void |
|
452 | + * |
|
453 | + * @throws InvalidUriComponentException |
|
454 | + * If the host isn't valid. |
|
455 | + */ |
|
456 | + final protected function setHost($host): void |
|
457 | + { |
|
458 | + $this->host = (new Host($host))->getValue(); |
|
459 | + } |
|
460 | + |
|
461 | + /** |
|
462 | + * Sets the given port to the URI |
|
463 | + * |
|
464 | + * @param mixed $port |
|
465 | + * |
|
466 | + * @return void |
|
467 | + * |
|
468 | + * @throws InvalidUriComponentException |
|
469 | + * If the port isn't valid. |
|
470 | + */ |
|
471 | + final protected function setPort($port): void |
|
472 | + { |
|
473 | + $this->port = (new Port($port))->getValue(); |
|
474 | + } |
|
475 | + |
|
476 | + /** |
|
477 | + * Sets the given path to the URI |
|
478 | + * |
|
479 | + * @param mixed $path |
|
480 | + * |
|
481 | + * @return void |
|
482 | + * |
|
483 | + * @throws InvalidUriComponentException |
|
484 | + * If the path isn't valid. |
|
485 | + */ |
|
486 | + final protected function setPath($path): void |
|
487 | + { |
|
488 | + $this->path = (new Path($path))->getValue(); |
|
489 | + } |
|
490 | + |
|
491 | + /** |
|
492 | + * Sets the given query to the URI |
|
493 | + * |
|
494 | + * @param mixed $query |
|
495 | + * |
|
496 | + * @return void |
|
497 | + * |
|
498 | + * @throws InvalidUriComponentException |
|
499 | + * If the query isn't valid. |
|
500 | + */ |
|
501 | + final protected function setQuery($query): void |
|
502 | + { |
|
503 | + $this->query = (new Query($query))->getValue(); |
|
504 | + } |
|
505 | + |
|
506 | + /** |
|
507 | + * Sets the given fragment to the URI |
|
508 | + * |
|
509 | + * @param mixed $fragment |
|
510 | + * |
|
511 | + * @return void |
|
512 | + * |
|
513 | + * @throws InvalidUriComponentException |
|
514 | + * If the fragment isn't valid. |
|
515 | + */ |
|
516 | + final protected function setFragment($fragment): void |
|
517 | + { |
|
518 | + $this->fragment = (new Fragment($fragment))->getValue(); |
|
519 | + } |
|
520 | 520 | } |
@@ -19,48 +19,48 @@ |
||
19 | 19 | final class UserInfo implements ComponentInterface |
20 | 20 | { |
21 | 21 | |
22 | - /** |
|
23 | - * URI component "user" |
|
24 | - * |
|
25 | - * @var User |
|
26 | - */ |
|
27 | - private User $user; |
|
22 | + /** |
|
23 | + * URI component "user" |
|
24 | + * |
|
25 | + * @var User |
|
26 | + */ |
|
27 | + private User $user; |
|
28 | 28 | |
29 | - /** |
|
30 | - * URI component "password" |
|
31 | - * |
|
32 | - * @var Password|null |
|
33 | - */ |
|
34 | - private ?Password $password = null; |
|
29 | + /** |
|
30 | + * URI component "password" |
|
31 | + * |
|
32 | + * @var Password|null |
|
33 | + */ |
|
34 | + private ?Password $password = null; |
|
35 | 35 | |
36 | - /** |
|
37 | - * Constructor of the class |
|
38 | - * |
|
39 | - * @param mixed $user |
|
40 | - * @param mixed $password |
|
41 | - */ |
|
42 | - public function __construct($user, $password = null) |
|
43 | - { |
|
44 | - $this->user = User::create($user); |
|
36 | + /** |
|
37 | + * Constructor of the class |
|
38 | + * |
|
39 | + * @param mixed $user |
|
40 | + * @param mixed $password |
|
41 | + */ |
|
42 | + public function __construct($user, $password = null) |
|
43 | + { |
|
44 | + $this->user = User::create($user); |
|
45 | 45 | |
46 | - if (isset($password)) { |
|
47 | - $this->password = Password::create($password); |
|
48 | - } |
|
49 | - } |
|
46 | + if (isset($password)) { |
|
47 | + $this->password = Password::create($password); |
|
48 | + } |
|
49 | + } |
|
50 | 50 | |
51 | - /** |
|
52 | - * {@inheritdoc} |
|
53 | - * |
|
54 | - * @return string |
|
55 | - */ |
|
56 | - public function getValue(): string |
|
57 | - { |
|
58 | - $value = $this->user->getValue(); |
|
51 | + /** |
|
52 | + * {@inheritdoc} |
|
53 | + * |
|
54 | + * @return string |
|
55 | + */ |
|
56 | + public function getValue(): string |
|
57 | + { |
|
58 | + $value = $this->user->getValue(); |
|
59 | 59 | |
60 | - if (isset($this->password)) { |
|
61 | - $value .= ':' . $this->password->getValue(); |
|
62 | - } |
|
60 | + if (isset($this->password)) { |
|
61 | + $value .= ':' . $this->password->getValue(); |
|
62 | + } |
|
63 | 63 | |
64 | - return $value; |
|
65 | - } |
|
64 | + return $value; |
|
65 | + } |
|
66 | 66 | } |