@@ -43,44 +43,44 @@ |
||
| 43 | 43 | */ |
| 44 | 44 | function server_request_files(?array $files = null): array |
| 45 | 45 | { |
| 46 | - $files ??= $_FILES; |
|
| 46 | + $files ??= $_FILES; |
|
| 47 | 47 | |
| 48 | - $walker = static function ($path, $size, $error, $name, $type) use (&$walker) { |
|
| 49 | - if (!is_array($path)) { |
|
| 50 | - // It makes no sense to create a stream if the file has not been successfully uploaded. |
|
| 51 | - $stream = UPLOAD_ERR_OK <> $error ? null : new FileStream($path, 'rb'); |
|
| 48 | + $walker = static function ($path, $size, $error, $name, $type) use (&$walker) { |
|
| 49 | + if (!is_array($path)) { |
|
| 50 | + // It makes no sense to create a stream if the file has not been successfully uploaded. |
|
| 51 | + $stream = UPLOAD_ERR_OK <> $error ? null : new FileStream($path, 'rb'); |
|
| 52 | 52 | |
| 53 | - return new UploadedFile($stream, $size, $error, $name, $type); |
|
| 54 | - } |
|
| 53 | + return new UploadedFile($stream, $size, $error, $name, $type); |
|
| 54 | + } |
|
| 55 | 55 | |
| 56 | - $result = []; |
|
| 57 | - foreach ($path as $key => $_) { |
|
| 58 | - if (UPLOAD_ERR_NO_FILE <> $error[$key]) { |
|
| 59 | - $result[$key] = $walker( |
|
| 60 | - $path[$key], |
|
| 61 | - $size[$key], |
|
| 62 | - $error[$key], |
|
| 63 | - $name[$key], |
|
| 64 | - $type[$key] |
|
| 65 | - ); |
|
| 66 | - } |
|
| 67 | - } |
|
| 56 | + $result = []; |
|
| 57 | + foreach ($path as $key => $_) { |
|
| 58 | + if (UPLOAD_ERR_NO_FILE <> $error[$key]) { |
|
| 59 | + $result[$key] = $walker( |
|
| 60 | + $path[$key], |
|
| 61 | + $size[$key], |
|
| 62 | + $error[$key], |
|
| 63 | + $name[$key], |
|
| 64 | + $type[$key] |
|
| 65 | + ); |
|
| 66 | + } |
|
| 67 | + } |
|
| 68 | 68 | |
| 69 | - return $result; |
|
| 70 | - }; |
|
| 69 | + return $result; |
|
| 70 | + }; |
|
| 71 | 71 | |
| 72 | - $result = []; |
|
| 73 | - foreach ($files as $key => $file) { |
|
| 74 | - if (UPLOAD_ERR_NO_FILE <> $file['error']) { |
|
| 75 | - $result[$key] = $walker( |
|
| 76 | - $file['tmp_name'], |
|
| 77 | - $file['size'], |
|
| 78 | - $file['error'], |
|
| 79 | - $file['name'], |
|
| 80 | - $file['type'] |
|
| 81 | - ); |
|
| 82 | - } |
|
| 83 | - } |
|
| 72 | + $result = []; |
|
| 73 | + foreach ($files as $key => $file) { |
|
| 74 | + if (UPLOAD_ERR_NO_FILE <> $file['error']) { |
|
| 75 | + $result[$key] = $walker( |
|
| 76 | + $file['tmp_name'], |
|
| 77 | + $file['size'], |
|
| 78 | + $file['error'], |
|
| 79 | + $file['name'], |
|
| 80 | + $file['type'] |
|
| 81 | + ); |
|
| 82 | + } |
|
| 83 | + } |
|
| 84 | 84 | |
| 85 | - return $result; |
|
| 85 | + return $result; |
|
| 86 | 86 | } |
@@ -45,7 +45,7 @@ |
||
| 45 | 45 | { |
| 46 | 46 | $files ??= $_FILES; |
| 47 | 47 | |
| 48 | - $walker = static function ($path, $size, $error, $name, $type) use (&$walker) { |
|
| 48 | + $walker = static function($path, $size, $error, $name, $type) use (&$walker) { |
|
| 49 | 49 | if (!is_array($path)) { |
| 50 | 50 | // It makes no sense to create a stream if the file has not been successfully uploaded. |
| 51 | 51 | $stream = UPLOAD_ERR_OK <> $error ? null : new FileStream($path, 'rb'); |
@@ -28,7 +28,7 @@ |
||
| 28 | 28 | */ |
| 29 | 29 | function server_request_method(?array $serverParams = null): string |
| 30 | 30 | { |
| 31 | - $serverParams ??= $_SERVER; |
|
| 31 | + $serverParams ??= $_SERVER; |
|
| 32 | 32 | |
| 33 | - return $serverParams['REQUEST_METHOD'] ?? RequestMethodInterface::METHOD_GET; |
|
| 33 | + return $serverParams['REQUEST_METHOD'] ?? RequestMethodInterface::METHOD_GET; |
|
| 34 | 34 | } |
@@ -49,230 +49,230 @@ |
||
| 49 | 49 | class UploadedFile implements UploadedFileInterface |
| 50 | 50 | { |
| 51 | 51 | |
| 52 | - /** |
|
| 53 | - * List of upload errors |
|
| 54 | - * |
|
| 55 | - * @link https://www.php.net/manual/en/features.file-upload.errors.php |
|
| 56 | - * |
|
| 57 | - * @var array<int, non-empty-string> |
|
| 58 | - */ |
|
| 59 | - public const UPLOAD_ERRORS = [ |
|
| 60 | - UPLOAD_ERR_OK => 'No error', |
|
| 61 | - UPLOAD_ERR_INI_SIZE => 'Uploaded file exceeds the upload_max_filesize directive in the php.ini', |
|
| 62 | - UPLOAD_ERR_FORM_SIZE => 'Uploaded file exceeds the MAX_FILE_SIZE directive in the HTML form', |
|
| 63 | - UPLOAD_ERR_PARTIAL => 'Uploaded file was only partially uploaded', |
|
| 64 | - UPLOAD_ERR_NO_FILE => 'No file was uploaded', |
|
| 65 | - UPLOAD_ERR_NO_TMP_DIR => 'Missing temporary directory', |
|
| 66 | - UPLOAD_ERR_CANT_WRITE => 'Failed to write file to disk', |
|
| 67 | - UPLOAD_ERR_EXTENSION => 'File upload was stopped by a PHP extension', |
|
| 68 | - ]; |
|
| 69 | - |
|
| 70 | - /** |
|
| 71 | - * The file stream |
|
| 72 | - * |
|
| 73 | - * @var StreamInterface|null |
|
| 74 | - */ |
|
| 75 | - private ?StreamInterface $stream = null; |
|
| 76 | - |
|
| 77 | - /** |
|
| 78 | - * The file size |
|
| 79 | - * |
|
| 80 | - * @var int|null |
|
| 81 | - */ |
|
| 82 | - private ?int $size; |
|
| 83 | - |
|
| 84 | - /** |
|
| 85 | - * The file's error code |
|
| 86 | - * |
|
| 87 | - * @var int |
|
| 88 | - */ |
|
| 89 | - private int $errorCode; |
|
| 90 | - |
|
| 91 | - /** |
|
| 92 | - * The file's error message |
|
| 93 | - * |
|
| 94 | - * @var string |
|
| 95 | - */ |
|
| 96 | - private string $errorMessage; |
|
| 97 | - |
|
| 98 | - /** |
|
| 99 | - * The client's file name |
|
| 100 | - * |
|
| 101 | - * @var string|null |
|
| 102 | - */ |
|
| 103 | - private ?string $clientFilename; |
|
| 104 | - |
|
| 105 | - /** |
|
| 106 | - * The client's file media type |
|
| 107 | - * |
|
| 108 | - * @var string|null |
|
| 109 | - */ |
|
| 110 | - private ?string $clientMediaType; |
|
| 111 | - |
|
| 112 | - /** |
|
| 113 | - * Constructor of the class |
|
| 114 | - * |
|
| 115 | - * @param StreamInterface|null $stream |
|
| 116 | - * @param int|null $size |
|
| 117 | - * @param int $error |
|
| 118 | - * @param string|null $clientFilename |
|
| 119 | - * @param string|null $clientMediaType |
|
| 120 | - */ |
|
| 121 | - public function __construct( |
|
| 122 | - ?StreamInterface $stream, |
|
| 123 | - ?int $size = null, |
|
| 124 | - int $error = UPLOAD_ERR_OK, |
|
| 125 | - ?string $clientFilename = null, |
|
| 126 | - ?string $clientMediaType = null |
|
| 127 | - ) { |
|
| 128 | - // It doesn't make sense to keep the stream if the file wasn't successfully uploaded... |
|
| 129 | - if (UPLOAD_ERR_OK === $error) { |
|
| 130 | - $this->stream = $stream; |
|
| 131 | - } |
|
| 132 | - |
|
| 133 | - $this->size = $size; |
|
| 134 | - $this->errorCode = $error; |
|
| 135 | - $this->errorMessage = self::UPLOAD_ERRORS[$error] ?? 'Unknown error'; |
|
| 136 | - $this->clientFilename = $clientFilename; |
|
| 137 | - $this->clientMediaType = $clientMediaType; |
|
| 138 | - } |
|
| 139 | - |
|
| 140 | - /** |
|
| 141 | - * Gets the file stream |
|
| 142 | - * |
|
| 143 | - * @return StreamInterface |
|
| 144 | - * |
|
| 145 | - * @throws RuntimeException |
|
| 146 | - * - If the file has no a stream due to an error; |
|
| 147 | - * - If the file was already moved. |
|
| 148 | - */ |
|
| 149 | - public function getStream(): StreamInterface |
|
| 150 | - { |
|
| 151 | - if (UPLOAD_ERR_OK <> $this->errorCode) { |
|
| 152 | - throw new RuntimeException(sprintf( |
|
| 153 | - 'Uploaded file has no a stream due to the error #%d (%s)', |
|
| 154 | - $this->errorCode, |
|
| 155 | - $this->errorMessage |
|
| 156 | - )); |
|
| 157 | - } |
|
| 158 | - |
|
| 159 | - if (!isset($this->stream)) { |
|
| 160 | - throw new RuntimeException( |
|
| 161 | - 'Uploaded file has no a stream because it was already moved' |
|
| 162 | - ); |
|
| 163 | - } |
|
| 164 | - |
|
| 165 | - return $this->stream; |
|
| 166 | - } |
|
| 167 | - |
|
| 168 | - /** |
|
| 169 | - * Moves the file to the given path |
|
| 170 | - * |
|
| 171 | - * @param string $targetPath |
|
| 172 | - * |
|
| 173 | - * @return void |
|
| 174 | - * |
|
| 175 | - * @throws RuntimeException |
|
| 176 | - * - If the file has no a stream due to an error; |
|
| 177 | - * - If the file was already moved; |
|
| 178 | - * - If the file cannot be read; |
|
| 179 | - * - If the target path cannot be used. |
|
| 180 | - */ |
|
| 181 | - public function moveTo($targetPath): void |
|
| 182 | - { |
|
| 183 | - if (UPLOAD_ERR_OK <> $this->errorCode) { |
|
| 184 | - throw new RuntimeException(sprintf( |
|
| 185 | - 'Uploaded file cannot be moved due to the error #%d (%s)', |
|
| 186 | - $this->errorCode, |
|
| 187 | - $this->errorMessage |
|
| 188 | - )); |
|
| 189 | - } |
|
| 190 | - |
|
| 191 | - if (!isset($this->stream)) { |
|
| 192 | - throw new RuntimeException( |
|
| 193 | - 'Uploaded file cannot be moved because it was already moved' |
|
| 194 | - ); |
|
| 195 | - } |
|
| 196 | - |
|
| 197 | - if (!$this->stream->isReadable()) { |
|
| 198 | - throw new RuntimeException( |
|
| 199 | - 'Uploaded file cannot be moved because it is not readable' |
|
| 200 | - ); |
|
| 201 | - } |
|
| 202 | - |
|
| 203 | - $targetDir = dirname($targetPath); |
|
| 204 | - if (!is_dir($targetDir) || !is_writable($targetDir)) { |
|
| 205 | - throw new RuntimeException(sprintf( |
|
| 206 | - 'Uploaded file cannot be moved because the directory "%s" is not writable', |
|
| 207 | - $targetDir |
|
| 208 | - )); |
|
| 209 | - } |
|
| 210 | - |
|
| 211 | - $targetStream = new FileStream($targetPath, 'wb'); |
|
| 212 | - |
|
| 213 | - if ($this->stream->isSeekable()) { |
|
| 214 | - $this->stream->rewind(); |
|
| 215 | - } |
|
| 216 | - |
|
| 217 | - while (!$this->stream->eof()) { |
|
| 218 | - $targetStream->write( |
|
| 219 | - $this->stream->read(4096) |
|
| 220 | - ); |
|
| 221 | - } |
|
| 222 | - |
|
| 223 | - $targetStream->close(); |
|
| 224 | - |
|
| 225 | - /** @var string|null */ |
|
| 226 | - $sourcePath = $this->stream->getMetadata('uri'); |
|
| 227 | - |
|
| 228 | - $this->stream->close(); |
|
| 229 | - $this->stream = null; |
|
| 230 | - |
|
| 231 | - if (isset($sourcePath) && is_file($sourcePath)) { |
|
| 232 | - $sourceDir = dirname($sourcePath); |
|
| 233 | - if (is_writable($sourceDir)) { |
|
| 234 | - unlink($sourcePath); |
|
| 235 | - } |
|
| 236 | - } |
|
| 237 | - } |
|
| 238 | - |
|
| 239 | - /** |
|
| 240 | - * Gets the file size |
|
| 241 | - * |
|
| 242 | - * @return int|null |
|
| 243 | - */ |
|
| 244 | - public function getSize(): ?int |
|
| 245 | - { |
|
| 246 | - return $this->size; |
|
| 247 | - } |
|
| 248 | - |
|
| 249 | - /** |
|
| 250 | - * Gets the file's error code |
|
| 251 | - * |
|
| 252 | - * @return int |
|
| 253 | - */ |
|
| 254 | - public function getError(): int |
|
| 255 | - { |
|
| 256 | - return $this->errorCode; |
|
| 257 | - } |
|
| 258 | - |
|
| 259 | - /** |
|
| 260 | - * Gets the client's file name |
|
| 261 | - * |
|
| 262 | - * @return string|null |
|
| 263 | - */ |
|
| 264 | - public function getClientFilename(): ?string |
|
| 265 | - { |
|
| 266 | - return $this->clientFilename; |
|
| 267 | - } |
|
| 268 | - |
|
| 269 | - /** |
|
| 270 | - * Gets the client's file media type |
|
| 271 | - * |
|
| 272 | - * @return string|null |
|
| 273 | - */ |
|
| 274 | - public function getClientMediaType(): ?string |
|
| 275 | - { |
|
| 276 | - return $this->clientMediaType; |
|
| 277 | - } |
|
| 52 | + /** |
|
| 53 | + * List of upload errors |
|
| 54 | + * |
|
| 55 | + * @link https://www.php.net/manual/en/features.file-upload.errors.php |
|
| 56 | + * |
|
| 57 | + * @var array<int, non-empty-string> |
|
| 58 | + */ |
|
| 59 | + public const UPLOAD_ERRORS = [ |
|
| 60 | + UPLOAD_ERR_OK => 'No error', |
|
| 61 | + UPLOAD_ERR_INI_SIZE => 'Uploaded file exceeds the upload_max_filesize directive in the php.ini', |
|
| 62 | + UPLOAD_ERR_FORM_SIZE => 'Uploaded file exceeds the MAX_FILE_SIZE directive in the HTML form', |
|
| 63 | + UPLOAD_ERR_PARTIAL => 'Uploaded file was only partially uploaded', |
|
| 64 | + UPLOAD_ERR_NO_FILE => 'No file was uploaded', |
|
| 65 | + UPLOAD_ERR_NO_TMP_DIR => 'Missing temporary directory', |
|
| 66 | + UPLOAD_ERR_CANT_WRITE => 'Failed to write file to disk', |
|
| 67 | + UPLOAD_ERR_EXTENSION => 'File upload was stopped by a PHP extension', |
|
| 68 | + ]; |
|
| 69 | + |
|
| 70 | + /** |
|
| 71 | + * The file stream |
|
| 72 | + * |
|
| 73 | + * @var StreamInterface|null |
|
| 74 | + */ |
|
| 75 | + private ?StreamInterface $stream = null; |
|
| 76 | + |
|
| 77 | + /** |
|
| 78 | + * The file size |
|
| 79 | + * |
|
| 80 | + * @var int|null |
|
| 81 | + */ |
|
| 82 | + private ?int $size; |
|
| 83 | + |
|
| 84 | + /** |
|
| 85 | + * The file's error code |
|
| 86 | + * |
|
| 87 | + * @var int |
|
| 88 | + */ |
|
| 89 | + private int $errorCode; |
|
| 90 | + |
|
| 91 | + /** |
|
| 92 | + * The file's error message |
|
| 93 | + * |
|
| 94 | + * @var string |
|
| 95 | + */ |
|
| 96 | + private string $errorMessage; |
|
| 97 | + |
|
| 98 | + /** |
|
| 99 | + * The client's file name |
|
| 100 | + * |
|
| 101 | + * @var string|null |
|
| 102 | + */ |
|
| 103 | + private ?string $clientFilename; |
|
| 104 | + |
|
| 105 | + /** |
|
| 106 | + * The client's file media type |
|
| 107 | + * |
|
| 108 | + * @var string|null |
|
| 109 | + */ |
|
| 110 | + private ?string $clientMediaType; |
|
| 111 | + |
|
| 112 | + /** |
|
| 113 | + * Constructor of the class |
|
| 114 | + * |
|
| 115 | + * @param StreamInterface|null $stream |
|
| 116 | + * @param int|null $size |
|
| 117 | + * @param int $error |
|
| 118 | + * @param string|null $clientFilename |
|
| 119 | + * @param string|null $clientMediaType |
|
| 120 | + */ |
|
| 121 | + public function __construct( |
|
| 122 | + ?StreamInterface $stream, |
|
| 123 | + ?int $size = null, |
|
| 124 | + int $error = UPLOAD_ERR_OK, |
|
| 125 | + ?string $clientFilename = null, |
|
| 126 | + ?string $clientMediaType = null |
|
| 127 | + ) { |
|
| 128 | + // It doesn't make sense to keep the stream if the file wasn't successfully uploaded... |
|
| 129 | + if (UPLOAD_ERR_OK === $error) { |
|
| 130 | + $this->stream = $stream; |
|
| 131 | + } |
|
| 132 | + |
|
| 133 | + $this->size = $size; |
|
| 134 | + $this->errorCode = $error; |
|
| 135 | + $this->errorMessage = self::UPLOAD_ERRORS[$error] ?? 'Unknown error'; |
|
| 136 | + $this->clientFilename = $clientFilename; |
|
| 137 | + $this->clientMediaType = $clientMediaType; |
|
| 138 | + } |
|
| 139 | + |
|
| 140 | + /** |
|
| 141 | + * Gets the file stream |
|
| 142 | + * |
|
| 143 | + * @return StreamInterface |
|
| 144 | + * |
|
| 145 | + * @throws RuntimeException |
|
| 146 | + * - If the file has no a stream due to an error; |
|
| 147 | + * - If the file was already moved. |
|
| 148 | + */ |
|
| 149 | + public function getStream(): StreamInterface |
|
| 150 | + { |
|
| 151 | + if (UPLOAD_ERR_OK <> $this->errorCode) { |
|
| 152 | + throw new RuntimeException(sprintf( |
|
| 153 | + 'Uploaded file has no a stream due to the error #%d (%s)', |
|
| 154 | + $this->errorCode, |
|
| 155 | + $this->errorMessage |
|
| 156 | + )); |
|
| 157 | + } |
|
| 158 | + |
|
| 159 | + if (!isset($this->stream)) { |
|
| 160 | + throw new RuntimeException( |
|
| 161 | + 'Uploaded file has no a stream because it was already moved' |
|
| 162 | + ); |
|
| 163 | + } |
|
| 164 | + |
|
| 165 | + return $this->stream; |
|
| 166 | + } |
|
| 167 | + |
|
| 168 | + /** |
|
| 169 | + * Moves the file to the given path |
|
| 170 | + * |
|
| 171 | + * @param string $targetPath |
|
| 172 | + * |
|
| 173 | + * @return void |
|
| 174 | + * |
|
| 175 | + * @throws RuntimeException |
|
| 176 | + * - If the file has no a stream due to an error; |
|
| 177 | + * - If the file was already moved; |
|
| 178 | + * - If the file cannot be read; |
|
| 179 | + * - If the target path cannot be used. |
|
| 180 | + */ |
|
| 181 | + public function moveTo($targetPath): void |
|
| 182 | + { |
|
| 183 | + if (UPLOAD_ERR_OK <> $this->errorCode) { |
|
| 184 | + throw new RuntimeException(sprintf( |
|
| 185 | + 'Uploaded file cannot be moved due to the error #%d (%s)', |
|
| 186 | + $this->errorCode, |
|
| 187 | + $this->errorMessage |
|
| 188 | + )); |
|
| 189 | + } |
|
| 190 | + |
|
| 191 | + if (!isset($this->stream)) { |
|
| 192 | + throw new RuntimeException( |
|
| 193 | + 'Uploaded file cannot be moved because it was already moved' |
|
| 194 | + ); |
|
| 195 | + } |
|
| 196 | + |
|
| 197 | + if (!$this->stream->isReadable()) { |
|
| 198 | + throw new RuntimeException( |
|
| 199 | + 'Uploaded file cannot be moved because it is not readable' |
|
| 200 | + ); |
|
| 201 | + } |
|
| 202 | + |
|
| 203 | + $targetDir = dirname($targetPath); |
|
| 204 | + if (!is_dir($targetDir) || !is_writable($targetDir)) { |
|
| 205 | + throw new RuntimeException(sprintf( |
|
| 206 | + 'Uploaded file cannot be moved because the directory "%s" is not writable', |
|
| 207 | + $targetDir |
|
| 208 | + )); |
|
| 209 | + } |
|
| 210 | + |
|
| 211 | + $targetStream = new FileStream($targetPath, 'wb'); |
|
| 212 | + |
|
| 213 | + if ($this->stream->isSeekable()) { |
|
| 214 | + $this->stream->rewind(); |
|
| 215 | + } |
|
| 216 | + |
|
| 217 | + while (!$this->stream->eof()) { |
|
| 218 | + $targetStream->write( |
|
| 219 | + $this->stream->read(4096) |
|
| 220 | + ); |
|
| 221 | + } |
|
| 222 | + |
|
| 223 | + $targetStream->close(); |
|
| 224 | + |
|
| 225 | + /** @var string|null */ |
|
| 226 | + $sourcePath = $this->stream->getMetadata('uri'); |
|
| 227 | + |
|
| 228 | + $this->stream->close(); |
|
| 229 | + $this->stream = null; |
|
| 230 | + |
|
| 231 | + if (isset($sourcePath) && is_file($sourcePath)) { |
|
| 232 | + $sourceDir = dirname($sourcePath); |
|
| 233 | + if (is_writable($sourceDir)) { |
|
| 234 | + unlink($sourcePath); |
|
| 235 | + } |
|
| 236 | + } |
|
| 237 | + } |
|
| 238 | + |
|
| 239 | + /** |
|
| 240 | + * Gets the file size |
|
| 241 | + * |
|
| 242 | + * @return int|null |
|
| 243 | + */ |
|
| 244 | + public function getSize(): ?int |
|
| 245 | + { |
|
| 246 | + return $this->size; |
|
| 247 | + } |
|
| 248 | + |
|
| 249 | + /** |
|
| 250 | + * Gets the file's error code |
|
| 251 | + * |
|
| 252 | + * @return int |
|
| 253 | + */ |
|
| 254 | + public function getError(): int |
|
| 255 | + { |
|
| 256 | + return $this->errorCode; |
|
| 257 | + } |
|
| 258 | + |
|
| 259 | + /** |
|
| 260 | + * Gets the client's file name |
|
| 261 | + * |
|
| 262 | + * @return string|null |
|
| 263 | + */ |
|
| 264 | + public function getClientFilename(): ?string |
|
| 265 | + { |
|
| 266 | + return $this->clientFilename; |
|
| 267 | + } |
|
| 268 | + |
|
| 269 | + /** |
|
| 270 | + * Gets the client's file media type |
|
| 271 | + * |
|
| 272 | + * @return string|null |
|
| 273 | + */ |
|
| 274 | + public function getClientMediaType(): ?string |
|
| 275 | + { |
|
| 276 | + return $this->clientMediaType; |
|
| 277 | + } |
|
| 278 | 278 | } |
@@ -47,386 +47,386 @@ |
||
| 47 | 47 | class Stream implements StreamInterface |
| 48 | 48 | { |
| 49 | 49 | |
| 50 | - /** |
|
| 51 | - * The stream resource |
|
| 52 | - * |
|
| 53 | - * @var resource|null |
|
| 54 | - */ |
|
| 55 | - private $resource; |
|
| 56 | - |
|
| 57 | - /** |
|
| 58 | - * Signals to close the stream on destruction |
|
| 59 | - * |
|
| 60 | - * @var bool |
|
| 61 | - */ |
|
| 62 | - private $autoClose; |
|
| 63 | - |
|
| 64 | - /** |
|
| 65 | - * Constructor of the class |
|
| 66 | - * |
|
| 67 | - * @param mixed $resource |
|
| 68 | - * @param bool $autoClose |
|
| 69 | - * |
|
| 70 | - * @throws InvalidArgumentException |
|
| 71 | - * If the stream cannot be initialized with the resource. |
|
| 72 | - */ |
|
| 73 | - public function __construct($resource, bool $autoClose = true) |
|
| 74 | - { |
|
| 75 | - if (!is_resource($resource)) { |
|
| 76 | - throw new InvalidArgumentException('Unexpected stream resource'); |
|
| 77 | - } |
|
| 78 | - |
|
| 79 | - $this->resource = $resource; |
|
| 80 | - $this->autoClose = $autoClose; |
|
| 81 | - } |
|
| 82 | - |
|
| 83 | - /** |
|
| 84 | - * Creates a stream |
|
| 85 | - * |
|
| 86 | - * @param mixed $resource |
|
| 87 | - * |
|
| 88 | - * @return StreamInterface |
|
| 89 | - * |
|
| 90 | - * @throws InvalidArgumentException |
|
| 91 | - * If the stream cannot be initialized with the resource. |
|
| 92 | - */ |
|
| 93 | - public static function create($resource): StreamInterface |
|
| 94 | - { |
|
| 95 | - if ($resource instanceof StreamInterface) { |
|
| 96 | - return $resource; |
|
| 97 | - } |
|
| 98 | - |
|
| 99 | - return new self($resource); |
|
| 100 | - } |
|
| 101 | - |
|
| 102 | - /** |
|
| 103 | - * Destructor of the class |
|
| 104 | - */ |
|
| 105 | - public function __destruct() |
|
| 106 | - { |
|
| 107 | - if ($this->autoClose) { |
|
| 108 | - $this->close(); |
|
| 109 | - } |
|
| 110 | - } |
|
| 111 | - |
|
| 112 | - /** |
|
| 113 | - * Detaches a resource from the stream |
|
| 114 | - * |
|
| 115 | - * Returns NULL if the stream already without a resource. |
|
| 116 | - * |
|
| 117 | - * @return resource|null |
|
| 118 | - */ |
|
| 119 | - public function detach() |
|
| 120 | - { |
|
| 121 | - $resource = $this->resource; |
|
| 122 | - $this->resource = null; |
|
| 123 | - |
|
| 124 | - return $resource; |
|
| 125 | - } |
|
| 126 | - |
|
| 127 | - /** |
|
| 128 | - * Closes the stream |
|
| 129 | - * |
|
| 130 | - * @link http://php.net/manual/en/function.fclose.php |
|
| 131 | - * |
|
| 132 | - * @return void |
|
| 133 | - */ |
|
| 134 | - public function close(): void |
|
| 135 | - { |
|
| 136 | - $resource = $this->detach(); |
|
| 137 | - if (!is_resource($resource)) { |
|
| 138 | - return; |
|
| 139 | - } |
|
| 140 | - |
|
| 141 | - fclose($resource); |
|
| 142 | - } |
|
| 143 | - |
|
| 144 | - /** |
|
| 145 | - * Checks if the end of the stream is reached |
|
| 146 | - * |
|
| 147 | - * @link http://php.net/manual/en/function.feof.php |
|
| 148 | - * |
|
| 149 | - * @return bool |
|
| 150 | - */ |
|
| 151 | - public function eof(): bool |
|
| 152 | - { |
|
| 153 | - if (!is_resource($this->resource)) { |
|
| 154 | - return true; |
|
| 155 | - } |
|
| 156 | - |
|
| 157 | - return feof($this->resource); |
|
| 158 | - } |
|
| 159 | - |
|
| 160 | - /** |
|
| 161 | - * Gets the stream pointer position |
|
| 162 | - * |
|
| 163 | - * @link http://php.net/manual/en/function.ftell.php |
|
| 164 | - * |
|
| 165 | - * @return int |
|
| 166 | - * |
|
| 167 | - * @throws RuntimeException |
|
| 168 | - */ |
|
| 169 | - public function tell(): int |
|
| 170 | - { |
|
| 171 | - if (!is_resource($this->resource)) { |
|
| 172 | - throw new RuntimeException('The stream does not have a resource, so the operation is not possible'); |
|
| 173 | - } |
|
| 174 | - |
|
| 175 | - $result = ftell($this->resource); |
|
| 176 | - if ($result === false) { |
|
| 177 | - throw new RuntimeException('Unable to get the stream pointer position'); |
|
| 178 | - } |
|
| 179 | - |
|
| 180 | - return $result; |
|
| 181 | - } |
|
| 182 | - |
|
| 183 | - /** |
|
| 184 | - * Checks if the stream is seekable |
|
| 185 | - * |
|
| 186 | - * @return bool |
|
| 187 | - */ |
|
| 188 | - public function isSeekable(): bool |
|
| 189 | - { |
|
| 190 | - if (!is_resource($this->resource)) { |
|
| 191 | - return false; |
|
| 192 | - } |
|
| 193 | - |
|
| 194 | - /** @var array{seekable: bool} */ |
|
| 195 | - $metadata = stream_get_meta_data($this->resource); |
|
| 196 | - |
|
| 197 | - return $metadata['seekable']; |
|
| 198 | - } |
|
| 199 | - |
|
| 200 | - /** |
|
| 201 | - * Moves the stream pointer to the beginning |
|
| 202 | - * |
|
| 203 | - * @return void |
|
| 204 | - * |
|
| 205 | - * @throws RuntimeException |
|
| 206 | - */ |
|
| 207 | - public function rewind(): void |
|
| 208 | - { |
|
| 209 | - $this->seek(0); |
|
| 210 | - } |
|
| 211 | - |
|
| 212 | - /** |
|
| 213 | - * Moves the stream pointer to the given position |
|
| 214 | - * |
|
| 215 | - * @link http://php.net/manual/en/function.fseek.php |
|
| 216 | - * |
|
| 217 | - * @param int $offset |
|
| 218 | - * @param int $whence |
|
| 219 | - * |
|
| 220 | - * @return void |
|
| 221 | - * |
|
| 222 | - * @throws RuntimeException |
|
| 223 | - */ |
|
| 224 | - public function seek($offset, $whence = SEEK_SET): void |
|
| 225 | - { |
|
| 226 | - if (!is_resource($this->resource)) { |
|
| 227 | - throw new RuntimeException('The stream does not have a resource, so the operation is not possible'); |
|
| 228 | - } |
|
| 229 | - |
|
| 230 | - if (!$this->isSeekable()) { |
|
| 231 | - throw new RuntimeException('Stream is not seekable'); |
|
| 232 | - } |
|
| 233 | - |
|
| 234 | - $result = fseek($this->resource, $offset, $whence); |
|
| 235 | - if ($result !== 0) { |
|
| 236 | - throw new RuntimeException('Unable to move the stream pointer position'); |
|
| 237 | - } |
|
| 238 | - } |
|
| 239 | - |
|
| 240 | - /** |
|
| 241 | - * Checks if the stream is writable |
|
| 242 | - * |
|
| 243 | - * @return bool |
|
| 244 | - */ |
|
| 245 | - public function isWritable(): bool |
|
| 246 | - { |
|
| 247 | - if (!is_resource($this->resource)) { |
|
| 248 | - return false; |
|
| 249 | - } |
|
| 250 | - |
|
| 251 | - /** @var array{mode: string} */ |
|
| 252 | - $metadata = stream_get_meta_data($this->resource); |
|
| 253 | - |
|
| 254 | - return strpbrk($metadata['mode'], '+acwx') !== false; |
|
| 255 | - } |
|
| 256 | - |
|
| 257 | - /** |
|
| 258 | - * Writes the given string to the stream |
|
| 259 | - * |
|
| 260 | - * Returns the number of bytes written to the stream. |
|
| 261 | - * |
|
| 262 | - * @link http://php.net/manual/en/function.fwrite.php |
|
| 263 | - * |
|
| 264 | - * @param string $string |
|
| 265 | - * |
|
| 266 | - * @return int |
|
| 267 | - * |
|
| 268 | - * @throws RuntimeException |
|
| 269 | - */ |
|
| 270 | - public function write($string): int |
|
| 271 | - { |
|
| 272 | - if (!is_resource($this->resource)) { |
|
| 273 | - throw new RuntimeException('The stream does not have a resource, so the operation is not possible'); |
|
| 274 | - } |
|
| 275 | - |
|
| 276 | - if (!$this->isWritable()) { |
|
| 277 | - throw new RuntimeException('Stream is not writable'); |
|
| 278 | - } |
|
| 279 | - |
|
| 280 | - $result = fwrite($this->resource, $string); |
|
| 281 | - if ($result === false) { |
|
| 282 | - throw new RuntimeException('Unable to write to the stream'); |
|
| 283 | - } |
|
| 284 | - |
|
| 285 | - return $result; |
|
| 286 | - } |
|
| 287 | - |
|
| 288 | - /** |
|
| 289 | - * Checks if the stream is readable |
|
| 290 | - * |
|
| 291 | - * @return bool |
|
| 292 | - */ |
|
| 293 | - public function isReadable(): bool |
|
| 294 | - { |
|
| 295 | - if (!is_resource($this->resource)) { |
|
| 296 | - return false; |
|
| 297 | - } |
|
| 298 | - |
|
| 299 | - /** @var array{mode: string} */ |
|
| 300 | - $metadata = stream_get_meta_data($this->resource); |
|
| 301 | - |
|
| 302 | - return strpbrk($metadata['mode'], '+r') !== false; |
|
| 303 | - } |
|
| 304 | - |
|
| 305 | - /** |
|
| 306 | - * Reads the given number of bytes from the stream |
|
| 307 | - * |
|
| 308 | - * @link http://php.net/manual/en/function.fread.php |
|
| 309 | - * |
|
| 310 | - * @param int $length |
|
| 311 | - * |
|
| 312 | - * @return string |
|
| 313 | - * |
|
| 314 | - * @throws RuntimeException |
|
| 315 | - */ |
|
| 316 | - public function read($length): string |
|
| 317 | - { |
|
| 318 | - if (!is_resource($this->resource)) { |
|
| 319 | - throw new RuntimeException('The stream does not have a resource, so the operation is not possible'); |
|
| 320 | - } |
|
| 321 | - |
|
| 322 | - if (!$this->isReadable()) { |
|
| 323 | - throw new RuntimeException('Stream is not readable'); |
|
| 324 | - } |
|
| 325 | - |
|
| 326 | - $result = fread($this->resource, $length); |
|
| 327 | - if ($result === false) { |
|
| 328 | - throw new RuntimeException('Unable to read from the stream'); |
|
| 329 | - } |
|
| 330 | - |
|
| 331 | - return $result; |
|
| 332 | - } |
|
| 333 | - |
|
| 334 | - /** |
|
| 335 | - * Reads the remainder of the stream |
|
| 336 | - * |
|
| 337 | - * @link http://php.net/manual/en/function.stream-get-contents.php |
|
| 338 | - * |
|
| 339 | - * @return string |
|
| 340 | - * |
|
| 341 | - * @throws RuntimeException |
|
| 342 | - */ |
|
| 343 | - public function getContents(): string |
|
| 344 | - { |
|
| 345 | - if (!is_resource($this->resource)) { |
|
| 346 | - throw new RuntimeException('The stream does not have a resource, so the operation is not possible'); |
|
| 347 | - } |
|
| 348 | - |
|
| 349 | - if (!$this->isReadable()) { |
|
| 350 | - throw new RuntimeException('Stream is not readable'); |
|
| 351 | - } |
|
| 352 | - |
|
| 353 | - $result = stream_get_contents($this->resource); |
|
| 354 | - if ($result === false) { |
|
| 355 | - throw new RuntimeException('Unable to read the remainder of the stream'); |
|
| 356 | - } |
|
| 357 | - |
|
| 358 | - return $result; |
|
| 359 | - } |
|
| 360 | - |
|
| 361 | - /** |
|
| 362 | - * Gets the stream metadata |
|
| 363 | - * |
|
| 364 | - * @link http://php.net/manual/en/function.stream-get-meta-data.php |
|
| 365 | - * |
|
| 366 | - * @param string|null $key |
|
| 367 | - * |
|
| 368 | - * @return mixed |
|
| 369 | - */ |
|
| 370 | - public function getMetadata($key = null) |
|
| 371 | - { |
|
| 372 | - if (!is_resource($this->resource)) { |
|
| 373 | - return null; |
|
| 374 | - } |
|
| 375 | - |
|
| 376 | - $metadata = stream_get_meta_data($this->resource); |
|
| 377 | - if ($key === null) { |
|
| 378 | - return $metadata; |
|
| 379 | - } |
|
| 380 | - |
|
| 381 | - return $metadata[$key] ?? null; |
|
| 382 | - } |
|
| 383 | - |
|
| 384 | - /** |
|
| 385 | - * Gets the stream size |
|
| 386 | - * |
|
| 387 | - * Returns NULL if the stream doesn't have a resource, |
|
| 388 | - * or if the stream size cannot be determined. |
|
| 389 | - * |
|
| 390 | - * @link http://php.net/manual/en/function.fstat.php |
|
| 391 | - * |
|
| 392 | - * @return int|null |
|
| 393 | - */ |
|
| 394 | - public function getSize(): ?int |
|
| 395 | - { |
|
| 396 | - if (!is_resource($this->resource)) { |
|
| 397 | - return null; |
|
| 398 | - } |
|
| 399 | - |
|
| 400 | - /** @var array{size: int}|false */ |
|
| 401 | - $stats = fstat($this->resource); |
|
| 402 | - if ($stats === false) { |
|
| 403 | - return null; |
|
| 404 | - } |
|
| 405 | - |
|
| 406 | - return $stats['size']; |
|
| 407 | - } |
|
| 408 | - |
|
| 409 | - /** |
|
| 410 | - * Converts the stream to a string |
|
| 411 | - * |
|
| 412 | - * @link http://php.net/manual/en/language.oop5.magic.php#object.tostring |
|
| 413 | - * |
|
| 414 | - * @return string |
|
| 415 | - */ |
|
| 416 | - public function __toString(): string |
|
| 417 | - { |
|
| 418 | - if (!$this->isReadable()) { |
|
| 419 | - return ''; |
|
| 420 | - } |
|
| 421 | - |
|
| 422 | - try { |
|
| 423 | - if ($this->isSeekable()) { |
|
| 424 | - $this->rewind(); |
|
| 425 | - } |
|
| 426 | - |
|
| 427 | - return $this->getContents(); |
|
| 428 | - } catch (Throwable $e) { |
|
| 429 | - return ''; |
|
| 430 | - } |
|
| 431 | - } |
|
| 50 | + /** |
|
| 51 | + * The stream resource |
|
| 52 | + * |
|
| 53 | + * @var resource|null |
|
| 54 | + */ |
|
| 55 | + private $resource; |
|
| 56 | + |
|
| 57 | + /** |
|
| 58 | + * Signals to close the stream on destruction |
|
| 59 | + * |
|
| 60 | + * @var bool |
|
| 61 | + */ |
|
| 62 | + private $autoClose; |
|
| 63 | + |
|
| 64 | + /** |
|
| 65 | + * Constructor of the class |
|
| 66 | + * |
|
| 67 | + * @param mixed $resource |
|
| 68 | + * @param bool $autoClose |
|
| 69 | + * |
|
| 70 | + * @throws InvalidArgumentException |
|
| 71 | + * If the stream cannot be initialized with the resource. |
|
| 72 | + */ |
|
| 73 | + public function __construct($resource, bool $autoClose = true) |
|
| 74 | + { |
|
| 75 | + if (!is_resource($resource)) { |
|
| 76 | + throw new InvalidArgumentException('Unexpected stream resource'); |
|
| 77 | + } |
|
| 78 | + |
|
| 79 | + $this->resource = $resource; |
|
| 80 | + $this->autoClose = $autoClose; |
|
| 81 | + } |
|
| 82 | + |
|
| 83 | + /** |
|
| 84 | + * Creates a stream |
|
| 85 | + * |
|
| 86 | + * @param mixed $resource |
|
| 87 | + * |
|
| 88 | + * @return StreamInterface |
|
| 89 | + * |
|
| 90 | + * @throws InvalidArgumentException |
|
| 91 | + * If the stream cannot be initialized with the resource. |
|
| 92 | + */ |
|
| 93 | + public static function create($resource): StreamInterface |
|
| 94 | + { |
|
| 95 | + if ($resource instanceof StreamInterface) { |
|
| 96 | + return $resource; |
|
| 97 | + } |
|
| 98 | + |
|
| 99 | + return new self($resource); |
|
| 100 | + } |
|
| 101 | + |
|
| 102 | + /** |
|
| 103 | + * Destructor of the class |
|
| 104 | + */ |
|
| 105 | + public function __destruct() |
|
| 106 | + { |
|
| 107 | + if ($this->autoClose) { |
|
| 108 | + $this->close(); |
|
| 109 | + } |
|
| 110 | + } |
|
| 111 | + |
|
| 112 | + /** |
|
| 113 | + * Detaches a resource from the stream |
|
| 114 | + * |
|
| 115 | + * Returns NULL if the stream already without a resource. |
|
| 116 | + * |
|
| 117 | + * @return resource|null |
|
| 118 | + */ |
|
| 119 | + public function detach() |
|
| 120 | + { |
|
| 121 | + $resource = $this->resource; |
|
| 122 | + $this->resource = null; |
|
| 123 | + |
|
| 124 | + return $resource; |
|
| 125 | + } |
|
| 126 | + |
|
| 127 | + /** |
|
| 128 | + * Closes the stream |
|
| 129 | + * |
|
| 130 | + * @link http://php.net/manual/en/function.fclose.php |
|
| 131 | + * |
|
| 132 | + * @return void |
|
| 133 | + */ |
|
| 134 | + public function close(): void |
|
| 135 | + { |
|
| 136 | + $resource = $this->detach(); |
|
| 137 | + if (!is_resource($resource)) { |
|
| 138 | + return; |
|
| 139 | + } |
|
| 140 | + |
|
| 141 | + fclose($resource); |
|
| 142 | + } |
|
| 143 | + |
|
| 144 | + /** |
|
| 145 | + * Checks if the end of the stream is reached |
|
| 146 | + * |
|
| 147 | + * @link http://php.net/manual/en/function.feof.php |
|
| 148 | + * |
|
| 149 | + * @return bool |
|
| 150 | + */ |
|
| 151 | + public function eof(): bool |
|
| 152 | + { |
|
| 153 | + if (!is_resource($this->resource)) { |
|
| 154 | + return true; |
|
| 155 | + } |
|
| 156 | + |
|
| 157 | + return feof($this->resource); |
|
| 158 | + } |
|
| 159 | + |
|
| 160 | + /** |
|
| 161 | + * Gets the stream pointer position |
|
| 162 | + * |
|
| 163 | + * @link http://php.net/manual/en/function.ftell.php |
|
| 164 | + * |
|
| 165 | + * @return int |
|
| 166 | + * |
|
| 167 | + * @throws RuntimeException |
|
| 168 | + */ |
|
| 169 | + public function tell(): int |
|
| 170 | + { |
|
| 171 | + if (!is_resource($this->resource)) { |
|
| 172 | + throw new RuntimeException('The stream does not have a resource, so the operation is not possible'); |
|
| 173 | + } |
|
| 174 | + |
|
| 175 | + $result = ftell($this->resource); |
|
| 176 | + if ($result === false) { |
|
| 177 | + throw new RuntimeException('Unable to get the stream pointer position'); |
|
| 178 | + } |
|
| 179 | + |
|
| 180 | + return $result; |
|
| 181 | + } |
|
| 182 | + |
|
| 183 | + /** |
|
| 184 | + * Checks if the stream is seekable |
|
| 185 | + * |
|
| 186 | + * @return bool |
|
| 187 | + */ |
|
| 188 | + public function isSeekable(): bool |
|
| 189 | + { |
|
| 190 | + if (!is_resource($this->resource)) { |
|
| 191 | + return false; |
|
| 192 | + } |
|
| 193 | + |
|
| 194 | + /** @var array{seekable: bool} */ |
|
| 195 | + $metadata = stream_get_meta_data($this->resource); |
|
| 196 | + |
|
| 197 | + return $metadata['seekable']; |
|
| 198 | + } |
|
| 199 | + |
|
| 200 | + /** |
|
| 201 | + * Moves the stream pointer to the beginning |
|
| 202 | + * |
|
| 203 | + * @return void |
|
| 204 | + * |
|
| 205 | + * @throws RuntimeException |
|
| 206 | + */ |
|
| 207 | + public function rewind(): void |
|
| 208 | + { |
|
| 209 | + $this->seek(0); |
|
| 210 | + } |
|
| 211 | + |
|
| 212 | + /** |
|
| 213 | + * Moves the stream pointer to the given position |
|
| 214 | + * |
|
| 215 | + * @link http://php.net/manual/en/function.fseek.php |
|
| 216 | + * |
|
| 217 | + * @param int $offset |
|
| 218 | + * @param int $whence |
|
| 219 | + * |
|
| 220 | + * @return void |
|
| 221 | + * |
|
| 222 | + * @throws RuntimeException |
|
| 223 | + */ |
|
| 224 | + public function seek($offset, $whence = SEEK_SET): void |
|
| 225 | + { |
|
| 226 | + if (!is_resource($this->resource)) { |
|
| 227 | + throw new RuntimeException('The stream does not have a resource, so the operation is not possible'); |
|
| 228 | + } |
|
| 229 | + |
|
| 230 | + if (!$this->isSeekable()) { |
|
| 231 | + throw new RuntimeException('Stream is not seekable'); |
|
| 232 | + } |
|
| 233 | + |
|
| 234 | + $result = fseek($this->resource, $offset, $whence); |
|
| 235 | + if ($result !== 0) { |
|
| 236 | + throw new RuntimeException('Unable to move the stream pointer position'); |
|
| 237 | + } |
|
| 238 | + } |
|
| 239 | + |
|
| 240 | + /** |
|
| 241 | + * Checks if the stream is writable |
|
| 242 | + * |
|
| 243 | + * @return bool |
|
| 244 | + */ |
|
| 245 | + public function isWritable(): bool |
|
| 246 | + { |
|
| 247 | + if (!is_resource($this->resource)) { |
|
| 248 | + return false; |
|
| 249 | + } |
|
| 250 | + |
|
| 251 | + /** @var array{mode: string} */ |
|
| 252 | + $metadata = stream_get_meta_data($this->resource); |
|
| 253 | + |
|
| 254 | + return strpbrk($metadata['mode'], '+acwx') !== false; |
|
| 255 | + } |
|
| 256 | + |
|
| 257 | + /** |
|
| 258 | + * Writes the given string to the stream |
|
| 259 | + * |
|
| 260 | + * Returns the number of bytes written to the stream. |
|
| 261 | + * |
|
| 262 | + * @link http://php.net/manual/en/function.fwrite.php |
|
| 263 | + * |
|
| 264 | + * @param string $string |
|
| 265 | + * |
|
| 266 | + * @return int |
|
| 267 | + * |
|
| 268 | + * @throws RuntimeException |
|
| 269 | + */ |
|
| 270 | + public function write($string): int |
|
| 271 | + { |
|
| 272 | + if (!is_resource($this->resource)) { |
|
| 273 | + throw new RuntimeException('The stream does not have a resource, so the operation is not possible'); |
|
| 274 | + } |
|
| 275 | + |
|
| 276 | + if (!$this->isWritable()) { |
|
| 277 | + throw new RuntimeException('Stream is not writable'); |
|
| 278 | + } |
|
| 279 | + |
|
| 280 | + $result = fwrite($this->resource, $string); |
|
| 281 | + if ($result === false) { |
|
| 282 | + throw new RuntimeException('Unable to write to the stream'); |
|
| 283 | + } |
|
| 284 | + |
|
| 285 | + return $result; |
|
| 286 | + } |
|
| 287 | + |
|
| 288 | + /** |
|
| 289 | + * Checks if the stream is readable |
|
| 290 | + * |
|
| 291 | + * @return bool |
|
| 292 | + */ |
|
| 293 | + public function isReadable(): bool |
|
| 294 | + { |
|
| 295 | + if (!is_resource($this->resource)) { |
|
| 296 | + return false; |
|
| 297 | + } |
|
| 298 | + |
|
| 299 | + /** @var array{mode: string} */ |
|
| 300 | + $metadata = stream_get_meta_data($this->resource); |
|
| 301 | + |
|
| 302 | + return strpbrk($metadata['mode'], '+r') !== false; |
|
| 303 | + } |
|
| 304 | + |
|
| 305 | + /** |
|
| 306 | + * Reads the given number of bytes from the stream |
|
| 307 | + * |
|
| 308 | + * @link http://php.net/manual/en/function.fread.php |
|
| 309 | + * |
|
| 310 | + * @param int $length |
|
| 311 | + * |
|
| 312 | + * @return string |
|
| 313 | + * |
|
| 314 | + * @throws RuntimeException |
|
| 315 | + */ |
|
| 316 | + public function read($length): string |
|
| 317 | + { |
|
| 318 | + if (!is_resource($this->resource)) { |
|
| 319 | + throw new RuntimeException('The stream does not have a resource, so the operation is not possible'); |
|
| 320 | + } |
|
| 321 | + |
|
| 322 | + if (!$this->isReadable()) { |
|
| 323 | + throw new RuntimeException('Stream is not readable'); |
|
| 324 | + } |
|
| 325 | + |
|
| 326 | + $result = fread($this->resource, $length); |
|
| 327 | + if ($result === false) { |
|
| 328 | + throw new RuntimeException('Unable to read from the stream'); |
|
| 329 | + } |
|
| 330 | + |
|
| 331 | + return $result; |
|
| 332 | + } |
|
| 333 | + |
|
| 334 | + /** |
|
| 335 | + * Reads the remainder of the stream |
|
| 336 | + * |
|
| 337 | + * @link http://php.net/manual/en/function.stream-get-contents.php |
|
| 338 | + * |
|
| 339 | + * @return string |
|
| 340 | + * |
|
| 341 | + * @throws RuntimeException |
|
| 342 | + */ |
|
| 343 | + public function getContents(): string |
|
| 344 | + { |
|
| 345 | + if (!is_resource($this->resource)) { |
|
| 346 | + throw new RuntimeException('The stream does not have a resource, so the operation is not possible'); |
|
| 347 | + } |
|
| 348 | + |
|
| 349 | + if (!$this->isReadable()) { |
|
| 350 | + throw new RuntimeException('Stream is not readable'); |
|
| 351 | + } |
|
| 352 | + |
|
| 353 | + $result = stream_get_contents($this->resource); |
|
| 354 | + if ($result === false) { |
|
| 355 | + throw new RuntimeException('Unable to read the remainder of the stream'); |
|
| 356 | + } |
|
| 357 | + |
|
| 358 | + return $result; |
|
| 359 | + } |
|
| 360 | + |
|
| 361 | + /** |
|
| 362 | + * Gets the stream metadata |
|
| 363 | + * |
|
| 364 | + * @link http://php.net/manual/en/function.stream-get-meta-data.php |
|
| 365 | + * |
|
| 366 | + * @param string|null $key |
|
| 367 | + * |
|
| 368 | + * @return mixed |
|
| 369 | + */ |
|
| 370 | + public function getMetadata($key = null) |
|
| 371 | + { |
|
| 372 | + if (!is_resource($this->resource)) { |
|
| 373 | + return null; |
|
| 374 | + } |
|
| 375 | + |
|
| 376 | + $metadata = stream_get_meta_data($this->resource); |
|
| 377 | + if ($key === null) { |
|
| 378 | + return $metadata; |
|
| 379 | + } |
|
| 380 | + |
|
| 381 | + return $metadata[$key] ?? null; |
|
| 382 | + } |
|
| 383 | + |
|
| 384 | + /** |
|
| 385 | + * Gets the stream size |
|
| 386 | + * |
|
| 387 | + * Returns NULL if the stream doesn't have a resource, |
|
| 388 | + * or if the stream size cannot be determined. |
|
| 389 | + * |
|
| 390 | + * @link http://php.net/manual/en/function.fstat.php |
|
| 391 | + * |
|
| 392 | + * @return int|null |
|
| 393 | + */ |
|
| 394 | + public function getSize(): ?int |
|
| 395 | + { |
|
| 396 | + if (!is_resource($this->resource)) { |
|
| 397 | + return null; |
|
| 398 | + } |
|
| 399 | + |
|
| 400 | + /** @var array{size: int}|false */ |
|
| 401 | + $stats = fstat($this->resource); |
|
| 402 | + if ($stats === false) { |
|
| 403 | + return null; |
|
| 404 | + } |
|
| 405 | + |
|
| 406 | + return $stats['size']; |
|
| 407 | + } |
|
| 408 | + |
|
| 409 | + /** |
|
| 410 | + * Converts the stream to a string |
|
| 411 | + * |
|
| 412 | + * @link http://php.net/manual/en/language.oop5.magic.php#object.tostring |
|
| 413 | + * |
|
| 414 | + * @return string |
|
| 415 | + */ |
|
| 416 | + public function __toString(): string |
|
| 417 | + { |
|
| 418 | + if (!$this->isReadable()) { |
|
| 419 | + return ''; |
|
| 420 | + } |
|
| 421 | + |
|
| 422 | + try { |
|
| 423 | + if ($this->isSeekable()) { |
|
| 424 | + $this->rewind(); |
|
| 425 | + } |
|
| 426 | + |
|
| 427 | + return $this->getContents(); |
|
| 428 | + } catch (Throwable $e) { |
|
| 429 | + return ''; |
|
| 430 | + } |
|
| 431 | + } |
|
| 432 | 432 | } |
@@ -35,354 +35,354 @@ |
||
| 35 | 35 | class ServerRequest extends Request implements ServerRequestInterface |
| 36 | 36 | { |
| 37 | 37 | |
| 38 | - /** |
|
| 39 | - * The server parameters |
|
| 40 | - * |
|
| 41 | - * @var array |
|
| 42 | - */ |
|
| 43 | - private array $serverParams; |
|
| 44 | - |
|
| 45 | - /** |
|
| 46 | - * The request's query parameters |
|
| 47 | - * |
|
| 48 | - * @var array |
|
| 49 | - */ |
|
| 50 | - private array $queryParams; |
|
| 51 | - |
|
| 52 | - /** |
|
| 53 | - * The request's cookie parameters |
|
| 54 | - * |
|
| 55 | - * @var array |
|
| 56 | - */ |
|
| 57 | - private array $cookieParams; |
|
| 58 | - |
|
| 59 | - /** |
|
| 60 | - * The request's uploaded files |
|
| 61 | - * |
|
| 62 | - * @var array |
|
| 63 | - */ |
|
| 64 | - private array $uploadedFiles = []; |
|
| 65 | - |
|
| 66 | - /** |
|
| 67 | - * The request's parsed body |
|
| 68 | - * |
|
| 69 | - * @var array|object|null |
|
| 70 | - */ |
|
| 71 | - private $parsedBody = null; |
|
| 72 | - |
|
| 73 | - /** |
|
| 74 | - * The request attributes |
|
| 75 | - * |
|
| 76 | - * @var array |
|
| 77 | - */ |
|
| 78 | - private array $attributes; |
|
| 79 | - |
|
| 80 | - /** |
|
| 81 | - * Constructor of the class |
|
| 82 | - * |
|
| 83 | - * @param string|null $protocolVersion |
|
| 84 | - * @param string|null $method |
|
| 85 | - * @param mixed $uri |
|
| 86 | - * @param array<string, string|string[]>|null $headers |
|
| 87 | - * @param StreamInterface|null $body |
|
| 88 | - * |
|
| 89 | - * @param array $serverParams |
|
| 90 | - * @param array $queryParams |
|
| 91 | - * @param array $cookieParams |
|
| 92 | - * @param array $uploadedFiles |
|
| 93 | - * @param array|object|null $parsedBody |
|
| 94 | - * @param array $attributes |
|
| 95 | - * |
|
| 96 | - * @throws InvalidArgumentException |
|
| 97 | - * If one of the arguments isn't valid. |
|
| 98 | - */ |
|
| 99 | - public function __construct( |
|
| 100 | - ?string $protocolVersion = null, |
|
| 101 | - ?string $method = null, |
|
| 102 | - $uri = null, |
|
| 103 | - ?array $headers = null, |
|
| 104 | - ?StreamInterface $body = null, |
|
| 105 | - array $serverParams = [], |
|
| 106 | - array $queryParams = [], |
|
| 107 | - array $cookieParams = [], |
|
| 108 | - array $uploadedFiles = [], |
|
| 109 | - $parsedBody = null, |
|
| 110 | - array $attributes = [] |
|
| 111 | - ) { |
|
| 112 | - parent::__construct($method, $uri, $headers, $body); |
|
| 113 | - |
|
| 114 | - if (isset($protocolVersion)) { |
|
| 115 | - $this->setProtocolVersion($protocolVersion); |
|
| 116 | - } |
|
| 117 | - |
|
| 118 | - if (!empty($uploadedFiles)) { |
|
| 119 | - $this->setUploadedFiles($uploadedFiles); |
|
| 120 | - } |
|
| 121 | - |
|
| 122 | - if (isset($parsedBody)) { |
|
| 123 | - $this->setParsedBody($parsedBody); |
|
| 124 | - } |
|
| 125 | - |
|
| 126 | - $this->serverParams = $serverParams; |
|
| 127 | - $this->queryParams = $queryParams; |
|
| 128 | - $this->cookieParams = $cookieParams; |
|
| 129 | - $this->attributes = $attributes; |
|
| 130 | - } |
|
| 131 | - |
|
| 132 | - /** |
|
| 133 | - * Gets the server parameters |
|
| 134 | - * |
|
| 135 | - * @return array |
|
| 136 | - */ |
|
| 137 | - public function getServerParams(): array |
|
| 138 | - { |
|
| 139 | - return $this->serverParams; |
|
| 140 | - } |
|
| 141 | - |
|
| 142 | - /** |
|
| 143 | - * Gets the request's query parameters |
|
| 144 | - * |
|
| 145 | - * @return array |
|
| 146 | - */ |
|
| 147 | - public function getQueryParams(): array |
|
| 148 | - { |
|
| 149 | - return $this->queryParams; |
|
| 150 | - } |
|
| 151 | - |
|
| 152 | - /** |
|
| 153 | - * Creates a new instance of the request with the given query parameters |
|
| 154 | - * |
|
| 155 | - * @param array $query |
|
| 156 | - * |
|
| 157 | - * @return static |
|
| 158 | - */ |
|
| 159 | - public function withQueryParams(array $query): ServerRequestInterface |
|
| 160 | - { |
|
| 161 | - $clone = clone $this; |
|
| 162 | - $clone->queryParams = $query; |
|
| 163 | - |
|
| 164 | - return $clone; |
|
| 165 | - } |
|
| 166 | - |
|
| 167 | - /** |
|
| 168 | - * Gets the request's cookie parameters |
|
| 169 | - * |
|
| 170 | - * @return array |
|
| 171 | - */ |
|
| 172 | - public function getCookieParams(): array |
|
| 173 | - { |
|
| 174 | - return $this->cookieParams; |
|
| 175 | - } |
|
| 176 | - |
|
| 177 | - /** |
|
| 178 | - * Creates a new instance of the request with the given cookie parameters |
|
| 179 | - * |
|
| 180 | - * @param array $cookies |
|
| 181 | - * |
|
| 182 | - * @return static |
|
| 183 | - */ |
|
| 184 | - public function withCookieParams(array $cookies): ServerRequestInterface |
|
| 185 | - { |
|
| 186 | - $clone = clone $this; |
|
| 187 | - $clone->cookieParams = $cookies; |
|
| 188 | - |
|
| 189 | - return $clone; |
|
| 190 | - } |
|
| 191 | - |
|
| 192 | - /** |
|
| 193 | - * Gets the request's uploaded files |
|
| 194 | - * |
|
| 195 | - * @return array |
|
| 196 | - */ |
|
| 197 | - public function getUploadedFiles(): array |
|
| 198 | - { |
|
| 199 | - return $this->uploadedFiles; |
|
| 200 | - } |
|
| 201 | - |
|
| 202 | - /** |
|
| 203 | - * Creates a new instance of the request with the given uploaded files |
|
| 204 | - * |
|
| 205 | - * @param array $uploadedFiles |
|
| 206 | - * |
|
| 207 | - * @return static |
|
| 208 | - * |
|
| 209 | - * @throws InvalidArgumentException |
|
| 210 | - * If one of the files isn't valid. |
|
| 211 | - */ |
|
| 212 | - public function withUploadedFiles(array $uploadedFiles): ServerRequestInterface |
|
| 213 | - { |
|
| 214 | - $clone = clone $this; |
|
| 215 | - $clone->setUploadedFiles($uploadedFiles); |
|
| 216 | - |
|
| 217 | - return $clone; |
|
| 218 | - } |
|
| 219 | - |
|
| 220 | - /** |
|
| 221 | - * Gets the request's parsed body |
|
| 222 | - * |
|
| 223 | - * @return array|object|null |
|
| 224 | - */ |
|
| 225 | - public function getParsedBody() |
|
| 226 | - { |
|
| 227 | - return $this->parsedBody; |
|
| 228 | - } |
|
| 229 | - |
|
| 230 | - /** |
|
| 231 | - * Creates a new instance of the request with the given parsed body |
|
| 232 | - * |
|
| 233 | - * @param array|object|null $data |
|
| 234 | - * |
|
| 235 | - * @return static |
|
| 236 | - * |
|
| 237 | - * @throws InvalidArgumentException |
|
| 238 | - * If the data isn't valid. |
|
| 239 | - */ |
|
| 240 | - public function withParsedBody($data): ServerRequestInterface |
|
| 241 | - { |
|
| 242 | - $clone = clone $this; |
|
| 243 | - $clone->setParsedBody($data); |
|
| 244 | - |
|
| 245 | - return $clone; |
|
| 246 | - } |
|
| 247 | - |
|
| 248 | - /** |
|
| 249 | - * Gets the request attributes |
|
| 250 | - * |
|
| 251 | - * @return array |
|
| 252 | - */ |
|
| 253 | - public function getAttributes(): array |
|
| 254 | - { |
|
| 255 | - return $this->attributes; |
|
| 256 | - } |
|
| 257 | - |
|
| 258 | - /** |
|
| 259 | - * Gets the request's attribute value by the given name |
|
| 260 | - * |
|
| 261 | - * Returns the default value if the attribute wasn't found. |
|
| 262 | - * |
|
| 263 | - * @param array-key $name |
|
| 264 | - * @param mixed $default |
|
| 265 | - * |
|
| 266 | - * @return mixed |
|
| 267 | - */ |
|
| 268 | - public function getAttribute($name, $default = null) |
|
| 269 | - { |
|
| 270 | - if (!array_key_exists($name, $this->attributes)) { |
|
| 271 | - return $default; |
|
| 272 | - } |
|
| 273 | - |
|
| 274 | - return $this->attributes[$name]; |
|
| 275 | - } |
|
| 276 | - |
|
| 277 | - /** |
|
| 278 | - * Creates a new instance of the request with the given attribute |
|
| 279 | - * |
|
| 280 | - * @param array-key $name |
|
| 281 | - * @param mixed $value |
|
| 282 | - * |
|
| 283 | - * @return static |
|
| 284 | - */ |
|
| 285 | - public function withAttribute($name, $value): ServerRequestInterface |
|
| 286 | - { |
|
| 287 | - $clone = clone $this; |
|
| 288 | - $clone->attributes[$name] = $value; |
|
| 289 | - |
|
| 290 | - return $clone; |
|
| 291 | - } |
|
| 292 | - |
|
| 293 | - /** |
|
| 294 | - * Creates a new instance of the request without an attribute with the given name |
|
| 295 | - * |
|
| 296 | - * @param array-key $name |
|
| 297 | - * |
|
| 298 | - * @return static |
|
| 299 | - */ |
|
| 300 | - public function withoutAttribute($name): ServerRequestInterface |
|
| 301 | - { |
|
| 302 | - $clone = clone $this; |
|
| 303 | - unset($clone->attributes[$name]); |
|
| 304 | - |
|
| 305 | - return $clone; |
|
| 306 | - } |
|
| 307 | - |
|
| 308 | - /** |
|
| 309 | - * Sets the given uploaded files to the request |
|
| 310 | - * |
|
| 311 | - * @param array $files |
|
| 312 | - * |
|
| 313 | - * @return void |
|
| 314 | - * |
|
| 315 | - * @throws InvalidArgumentException |
|
| 316 | - * If one of the files isn't valid. |
|
| 317 | - */ |
|
| 318 | - final protected function setUploadedFiles(array $files): void |
|
| 319 | - { |
|
| 320 | - $this->validateUploadedFiles($files); |
|
| 321 | - |
|
| 322 | - $this->uploadedFiles = $files; |
|
| 323 | - } |
|
| 324 | - |
|
| 325 | - /** |
|
| 326 | - * Sets the given parsed body to the request |
|
| 327 | - * |
|
| 328 | - * @param array|object|null $data |
|
| 329 | - * |
|
| 330 | - * @return void |
|
| 331 | - * |
|
| 332 | - * @throws InvalidArgumentException |
|
| 333 | - * If the parsed body isn't valid. |
|
| 334 | - */ |
|
| 335 | - final protected function setParsedBody($data): void |
|
| 336 | - { |
|
| 337 | - $this->validateParsedBody($data); |
|
| 338 | - |
|
| 339 | - $this->parsedBody = $data; |
|
| 340 | - } |
|
| 341 | - |
|
| 342 | - /** |
|
| 343 | - * Validates the given uploaded files |
|
| 344 | - * |
|
| 345 | - * @param array $files |
|
| 346 | - * |
|
| 347 | - * @return void |
|
| 348 | - * |
|
| 349 | - * @throws InvalidArgumentException |
|
| 350 | - * If one of the files isn't valid. |
|
| 351 | - */ |
|
| 352 | - private function validateUploadedFiles(array $files): void |
|
| 353 | - { |
|
| 354 | - if ([] === $files) { |
|
| 355 | - return; |
|
| 356 | - } |
|
| 357 | - |
|
| 358 | - /** |
|
| 359 | - * @psalm-suppress MissingClosureParamType |
|
| 360 | - */ |
|
| 361 | - array_walk_recursive($files, static function ($file): void { |
|
| 362 | - if (! ($file instanceof UploadedFileInterface)) { |
|
| 363 | - throw new InvalidArgumentException('Invalid uploaded file'); |
|
| 364 | - } |
|
| 365 | - }); |
|
| 366 | - } |
|
| 367 | - |
|
| 368 | - /** |
|
| 369 | - * Validates the given parsed body |
|
| 370 | - * |
|
| 371 | - * @param mixed $data |
|
| 372 | - * |
|
| 373 | - * @return void |
|
| 374 | - * |
|
| 375 | - * @throws InvalidArgumentException |
|
| 376 | - * If the parsed body isn't valid. |
|
| 377 | - */ |
|
| 378 | - private function validateParsedBody($data): void |
|
| 379 | - { |
|
| 380 | - if (null === $data) { |
|
| 381 | - return; |
|
| 382 | - } |
|
| 383 | - |
|
| 384 | - if (!is_array($data) && !is_object($data)) { |
|
| 385 | - throw new InvalidArgumentException('Invalid parsed body'); |
|
| 386 | - } |
|
| 387 | - } |
|
| 38 | + /** |
|
| 39 | + * The server parameters |
|
| 40 | + * |
|
| 41 | + * @var array |
|
| 42 | + */ |
|
| 43 | + private array $serverParams; |
|
| 44 | + |
|
| 45 | + /** |
|
| 46 | + * The request's query parameters |
|
| 47 | + * |
|
| 48 | + * @var array |
|
| 49 | + */ |
|
| 50 | + private array $queryParams; |
|
| 51 | + |
|
| 52 | + /** |
|
| 53 | + * The request's cookie parameters |
|
| 54 | + * |
|
| 55 | + * @var array |
|
| 56 | + */ |
|
| 57 | + private array $cookieParams; |
|
| 58 | + |
|
| 59 | + /** |
|
| 60 | + * The request's uploaded files |
|
| 61 | + * |
|
| 62 | + * @var array |
|
| 63 | + */ |
|
| 64 | + private array $uploadedFiles = []; |
|
| 65 | + |
|
| 66 | + /** |
|
| 67 | + * The request's parsed body |
|
| 68 | + * |
|
| 69 | + * @var array|object|null |
|
| 70 | + */ |
|
| 71 | + private $parsedBody = null; |
|
| 72 | + |
|
| 73 | + /** |
|
| 74 | + * The request attributes |
|
| 75 | + * |
|
| 76 | + * @var array |
|
| 77 | + */ |
|
| 78 | + private array $attributes; |
|
| 79 | + |
|
| 80 | + /** |
|
| 81 | + * Constructor of the class |
|
| 82 | + * |
|
| 83 | + * @param string|null $protocolVersion |
|
| 84 | + * @param string|null $method |
|
| 85 | + * @param mixed $uri |
|
| 86 | + * @param array<string, string|string[]>|null $headers |
|
| 87 | + * @param StreamInterface|null $body |
|
| 88 | + * |
|
| 89 | + * @param array $serverParams |
|
| 90 | + * @param array $queryParams |
|
| 91 | + * @param array $cookieParams |
|
| 92 | + * @param array $uploadedFiles |
|
| 93 | + * @param array|object|null $parsedBody |
|
| 94 | + * @param array $attributes |
|
| 95 | + * |
|
| 96 | + * @throws InvalidArgumentException |
|
| 97 | + * If one of the arguments isn't valid. |
|
| 98 | + */ |
|
| 99 | + public function __construct( |
|
| 100 | + ?string $protocolVersion = null, |
|
| 101 | + ?string $method = null, |
|
| 102 | + $uri = null, |
|
| 103 | + ?array $headers = null, |
|
| 104 | + ?StreamInterface $body = null, |
|
| 105 | + array $serverParams = [], |
|
| 106 | + array $queryParams = [], |
|
| 107 | + array $cookieParams = [], |
|
| 108 | + array $uploadedFiles = [], |
|
| 109 | + $parsedBody = null, |
|
| 110 | + array $attributes = [] |
|
| 111 | + ) { |
|
| 112 | + parent::__construct($method, $uri, $headers, $body); |
|
| 113 | + |
|
| 114 | + if (isset($protocolVersion)) { |
|
| 115 | + $this->setProtocolVersion($protocolVersion); |
|
| 116 | + } |
|
| 117 | + |
|
| 118 | + if (!empty($uploadedFiles)) { |
|
| 119 | + $this->setUploadedFiles($uploadedFiles); |
|
| 120 | + } |
|
| 121 | + |
|
| 122 | + if (isset($parsedBody)) { |
|
| 123 | + $this->setParsedBody($parsedBody); |
|
| 124 | + } |
|
| 125 | + |
|
| 126 | + $this->serverParams = $serverParams; |
|
| 127 | + $this->queryParams = $queryParams; |
|
| 128 | + $this->cookieParams = $cookieParams; |
|
| 129 | + $this->attributes = $attributes; |
|
| 130 | + } |
|
| 131 | + |
|
| 132 | + /** |
|
| 133 | + * Gets the server parameters |
|
| 134 | + * |
|
| 135 | + * @return array |
|
| 136 | + */ |
|
| 137 | + public function getServerParams(): array |
|
| 138 | + { |
|
| 139 | + return $this->serverParams; |
|
| 140 | + } |
|
| 141 | + |
|
| 142 | + /** |
|
| 143 | + * Gets the request's query parameters |
|
| 144 | + * |
|
| 145 | + * @return array |
|
| 146 | + */ |
|
| 147 | + public function getQueryParams(): array |
|
| 148 | + { |
|
| 149 | + return $this->queryParams; |
|
| 150 | + } |
|
| 151 | + |
|
| 152 | + /** |
|
| 153 | + * Creates a new instance of the request with the given query parameters |
|
| 154 | + * |
|
| 155 | + * @param array $query |
|
| 156 | + * |
|
| 157 | + * @return static |
|
| 158 | + */ |
|
| 159 | + public function withQueryParams(array $query): ServerRequestInterface |
|
| 160 | + { |
|
| 161 | + $clone = clone $this; |
|
| 162 | + $clone->queryParams = $query; |
|
| 163 | + |
|
| 164 | + return $clone; |
|
| 165 | + } |
|
| 166 | + |
|
| 167 | + /** |
|
| 168 | + * Gets the request's cookie parameters |
|
| 169 | + * |
|
| 170 | + * @return array |
|
| 171 | + */ |
|
| 172 | + public function getCookieParams(): array |
|
| 173 | + { |
|
| 174 | + return $this->cookieParams; |
|
| 175 | + } |
|
| 176 | + |
|
| 177 | + /** |
|
| 178 | + * Creates a new instance of the request with the given cookie parameters |
|
| 179 | + * |
|
| 180 | + * @param array $cookies |
|
| 181 | + * |
|
| 182 | + * @return static |
|
| 183 | + */ |
|
| 184 | + public function withCookieParams(array $cookies): ServerRequestInterface |
|
| 185 | + { |
|
| 186 | + $clone = clone $this; |
|
| 187 | + $clone->cookieParams = $cookies; |
|
| 188 | + |
|
| 189 | + return $clone; |
|
| 190 | + } |
|
| 191 | + |
|
| 192 | + /** |
|
| 193 | + * Gets the request's uploaded files |
|
| 194 | + * |
|
| 195 | + * @return array |
|
| 196 | + */ |
|
| 197 | + public function getUploadedFiles(): array |
|
| 198 | + { |
|
| 199 | + return $this->uploadedFiles; |
|
| 200 | + } |
|
| 201 | + |
|
| 202 | + /** |
|
| 203 | + * Creates a new instance of the request with the given uploaded files |
|
| 204 | + * |
|
| 205 | + * @param array $uploadedFiles |
|
| 206 | + * |
|
| 207 | + * @return static |
|
| 208 | + * |
|
| 209 | + * @throws InvalidArgumentException |
|
| 210 | + * If one of the files isn't valid. |
|
| 211 | + */ |
|
| 212 | + public function withUploadedFiles(array $uploadedFiles): ServerRequestInterface |
|
| 213 | + { |
|
| 214 | + $clone = clone $this; |
|
| 215 | + $clone->setUploadedFiles($uploadedFiles); |
|
| 216 | + |
|
| 217 | + return $clone; |
|
| 218 | + } |
|
| 219 | + |
|
| 220 | + /** |
|
| 221 | + * Gets the request's parsed body |
|
| 222 | + * |
|
| 223 | + * @return array|object|null |
|
| 224 | + */ |
|
| 225 | + public function getParsedBody() |
|
| 226 | + { |
|
| 227 | + return $this->parsedBody; |
|
| 228 | + } |
|
| 229 | + |
|
| 230 | + /** |
|
| 231 | + * Creates a new instance of the request with the given parsed body |
|
| 232 | + * |
|
| 233 | + * @param array|object|null $data |
|
| 234 | + * |
|
| 235 | + * @return static |
|
| 236 | + * |
|
| 237 | + * @throws InvalidArgumentException |
|
| 238 | + * If the data isn't valid. |
|
| 239 | + */ |
|
| 240 | + public function withParsedBody($data): ServerRequestInterface |
|
| 241 | + { |
|
| 242 | + $clone = clone $this; |
|
| 243 | + $clone->setParsedBody($data); |
|
| 244 | + |
|
| 245 | + return $clone; |
|
| 246 | + } |
|
| 247 | + |
|
| 248 | + /** |
|
| 249 | + * Gets the request attributes |
|
| 250 | + * |
|
| 251 | + * @return array |
|
| 252 | + */ |
|
| 253 | + public function getAttributes(): array |
|
| 254 | + { |
|
| 255 | + return $this->attributes; |
|
| 256 | + } |
|
| 257 | + |
|
| 258 | + /** |
|
| 259 | + * Gets the request's attribute value by the given name |
|
| 260 | + * |
|
| 261 | + * Returns the default value if the attribute wasn't found. |
|
| 262 | + * |
|
| 263 | + * @param array-key $name |
|
| 264 | + * @param mixed $default |
|
| 265 | + * |
|
| 266 | + * @return mixed |
|
| 267 | + */ |
|
| 268 | + public function getAttribute($name, $default = null) |
|
| 269 | + { |
|
| 270 | + if (!array_key_exists($name, $this->attributes)) { |
|
| 271 | + return $default; |
|
| 272 | + } |
|
| 273 | + |
|
| 274 | + return $this->attributes[$name]; |
|
| 275 | + } |
|
| 276 | + |
|
| 277 | + /** |
|
| 278 | + * Creates a new instance of the request with the given attribute |
|
| 279 | + * |
|
| 280 | + * @param array-key $name |
|
| 281 | + * @param mixed $value |
|
| 282 | + * |
|
| 283 | + * @return static |
|
| 284 | + */ |
|
| 285 | + public function withAttribute($name, $value): ServerRequestInterface |
|
| 286 | + { |
|
| 287 | + $clone = clone $this; |
|
| 288 | + $clone->attributes[$name] = $value; |
|
| 289 | + |
|
| 290 | + return $clone; |
|
| 291 | + } |
|
| 292 | + |
|
| 293 | + /** |
|
| 294 | + * Creates a new instance of the request without an attribute with the given name |
|
| 295 | + * |
|
| 296 | + * @param array-key $name |
|
| 297 | + * |
|
| 298 | + * @return static |
|
| 299 | + */ |
|
| 300 | + public function withoutAttribute($name): ServerRequestInterface |
|
| 301 | + { |
|
| 302 | + $clone = clone $this; |
|
| 303 | + unset($clone->attributes[$name]); |
|
| 304 | + |
|
| 305 | + return $clone; |
|
| 306 | + } |
|
| 307 | + |
|
| 308 | + /** |
|
| 309 | + * Sets the given uploaded files to the request |
|
| 310 | + * |
|
| 311 | + * @param array $files |
|
| 312 | + * |
|
| 313 | + * @return void |
|
| 314 | + * |
|
| 315 | + * @throws InvalidArgumentException |
|
| 316 | + * If one of the files isn't valid. |
|
| 317 | + */ |
|
| 318 | + final protected function setUploadedFiles(array $files): void |
|
| 319 | + { |
|
| 320 | + $this->validateUploadedFiles($files); |
|
| 321 | + |
|
| 322 | + $this->uploadedFiles = $files; |
|
| 323 | + } |
|
| 324 | + |
|
| 325 | + /** |
|
| 326 | + * Sets the given parsed body to the request |
|
| 327 | + * |
|
| 328 | + * @param array|object|null $data |
|
| 329 | + * |
|
| 330 | + * @return void |
|
| 331 | + * |
|
| 332 | + * @throws InvalidArgumentException |
|
| 333 | + * If the parsed body isn't valid. |
|
| 334 | + */ |
|
| 335 | + final protected function setParsedBody($data): void |
|
| 336 | + { |
|
| 337 | + $this->validateParsedBody($data); |
|
| 338 | + |
|
| 339 | + $this->parsedBody = $data; |
|
| 340 | + } |
|
| 341 | + |
|
| 342 | + /** |
|
| 343 | + * Validates the given uploaded files |
|
| 344 | + * |
|
| 345 | + * @param array $files |
|
| 346 | + * |
|
| 347 | + * @return void |
|
| 348 | + * |
|
| 349 | + * @throws InvalidArgumentException |
|
| 350 | + * If one of the files isn't valid. |
|
| 351 | + */ |
|
| 352 | + private function validateUploadedFiles(array $files): void |
|
| 353 | + { |
|
| 354 | + if ([] === $files) { |
|
| 355 | + return; |
|
| 356 | + } |
|
| 357 | + |
|
| 358 | + /** |
|
| 359 | + * @psalm-suppress MissingClosureParamType |
|
| 360 | + */ |
|
| 361 | + array_walk_recursive($files, static function ($file): void { |
|
| 362 | + if (! ($file instanceof UploadedFileInterface)) { |
|
| 363 | + throw new InvalidArgumentException('Invalid uploaded file'); |
|
| 364 | + } |
|
| 365 | + }); |
|
| 366 | + } |
|
| 367 | + |
|
| 368 | + /** |
|
| 369 | + * Validates the given parsed body |
|
| 370 | + * |
|
| 371 | + * @param mixed $data |
|
| 372 | + * |
|
| 373 | + * @return void |
|
| 374 | + * |
|
| 375 | + * @throws InvalidArgumentException |
|
| 376 | + * If the parsed body isn't valid. |
|
| 377 | + */ |
|
| 378 | + private function validateParsedBody($data): void |
|
| 379 | + { |
|
| 380 | + if (null === $data) { |
|
| 381 | + return; |
|
| 382 | + } |
|
| 383 | + |
|
| 384 | + if (!is_array($data) && !is_object($data)) { |
|
| 385 | + throw new InvalidArgumentException('Invalid parsed body'); |
|
| 386 | + } |
|
| 387 | + } |
|
| 388 | 388 | } |
@@ -358,8 +358,8 @@ |
||
| 358 | 358 | /** |
| 359 | 359 | * @psalm-suppress MissingClosureParamType |
| 360 | 360 | */ |
| 361 | - array_walk_recursive($files, static function ($file): void { |
|
| 362 | - if (! ($file instanceof UploadedFileInterface)) { |
|
| 361 | + array_walk_recursive($files, static function($file): void { |
|
| 362 | + if (!($file instanceof UploadedFileInterface)) { |
|
| 363 | 363 | throw new InvalidArgumentException('Invalid uploaded file'); |
| 364 | 364 | } |
| 365 | 365 | }); |
@@ -36,55 +36,55 @@ |
||
| 36 | 36 | final class JsonResponse extends Response |
| 37 | 37 | { |
| 38 | 38 | |
| 39 | - /** |
|
| 40 | - * Constructor of the class |
|
| 41 | - * |
|
| 42 | - * @param int $statusCode |
|
| 43 | - * @param mixed $data |
|
| 44 | - * @param int $flags |
|
| 45 | - * @param int<1, max> $depth |
|
| 46 | - * |
|
| 47 | - * @throws InvalidArgumentException |
|
| 48 | - */ |
|
| 49 | - public function __construct(int $statusCode, $data, int $flags = 0, int $depth = 512) |
|
| 50 | - { |
|
| 51 | - parent::__construct($statusCode); |
|
| 39 | + /** |
|
| 40 | + * Constructor of the class |
|
| 41 | + * |
|
| 42 | + * @param int $statusCode |
|
| 43 | + * @param mixed $data |
|
| 44 | + * @param int $flags |
|
| 45 | + * @param int<1, max> $depth |
|
| 46 | + * |
|
| 47 | + * @throws InvalidArgumentException |
|
| 48 | + */ |
|
| 49 | + public function __construct(int $statusCode, $data, int $flags = 0, int $depth = 512) |
|
| 50 | + { |
|
| 51 | + parent::__construct($statusCode); |
|
| 52 | 52 | |
| 53 | - $this->setBody($this->createBody($data, $flags, $depth)); |
|
| 53 | + $this->setBody($this->createBody($data, $flags, $depth)); |
|
| 54 | 54 | |
| 55 | - $this->setHeader('Content-Type', 'application/json; charset=utf-8'); |
|
| 56 | - } |
|
| 55 | + $this->setHeader('Content-Type', 'application/json; charset=utf-8'); |
|
| 56 | + } |
|
| 57 | 57 | |
| 58 | - /** |
|
| 59 | - * Creates the response body from the given JSON data |
|
| 60 | - * |
|
| 61 | - * @param mixed $data |
|
| 62 | - * @param int $flags |
|
| 63 | - * @param int<1, max> $depth |
|
| 64 | - * |
|
| 65 | - * @return StreamInterface |
|
| 66 | - * |
|
| 67 | - * @throws InvalidArgumentException |
|
| 68 | - */ |
|
| 69 | - private function createBody($data, int $flags, int $depth): StreamInterface |
|
| 70 | - { |
|
| 71 | - if ($data instanceof StreamInterface) { |
|
| 72 | - return $data; |
|
| 73 | - } |
|
| 58 | + /** |
|
| 59 | + * Creates the response body from the given JSON data |
|
| 60 | + * |
|
| 61 | + * @param mixed $data |
|
| 62 | + * @param int $flags |
|
| 63 | + * @param int<1, max> $depth |
|
| 64 | + * |
|
| 65 | + * @return StreamInterface |
|
| 66 | + * |
|
| 67 | + * @throws InvalidArgumentException |
|
| 68 | + */ |
|
| 69 | + private function createBody($data, int $flags, int $depth): StreamInterface |
|
| 70 | + { |
|
| 71 | + if ($data instanceof StreamInterface) { |
|
| 72 | + return $data; |
|
| 73 | + } |
|
| 74 | 74 | |
| 75 | - try { |
|
| 76 | - $payload = json_encode($data, $flags | JSON_THROW_ON_ERROR, $depth); |
|
| 77 | - } catch (JsonException $e) { |
|
| 78 | - throw new InvalidArgumentException(sprintf( |
|
| 79 | - 'Unable to create JSON response due to invalid JSON data: %s', |
|
| 80 | - $e->getMessage() |
|
| 81 | - ), 0, $e); |
|
| 82 | - } |
|
| 75 | + try { |
|
| 76 | + $payload = json_encode($data, $flags | JSON_THROW_ON_ERROR, $depth); |
|
| 77 | + } catch (JsonException $e) { |
|
| 78 | + throw new InvalidArgumentException(sprintf( |
|
| 79 | + 'Unable to create JSON response due to invalid JSON data: %s', |
|
| 80 | + $e->getMessage() |
|
| 81 | + ), 0, $e); |
|
| 82 | + } |
|
| 83 | 83 | |
| 84 | - $stream = new PhpTempStream('r+b'); |
|
| 85 | - $stream->write($payload); |
|
| 86 | - $stream->rewind(); |
|
| 84 | + $stream = new PhpTempStream('r+b'); |
|
| 85 | + $stream->write($payload); |
|
| 86 | + $stream->rewind(); |
|
| 87 | 87 | |
| 88 | - return $stream; |
|
| 89 | - } |
|
| 88 | + return $stream; |
|
| 89 | + } |
|
| 90 | 90 | } |
@@ -31,53 +31,53 @@ |
||
| 31 | 31 | final class Path implements ComponentInterface |
| 32 | 32 | { |
| 33 | 33 | |
| 34 | - /** |
|
| 35 | - * Regular expression used for the component normalization |
|
| 36 | - * |
|
| 37 | - * @var string |
|
| 38 | - */ |
|
| 39 | - // phpcs:ignore Generic.Files.LineLength |
|
| 40 | - private const NORMALIZATION_REGEX = '/(?:%[0-9A-Fa-f]{2}|[\x21\x24\x26-\x3b\x3d\x40-\x5a\x5f\x61-\x7a\x7e]+)|(.?)/u'; |
|
| 34 | + /** |
|
| 35 | + * Regular expression used for the component normalization |
|
| 36 | + * |
|
| 37 | + * @var string |
|
| 38 | + */ |
|
| 39 | + // phpcs:ignore Generic.Files.LineLength |
|
| 40 | + private const NORMALIZATION_REGEX = '/(?:%[0-9A-Fa-f]{2}|[\x21\x24\x26-\x3b\x3d\x40-\x5a\x5f\x61-\x7a\x7e]+)|(.?)/u'; |
|
| 41 | 41 | |
| 42 | - /** |
|
| 43 | - * The component value |
|
| 44 | - * |
|
| 45 | - * @var string |
|
| 46 | - */ |
|
| 47 | - private string $value = ''; |
|
| 42 | + /** |
|
| 43 | + * The component value |
|
| 44 | + * |
|
| 45 | + * @var string |
|
| 46 | + */ |
|
| 47 | + private string $value = ''; |
|
| 48 | 48 | |
| 49 | - /** |
|
| 50 | - * Constructor of the class |
|
| 51 | - * |
|
| 52 | - * @param mixed $value |
|
| 53 | - * |
|
| 54 | - * @throws InvalidArgumentException |
|
| 55 | - * If the component isn't valid. |
|
| 56 | - */ |
|
| 57 | - public function __construct($value) |
|
| 58 | - { |
|
| 59 | - if ($value === '') { |
|
| 60 | - return; |
|
| 61 | - } |
|
| 49 | + /** |
|
| 50 | + * Constructor of the class |
|
| 51 | + * |
|
| 52 | + * @param mixed $value |
|
| 53 | + * |
|
| 54 | + * @throws InvalidArgumentException |
|
| 55 | + * If the component isn't valid. |
|
| 56 | + */ |
|
| 57 | + public function __construct($value) |
|
| 58 | + { |
|
| 59 | + if ($value === '') { |
|
| 60 | + return; |
|
| 61 | + } |
|
| 62 | 62 | |
| 63 | - if (!is_string($value)) { |
|
| 64 | - throw new InvalidArgumentException('URI component "path" must be a string'); |
|
| 65 | - } |
|
| 63 | + if (!is_string($value)) { |
|
| 64 | + throw new InvalidArgumentException('URI component "path" must be a string'); |
|
| 65 | + } |
|
| 66 | 66 | |
| 67 | - $this->value = (string) preg_replace_callback(self::NORMALIZATION_REGEX, function (array $match): string { |
|
| 68 | - /** @var array{0: string, 1?: string} $match */ |
|
| 67 | + $this->value = (string) preg_replace_callback(self::NORMALIZATION_REGEX, function (array $match): string { |
|
| 68 | + /** @var array{0: string, 1?: string} $match */ |
|
| 69 | 69 | |
| 70 | - return isset($match[1]) ? rawurlencode($match[1]) : $match[0]; |
|
| 71 | - }, $value); |
|
| 72 | - } |
|
| 70 | + return isset($match[1]) ? rawurlencode($match[1]) : $match[0]; |
|
| 71 | + }, $value); |
|
| 72 | + } |
|
| 73 | 73 | |
| 74 | - /** |
|
| 75 | - * {@inheritdoc} |
|
| 76 | - * |
|
| 77 | - * @return string |
|
| 78 | - */ |
|
| 79 | - public function getValue(): string |
|
| 80 | - { |
|
| 81 | - return $this->value; |
|
| 82 | - } |
|
| 74 | + /** |
|
| 75 | + * {@inheritdoc} |
|
| 76 | + * |
|
| 77 | + * @return string |
|
| 78 | + */ |
|
| 79 | + public function getValue(): string |
|
| 80 | + { |
|
| 81 | + return $this->value; |
|
| 82 | + } |
|
| 83 | 83 | } |
@@ -64,7 +64,7 @@ |
||
| 64 | 64 | throw new InvalidArgumentException('URI component "path" must be a string'); |
| 65 | 65 | } |
| 66 | 66 | |
| 67 | - $this->value = (string) preg_replace_callback(self::NORMALIZATION_REGEX, function (array $match): string { |
|
| 67 | + $this->value = (string) preg_replace_callback(self::NORMALIZATION_REGEX, function(array $match): string { |
|
| 68 | 68 | /** @var array{0: string, 1?: string} $match */ |
| 69 | 69 | |
| 70 | 70 | return isset($match[1]) ? rawurlencode($match[1]) : $match[0]; |
@@ -31,71 +31,71 @@ |
||
| 31 | 31 | final class User implements ComponentInterface |
| 32 | 32 | { |
| 33 | 33 | |
| 34 | - /** |
|
| 35 | - * Regular expression used for the component normalization |
|
| 36 | - * |
|
| 37 | - * @var string |
|
| 38 | - */ |
|
| 39 | - // phpcs:ignore Generic.Files.LineLength |
|
| 40 | - private const NORMALIZATION_REGEX = '/(?:%[0-9A-Fa-f]{2}|[\x21\x24\x26-\x2e\x30-\x39\x3b\x3d\x41-\x5a\x5f\x61-\x7a\x7e]+)|(.?)/u'; |
|
| 34 | + /** |
|
| 35 | + * Regular expression used for the component normalization |
|
| 36 | + * |
|
| 37 | + * @var string |
|
| 38 | + */ |
|
| 39 | + // phpcs:ignore Generic.Files.LineLength |
|
| 40 | + private const NORMALIZATION_REGEX = '/(?:%[0-9A-Fa-f]{2}|[\x21\x24\x26-\x2e\x30-\x39\x3b\x3d\x41-\x5a\x5f\x61-\x7a\x7e]+)|(.?)/u'; |
|
| 41 | 41 | |
| 42 | - /** |
|
| 43 | - * The component value |
|
| 44 | - * |
|
| 45 | - * @var string |
|
| 46 | - */ |
|
| 47 | - private string $value = ''; |
|
| 42 | + /** |
|
| 43 | + * The component value |
|
| 44 | + * |
|
| 45 | + * @var string |
|
| 46 | + */ |
|
| 47 | + private string $value = ''; |
|
| 48 | 48 | |
| 49 | - /** |
|
| 50 | - * Constructor of the class |
|
| 51 | - * |
|
| 52 | - * @param mixed $value |
|
| 53 | - * |
|
| 54 | - * @throws InvalidArgumentException |
|
| 55 | - * If the component isn't valid. |
|
| 56 | - */ |
|
| 57 | - public function __construct($value) |
|
| 58 | - { |
|
| 59 | - if ($value === '') { |
|
| 60 | - return; |
|
| 61 | - } |
|
| 49 | + /** |
|
| 50 | + * Constructor of the class |
|
| 51 | + * |
|
| 52 | + * @param mixed $value |
|
| 53 | + * |
|
| 54 | + * @throws InvalidArgumentException |
|
| 55 | + * If the component isn't valid. |
|
| 56 | + */ |
|
| 57 | + public function __construct($value) |
|
| 58 | + { |
|
| 59 | + if ($value === '') { |
|
| 60 | + return; |
|
| 61 | + } |
|
| 62 | 62 | |
| 63 | - if (!is_string($value)) { |
|
| 64 | - throw new InvalidArgumentException('URI component "user" must be a string'); |
|
| 65 | - } |
|
| 63 | + if (!is_string($value)) { |
|
| 64 | + throw new InvalidArgumentException('URI component "user" must be a string'); |
|
| 65 | + } |
|
| 66 | 66 | |
| 67 | - $this->value = (string) preg_replace_callback(self::NORMALIZATION_REGEX, function (array $match): string { |
|
| 68 | - /** @var array{0: string, 1?: string} $match */ |
|
| 67 | + $this->value = (string) preg_replace_callback(self::NORMALIZATION_REGEX, function (array $match): string { |
|
| 68 | + /** @var array{0: string, 1?: string} $match */ |
|
| 69 | 69 | |
| 70 | - return isset($match[1]) ? rawurlencode($match[1]) : $match[0]; |
|
| 71 | - }, $value); |
|
| 72 | - } |
|
| 70 | + return isset($match[1]) ? rawurlencode($match[1]) : $match[0]; |
|
| 71 | + }, $value); |
|
| 72 | + } |
|
| 73 | 73 | |
| 74 | - /** |
|
| 75 | - * Creates a user component |
|
| 76 | - * |
|
| 77 | - * @param mixed $user |
|
| 78 | - * |
|
| 79 | - * @return User |
|
| 80 | - * |
|
| 81 | - * @throws InvalidArgumentException |
|
| 82 | - */ |
|
| 83 | - public static function create($user): User |
|
| 84 | - { |
|
| 85 | - if ($user instanceof User) { |
|
| 86 | - return $user; |
|
| 87 | - } |
|
| 74 | + /** |
|
| 75 | + * Creates a user component |
|
| 76 | + * |
|
| 77 | + * @param mixed $user |
|
| 78 | + * |
|
| 79 | + * @return User |
|
| 80 | + * |
|
| 81 | + * @throws InvalidArgumentException |
|
| 82 | + */ |
|
| 83 | + public static function create($user): User |
|
| 84 | + { |
|
| 85 | + if ($user instanceof User) { |
|
| 86 | + return $user; |
|
| 87 | + } |
|
| 88 | 88 | |
| 89 | - return new User($user); |
|
| 90 | - } |
|
| 89 | + return new User($user); |
|
| 90 | + } |
|
| 91 | 91 | |
| 92 | - /** |
|
| 93 | - * {@inheritdoc} |
|
| 94 | - * |
|
| 95 | - * @return string |
|
| 96 | - */ |
|
| 97 | - public function getValue(): string |
|
| 98 | - { |
|
| 99 | - return $this->value; |
|
| 100 | - } |
|
| 92 | + /** |
|
| 93 | + * {@inheritdoc} |
|
| 94 | + * |
|
| 95 | + * @return string |
|
| 96 | + */ |
|
| 97 | + public function getValue(): string |
|
| 98 | + { |
|
| 99 | + return $this->value; |
|
| 100 | + } |
|
| 101 | 101 | } |
@@ -64,7 +64,7 @@ |
||
| 64 | 64 | throw new InvalidArgumentException('URI component "user" must be a string'); |
| 65 | 65 | } |
| 66 | 66 | |
| 67 | - $this->value = (string) preg_replace_callback(self::NORMALIZATION_REGEX, function (array $match): string { |
|
| 67 | + $this->value = (string) preg_replace_callback(self::NORMALIZATION_REGEX, function(array $match): string { |
|
| 68 | 68 | /** @var array{0: string, 1?: string} $match */ |
| 69 | 69 | |
| 70 | 70 | return isset($match[1]) ? rawurlencode($match[1]) : $match[0]; |
@@ -31,71 +31,71 @@ |
||
| 31 | 31 | final class Password implements ComponentInterface |
| 32 | 32 | { |
| 33 | 33 | |
| 34 | - /** |
|
| 35 | - * Regular expression used for the component normalization |
|
| 36 | - * |
|
| 37 | - * @var string |
|
| 38 | - */ |
|
| 39 | - // phpcs:ignore Generic.Files.LineLength |
|
| 40 | - private const NORMALIZATION_REGEX = '/(?:%[0-9A-Fa-f]{2}|[\x21\x24\x26-\x2e\x30-\x39\x3b\x3d\x41-\x5a\x5f\x61-\x7a\x7e]+)|(.?)/u'; |
|
| 34 | + /** |
|
| 35 | + * Regular expression used for the component normalization |
|
| 36 | + * |
|
| 37 | + * @var string |
|
| 38 | + */ |
|
| 39 | + // phpcs:ignore Generic.Files.LineLength |
|
| 40 | + private const NORMALIZATION_REGEX = '/(?:%[0-9A-Fa-f]{2}|[\x21\x24\x26-\x2e\x30-\x39\x3b\x3d\x41-\x5a\x5f\x61-\x7a\x7e]+)|(.?)/u'; |
|
| 41 | 41 | |
| 42 | - /** |
|
| 43 | - * The component value |
|
| 44 | - * |
|
| 45 | - * @var string |
|
| 46 | - */ |
|
| 47 | - private string $value = ''; |
|
| 42 | + /** |
|
| 43 | + * The component value |
|
| 44 | + * |
|
| 45 | + * @var string |
|
| 46 | + */ |
|
| 47 | + private string $value = ''; |
|
| 48 | 48 | |
| 49 | - /** |
|
| 50 | - * Constructor of the class |
|
| 51 | - * |
|
| 52 | - * @param mixed $value |
|
| 53 | - * |
|
| 54 | - * @throws InvalidArgumentException |
|
| 55 | - * If the component isn't valid. |
|
| 56 | - */ |
|
| 57 | - public function __construct($value) |
|
| 58 | - { |
|
| 59 | - if ($value === '') { |
|
| 60 | - return; |
|
| 61 | - } |
|
| 49 | + /** |
|
| 50 | + * Constructor of the class |
|
| 51 | + * |
|
| 52 | + * @param mixed $value |
|
| 53 | + * |
|
| 54 | + * @throws InvalidArgumentException |
|
| 55 | + * If the component isn't valid. |
|
| 56 | + */ |
|
| 57 | + public function __construct($value) |
|
| 58 | + { |
|
| 59 | + if ($value === '') { |
|
| 60 | + return; |
|
| 61 | + } |
|
| 62 | 62 | |
| 63 | - if (!is_string($value)) { |
|
| 64 | - throw new InvalidArgumentException('URI component "password" must be a string'); |
|
| 65 | - } |
|
| 63 | + if (!is_string($value)) { |
|
| 64 | + throw new InvalidArgumentException('URI component "password" must be a string'); |
|
| 65 | + } |
|
| 66 | 66 | |
| 67 | - $this->value = (string) preg_replace_callback(self::NORMALIZATION_REGEX, function (array $match): string { |
|
| 68 | - /** @var array{0: string, 1?: string} $match */ |
|
| 67 | + $this->value = (string) preg_replace_callback(self::NORMALIZATION_REGEX, function (array $match): string { |
|
| 68 | + /** @var array{0: string, 1?: string} $match */ |
|
| 69 | 69 | |
| 70 | - return isset($match[1]) ? rawurlencode($match[1]) : $match[0]; |
|
| 71 | - }, $value); |
|
| 72 | - } |
|
| 70 | + return isset($match[1]) ? rawurlencode($match[1]) : $match[0]; |
|
| 71 | + }, $value); |
|
| 72 | + } |
|
| 73 | 73 | |
| 74 | - /** |
|
| 75 | - * Creates a password component |
|
| 76 | - * |
|
| 77 | - * @param mixed $password |
|
| 78 | - * |
|
| 79 | - * @return Password |
|
| 80 | - * |
|
| 81 | - * @throws InvalidArgumentException |
|
| 82 | - */ |
|
| 83 | - public static function create($password): Password |
|
| 84 | - { |
|
| 85 | - if ($password instanceof Password) { |
|
| 86 | - return $password; |
|
| 87 | - } |
|
| 74 | + /** |
|
| 75 | + * Creates a password component |
|
| 76 | + * |
|
| 77 | + * @param mixed $password |
|
| 78 | + * |
|
| 79 | + * @return Password |
|
| 80 | + * |
|
| 81 | + * @throws InvalidArgumentException |
|
| 82 | + */ |
|
| 83 | + public static function create($password): Password |
|
| 84 | + { |
|
| 85 | + if ($password instanceof Password) { |
|
| 86 | + return $password; |
|
| 87 | + } |
|
| 88 | 88 | |
| 89 | - return new Password($password); |
|
| 90 | - } |
|
| 89 | + return new Password($password); |
|
| 90 | + } |
|
| 91 | 91 | |
| 92 | - /** |
|
| 93 | - * {@inheritdoc} |
|
| 94 | - * |
|
| 95 | - * @return string |
|
| 96 | - */ |
|
| 97 | - public function getValue(): string |
|
| 98 | - { |
|
| 99 | - return $this->value; |
|
| 100 | - } |
|
| 92 | + /** |
|
| 93 | + * {@inheritdoc} |
|
| 94 | + * |
|
| 95 | + * @return string |
|
| 96 | + */ |
|
| 97 | + public function getValue(): string |
|
| 98 | + { |
|
| 99 | + return $this->value; |
|
| 100 | + } |
|
| 101 | 101 | } |
@@ -64,7 +64,7 @@ |
||
| 64 | 64 | throw new InvalidArgumentException('URI component "password" must be a string'); |
| 65 | 65 | } |
| 66 | 66 | |
| 67 | - $this->value = (string) preg_replace_callback(self::NORMALIZATION_REGEX, function (array $match): string { |
|
| 67 | + $this->value = (string) preg_replace_callback(self::NORMALIZATION_REGEX, function(array $match): string { |
|
| 68 | 68 | /** @var array{0: string, 1?: string} $match */ |
| 69 | 69 | |
| 70 | 70 | return isset($match[1]) ? rawurlencode($match[1]) : $match[0]; |