1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace BEAR\Resource; |
6
|
|
|
|
7
|
|
|
use InvalidArgumentException; |
8
|
|
|
use Koriym\FileUpload\ErrorFileUpload; |
9
|
|
|
use Koriym\FileUpload\FileUpload; |
10
|
|
|
use Override; |
11
|
|
|
use Ray\Di\InjectorInterface; |
12
|
|
|
use Ray\InputQuery\Attribute\InputFile; |
13
|
|
|
use Ray\InputQuery\FileUploadFactoryInterface; |
14
|
|
|
use ReflectionAttribute; |
15
|
|
|
use ReflectionParameter; |
16
|
|
|
|
17
|
|
|
use function array_key_exists; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @psalm-import-type Query from Types |
21
|
|
|
* @psalm-import-type RequestQuery from Types |
22
|
|
|
*/ |
23
|
|
|
final class InputFormParam implements ParamInterface |
24
|
|
|
{ |
25
|
|
|
/** @param array<ReflectionAttribute<InputFile>> $inputFileAttributes */ |
|
|
|
|
26
|
|
|
public function __construct( |
27
|
|
|
private readonly FileUploadFactoryInterface $factory, |
28
|
|
|
private readonly ReflectionParameter $parameter, |
29
|
|
|
private readonly array $inputFileAttributes = [], |
30
|
|
|
) { |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* {@inheritDoc} |
35
|
|
|
* |
36
|
|
|
* Returns form metadata instead of parsing query data |
37
|
|
|
* |
38
|
|
|
* @param RequestQuery $query |
|
|
|
|
39
|
|
|
*/ |
40
|
|
|
#[Override] |
41
|
|
|
public function __invoke(string $varName, array $query, InjectorInterface $injector): FileUpload|ErrorFileUpload|null |
42
|
|
|
{ |
43
|
|
|
// When user sets the FileUpload object, return it directly |
44
|
|
|
if (array_key_exists($varName, $query)) { |
45
|
|
|
$value = $query[$varName]; |
46
|
|
|
// Type check: ensure the value is a valid file upload object or null |
47
|
|
|
if ($value !== null && ! ($value instanceof FileUpload) && ! ($value instanceof ErrorFileUpload)) { |
48
|
|
|
throw new InvalidArgumentException($varName); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
return $value; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
unset($injector); |
55
|
|
|
$inputFileAttribute = $this->inputFileAttributes[0] ?? null; |
56
|
|
|
|
57
|
|
|
return $this->factory->create($this->parameter, $query, $inputFileAttribute); |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|