Open Journal Systems  3.3.0
vendor/guzzlehttp/psr7/src/UploadedFile.php
1 <?php
2 namespace GuzzleHttp\Psr7;
3 
4 use InvalidArgumentException;
7 use RuntimeException;
8 
10 {
14  private static $errors = [
15  UPLOAD_ERR_OK,
16  UPLOAD_ERR_INI_SIZE,
17  UPLOAD_ERR_FORM_SIZE,
18  UPLOAD_ERR_PARTIAL,
19  UPLOAD_ERR_NO_FILE,
20  UPLOAD_ERR_NO_TMP_DIR,
21  UPLOAD_ERR_CANT_WRITE,
22  UPLOAD_ERR_EXTENSION,
23  ];
24 
28  private $clientFilename;
29 
33  private $clientMediaType;
34 
38  private $error;
39 
43  private $file;
44 
48  private $moved = false;
49 
53  private $size;
54 
58  private $stream;
59 
67  public function __construct(
68  $streamOrFile,
69  $size,
70  $errorStatus,
71  $clientFilename = null,
72  $clientMediaType = null
73  ) {
74  $this->setError($errorStatus);
75  $this->setSize($size);
76  $this->setClientFilename($clientFilename);
77  $this->setClientMediaType($clientMediaType);
78 
79  if ($this->isOk()) {
80  $this->setStreamOrFile($streamOrFile);
81  }
82  }
83 
90  private function setStreamOrFile($streamOrFile)
91  {
92  if (is_string($streamOrFile)) {
93  $this->file = $streamOrFile;
94  } elseif (is_resource($streamOrFile)) {
95  $this->stream = new Stream($streamOrFile);
96  } elseif ($streamOrFile instanceof StreamInterface) {
97  $this->stream = $streamOrFile;
98  } else {
99  throw new InvalidArgumentException(
100  'Invalid stream or file provided for UploadedFile'
101  );
102  }
103  }
104 
109  private function setError($error)
110  {
111  if (false === is_int($error)) {
112  throw new InvalidArgumentException(
113  'Upload file error status must be an integer'
114  );
115  }
116 
117  if (false === in_array($error, UploadedFile::$errors)) {
118  throw new InvalidArgumentException(
119  'Invalid error status for UploadedFile'
120  );
121  }
122 
123  $this->error = $error;
124  }
125 
130  private function setSize($size)
131  {
132  if (false === is_int($size)) {
133  throw new InvalidArgumentException(
134  'Upload file size must be an integer'
135  );
136  }
137 
138  $this->size = $size;
139  }
140 
145  private function isStringOrNull($param)
146  {
147  return in_array(gettype($param), ['string', 'NULL']);
148  }
149 
154  private function isStringNotEmpty($param)
155  {
156  return is_string($param) && false === empty($param);
157  }
158 
163  private function setClientFilename($clientFilename)
164  {
165  if (false === $this->isStringOrNull($clientFilename)) {
166  throw new InvalidArgumentException(
167  'Upload file client filename must be a string or null'
168  );
169  }
170 
171  $this->clientFilename = $clientFilename;
172  }
173 
178  private function setClientMediaType($clientMediaType)
179  {
180  if (false === $this->isStringOrNull($clientMediaType)) {
181  throw new InvalidArgumentException(
182  'Upload file client media type must be a string or null'
183  );
184  }
185 
186  $this->clientMediaType = $clientMediaType;
187  }
188 
194  private function isOk()
195  {
196  return $this->error === UPLOAD_ERR_OK;
197  }
198 
202  public function isMoved()
203  {
204  return $this->moved;
205  }
206 
210  private function validateActive()
211  {
212  if (false === $this->isOk()) {
213  throw new RuntimeException('Cannot retrieve stream due to upload error');
214  }
215 
216  if ($this->isMoved()) {
217  throw new RuntimeException('Cannot retrieve stream after it has already been moved');
218  }
219  }
220 
225  public function getStream()
226  {
227  $this->validateActive();
228 
229  if ($this->stream instanceof StreamInterface) {
230  return $this->stream;
231  }
232 
233  return new LazyOpenStream($this->file, 'r+');
234  }
235 
247  public function moveTo($targetPath)
248  {
249  $this->validateActive();
250 
251  if (false === $this->isStringNotEmpty($targetPath)) {
252  throw new InvalidArgumentException(
253  'Invalid path provided for move operation; must be a non-empty string'
254  );
255  }
256 
257  if ($this->file) {
258  $this->moved = php_sapi_name() == 'cli'
259  ? rename($this->file, $targetPath)
260  : move_uploaded_file($this->file, $targetPath);
261  } else {
263  $this->getStream(),
264  new LazyOpenStream($targetPath, 'w')
265  );
266 
267  $this->moved = true;
268  }
269 
270  if (false === $this->moved) {
271  throw new RuntimeException(
272  sprintf('Uploaded file could not be moved to %s', $targetPath)
273  );
274  }
275  }
276 
282  public function getSize()
283  {
284  return $this->size;
285  }
286 
293  public function getError()
294  {
295  return $this->error;
296  }
297 
304  public function getClientFilename()
305  {
306  return $this->clientFilename;
307  }
308 
312  public function getClientMediaType()
313  {
314  return $this->clientMediaType;
315  }
316 }
Psr\Http\Message\UploadedFileInterface
Definition: UploadedFileInterface.php:13
Psr\Http\Message\StreamInterface
Definition: vendor/psr/http-message/src/StreamInterface.php:12
GuzzleHttp\Psr7\UploadedFile\isMoved
isMoved()
Definition: vendor/guzzlehttp/psr7/src/UploadedFile.php:223
GuzzleHttp\Psr7\UploadedFile\getError
getError()
Definition: vendor/guzzlehttp/psr7/src/UploadedFile.php:314
GuzzleHttp\Psr7\UploadedFile\getStream
getStream()
Definition: vendor/guzzlehttp/psr7/src/UploadedFile.php:246
GuzzleHttp\Psr7\UploadedFile\getClientMediaType
getClientMediaType()
Definition: vendor/guzzlehttp/psr7/src/UploadedFile.php:333
GuzzleHttp\Psr7
Definition: AppendStream.php:2
GuzzleHttp\Psr7\LazyOpenStream
Definition: LazyOpenStream.php:10
GuzzleHttp\Psr7\UploadedFile\getClientFilename
getClientFilename()
Definition: vendor/guzzlehttp/psr7/src/UploadedFile.php:325
GuzzleHttp\Psr7\UploadedFile\getSize
getSize()
Definition: vendor/guzzlehttp/psr7/src/UploadedFile.php:303
GuzzleHttp\Psr7\copy_to_stream
copy_to_stream(StreamInterface $source, StreamInterface $dest, $maxLen=-1)
Definition: guzzlehttp/psr7/src/functions.php:373
GuzzleHttp\Psr7\UploadedFile
Definition: vendor/guzzlehttp/psr7/src/UploadedFile.php:9
GuzzleHttp\Psr7\UploadedFile\__construct
__construct( $streamOrFile, $size, $errorStatus, $clientFilename=null, $clientMediaType=null)
Definition: vendor/guzzlehttp/psr7/src/UploadedFile.php:88
GuzzleHttp\Psr7\Stream
Definition: vendor/guzzlehttp/psr7/src/Stream.php:11
GuzzleHttp\Psr7\UploadedFile\moveTo
moveTo($targetPath)
Definition: vendor/guzzlehttp/psr7/src/UploadedFile.php:268
GuzzleHttp\Exception\InvalidArgumentException
Definition: vendor/guzzlehttp/guzzle/src/Exception/InvalidArgumentException.php:5