| 1 |
<?php |
| 2 |
|
| 3 |
namespace React\Http\Io; |
| 4 |
|
| 5 |
use Psr\Http\Message\StreamInterface; |
| 6 |
use Psr\Http\Message\UploadedFileInterface; |
| 7 |
use InvalidArgumentException; |
| 8 |
use RuntimeException; |
| 9 |
|
| 10 |
/** |
| 11 |
* [Internal] Implementation of the PSR-7 `UploadedFileInterface` |
| 12 |
* |
| 13 |
* This is used internally to represent each incoming file upload. |
| 14 |
* |
| 15 |
* Note that this is an internal class only and nothing you should usually care |
| 16 |
* about. See the `UploadedFileInterface` for more details. |
| 17 |
* |
| 18 |
* @see UploadedFileInterface |
| 19 |
* @internal |
| 20 |
*/ |
| 21 |
final class UploadedFile implements UploadedFileInterface |
| 22 |
{ |
| 23 |
/** |
| 24 |
* @var StreamInterface |
| 25 |
*/ |
| 26 |
private $stream; |
| 27 |
|
| 28 |
/** |
| 29 |
* @var int |
| 30 |
*/ |
| 31 |
private $size; |
| 32 |
|
| 33 |
/** |
| 34 |
* @var int |
| 35 |
*/ |
| 36 |
private $error; |
| 37 |
|
| 38 |
/** |
| 39 |
* @var string |
| 40 |
*/ |
| 41 |
private $filename; |
| 42 |
|
| 43 |
/** |
| 44 |
* @var string |
| 45 |
*/ |
| 46 |
private $mediaType; |
| 47 |
|
| 48 |
/** |
| 49 |
* @param StreamInterface $stream |
| 50 |
* @param int $size |
| 51 |
* @param int $error |
| 52 |
* @param string $filename |
| 53 |
* @param string $mediaType |
| 54 |
*/ |
| 55 |
public function __construct(StreamInterface $stream, $size, $error, $filename, $mediaType) |
| 56 |
{ |
| 57 |
$this->stream = $stream; |
| 58 |
$this->size = $size; |
| 59 |
|
| 60 |
if (!\is_int($error) || !\in_array($error, array( |
| 61 |
\UPLOAD_ERR_OK, |
| 62 |
\UPLOAD_ERR_INI_SIZE, |
| 63 |
\UPLOAD_ERR_FORM_SIZE, |
| 64 |
\UPLOAD_ERR_PARTIAL, |
| 65 |
\UPLOAD_ERR_NO_FILE, |
| 66 |
\UPLOAD_ERR_NO_TMP_DIR, |
| 67 |
\UPLOAD_ERR_CANT_WRITE, |
| 68 |
\UPLOAD_ERR_EXTENSION, |
| 69 |
))) { |
| 70 |
throw new InvalidArgumentException( |
| 71 |
'Invalid error code, must be an UPLOAD_ERR_* constant' |
| 72 |
); |
| 73 |
} |
| 74 |
$this->error = $error; |
| 75 |
$this->filename = $filename; |
| 76 |
$this->mediaType = $mediaType; |
| 77 |
} |
| 78 |
|
| 79 |
/** |
| 80 |
* {@inheritdoc} |
| 81 |
*/ |
| 82 |
public function getStream() |
| 83 |
{ |
| 84 |
if ($this->error !== \UPLOAD_ERR_OK) { |
| 85 |
throw new RuntimeException('Cannot retrieve stream due to upload error'); |
| 86 |
} |
| 87 |
|
| 88 |
return $this->stream; |
| 89 |
} |
| 90 |
|
| 91 |
/** |
| 92 |
* {@inheritdoc} |
| 93 |
*/ |
| 94 |
public function moveTo($targetPath) |
| 95 |
{ |
| 96 |
throw new RuntimeException('Not implemented'); |
| 97 |
} |
| 98 |
|
| 99 |
/** |
| 100 |
* {@inheritdoc} |
| 101 |
*/ |
| 102 |
public function getSize() |
| 103 |
{ |
| 104 |
return $this->size; |
| 105 |
} |
| 106 |
|
| 107 |
/** |
| 108 |
* {@inheritdoc} |
| 109 |
*/ |
| 110 |
public function getError() |
| 111 |
{ |
| 112 |
return $this->error; |
| 113 |
} |
| 114 |
|
| 115 |
/** |
| 116 |
* {@inheritdoc} |
| 117 |
*/ |
| 118 |
public function getClientFilename() |
| 119 |
{ |
| 120 |
return $this->filename; |
| 121 |
} |
| 122 |
|
| 123 |
/** |
| 124 |
* {@inheritdoc} |
| 125 |
*/ |
| 126 |
public function getClientMediaType() |
| 127 |
{ |
| 128 |
return $this->mediaType; |
| 129 |
} |
| 130 |
} |