@@ -27,13 +27,13 @@ |
||
| 27 | 27 | class PhpMemoryStream extends Stream |
| 28 | 28 | { |
| 29 | 29 | |
| 30 | - /** |
|
| 31 | - * Constructor of the class |
|
| 32 | - * |
|
| 33 | - * @param string $mode |
|
| 34 | - */ |
|
| 35 | - public function __construct(string $mode = 'r+b') |
|
| 36 | - { |
|
| 37 | - parent::__construct(fopen('php://memory', $mode)); |
|
| 38 | - } |
|
| 30 | + /** |
|
| 31 | + * Constructor of the class |
|
| 32 | + * |
|
| 33 | + * @param string $mode |
|
| 34 | + */ |
|
| 35 | + public function __construct(string $mode = 'r+b') |
|
| 36 | + { |
|
| 37 | + parent::__construct(fopen('php://memory', $mode)); |
|
| 38 | + } |
|
| 39 | 39 | } |
@@ -35,23 +35,23 @@ |
||
| 35 | 35 | 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 | - $tmpdir = sys_get_temp_dir(); |
|
| 46 | - if (!is_writable($tmpdir)) { |
|
| 47 | - throw new RuntimeException('Temporary files directory is not writable'); |
|
| 48 | - } |
|
| 49 | - |
|
| 50 | - $tmpfile = tmpfile(); |
|
| 51 | - if (!is_resource($tmpfile)) { |
|
| 52 | - throw new RuntimeException('Temporary file cannot be created or opened'); |
|
| 53 | - } |
|
| 54 | - |
|
| 55 | - parent::__construct($tmpfile); |
|
| 56 | - } |
|
| 38 | + /** |
|
| 39 | + * Constructor of the class |
|
| 40 | + * |
|
| 41 | + * @throws RuntimeException |
|
| 42 | + */ |
|
| 43 | + public function __construct() |
|
| 44 | + { |
|
| 45 | + $tmpdir = sys_get_temp_dir(); |
|
| 46 | + if (!is_writable($tmpdir)) { |
|
| 47 | + throw new RuntimeException('Temporary files directory is not writable'); |
|
| 48 | + } |
|
| 49 | + |
|
| 50 | + $tmpfile = tmpfile(); |
|
| 51 | + if (!is_resource($tmpfile)) { |
|
| 52 | + throw new RuntimeException('Temporary file cannot be created or opened'); |
|
| 53 | + } |
|
| 54 | + |
|
| 55 | + parent::__construct($tmpfile); |
|
| 56 | + } |
|
| 57 | 57 | } |
@@ -28,18 +28,18 @@ |
||
| 28 | 28 | 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 | } |
@@ -43,492 +43,492 @@ |
||
| 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 a URI cannot be created. |
|
| 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 | - $component = new Scheme($scheme); |
|
| 428 | - |
|
| 429 | - $this->scheme = $component->getValue(); |
|
| 430 | - } |
|
| 431 | - |
|
| 432 | - /** |
|
| 433 | - * Sets the given user information to the URI |
|
| 434 | - * |
|
| 435 | - * @param mixed $user |
|
| 436 | - * @param mixed $password |
|
| 437 | - * |
|
| 438 | - * @return void |
|
| 439 | - * |
|
| 440 | - * @throws InvalidUriComponentException |
|
| 441 | - * If the user information isn't valid. |
|
| 442 | - */ |
|
| 443 | - final protected function setUserInfo($user, $password): void |
|
| 444 | - { |
|
| 445 | - $component = new UserInfo($user, $password); |
|
| 446 | - |
|
| 447 | - $this->userInfo = $component->getValue(); |
|
| 448 | - } |
|
| 449 | - |
|
| 450 | - /** |
|
| 451 | - * Sets the given host to the URI |
|
| 452 | - * |
|
| 453 | - * @param mixed $host |
|
| 454 | - * |
|
| 455 | - * @return void |
|
| 456 | - * |
|
| 457 | - * @throws InvalidUriComponentException |
|
| 458 | - * If the host isn't valid. |
|
| 459 | - */ |
|
| 460 | - final protected function setHost($host): void |
|
| 461 | - { |
|
| 462 | - $component = new Host($host); |
|
| 463 | - |
|
| 464 | - $this->host = $component->getValue(); |
|
| 465 | - } |
|
| 466 | - |
|
| 467 | - /** |
|
| 468 | - * Sets the given port to the URI |
|
| 469 | - * |
|
| 470 | - * @param mixed $port |
|
| 471 | - * |
|
| 472 | - * @return void |
|
| 473 | - * |
|
| 474 | - * @throws InvalidUriComponentException |
|
| 475 | - * If the port isn't valid. |
|
| 476 | - */ |
|
| 477 | - final protected function setPort($port): void |
|
| 478 | - { |
|
| 479 | - $component = new Port($port); |
|
| 480 | - |
|
| 481 | - $this->port = $component->getValue(); |
|
| 482 | - } |
|
| 483 | - |
|
| 484 | - /** |
|
| 485 | - * Sets the given path to the URI |
|
| 486 | - * |
|
| 487 | - * @param mixed $path |
|
| 488 | - * |
|
| 489 | - * @return void |
|
| 490 | - * |
|
| 491 | - * @throws InvalidUriComponentException |
|
| 492 | - * If the path isn't valid. |
|
| 493 | - */ |
|
| 494 | - final protected function setPath($path): void |
|
| 495 | - { |
|
| 496 | - $component = new Path($path); |
|
| 497 | - |
|
| 498 | - $this->path = $component->getValue(); |
|
| 499 | - } |
|
| 500 | - |
|
| 501 | - /** |
|
| 502 | - * Sets the given query to the URI |
|
| 503 | - * |
|
| 504 | - * @param mixed $query |
|
| 505 | - * |
|
| 506 | - * @return void |
|
| 507 | - * |
|
| 508 | - * @throws InvalidUriComponentException |
|
| 509 | - * If the query isn't valid. |
|
| 510 | - */ |
|
| 511 | - final protected function setQuery($query): void |
|
| 512 | - { |
|
| 513 | - $component = new Query($query); |
|
| 514 | - |
|
| 515 | - $this->query = $component->getValue(); |
|
| 516 | - } |
|
| 517 | - |
|
| 518 | - /** |
|
| 519 | - * Sets the given fragment to the URI |
|
| 520 | - * |
|
| 521 | - * @param mixed $fragment |
|
| 522 | - * |
|
| 523 | - * @return void |
|
| 524 | - * |
|
| 525 | - * @throws InvalidUriComponentException |
|
| 526 | - * If the fragment isn't valid. |
|
| 527 | - */ |
|
| 528 | - final protected function setFragment($fragment): void |
|
| 529 | - { |
|
| 530 | - $component = new Fragment($fragment); |
|
| 531 | - |
|
| 532 | - $this->fragment = $component->getValue(); |
|
| 533 | - } |
|
| 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 a URI cannot be created. |
|
| 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 | + $component = new Scheme($scheme); |
|
| 428 | + |
|
| 429 | + $this->scheme = $component->getValue(); |
|
| 430 | + } |
|
| 431 | + |
|
| 432 | + /** |
|
| 433 | + * Sets the given user information to the URI |
|
| 434 | + * |
|
| 435 | + * @param mixed $user |
|
| 436 | + * @param mixed $password |
|
| 437 | + * |
|
| 438 | + * @return void |
|
| 439 | + * |
|
| 440 | + * @throws InvalidUriComponentException |
|
| 441 | + * If the user information isn't valid. |
|
| 442 | + */ |
|
| 443 | + final protected function setUserInfo($user, $password): void |
|
| 444 | + { |
|
| 445 | + $component = new UserInfo($user, $password); |
|
| 446 | + |
|
| 447 | + $this->userInfo = $component->getValue(); |
|
| 448 | + } |
|
| 449 | + |
|
| 450 | + /** |
|
| 451 | + * Sets the given host to the URI |
|
| 452 | + * |
|
| 453 | + * @param mixed $host |
|
| 454 | + * |
|
| 455 | + * @return void |
|
| 456 | + * |
|
| 457 | + * @throws InvalidUriComponentException |
|
| 458 | + * If the host isn't valid. |
|
| 459 | + */ |
|
| 460 | + final protected function setHost($host): void |
|
| 461 | + { |
|
| 462 | + $component = new Host($host); |
|
| 463 | + |
|
| 464 | + $this->host = $component->getValue(); |
|
| 465 | + } |
|
| 466 | + |
|
| 467 | + /** |
|
| 468 | + * Sets the given port to the URI |
|
| 469 | + * |
|
| 470 | + * @param mixed $port |
|
| 471 | + * |
|
| 472 | + * @return void |
|
| 473 | + * |
|
| 474 | + * @throws InvalidUriComponentException |
|
| 475 | + * If the port isn't valid. |
|
| 476 | + */ |
|
| 477 | + final protected function setPort($port): void |
|
| 478 | + { |
|
| 479 | + $component = new Port($port); |
|
| 480 | + |
|
| 481 | + $this->port = $component->getValue(); |
|
| 482 | + } |
|
| 483 | + |
|
| 484 | + /** |
|
| 485 | + * Sets the given path to the URI |
|
| 486 | + * |
|
| 487 | + * @param mixed $path |
|
| 488 | + * |
|
| 489 | + * @return void |
|
| 490 | + * |
|
| 491 | + * @throws InvalidUriComponentException |
|
| 492 | + * If the path isn't valid. |
|
| 493 | + */ |
|
| 494 | + final protected function setPath($path): void |
|
| 495 | + { |
|
| 496 | + $component = new Path($path); |
|
| 497 | + |
|
| 498 | + $this->path = $component->getValue(); |
|
| 499 | + } |
|
| 500 | + |
|
| 501 | + /** |
|
| 502 | + * Sets the given query to the URI |
|
| 503 | + * |
|
| 504 | + * @param mixed $query |
|
| 505 | + * |
|
| 506 | + * @return void |
|
| 507 | + * |
|
| 508 | + * @throws InvalidUriComponentException |
|
| 509 | + * If the query isn't valid. |
|
| 510 | + */ |
|
| 511 | + final protected function setQuery($query): void |
|
| 512 | + { |
|
| 513 | + $component = new Query($query); |
|
| 514 | + |
|
| 515 | + $this->query = $component->getValue(); |
|
| 516 | + } |
|
| 517 | + |
|
| 518 | + /** |
|
| 519 | + * Sets the given fragment to the URI |
|
| 520 | + * |
|
| 521 | + * @param mixed $fragment |
|
| 522 | + * |
|
| 523 | + * @return void |
|
| 524 | + * |
|
| 525 | + * @throws InvalidUriComponentException |
|
| 526 | + * If the fragment isn't valid. |
|
| 527 | + */ |
|
| 528 | + final protected function setFragment($fragment): void |
|
| 529 | + { |
|
| 530 | + $component = new Fragment($fragment); |
|
| 531 | + |
|
| 532 | + $this->fragment = $component->getValue(); |
|
| 533 | + } |
|
| 534 | 534 | } |
@@ -32,59 +32,59 @@ |
||
| 32 | 32 | class HtmlResponse extends Response |
| 33 | 33 | { |
| 34 | 34 | |
| 35 | - /** |
|
| 36 | - * The response content type |
|
| 37 | - * |
|
| 38 | - * @var string |
|
| 39 | - */ |
|
| 40 | - public const CONTENT_TYPE = 'text/html; charset=utf-8'; |
|
| 35 | + /** |
|
| 36 | + * The response content type |
|
| 37 | + * |
|
| 38 | + * @var string |
|
| 39 | + */ |
|
| 40 | + public const CONTENT_TYPE = 'text/html; charset=utf-8'; |
|
| 41 | 41 | |
| 42 | - /** |
|
| 43 | - * Constructor of the class |
|
| 44 | - * |
|
| 45 | - * @param int $statusCode |
|
| 46 | - * @param mixed $html |
|
| 47 | - * |
|
| 48 | - * @throws InvalidArgumentException |
|
| 49 | - */ |
|
| 50 | - public function __construct(int $statusCode, $html) |
|
| 51 | - { |
|
| 52 | - $body = $this->createBody($html); |
|
| 42 | + /** |
|
| 43 | + * Constructor of the class |
|
| 44 | + * |
|
| 45 | + * @param int $statusCode |
|
| 46 | + * @param mixed $html |
|
| 47 | + * |
|
| 48 | + * @throws InvalidArgumentException |
|
| 49 | + */ |
|
| 50 | + public function __construct(int $statusCode, $html) |
|
| 51 | + { |
|
| 52 | + $body = $this->createBody($html); |
|
| 53 | 53 | |
| 54 | - $headers = ['Content-Type' => self::CONTENT_TYPE]; |
|
| 54 | + $headers = ['Content-Type' => self::CONTENT_TYPE]; |
|
| 55 | 55 | |
| 56 | - parent::__construct($statusCode, null, $headers, $body); |
|
| 57 | - } |
|
| 56 | + parent::__construct($statusCode, null, $headers, $body); |
|
| 57 | + } |
|
| 58 | 58 | |
| 59 | - /** |
|
| 60 | - * Creates the response body from the given HTML |
|
| 61 | - * |
|
| 62 | - * @param mixed $html |
|
| 63 | - * |
|
| 64 | - * @return StreamInterface |
|
| 65 | - * |
|
| 66 | - * @throws InvalidArgumentException |
|
| 67 | - * If the response body cannot be created from the given HTML. |
|
| 68 | - */ |
|
| 69 | - private function createBody($html): StreamInterface |
|
| 70 | - { |
|
| 71 | - if ($html instanceof StreamInterface) { |
|
| 72 | - return $html; |
|
| 73 | - } |
|
| 59 | + /** |
|
| 60 | + * Creates the response body from the given HTML |
|
| 61 | + * |
|
| 62 | + * @param mixed $html |
|
| 63 | + * |
|
| 64 | + * @return StreamInterface |
|
| 65 | + * |
|
| 66 | + * @throws InvalidArgumentException |
|
| 67 | + * If the response body cannot be created from the given HTML. |
|
| 68 | + */ |
|
| 69 | + private function createBody($html): StreamInterface |
|
| 70 | + { |
|
| 71 | + if ($html instanceof StreamInterface) { |
|
| 72 | + return $html; |
|
| 73 | + } |
|
| 74 | 74 | |
| 75 | - if (is_object($html) && method_exists($html, '__toString')) { |
|
| 76 | - /** @var string */ |
|
| 77 | - $html = $html->__toString(); |
|
| 78 | - } |
|
| 75 | + if (is_object($html) && method_exists($html, '__toString')) { |
|
| 76 | + /** @var string */ |
|
| 77 | + $html = $html->__toString(); |
|
| 78 | + } |
|
| 79 | 79 | |
| 80 | - if (!is_string($html)) { |
|
| 81 | - throw new InvalidArgumentException('Unable to create HTML response due to invalid body'); |
|
| 82 | - } |
|
| 80 | + if (!is_string($html)) { |
|
| 81 | + throw new InvalidArgumentException('Unable to create HTML response due to invalid body'); |
|
| 82 | + } |
|
| 83 | 83 | |
| 84 | - $stream = new PhpTempStream('r+b'); |
|
| 85 | - $stream->write($html); |
|
| 86 | - $stream->rewind(); |
|
| 84 | + $stream = new PhpTempStream('r+b'); |
|
| 85 | + $stream->write($html); |
|
| 86 | + $stream->rewind(); |
|
| 87 | 87 | |
| 88 | - return $stream; |
|
| 89 | - } |
|
| 88 | + return $stream; |
|
| 89 | + } |
|
| 90 | 90 | } |
@@ -36,65 +36,65 @@ |
||
| 36 | 36 | class JsonResponse extends Response |
| 37 | 37 | { |
| 38 | 38 | |
| 39 | - /** |
|
| 40 | - * The response content type |
|
| 41 | - * |
|
| 42 | - * @var string |
|
| 43 | - */ |
|
| 44 | - public const CONTENT_TYPE = 'application/json; charset=utf-8'; |
|
| 39 | + /** |
|
| 40 | + * The response content type |
|
| 41 | + * |
|
| 42 | + * @var string |
|
| 43 | + */ |
|
| 44 | + public const CONTENT_TYPE = 'application/json; charset=utf-8'; |
|
| 45 | 45 | |
| 46 | - /** |
|
| 47 | - * Constructor of the class |
|
| 48 | - * |
|
| 49 | - * @param int $statusCode |
|
| 50 | - * @param mixed $data |
|
| 51 | - * @param int $flags |
|
| 52 | - * @param int $depth |
|
| 53 | - * |
|
| 54 | - * @throws InvalidArgumentException |
|
| 55 | - */ |
|
| 56 | - public function __construct(int $statusCode, $data, int $flags = 0, int $depth = 512) |
|
| 57 | - { |
|
| 58 | - $body = $this->createBody($data, $flags, $depth); |
|
| 46 | + /** |
|
| 47 | + * Constructor of the class |
|
| 48 | + * |
|
| 49 | + * @param int $statusCode |
|
| 50 | + * @param mixed $data |
|
| 51 | + * @param int $flags |
|
| 52 | + * @param int $depth |
|
| 53 | + * |
|
| 54 | + * @throws InvalidArgumentException |
|
| 55 | + */ |
|
| 56 | + public function __construct(int $statusCode, $data, int $flags = 0, int $depth = 512) |
|
| 57 | + { |
|
| 58 | + $body = $this->createBody($data, $flags, $depth); |
|
| 59 | 59 | |
| 60 | - $headers = ['Content-Type' => self::CONTENT_TYPE]; |
|
| 60 | + $headers = ['Content-Type' => self::CONTENT_TYPE]; |
|
| 61 | 61 | |
| 62 | - parent::__construct($statusCode, null, $headers, $body); |
|
| 63 | - } |
|
| 62 | + parent::__construct($statusCode, null, $headers, $body); |
|
| 63 | + } |
|
| 64 | 64 | |
| 65 | - /** |
|
| 66 | - * Creates the response body from the given JSON data |
|
| 67 | - * |
|
| 68 | - * @param mixed $data |
|
| 69 | - * @param int $flags |
|
| 70 | - * @param int $depth |
|
| 71 | - * |
|
| 72 | - * @return StreamInterface |
|
| 73 | - * |
|
| 74 | - * @throws InvalidArgumentException |
|
| 75 | - * If the response body cannot be created from the given JSON data. |
|
| 76 | - */ |
|
| 77 | - private function createBody($data, int $flags, int $depth): StreamInterface |
|
| 78 | - { |
|
| 79 | - if ($data instanceof StreamInterface) { |
|
| 80 | - return $data; |
|
| 81 | - } |
|
| 65 | + /** |
|
| 66 | + * Creates the response body from the given JSON data |
|
| 67 | + * |
|
| 68 | + * @param mixed $data |
|
| 69 | + * @param int $flags |
|
| 70 | + * @param int $depth |
|
| 71 | + * |
|
| 72 | + * @return StreamInterface |
|
| 73 | + * |
|
| 74 | + * @throws InvalidArgumentException |
|
| 75 | + * If the response body cannot be created from the given JSON data. |
|
| 76 | + */ |
|
| 77 | + private function createBody($data, int $flags, int $depth): StreamInterface |
|
| 78 | + { |
|
| 79 | + if ($data instanceof StreamInterface) { |
|
| 80 | + return $data; |
|
| 81 | + } |
|
| 82 | 82 | |
| 83 | - $flags |= JSON_THROW_ON_ERROR; |
|
| 83 | + $flags |= JSON_THROW_ON_ERROR; |
|
| 84 | 84 | |
| 85 | - try { |
|
| 86 | - $payload = json_encode($data, $flags, $depth); |
|
| 87 | - } catch (JsonException $e) { |
|
| 88 | - throw new InvalidArgumentException(sprintf( |
|
| 89 | - 'Unable to create JSON response due to invalid JSON data: %s', |
|
| 90 | - $e->getMessage() |
|
| 91 | - ), 0, $e); |
|
| 92 | - } |
|
| 85 | + try { |
|
| 86 | + $payload = json_encode($data, $flags, $depth); |
|
| 87 | + } catch (JsonException $e) { |
|
| 88 | + throw new InvalidArgumentException(sprintf( |
|
| 89 | + 'Unable to create JSON response due to invalid JSON data: %s', |
|
| 90 | + $e->getMessage() |
|
| 91 | + ), 0, $e); |
|
| 92 | + } |
|
| 93 | 93 | |
| 94 | - $stream = new PhpTempStream('r+b'); |
|
| 95 | - $stream->write($payload); |
|
| 96 | - $stream->rewind(); |
|
| 94 | + $stream = new PhpTempStream('r+b'); |
|
| 95 | + $stream->write($payload); |
|
| 96 | + $stream->rewind(); |
|
| 97 | 97 | |
| 98 | - return $stream; |
|
| 99 | - } |
|
| 98 | + return $stream; |
|
| 99 | + } |
|
| 100 | 100 | } |
@@ -49,397 +49,397 @@ |
||
| 49 | 49 | class Stream implements StreamInterface |
| 50 | 50 | { |
| 51 | 51 | |
| 52 | - /** |
|
| 53 | - * The stream resource |
|
| 54 | - * |
|
| 55 | - * @var resource|null |
|
| 56 | - */ |
|
| 57 | - private $resource; |
|
| 58 | - |
|
| 59 | - /** |
|
| 60 | - * Signals to close the stream on destruction |
|
| 61 | - * |
|
| 62 | - * @var bool |
|
| 63 | - */ |
|
| 64 | - private $autoClose; |
|
| 65 | - |
|
| 66 | - /** |
|
| 67 | - * Constructor of the class |
|
| 68 | - * |
|
| 69 | - * @param mixed $resource |
|
| 70 | - * @param bool $autoClose |
|
| 71 | - * |
|
| 72 | - * @throws InvalidArgumentException |
|
| 73 | - * If the stream cannot be initialized with the resource. |
|
| 74 | - */ |
|
| 75 | - public function __construct($resource, bool $autoClose = true) |
|
| 76 | - { |
|
| 77 | - if (!is_resource($resource)) { |
|
| 78 | - throw new InvalidArgumentException('Unexpected stream resource'); |
|
| 79 | - } |
|
| 80 | - |
|
| 81 | - $this->resource = $resource; |
|
| 82 | - $this->autoClose = $autoClose; |
|
| 83 | - } |
|
| 84 | - |
|
| 85 | - /** |
|
| 86 | - * Creates a stream |
|
| 87 | - * |
|
| 88 | - * @param mixed $resource |
|
| 89 | - * |
|
| 90 | - * @return StreamInterface |
|
| 91 | - * |
|
| 92 | - * @throws InvalidArgumentException |
|
| 93 | - * If a stream cannot be created. |
|
| 94 | - */ |
|
| 95 | - public static function create($resource): StreamInterface |
|
| 96 | - { |
|
| 97 | - if ($resource instanceof StreamInterface) { |
|
| 98 | - return $resource; |
|
| 99 | - } |
|
| 100 | - |
|
| 101 | - return new self($resource); |
|
| 102 | - } |
|
| 103 | - |
|
| 104 | - /** |
|
| 105 | - * Destructor of the class |
|
| 106 | - */ |
|
| 107 | - public function __destruct() |
|
| 108 | - { |
|
| 109 | - if ($this->autoClose) { |
|
| 110 | - $this->close(); |
|
| 111 | - } |
|
| 112 | - } |
|
| 113 | - |
|
| 114 | - /** |
|
| 115 | - * Detaches a resource from the stream |
|
| 116 | - * |
|
| 117 | - * Returns NULL if the stream already without a resource. |
|
| 118 | - * |
|
| 119 | - * @return resource|null |
|
| 120 | - */ |
|
| 121 | - public function detach() |
|
| 122 | - { |
|
| 123 | - $resource = $this->resource; |
|
| 124 | - $this->resource = null; |
|
| 125 | - |
|
| 126 | - return $resource; |
|
| 127 | - } |
|
| 128 | - |
|
| 129 | - /** |
|
| 130 | - * Closes the stream |
|
| 131 | - * |
|
| 132 | - * @link http://php.net/manual/en/function.fclose.php |
|
| 133 | - * |
|
| 134 | - * @return void |
|
| 135 | - */ |
|
| 136 | - public function close(): void |
|
| 137 | - { |
|
| 138 | - $resource = $this->detach(); |
|
| 139 | - if (!is_resource($resource)) { |
|
| 140 | - return; |
|
| 141 | - } |
|
| 142 | - |
|
| 143 | - fclose($resource); |
|
| 144 | - } |
|
| 145 | - |
|
| 146 | - /** |
|
| 147 | - * Checks if the end of the stream is reached |
|
| 148 | - * |
|
| 149 | - * @link http://php.net/manual/en/function.feof.php |
|
| 150 | - * |
|
| 151 | - * @return bool |
|
| 152 | - */ |
|
| 153 | - public function eof(): bool |
|
| 154 | - { |
|
| 155 | - if (!is_resource($this->resource)) { |
|
| 156 | - return true; |
|
| 157 | - } |
|
| 158 | - |
|
| 159 | - return feof($this->resource); |
|
| 160 | - } |
|
| 161 | - |
|
| 162 | - /** |
|
| 163 | - * Gets the stream pointer position |
|
| 164 | - * |
|
| 165 | - * @link http://php.net/manual/en/function.ftell.php |
|
| 166 | - * |
|
| 167 | - * @return int |
|
| 168 | - * |
|
| 169 | - * @throws InvalidStreamException |
|
| 170 | - * @throws FailedStreamOperationException |
|
| 171 | - */ |
|
| 172 | - public function tell(): int |
|
| 173 | - { |
|
| 174 | - if (!is_resource($this->resource)) { |
|
| 175 | - throw InvalidStreamException::noResource(); |
|
| 176 | - } |
|
| 177 | - |
|
| 178 | - $result = ftell($this->resource); |
|
| 179 | - if ($result === false) { |
|
| 180 | - throw new FailedStreamOperationException('Unable to get the stream pointer position'); |
|
| 181 | - } |
|
| 182 | - |
|
| 183 | - return $result; |
|
| 184 | - } |
|
| 185 | - |
|
| 186 | - /** |
|
| 187 | - * Checks if the stream is seekable |
|
| 188 | - * |
|
| 189 | - * @return bool |
|
| 190 | - */ |
|
| 191 | - public function isSeekable(): bool |
|
| 192 | - { |
|
| 193 | - if (!is_resource($this->resource)) { |
|
| 194 | - return false; |
|
| 195 | - } |
|
| 196 | - |
|
| 197 | - /** @var array{seekable: bool} */ |
|
| 198 | - $metadata = stream_get_meta_data($this->resource); |
|
| 199 | - |
|
| 200 | - return $metadata['seekable']; |
|
| 201 | - } |
|
| 202 | - |
|
| 203 | - /** |
|
| 204 | - * Moves the stream pointer to the beginning |
|
| 205 | - * |
|
| 206 | - * @return void |
|
| 207 | - * |
|
| 208 | - * @throws InvalidStreamException |
|
| 209 | - * @throws InvalidStreamOperationException |
|
| 210 | - * @throws FailedStreamOperationException |
|
| 211 | - */ |
|
| 212 | - public function rewind(): void |
|
| 213 | - { |
|
| 214 | - $this->seek(0); |
|
| 215 | - } |
|
| 216 | - |
|
| 217 | - /** |
|
| 218 | - * Moves the stream pointer to the given position |
|
| 219 | - * |
|
| 220 | - * @link http://php.net/manual/en/function.fseek.php |
|
| 221 | - * |
|
| 222 | - * @param int $offset |
|
| 223 | - * @param int $whence |
|
| 224 | - * |
|
| 225 | - * @return void |
|
| 226 | - * |
|
| 227 | - * @throws InvalidStreamException |
|
| 228 | - * @throws InvalidStreamOperationException |
|
| 229 | - * @throws FailedStreamOperationException |
|
| 230 | - */ |
|
| 231 | - public function seek($offset, $whence = SEEK_SET): void |
|
| 232 | - { |
|
| 233 | - if (!is_resource($this->resource)) { |
|
| 234 | - throw InvalidStreamException::noResource(); |
|
| 235 | - } |
|
| 236 | - |
|
| 237 | - if (!$this->isSeekable()) { |
|
| 238 | - throw new InvalidStreamOperationException('Stream is not seekable'); |
|
| 239 | - } |
|
| 240 | - |
|
| 241 | - $result = fseek($this->resource, $offset, $whence); |
|
| 242 | - if ($result !== 0) { |
|
| 243 | - throw new FailedStreamOperationException('Unable to move the stream pointer position'); |
|
| 244 | - } |
|
| 245 | - } |
|
| 246 | - |
|
| 247 | - /** |
|
| 248 | - * Checks if the stream is writable |
|
| 249 | - * |
|
| 250 | - * @return bool |
|
| 251 | - */ |
|
| 252 | - public function isWritable(): bool |
|
| 253 | - { |
|
| 254 | - if (!is_resource($this->resource)) { |
|
| 255 | - return false; |
|
| 256 | - } |
|
| 257 | - |
|
| 258 | - /** @var array{mode: string} */ |
|
| 259 | - $metadata = stream_get_meta_data($this->resource); |
|
| 260 | - |
|
| 261 | - return strpbrk($metadata['mode'], '+acwx') !== false; |
|
| 262 | - } |
|
| 263 | - |
|
| 264 | - /** |
|
| 265 | - * Writes the given string to the stream |
|
| 266 | - * |
|
| 267 | - * Returns the number of bytes written to the stream. |
|
| 268 | - * |
|
| 269 | - * @link http://php.net/manual/en/function.fwrite.php |
|
| 270 | - * |
|
| 271 | - * @param string $string |
|
| 272 | - * |
|
| 273 | - * @return int |
|
| 274 | - * |
|
| 275 | - * @throws InvalidStreamException |
|
| 276 | - * @throws InvalidStreamOperationException |
|
| 277 | - * @throws FailedStreamOperationException |
|
| 278 | - */ |
|
| 279 | - public function write($string): int |
|
| 280 | - { |
|
| 281 | - if (!is_resource($this->resource)) { |
|
| 282 | - throw InvalidStreamException::noResource(); |
|
| 283 | - } |
|
| 284 | - |
|
| 285 | - if (!$this->isWritable()) { |
|
| 286 | - throw new InvalidStreamOperationException('Stream is not writable'); |
|
| 287 | - } |
|
| 288 | - |
|
| 289 | - $result = fwrite($this->resource, $string); |
|
| 290 | - if ($result === false) { |
|
| 291 | - throw new FailedStreamOperationException('Unable to write to the stream'); |
|
| 292 | - } |
|
| 293 | - |
|
| 294 | - return $result; |
|
| 295 | - } |
|
| 296 | - |
|
| 297 | - /** |
|
| 298 | - * Checks if the stream is readable |
|
| 299 | - * |
|
| 300 | - * @return bool |
|
| 301 | - */ |
|
| 302 | - public function isReadable(): bool |
|
| 303 | - { |
|
| 304 | - if (!is_resource($this->resource)) { |
|
| 305 | - return false; |
|
| 306 | - } |
|
| 307 | - |
|
| 308 | - /** @var array{mode: string} */ |
|
| 309 | - $metadata = stream_get_meta_data($this->resource); |
|
| 310 | - |
|
| 311 | - return strpbrk($metadata['mode'], '+r') !== false; |
|
| 312 | - } |
|
| 313 | - |
|
| 314 | - /** |
|
| 315 | - * Reads the given number of bytes from the stream |
|
| 316 | - * |
|
| 317 | - * @link http://php.net/manual/en/function.fread.php |
|
| 318 | - * |
|
| 319 | - * @param int $length |
|
| 320 | - * |
|
| 321 | - * @return string |
|
| 322 | - * |
|
| 323 | - * @throws InvalidStreamException |
|
| 324 | - * @throws InvalidStreamOperationException |
|
| 325 | - * @throws FailedStreamOperationException |
|
| 326 | - */ |
|
| 327 | - public function read($length): string |
|
| 328 | - { |
|
| 329 | - if (!is_resource($this->resource)) { |
|
| 330 | - throw InvalidStreamException::noResource(); |
|
| 331 | - } |
|
| 332 | - |
|
| 333 | - if (!$this->isReadable()) { |
|
| 334 | - throw new InvalidStreamOperationException('Stream is not readable'); |
|
| 335 | - } |
|
| 336 | - |
|
| 337 | - $result = fread($this->resource, $length); |
|
| 338 | - if ($result === false) { |
|
| 339 | - throw new FailedStreamOperationException('Unable to read from the stream'); |
|
| 340 | - } |
|
| 341 | - |
|
| 342 | - return $result; |
|
| 343 | - } |
|
| 344 | - |
|
| 345 | - /** |
|
| 346 | - * Reads the remainder of the stream |
|
| 347 | - * |
|
| 348 | - * @link http://php.net/manual/en/function.stream-get-contents.php |
|
| 349 | - * |
|
| 350 | - * @return string |
|
| 351 | - * |
|
| 352 | - * @throws InvalidStreamException |
|
| 353 | - * @throws InvalidStreamOperationException |
|
| 354 | - * @throws FailedStreamOperationException |
|
| 355 | - */ |
|
| 356 | - public function getContents(): string |
|
| 357 | - { |
|
| 358 | - if (!is_resource($this->resource)) { |
|
| 359 | - throw InvalidStreamException::noResource(); |
|
| 360 | - } |
|
| 361 | - |
|
| 362 | - if (!$this->isReadable()) { |
|
| 363 | - throw new InvalidStreamOperationException('Stream is not readable'); |
|
| 364 | - } |
|
| 365 | - |
|
| 366 | - $result = stream_get_contents($this->resource); |
|
| 367 | - if ($result === false) { |
|
| 368 | - throw new FailedStreamOperationException('Unable to read the remainder of the stream'); |
|
| 369 | - } |
|
| 370 | - |
|
| 371 | - return $result; |
|
| 372 | - } |
|
| 373 | - |
|
| 374 | - /** |
|
| 375 | - * Gets the stream metadata |
|
| 376 | - * |
|
| 377 | - * @link http://php.net/manual/en/function.stream-get-meta-data.php |
|
| 378 | - * |
|
| 379 | - * @param string|null $key |
|
| 380 | - * |
|
| 381 | - * @return mixed |
|
| 382 | - */ |
|
| 383 | - public function getMetadata($key = null) |
|
| 384 | - { |
|
| 385 | - if (!is_resource($this->resource)) { |
|
| 386 | - return null; |
|
| 387 | - } |
|
| 388 | - |
|
| 389 | - $metadata = stream_get_meta_data($this->resource); |
|
| 390 | - if ($key === null) { |
|
| 391 | - return $metadata; |
|
| 392 | - } |
|
| 393 | - |
|
| 394 | - return $metadata[$key] ?? null; |
|
| 395 | - } |
|
| 396 | - |
|
| 397 | - /** |
|
| 398 | - * Gets the stream size |
|
| 399 | - * |
|
| 400 | - * Returns NULL if the stream without a resource, |
|
| 401 | - * or if the stream size cannot be determined. |
|
| 402 | - * |
|
| 403 | - * @link http://php.net/manual/en/function.fstat.php |
|
| 404 | - * |
|
| 405 | - * @return int|null |
|
| 406 | - */ |
|
| 407 | - public function getSize(): ?int |
|
| 408 | - { |
|
| 409 | - if (!is_resource($this->resource)) { |
|
| 410 | - return null; |
|
| 411 | - } |
|
| 412 | - |
|
| 413 | - /** @var array{size: int}|false */ |
|
| 414 | - $stats = fstat($this->resource); |
|
| 415 | - if ($stats === false) { |
|
| 416 | - return null; |
|
| 417 | - } |
|
| 418 | - |
|
| 419 | - return $stats['size']; |
|
| 420 | - } |
|
| 421 | - |
|
| 422 | - /** |
|
| 423 | - * Converts the stream to a string |
|
| 424 | - * |
|
| 425 | - * @link http://php.net/manual/en/language.oop5.magic.php#object.tostring |
|
| 426 | - * |
|
| 427 | - * @return string |
|
| 428 | - */ |
|
| 429 | - public function __toString(): string |
|
| 430 | - { |
|
| 431 | - if (!$this->isReadable()) { |
|
| 432 | - return ''; |
|
| 433 | - } |
|
| 434 | - |
|
| 435 | - try { |
|
| 436 | - if ($this->isSeekable()) { |
|
| 437 | - $this->rewind(); |
|
| 438 | - } |
|
| 439 | - |
|
| 440 | - return $this->getContents(); |
|
| 441 | - } catch (Throwable $e) { |
|
| 442 | - return ''; |
|
| 443 | - } |
|
| 444 | - } |
|
| 52 | + /** |
|
| 53 | + * The stream resource |
|
| 54 | + * |
|
| 55 | + * @var resource|null |
|
| 56 | + */ |
|
| 57 | + private $resource; |
|
| 58 | + |
|
| 59 | + /** |
|
| 60 | + * Signals to close the stream on destruction |
|
| 61 | + * |
|
| 62 | + * @var bool |
|
| 63 | + */ |
|
| 64 | + private $autoClose; |
|
| 65 | + |
|
| 66 | + /** |
|
| 67 | + * Constructor of the class |
|
| 68 | + * |
|
| 69 | + * @param mixed $resource |
|
| 70 | + * @param bool $autoClose |
|
| 71 | + * |
|
| 72 | + * @throws InvalidArgumentException |
|
| 73 | + * If the stream cannot be initialized with the resource. |
|
| 74 | + */ |
|
| 75 | + public function __construct($resource, bool $autoClose = true) |
|
| 76 | + { |
|
| 77 | + if (!is_resource($resource)) { |
|
| 78 | + throw new InvalidArgumentException('Unexpected stream resource'); |
|
| 79 | + } |
|
| 80 | + |
|
| 81 | + $this->resource = $resource; |
|
| 82 | + $this->autoClose = $autoClose; |
|
| 83 | + } |
|
| 84 | + |
|
| 85 | + /** |
|
| 86 | + * Creates a stream |
|
| 87 | + * |
|
| 88 | + * @param mixed $resource |
|
| 89 | + * |
|
| 90 | + * @return StreamInterface |
|
| 91 | + * |
|
| 92 | + * @throws InvalidArgumentException |
|
| 93 | + * If a stream cannot be created. |
|
| 94 | + */ |
|
| 95 | + public static function create($resource): StreamInterface |
|
| 96 | + { |
|
| 97 | + if ($resource instanceof StreamInterface) { |
|
| 98 | + return $resource; |
|
| 99 | + } |
|
| 100 | + |
|
| 101 | + return new self($resource); |
|
| 102 | + } |
|
| 103 | + |
|
| 104 | + /** |
|
| 105 | + * Destructor of the class |
|
| 106 | + */ |
|
| 107 | + public function __destruct() |
|
| 108 | + { |
|
| 109 | + if ($this->autoClose) { |
|
| 110 | + $this->close(); |
|
| 111 | + } |
|
| 112 | + } |
|
| 113 | + |
|
| 114 | + /** |
|
| 115 | + * Detaches a resource from the stream |
|
| 116 | + * |
|
| 117 | + * Returns NULL if the stream already without a resource. |
|
| 118 | + * |
|
| 119 | + * @return resource|null |
|
| 120 | + */ |
|
| 121 | + public function detach() |
|
| 122 | + { |
|
| 123 | + $resource = $this->resource; |
|
| 124 | + $this->resource = null; |
|
| 125 | + |
|
| 126 | + return $resource; |
|
| 127 | + } |
|
| 128 | + |
|
| 129 | + /** |
|
| 130 | + * Closes the stream |
|
| 131 | + * |
|
| 132 | + * @link http://php.net/manual/en/function.fclose.php |
|
| 133 | + * |
|
| 134 | + * @return void |
|
| 135 | + */ |
|
| 136 | + public function close(): void |
|
| 137 | + { |
|
| 138 | + $resource = $this->detach(); |
|
| 139 | + if (!is_resource($resource)) { |
|
| 140 | + return; |
|
| 141 | + } |
|
| 142 | + |
|
| 143 | + fclose($resource); |
|
| 144 | + } |
|
| 145 | + |
|
| 146 | + /** |
|
| 147 | + * Checks if the end of the stream is reached |
|
| 148 | + * |
|
| 149 | + * @link http://php.net/manual/en/function.feof.php |
|
| 150 | + * |
|
| 151 | + * @return bool |
|
| 152 | + */ |
|
| 153 | + public function eof(): bool |
|
| 154 | + { |
|
| 155 | + if (!is_resource($this->resource)) { |
|
| 156 | + return true; |
|
| 157 | + } |
|
| 158 | + |
|
| 159 | + return feof($this->resource); |
|
| 160 | + } |
|
| 161 | + |
|
| 162 | + /** |
|
| 163 | + * Gets the stream pointer position |
|
| 164 | + * |
|
| 165 | + * @link http://php.net/manual/en/function.ftell.php |
|
| 166 | + * |
|
| 167 | + * @return int |
|
| 168 | + * |
|
| 169 | + * @throws InvalidStreamException |
|
| 170 | + * @throws FailedStreamOperationException |
|
| 171 | + */ |
|
| 172 | + public function tell(): int |
|
| 173 | + { |
|
| 174 | + if (!is_resource($this->resource)) { |
|
| 175 | + throw InvalidStreamException::noResource(); |
|
| 176 | + } |
|
| 177 | + |
|
| 178 | + $result = ftell($this->resource); |
|
| 179 | + if ($result === false) { |
|
| 180 | + throw new FailedStreamOperationException('Unable to get the stream pointer position'); |
|
| 181 | + } |
|
| 182 | + |
|
| 183 | + return $result; |
|
| 184 | + } |
|
| 185 | + |
|
| 186 | + /** |
|
| 187 | + * Checks if the stream is seekable |
|
| 188 | + * |
|
| 189 | + * @return bool |
|
| 190 | + */ |
|
| 191 | + public function isSeekable(): bool |
|
| 192 | + { |
|
| 193 | + if (!is_resource($this->resource)) { |
|
| 194 | + return false; |
|
| 195 | + } |
|
| 196 | + |
|
| 197 | + /** @var array{seekable: bool} */ |
|
| 198 | + $metadata = stream_get_meta_data($this->resource); |
|
| 199 | + |
|
| 200 | + return $metadata['seekable']; |
|
| 201 | + } |
|
| 202 | + |
|
| 203 | + /** |
|
| 204 | + * Moves the stream pointer to the beginning |
|
| 205 | + * |
|
| 206 | + * @return void |
|
| 207 | + * |
|
| 208 | + * @throws InvalidStreamException |
|
| 209 | + * @throws InvalidStreamOperationException |
|
| 210 | + * @throws FailedStreamOperationException |
|
| 211 | + */ |
|
| 212 | + public function rewind(): void |
|
| 213 | + { |
|
| 214 | + $this->seek(0); |
|
| 215 | + } |
|
| 216 | + |
|
| 217 | + /** |
|
| 218 | + * Moves the stream pointer to the given position |
|
| 219 | + * |
|
| 220 | + * @link http://php.net/manual/en/function.fseek.php |
|
| 221 | + * |
|
| 222 | + * @param int $offset |
|
| 223 | + * @param int $whence |
|
| 224 | + * |
|
| 225 | + * @return void |
|
| 226 | + * |
|
| 227 | + * @throws InvalidStreamException |
|
| 228 | + * @throws InvalidStreamOperationException |
|
| 229 | + * @throws FailedStreamOperationException |
|
| 230 | + */ |
|
| 231 | + public function seek($offset, $whence = SEEK_SET): void |
|
| 232 | + { |
|
| 233 | + if (!is_resource($this->resource)) { |
|
| 234 | + throw InvalidStreamException::noResource(); |
|
| 235 | + } |
|
| 236 | + |
|
| 237 | + if (!$this->isSeekable()) { |
|
| 238 | + throw new InvalidStreamOperationException('Stream is not seekable'); |
|
| 239 | + } |
|
| 240 | + |
|
| 241 | + $result = fseek($this->resource, $offset, $whence); |
|
| 242 | + if ($result !== 0) { |
|
| 243 | + throw new FailedStreamOperationException('Unable to move the stream pointer position'); |
|
| 244 | + } |
|
| 245 | + } |
|
| 246 | + |
|
| 247 | + /** |
|
| 248 | + * Checks if the stream is writable |
|
| 249 | + * |
|
| 250 | + * @return bool |
|
| 251 | + */ |
|
| 252 | + public function isWritable(): bool |
|
| 253 | + { |
|
| 254 | + if (!is_resource($this->resource)) { |
|
| 255 | + return false; |
|
| 256 | + } |
|
| 257 | + |
|
| 258 | + /** @var array{mode: string} */ |
|
| 259 | + $metadata = stream_get_meta_data($this->resource); |
|
| 260 | + |
|
| 261 | + return strpbrk($metadata['mode'], '+acwx') !== false; |
|
| 262 | + } |
|
| 263 | + |
|
| 264 | + /** |
|
| 265 | + * Writes the given string to the stream |
|
| 266 | + * |
|
| 267 | + * Returns the number of bytes written to the stream. |
|
| 268 | + * |
|
| 269 | + * @link http://php.net/manual/en/function.fwrite.php |
|
| 270 | + * |
|
| 271 | + * @param string $string |
|
| 272 | + * |
|
| 273 | + * @return int |
|
| 274 | + * |
|
| 275 | + * @throws InvalidStreamException |
|
| 276 | + * @throws InvalidStreamOperationException |
|
| 277 | + * @throws FailedStreamOperationException |
|
| 278 | + */ |
|
| 279 | + public function write($string): int |
|
| 280 | + { |
|
| 281 | + if (!is_resource($this->resource)) { |
|
| 282 | + throw InvalidStreamException::noResource(); |
|
| 283 | + } |
|
| 284 | + |
|
| 285 | + if (!$this->isWritable()) { |
|
| 286 | + throw new InvalidStreamOperationException('Stream is not writable'); |
|
| 287 | + } |
|
| 288 | + |
|
| 289 | + $result = fwrite($this->resource, $string); |
|
| 290 | + if ($result === false) { |
|
| 291 | + throw new FailedStreamOperationException('Unable to write to the stream'); |
|
| 292 | + } |
|
| 293 | + |
|
| 294 | + return $result; |
|
| 295 | + } |
|
| 296 | + |
|
| 297 | + /** |
|
| 298 | + * Checks if the stream is readable |
|
| 299 | + * |
|
| 300 | + * @return bool |
|
| 301 | + */ |
|
| 302 | + public function isReadable(): bool |
|
| 303 | + { |
|
| 304 | + if (!is_resource($this->resource)) { |
|
| 305 | + return false; |
|
| 306 | + } |
|
| 307 | + |
|
| 308 | + /** @var array{mode: string} */ |
|
| 309 | + $metadata = stream_get_meta_data($this->resource); |
|
| 310 | + |
|
| 311 | + return strpbrk($metadata['mode'], '+r') !== false; |
|
| 312 | + } |
|
| 313 | + |
|
| 314 | + /** |
|
| 315 | + * Reads the given number of bytes from the stream |
|
| 316 | + * |
|
| 317 | + * @link http://php.net/manual/en/function.fread.php |
|
| 318 | + * |
|
| 319 | + * @param int $length |
|
| 320 | + * |
|
| 321 | + * @return string |
|
| 322 | + * |
|
| 323 | + * @throws InvalidStreamException |
|
| 324 | + * @throws InvalidStreamOperationException |
|
| 325 | + * @throws FailedStreamOperationException |
|
| 326 | + */ |
|
| 327 | + public function read($length): string |
|
| 328 | + { |
|
| 329 | + if (!is_resource($this->resource)) { |
|
| 330 | + throw InvalidStreamException::noResource(); |
|
| 331 | + } |
|
| 332 | + |
|
| 333 | + if (!$this->isReadable()) { |
|
| 334 | + throw new InvalidStreamOperationException('Stream is not readable'); |
|
| 335 | + } |
|
| 336 | + |
|
| 337 | + $result = fread($this->resource, $length); |
|
| 338 | + if ($result === false) { |
|
| 339 | + throw new FailedStreamOperationException('Unable to read from the stream'); |
|
| 340 | + } |
|
| 341 | + |
|
| 342 | + return $result; |
|
| 343 | + } |
|
| 344 | + |
|
| 345 | + /** |
|
| 346 | + * Reads the remainder of the stream |
|
| 347 | + * |
|
| 348 | + * @link http://php.net/manual/en/function.stream-get-contents.php |
|
| 349 | + * |
|
| 350 | + * @return string |
|
| 351 | + * |
|
| 352 | + * @throws InvalidStreamException |
|
| 353 | + * @throws InvalidStreamOperationException |
|
| 354 | + * @throws FailedStreamOperationException |
|
| 355 | + */ |
|
| 356 | + public function getContents(): string |
|
| 357 | + { |
|
| 358 | + if (!is_resource($this->resource)) { |
|
| 359 | + throw InvalidStreamException::noResource(); |
|
| 360 | + } |
|
| 361 | + |
|
| 362 | + if (!$this->isReadable()) { |
|
| 363 | + throw new InvalidStreamOperationException('Stream is not readable'); |
|
| 364 | + } |
|
| 365 | + |
|
| 366 | + $result = stream_get_contents($this->resource); |
|
| 367 | + if ($result === false) { |
|
| 368 | + throw new FailedStreamOperationException('Unable to read the remainder of the stream'); |
|
| 369 | + } |
|
| 370 | + |
|
| 371 | + return $result; |
|
| 372 | + } |
|
| 373 | + |
|
| 374 | + /** |
|
| 375 | + * Gets the stream metadata |
|
| 376 | + * |
|
| 377 | + * @link http://php.net/manual/en/function.stream-get-meta-data.php |
|
| 378 | + * |
|
| 379 | + * @param string|null $key |
|
| 380 | + * |
|
| 381 | + * @return mixed |
|
| 382 | + */ |
|
| 383 | + public function getMetadata($key = null) |
|
| 384 | + { |
|
| 385 | + if (!is_resource($this->resource)) { |
|
| 386 | + return null; |
|
| 387 | + } |
|
| 388 | + |
|
| 389 | + $metadata = stream_get_meta_data($this->resource); |
|
| 390 | + if ($key === null) { |
|
| 391 | + return $metadata; |
|
| 392 | + } |
|
| 393 | + |
|
| 394 | + return $metadata[$key] ?? null; |
|
| 395 | + } |
|
| 396 | + |
|
| 397 | + /** |
|
| 398 | + * Gets the stream size |
|
| 399 | + * |
|
| 400 | + * Returns NULL if the stream without a resource, |
|
| 401 | + * or if the stream size cannot be determined. |
|
| 402 | + * |
|
| 403 | + * @link http://php.net/manual/en/function.fstat.php |
|
| 404 | + * |
|
| 405 | + * @return int|null |
|
| 406 | + */ |
|
| 407 | + public function getSize(): ?int |
|
| 408 | + { |
|
| 409 | + if (!is_resource($this->resource)) { |
|
| 410 | + return null; |
|
| 411 | + } |
|
| 412 | + |
|
| 413 | + /** @var array{size: int}|false */ |
|
| 414 | + $stats = fstat($this->resource); |
|
| 415 | + if ($stats === false) { |
|
| 416 | + return null; |
|
| 417 | + } |
|
| 418 | + |
|
| 419 | + return $stats['size']; |
|
| 420 | + } |
|
| 421 | + |
|
| 422 | + /** |
|
| 423 | + * Converts the stream to a string |
|
| 424 | + * |
|
| 425 | + * @link http://php.net/manual/en/language.oop5.magic.php#object.tostring |
|
| 426 | + * |
|
| 427 | + * @return string |
|
| 428 | + */ |
|
| 429 | + public function __toString(): string |
|
| 430 | + { |
|
| 431 | + if (!$this->isReadable()) { |
|
| 432 | + return ''; |
|
| 433 | + } |
|
| 434 | + |
|
| 435 | + try { |
|
| 436 | + if ($this->isSeekable()) { |
|
| 437 | + $this->rewind(); |
|
| 438 | + } |
|
| 439 | + |
|
| 440 | + return $this->getContents(); |
|
| 441 | + } catch (Throwable $e) { |
|
| 442 | + return ''; |
|
| 443 | + } |
|
| 444 | + } |
|
| 445 | 445 | } |
@@ -51,242 +51,242 @@ |
||
| 51 | 51 | class UploadedFile implements UploadedFileInterface |
| 52 | 52 | { |
| 53 | 53 | |
| 54 | - /** |
|
| 55 | - * List of upload errors |
|
| 56 | - * |
|
| 57 | - * @link https://www.php.net/manual/en/features.file-upload.errors.php |
|
| 58 | - * |
|
| 59 | - * @var array<int, string> |
|
| 60 | - */ |
|
| 61 | - public const UPLOAD_ERRORS = [ |
|
| 62 | - UPLOAD_ERR_OK => 'No error', |
|
| 63 | - UPLOAD_ERR_INI_SIZE => 'The uploaded file exceeds the upload_max_filesize directive in php.ini', |
|
| 64 | - UPLOAD_ERR_FORM_SIZE => 'The uploaded file exceeds the MAX_FILE_SIZE directive ' . |
|
| 65 | - 'that was specified in the HTML form', |
|
| 66 | - UPLOAD_ERR_PARTIAL => 'The uploaded file was only partially uploaded', |
|
| 67 | - UPLOAD_ERR_NO_FILE => 'No file was uploaded', |
|
| 68 | - UPLOAD_ERR_NO_TMP_DIR => 'Missing a temporary folder', |
|
| 69 | - UPLOAD_ERR_CANT_WRITE => 'Failed to write file to disk', |
|
| 70 | - UPLOAD_ERR_EXTENSION => 'File upload stopped by extension', |
|
| 71 | - ]; |
|
| 72 | - |
|
| 73 | - /** |
|
| 74 | - * Unknown error description |
|
| 75 | - * |
|
| 76 | - * @var string |
|
| 77 | - */ |
|
| 78 | - public const UNKNOWN_ERROR_TEXT = 'Unknown error'; |
|
| 79 | - |
|
| 80 | - /** |
|
| 81 | - * The file stream |
|
| 82 | - * |
|
| 83 | - * @var StreamInterface|null |
|
| 84 | - */ |
|
| 85 | - private ?StreamInterface $stream = null; |
|
| 86 | - |
|
| 87 | - /** |
|
| 88 | - * The file size |
|
| 89 | - * |
|
| 90 | - * @var int|null |
|
| 91 | - */ |
|
| 92 | - private ?int $size; |
|
| 93 | - |
|
| 94 | - /** |
|
| 95 | - * The file's error code |
|
| 96 | - * |
|
| 97 | - * @var int |
|
| 98 | - */ |
|
| 99 | - private int $errorCode; |
|
| 100 | - |
|
| 101 | - /** |
|
| 102 | - * The file's error message |
|
| 103 | - * |
|
| 104 | - * @var string |
|
| 105 | - */ |
|
| 106 | - private string $errorMessage; |
|
| 107 | - |
|
| 108 | - /** |
|
| 109 | - * The file name |
|
| 110 | - * |
|
| 111 | - * @var string|null |
|
| 112 | - */ |
|
| 113 | - private ?string $clientFilename; |
|
| 114 | - |
|
| 115 | - /** |
|
| 116 | - * The file type |
|
| 117 | - * |
|
| 118 | - * @var string|null |
|
| 119 | - */ |
|
| 120 | - private ?string $clientMediaType; |
|
| 121 | - |
|
| 122 | - /** |
|
| 123 | - * Constructor of the class |
|
| 124 | - * |
|
| 125 | - * @param StreamInterface $stream |
|
| 126 | - * @param int|null $size |
|
| 127 | - * @param int $error |
|
| 128 | - * @param string|null $clientFilename |
|
| 129 | - * @param string|null $clientMediaType |
|
| 130 | - */ |
|
| 131 | - public function __construct( |
|
| 132 | - StreamInterface $stream, |
|
| 133 | - ?int $size = null, |
|
| 134 | - int $error = UPLOAD_ERR_OK, |
|
| 135 | - ?string $clientFilename = null, |
|
| 136 | - ?string $clientMediaType = null |
|
| 137 | - ) { |
|
| 138 | - if (UPLOAD_ERR_OK === $error) { |
|
| 139 | - $this->stream = $stream; |
|
| 140 | - } |
|
| 141 | - |
|
| 142 | - $errorMessage = self::UPLOAD_ERRORS[$error] ?? self::UNKNOWN_ERROR_TEXT; |
|
| 143 | - |
|
| 144 | - $this->size = $size; |
|
| 145 | - $this->errorCode = $error; |
|
| 146 | - $this->errorMessage = $errorMessage; |
|
| 147 | - $this->clientFilename = $clientFilename; |
|
| 148 | - $this->clientMediaType = $clientMediaType; |
|
| 149 | - } |
|
| 150 | - |
|
| 151 | - /** |
|
| 152 | - * Gets the file stream |
|
| 153 | - * |
|
| 154 | - * @return StreamInterface |
|
| 155 | - * |
|
| 156 | - * @throws InvalidUploadedFileException |
|
| 157 | - * If the file has no a stream due to an error or |
|
| 158 | - * if the file was already moved. |
|
| 159 | - */ |
|
| 160 | - public function getStream(): StreamInterface |
|
| 161 | - { |
|
| 162 | - if (UPLOAD_ERR_OK <> $this->errorCode) { |
|
| 163 | - throw new InvalidUploadedFileException(sprintf( |
|
| 164 | - 'The uploaded file has no a stream due to the error #%d (%s)', |
|
| 165 | - $this->errorCode, |
|
| 166 | - $this->errorMessage |
|
| 167 | - )); |
|
| 168 | - } |
|
| 169 | - |
|
| 170 | - if (!isset($this->stream)) { |
|
| 171 | - throw new InvalidUploadedFileException( |
|
| 172 | - 'The uploaded file has no a stream because it was already moved' |
|
| 173 | - ); |
|
| 174 | - } |
|
| 175 | - |
|
| 176 | - return $this->stream; |
|
| 177 | - } |
|
| 178 | - |
|
| 179 | - /** |
|
| 180 | - * Moves the file to the given path |
|
| 181 | - * |
|
| 182 | - * @param string $targetPath |
|
| 183 | - * |
|
| 184 | - * @return void |
|
| 185 | - * |
|
| 186 | - * @throws InvalidUploadedFileException |
|
| 187 | - * If the file has no a stream due to an error or |
|
| 188 | - * if the file was already moved. |
|
| 189 | - * |
|
| 190 | - * @throws InvalidUploadedFileOperationException |
|
| 191 | - * If the file cannot be read. |
|
| 192 | - * |
|
| 193 | - * @throws FailedUploadedFileOperationException |
|
| 194 | - * If the target path cannot be used. |
|
| 195 | - */ |
|
| 196 | - public function moveTo($targetPath): void |
|
| 197 | - { |
|
| 198 | - if (UPLOAD_ERR_OK <> $this->errorCode) { |
|
| 199 | - throw new InvalidUploadedFileException(sprintf( |
|
| 200 | - 'The uploaded file cannot be moved due to the error #%d (%s)', |
|
| 201 | - $this->errorCode, |
|
| 202 | - $this->errorMessage |
|
| 203 | - )); |
|
| 204 | - } |
|
| 205 | - |
|
| 206 | - if (!isset($this->stream)) { |
|
| 207 | - throw new InvalidUploadedFileException( |
|
| 208 | - 'The uploaded file cannot be moved because it was already moved' |
|
| 209 | - ); |
|
| 210 | - } |
|
| 211 | - |
|
| 212 | - if (!$this->stream->isReadable()) { |
|
| 213 | - throw new InvalidUploadedFileOperationException( |
|
| 214 | - 'The uploaded file cannot be moved because it is not readable' |
|
| 215 | - ); |
|
| 216 | - } |
|
| 217 | - |
|
| 218 | - $targetDir = dirname($targetPath); |
|
| 219 | - if (!is_dir($targetDir) || !is_writable($targetDir)) { |
|
| 220 | - throw new FailedUploadedFileOperationException(sprintf( |
|
| 221 | - 'The uploaded file cannot be moved because the directory "%s" is not writable', |
|
| 222 | - $targetDir |
|
| 223 | - )); |
|
| 224 | - } |
|
| 225 | - |
|
| 226 | - $targetStream = new FileStream($targetPath, 'wb'); |
|
| 227 | - |
|
| 228 | - if ($this->stream->isSeekable()) { |
|
| 229 | - $this->stream->rewind(); |
|
| 230 | - } |
|
| 231 | - |
|
| 232 | - while (!$this->stream->eof()) { |
|
| 233 | - $piece = $this->stream->read(4096); |
|
| 234 | - $targetStream->write($piece); |
|
| 235 | - } |
|
| 236 | - |
|
| 237 | - $targetStream->close(); |
|
| 238 | - |
|
| 239 | - /** @var string|null */ |
|
| 240 | - $sourcePath = $this->stream->getMetadata('uri'); |
|
| 241 | - |
|
| 242 | - $this->stream->close(); |
|
| 243 | - $this->stream = null; |
|
| 244 | - |
|
| 245 | - if (isset($sourcePath) && is_file($sourcePath)) { |
|
| 246 | - $sourceDir = dirname($sourcePath); |
|
| 247 | - if (is_writable($sourceDir)) { |
|
| 248 | - unlink($sourcePath); |
|
| 249 | - } |
|
| 250 | - } |
|
| 251 | - } |
|
| 252 | - |
|
| 253 | - /** |
|
| 254 | - * Gets the file size |
|
| 255 | - * |
|
| 256 | - * @return int|null |
|
| 257 | - */ |
|
| 258 | - public function getSize(): ?int |
|
| 259 | - { |
|
| 260 | - return $this->size; |
|
| 261 | - } |
|
| 262 | - |
|
| 263 | - /** |
|
| 264 | - * Gets the file's error code |
|
| 265 | - * |
|
| 266 | - * @return int |
|
| 267 | - */ |
|
| 268 | - public function getError(): int |
|
| 269 | - { |
|
| 270 | - return $this->errorCode; |
|
| 271 | - } |
|
| 272 | - |
|
| 273 | - /** |
|
| 274 | - * Gets the file name |
|
| 275 | - * |
|
| 276 | - * @return string|null |
|
| 277 | - */ |
|
| 278 | - public function getClientFilename(): ?string |
|
| 279 | - { |
|
| 280 | - return $this->clientFilename; |
|
| 281 | - } |
|
| 282 | - |
|
| 283 | - /** |
|
| 284 | - * Gets the file type |
|
| 285 | - * |
|
| 286 | - * @return string|null |
|
| 287 | - */ |
|
| 288 | - public function getClientMediaType(): ?string |
|
| 289 | - { |
|
| 290 | - return $this->clientMediaType; |
|
| 291 | - } |
|
| 54 | + /** |
|
| 55 | + * List of upload errors |
|
| 56 | + * |
|
| 57 | + * @link https://www.php.net/manual/en/features.file-upload.errors.php |
|
| 58 | + * |
|
| 59 | + * @var array<int, string> |
|
| 60 | + */ |
|
| 61 | + public const UPLOAD_ERRORS = [ |
|
| 62 | + UPLOAD_ERR_OK => 'No error', |
|
| 63 | + UPLOAD_ERR_INI_SIZE => 'The uploaded file exceeds the upload_max_filesize directive in php.ini', |
|
| 64 | + UPLOAD_ERR_FORM_SIZE => 'The uploaded file exceeds the MAX_FILE_SIZE directive ' . |
|
| 65 | + 'that was specified in the HTML form', |
|
| 66 | + UPLOAD_ERR_PARTIAL => 'The uploaded file was only partially uploaded', |
|
| 67 | + UPLOAD_ERR_NO_FILE => 'No file was uploaded', |
|
| 68 | + UPLOAD_ERR_NO_TMP_DIR => 'Missing a temporary folder', |
|
| 69 | + UPLOAD_ERR_CANT_WRITE => 'Failed to write file to disk', |
|
| 70 | + UPLOAD_ERR_EXTENSION => 'File upload stopped by extension', |
|
| 71 | + ]; |
|
| 72 | + |
|
| 73 | + /** |
|
| 74 | + * Unknown error description |
|
| 75 | + * |
|
| 76 | + * @var string |
|
| 77 | + */ |
|
| 78 | + public const UNKNOWN_ERROR_TEXT = 'Unknown error'; |
|
| 79 | + |
|
| 80 | + /** |
|
| 81 | + * The file stream |
|
| 82 | + * |
|
| 83 | + * @var StreamInterface|null |
|
| 84 | + */ |
|
| 85 | + private ?StreamInterface $stream = null; |
|
| 86 | + |
|
| 87 | + /** |
|
| 88 | + * The file size |
|
| 89 | + * |
|
| 90 | + * @var int|null |
|
| 91 | + */ |
|
| 92 | + private ?int $size; |
|
| 93 | + |
|
| 94 | + /** |
|
| 95 | + * The file's error code |
|
| 96 | + * |
|
| 97 | + * @var int |
|
| 98 | + */ |
|
| 99 | + private int $errorCode; |
|
| 100 | + |
|
| 101 | + /** |
|
| 102 | + * The file's error message |
|
| 103 | + * |
|
| 104 | + * @var string |
|
| 105 | + */ |
|
| 106 | + private string $errorMessage; |
|
| 107 | + |
|
| 108 | + /** |
|
| 109 | + * The file name |
|
| 110 | + * |
|
| 111 | + * @var string|null |
|
| 112 | + */ |
|
| 113 | + private ?string $clientFilename; |
|
| 114 | + |
|
| 115 | + /** |
|
| 116 | + * The file type |
|
| 117 | + * |
|
| 118 | + * @var string|null |
|
| 119 | + */ |
|
| 120 | + private ?string $clientMediaType; |
|
| 121 | + |
|
| 122 | + /** |
|
| 123 | + * Constructor of the class |
|
| 124 | + * |
|
| 125 | + * @param StreamInterface $stream |
|
| 126 | + * @param int|null $size |
|
| 127 | + * @param int $error |
|
| 128 | + * @param string|null $clientFilename |
|
| 129 | + * @param string|null $clientMediaType |
|
| 130 | + */ |
|
| 131 | + public function __construct( |
|
| 132 | + StreamInterface $stream, |
|
| 133 | + ?int $size = null, |
|
| 134 | + int $error = UPLOAD_ERR_OK, |
|
| 135 | + ?string $clientFilename = null, |
|
| 136 | + ?string $clientMediaType = null |
|
| 137 | + ) { |
|
| 138 | + if (UPLOAD_ERR_OK === $error) { |
|
| 139 | + $this->stream = $stream; |
|
| 140 | + } |
|
| 141 | + |
|
| 142 | + $errorMessage = self::UPLOAD_ERRORS[$error] ?? self::UNKNOWN_ERROR_TEXT; |
|
| 143 | + |
|
| 144 | + $this->size = $size; |
|
| 145 | + $this->errorCode = $error; |
|
| 146 | + $this->errorMessage = $errorMessage; |
|
| 147 | + $this->clientFilename = $clientFilename; |
|
| 148 | + $this->clientMediaType = $clientMediaType; |
|
| 149 | + } |
|
| 150 | + |
|
| 151 | + /** |
|
| 152 | + * Gets the file stream |
|
| 153 | + * |
|
| 154 | + * @return StreamInterface |
|
| 155 | + * |
|
| 156 | + * @throws InvalidUploadedFileException |
|
| 157 | + * If the file has no a stream due to an error or |
|
| 158 | + * if the file was already moved. |
|
| 159 | + */ |
|
| 160 | + public function getStream(): StreamInterface |
|
| 161 | + { |
|
| 162 | + if (UPLOAD_ERR_OK <> $this->errorCode) { |
|
| 163 | + throw new InvalidUploadedFileException(sprintf( |
|
| 164 | + 'The uploaded file has no a stream due to the error #%d (%s)', |
|
| 165 | + $this->errorCode, |
|
| 166 | + $this->errorMessage |
|
| 167 | + )); |
|
| 168 | + } |
|
| 169 | + |
|
| 170 | + if (!isset($this->stream)) { |
|
| 171 | + throw new InvalidUploadedFileException( |
|
| 172 | + 'The uploaded file has no a stream because it was already moved' |
|
| 173 | + ); |
|
| 174 | + } |
|
| 175 | + |
|
| 176 | + return $this->stream; |
|
| 177 | + } |
|
| 178 | + |
|
| 179 | + /** |
|
| 180 | + * Moves the file to the given path |
|
| 181 | + * |
|
| 182 | + * @param string $targetPath |
|
| 183 | + * |
|
| 184 | + * @return void |
|
| 185 | + * |
|
| 186 | + * @throws InvalidUploadedFileException |
|
| 187 | + * If the file has no a stream due to an error or |
|
| 188 | + * if the file was already moved. |
|
| 189 | + * |
|
| 190 | + * @throws InvalidUploadedFileOperationException |
|
| 191 | + * If the file cannot be read. |
|
| 192 | + * |
|
| 193 | + * @throws FailedUploadedFileOperationException |
|
| 194 | + * If the target path cannot be used. |
|
| 195 | + */ |
|
| 196 | + public function moveTo($targetPath): void |
|
| 197 | + { |
|
| 198 | + if (UPLOAD_ERR_OK <> $this->errorCode) { |
|
| 199 | + throw new InvalidUploadedFileException(sprintf( |
|
| 200 | + 'The uploaded file cannot be moved due to the error #%d (%s)', |
|
| 201 | + $this->errorCode, |
|
| 202 | + $this->errorMessage |
|
| 203 | + )); |
|
| 204 | + } |
|
| 205 | + |
|
| 206 | + if (!isset($this->stream)) { |
|
| 207 | + throw new InvalidUploadedFileException( |
|
| 208 | + 'The uploaded file cannot be moved because it was already moved' |
|
| 209 | + ); |
|
| 210 | + } |
|
| 211 | + |
|
| 212 | + if (!$this->stream->isReadable()) { |
|
| 213 | + throw new InvalidUploadedFileOperationException( |
|
| 214 | + 'The uploaded file cannot be moved because it is not readable' |
|
| 215 | + ); |
|
| 216 | + } |
|
| 217 | + |
|
| 218 | + $targetDir = dirname($targetPath); |
|
| 219 | + if (!is_dir($targetDir) || !is_writable($targetDir)) { |
|
| 220 | + throw new FailedUploadedFileOperationException(sprintf( |
|
| 221 | + 'The uploaded file cannot be moved because the directory "%s" is not writable', |
|
| 222 | + $targetDir |
|
| 223 | + )); |
|
| 224 | + } |
|
| 225 | + |
|
| 226 | + $targetStream = new FileStream($targetPath, 'wb'); |
|
| 227 | + |
|
| 228 | + if ($this->stream->isSeekable()) { |
|
| 229 | + $this->stream->rewind(); |
|
| 230 | + } |
|
| 231 | + |
|
| 232 | + while (!$this->stream->eof()) { |
|
| 233 | + $piece = $this->stream->read(4096); |
|
| 234 | + $targetStream->write($piece); |
|
| 235 | + } |
|
| 236 | + |
|
| 237 | + $targetStream->close(); |
|
| 238 | + |
|
| 239 | + /** @var string|null */ |
|
| 240 | + $sourcePath = $this->stream->getMetadata('uri'); |
|
| 241 | + |
|
| 242 | + $this->stream->close(); |
|
| 243 | + $this->stream = null; |
|
| 244 | + |
|
| 245 | + if (isset($sourcePath) && is_file($sourcePath)) { |
|
| 246 | + $sourceDir = dirname($sourcePath); |
|
| 247 | + if (is_writable($sourceDir)) { |
|
| 248 | + unlink($sourcePath); |
|
| 249 | + } |
|
| 250 | + } |
|
| 251 | + } |
|
| 252 | + |
|
| 253 | + /** |
|
| 254 | + * Gets the file size |
|
| 255 | + * |
|
| 256 | + * @return int|null |
|
| 257 | + */ |
|
| 258 | + public function getSize(): ?int |
|
| 259 | + { |
|
| 260 | + return $this->size; |
|
| 261 | + } |
|
| 262 | + |
|
| 263 | + /** |
|
| 264 | + * Gets the file's error code |
|
| 265 | + * |
|
| 266 | + * @return int |
|
| 267 | + */ |
|
| 268 | + public function getError(): int |
|
| 269 | + { |
|
| 270 | + return $this->errorCode; |
|
| 271 | + } |
|
| 272 | + |
|
| 273 | + /** |
|
| 274 | + * Gets the file name |
|
| 275 | + * |
|
| 276 | + * @return string|null |
|
| 277 | + */ |
|
| 278 | + public function getClientFilename(): ?string |
|
| 279 | + { |
|
| 280 | + return $this->clientFilename; |
|
| 281 | + } |
|
| 282 | + |
|
| 283 | + /** |
|
| 284 | + * Gets the file type |
|
| 285 | + * |
|
| 286 | + * @return string|null |
|
| 287 | + */ |
|
| 288 | + public function getClientMediaType(): ?string |
|
| 289 | + { |
|
| 290 | + return $this->clientMediaType; |
|
| 291 | + } |
|
| 292 | 292 | } |
@@ -25,11 +25,11 @@ |
||
| 25 | 25 | class ResponseFactory implements ResponseFactoryInterface |
| 26 | 26 | { |
| 27 | 27 | |
| 28 | - /** |
|
| 29 | - * {@inheritdoc} |
|
| 30 | - */ |
|
| 31 | - public function createResponse(int $code = 200, string $reasonPhrase = ''): ResponseInterface |
|
| 32 | - { |
|
| 33 | - return new Response($code, $reasonPhrase); |
|
| 34 | - } |
|
| 28 | + /** |
|
| 29 | + * {@inheritdoc} |
|
| 30 | + */ |
|
| 31 | + public function createResponse(int $code = 200, string $reasonPhrase = ''): ResponseInterface |
|
| 32 | + { |
|
| 33 | + return new Response($code, $reasonPhrase); |
|
| 34 | + } |
|
| 35 | 35 | } |