RequestFactory.php
2 months ago
ResponseFactory.php
2 months ago
ServerRequestFactory.php
2 months ago
StreamFactory.php
2 months ago
UploadedFileFactory.php
2 months ago
UriFactory.php
2 months ago
UploadedFileFactory.php
48 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Slim Framework (https://slimframework.com) |
| 5 | * |
| 6 | * @license https://github.com/slimphp/Slim-Psr7/blob/master/LICENSE.md (MIT License) |
| 7 | */ |
| 8 | |
| 9 | declare(strict_types=1); |
| 10 | |
| 11 | namespace Slim\Psr7\Factory; |
| 12 | |
| 13 | use InvalidArgumentException; |
| 14 | use AmeliaVendor\Psr\Http\Message\StreamInterface; |
| 15 | use AmeliaVendor\Psr\Http\Message\UploadedFileFactoryInterface; |
| 16 | use AmeliaVendor\Psr\Http\Message\UploadedFileInterface; |
| 17 | use Slim\Psr7\UploadedFile; |
| 18 | |
| 19 | use function is_string; |
| 20 | |
| 21 | use const UPLOAD_ERR_OK; |
| 22 | |
| 23 | class UploadedFileFactory implements UploadedFileFactoryInterface |
| 24 | { |
| 25 | /** |
| 26 | * {@inheritdoc} |
| 27 | */ |
| 28 | public function createUploadedFile( |
| 29 | StreamInterface $stream, |
| 30 | ?int $size = null, |
| 31 | int $error = UPLOAD_ERR_OK, |
| 32 | ?string $clientFilename = null, |
| 33 | ?string $clientMediaType = null |
| 34 | ): UploadedFileInterface { |
| 35 | $file = $stream->getMetadata('uri'); |
| 36 | |
| 37 | if (!is_string($file) || !$stream->isReadable()) { |
| 38 | throw new InvalidArgumentException('File is not readable.'); |
| 39 | } |
| 40 | |
| 41 | if ($size === null) { |
| 42 | $size = $stream->getSize(); |
| 43 | } |
| 44 | |
| 45 | return new UploadedFile($stream, $clientFilename, $clientMediaType, $size, $error); |
| 46 | } |
| 47 | } |
| 48 |