PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 28.5
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v28.5
28.5 28.4 28.3 28.2 28.1 28.0 27.9 27.8 27.7 27.6 27.5 trunk 18.0 18.1 18.2 18.3 18.4 18.4.1 18.5 18.5.1 18.6 18.7 18.8 18.9 19.0 All 129 releases
wordpress-seo / vendor_prefixed / guzzlehttp / psr7 / src / UploadedFile.php

UploadedFile.php in Yoast SEO – Advanced SEO with real-time guidance and built-in AI 28.5, at vendor_prefixed/guzzlehttp/psr7/src/UploadedFile.php

153 lines 5.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 declare (strict_types=1);
4 namespace YoastSEO_Vendor\GuzzleHttp\Psr7;
5
6 use InvalidArgumentException;
7 use YoastSEO_Vendor\Psr\Http\Message\StreamInterface;
8 use YoastSEO_Vendor\Psr\Http\Message\UploadedFileInterface;
9 use RuntimeException;
10 class UploadedFile implements \YoastSEO_Vendor\Psr\Http\Message\UploadedFileInterface
11 {
12 private const ERROR_MAP = [\UPLOAD_ERR_OK => 'UPLOAD_ERR_OK', \UPLOAD_ERR_INI_SIZE => 'UPLOAD_ERR_INI_SIZE', \UPLOAD_ERR_FORM_SIZE => 'UPLOAD_ERR_FORM_SIZE', \UPLOAD_ERR_PARTIAL => 'UPLOAD_ERR_PARTIAL', \UPLOAD_ERR_NO_FILE => 'UPLOAD_ERR_NO_FILE', \UPLOAD_ERR_NO_TMP_DIR => 'UPLOAD_ERR_NO_TMP_DIR', \UPLOAD_ERR_CANT_WRITE => 'UPLOAD_ERR_CANT_WRITE', \UPLOAD_ERR_EXTENSION => 'UPLOAD_ERR_EXTENSION'];
13 /**
14 * @var string|null
15 */
16 private $clientFilename;
17 /**
18 * @var string|null
19 */
20 private $clientMediaType;
21 /**
22 * @var int
23 */
24 private $error;
25 /**
26 * @var string|null
27 */
28 private $file;
29 /**
30 * @var bool
31 */
32 private $moved = \false;
33 /**
34 * @var int|null
35 */
36 private $size;
37 /**
38 * @var StreamInterface|null
39 */
40 private $stream;
41 /**
42 * @param StreamInterface|string|resource $streamOrFile
43 */
44 public function __construct($streamOrFile, ?int $size, int $errorStatus, ?string $clientFilename = null, ?string $clientMediaType = null)
45 {
46 $this->setError($errorStatus);
47 $this->size = $size;
48 $this->clientFilename = $clientFilename;
49 $this->clientMediaType = $clientMediaType;
50 if ($this->isOk()) {
51 $this->setStreamOrFile($streamOrFile);
52 }
53 }
54 /**
55 * Depending on the value set file or stream variable
56 *
57 * @param StreamInterface|string|resource $streamOrFile
58 *
59 * @throws InvalidArgumentException
60 */
61 private function setStreamOrFile($streamOrFile) : void
62 {
63 if (\is_string($streamOrFile)) {
64 $this->file = $streamOrFile;
65 } elseif (\is_resource($streamOrFile)) {
66 $this->stream = new \YoastSEO_Vendor\GuzzleHttp\Psr7\Stream($streamOrFile);
67 } elseif ($streamOrFile instanceof \YoastSEO_Vendor\Psr\Http\Message\StreamInterface) {
68 $this->stream = $streamOrFile;
69 } else {
70 throw new \InvalidArgumentException('Invalid stream or file provided for UploadedFile');
71 }
72 }
73 /**
74 * @throws InvalidArgumentException
75 */
76 private function setError(int $error) : void
77 {
78 if (!isset(\YoastSEO_Vendor\GuzzleHttp\Psr7\UploadedFile::ERROR_MAP[$error])) {
79 throw new \InvalidArgumentException('Invalid error status for UploadedFile');
80 }
81 $this->error = $error;
82 }
83 private static function isStringNotEmpty($param) : bool
84 {
85 return \is_string($param) && \false === empty($param);
86 }
87 /**
88 * Return true if there is no upload error
89 */
90 private function isOk() : bool
91 {
92 return $this->error === \UPLOAD_ERR_OK;
93 }
94 public function isMoved() : bool
95 {
96 return $this->moved;
97 }
98 /**
99 * @throws RuntimeException if is moved or not ok
100 */
101 private function validateActive() : void
102 {
103 if (\false === $this->isOk()) {
104 throw new \RuntimeException(\sprintf('Cannot retrieve stream due to upload error (%s)', self::ERROR_MAP[$this->error]));
105 }
106 if ($this->isMoved()) {
107 throw new \RuntimeException('Cannot retrieve stream after it has already been moved');
108 }
109 }
110 public function getStream() : \YoastSEO_Vendor\Psr\Http\Message\StreamInterface
111 {
112 $this->validateActive();
113 if ($this->stream instanceof \YoastSEO_Vendor\Psr\Http\Message\StreamInterface) {
114 return $this->stream;
115 }
116 /** @var string $file */
117 $file = $this->file;
118 return new \YoastSEO_Vendor\GuzzleHttp\Psr7\LazyOpenStream($file, 'r+');
119 }
120 public function moveTo($targetPath) : void
121 {
122 $this->validateActive();
123 if (\false === self::isStringNotEmpty($targetPath)) {
124 throw new \InvalidArgumentException('Invalid path provided for move operation; must be a non-empty string');
125 }
126 if ($this->file) {
127 $this->moved = \PHP_SAPI === 'cli' ? \rename($this->file, $targetPath) : \move_uploaded_file($this->file, $targetPath);
128 } else {
129 \YoastSEO_Vendor\GuzzleHttp\Psr7\Utils::copyToStream($this->getStream(), new \YoastSEO_Vendor\GuzzleHttp\Psr7\LazyOpenStream($targetPath, 'w'));
130 $this->moved = \true;
131 }
132 if (\false === $this->moved) {
133 throw new \RuntimeException(\sprintf('Uploaded file could not be moved to %s', $targetPath));
134 }
135 }
136 public function getSize() : ?int
137 {
138 return $this->size;
139 }
140 public function getError() : int
141 {
142 return $this->error;
143 }
144 public function getClientFilename() : ?string
145 {
146 return $this->clientFilename;
147 }
148 public function getClientMediaType() : ?string
149 {
150 return $this->clientMediaType;
151 }
152 }
153