| 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 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  * @psalm-import-type Query from Types | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  * @psalm-import-type RequestQuery from Types | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | final class InputFormParam implements ParamInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     /** @param array<ReflectionAttribute<InputFile>> $inputFileAttributes */ | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     public function __construct( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |         private readonly FileUploadFactoryInterface $factory, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |         private readonly ReflectionParameter $parameter, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |         private readonly array $inputFileAttributes = [], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |      * {@inheritDoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |      * Returns form metadata instead of parsing query data | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |      * @param RequestQuery $query | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 40 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |     #[Override] | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |     public function __invoke(string $varName, array $query, InjectorInterface $injector): AbstractFileUpload|null | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |         // When user sets the FileUpload object, return it directly | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |         if (array_key_exists($varName, $query)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |             $value = $query[$varName]; | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |             // Type check: ensure the value is a valid file upload object or null | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |             if ($value !== null && ! ($value instanceof FileUpload) && ! ($value instanceof ErrorFileUpload)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |                 throw new InvalidArgumentException($varName); | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |             return $value; | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |         unset($injector); | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |         $inputFileAttribute = $this->inputFileAttributes[0] ?? null; | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |         return $this->factory->create($this->parameter, $query, $inputFileAttribute); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 60 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 61 |  |  |  |