bearsunday /
BEAR.Resource
| 1 | <?php |
||
| 2 | |||
| 3 | declare(strict_types=1); |
||
| 4 | |||
| 5 | namespace BEAR\Resource; |
||
| 6 | |||
| 7 | use InvalidArgumentException; |
||
| 8 | use Koriym\FileUpload\AbstractFileUpload; |
||
| 9 | use Koriym\FileUpload\ErrorFileUpload; |
||
| 10 | use Koriym\FileUpload\FileUpload; |
||
| 11 | use Override; |
||
| 12 | use Ray\Di\InjectorInterface; |
||
| 13 | use Ray\InputQuery\Attribute\InputFile; |
||
| 14 | use Ray\InputQuery\FileUploadFactoryInterface; |
||
| 15 | use ReflectionAttribute; |
||
| 16 | use ReflectionParameter; |
||
| 17 | |||
| 18 | use function array_key_exists; |
||
| 19 | use function assert; |
||
| 20 | use function count; |
||
| 21 | use function is_array; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * @psalm-import-type Query from Types |
||
| 25 | * @psalm-import-type RequestQuery from Types |
||
| 26 | */ |
||
| 27 | final readonly class InputFormsParam implements ParamInterface |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 28 | { |
||
| 29 | /** @param array<ReflectionAttribute<InputFile>> $inputFileAttributes */ |
||
| 30 | public function __construct( |
||
| 31 | private FileUploadFactoryInterface $factory, |
||
| 32 | private ReflectionParameter $parameter, |
||
| 33 | private array $inputFileAttributes = [], |
||
| 34 | ) { |
||
| 35 | } |
||
| 36 | |||
| 37 | /** |
||
| 38 | * {@inheritDoc} |
||
| 39 | * |
||
| 40 | * Returns array of FileUpload objects using createMultiple() |
||
| 41 | * |
||
| 42 | * @param RequestQuery $query |
||
| 43 | * |
||
| 44 | * @return array<AbstractFileUpload> |
||
| 45 | */ |
||
| 46 | #[Override] |
||
| 47 | public function __invoke(string $varName, array $query, InjectorInterface $injector): array |
||
| 48 | { |
||
| 49 | if (array_key_exists($varName, $query)) { |
||
| 50 | return $this->getUserFileUploads($varName, $query); |
||
| 51 | } |
||
| 52 | |||
| 53 | $inputFileAttribute = $this->inputFileAttributes[0] ?? null; |
||
| 54 | |||
| 55 | return $this->factory->createMultiple($this->parameter, $query, $inputFileAttribute); |
||
| 56 | } |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @param RequestQuery $query |
||
| 60 | * |
||
| 61 | * @return array<AbstractFileUpload> |
||
| 62 | * |
||
| 63 | * @throws InvalidArgumentException When array contains invalid file objects. |
||
| 64 | */ |
||
| 65 | private function getUserFileUploads(string $varName, array $query): array |
||
| 66 | { |
||
| 67 | $fileUploads = $query[$varName]; |
||
| 68 | if (! is_array($fileUploads)) { |
||
| 69 | throw new InvalidArgumentException($varName); // Invalid type of array |
||
| 70 | } |
||
| 71 | |||
| 72 | /** @var array<AbstractFileUpload> $fileUploads */ |
||
| 73 | |||
| 74 | assert(count($fileUploads) > 0); |
||
| 75 | foreach ($fileUploads as $fileUpload) { |
||
| 76 | if (! ($fileUpload instanceof FileUpload) && ! ($fileUpload instanceof ErrorFileUpload)) { |
||
| 77 | throw new InvalidArgumentException($varName); // Invalid item of array tye of FileUpload or ErrorFileUpload |
||
| 78 | } |
||
| 79 | } |
||
| 80 | |||
| 81 | return $fileUploads; |
||
| 82 | } |
||
| 83 | } |
||
| 84 |