| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | namespace Kambo\HttpMessage\Factories\Enviroment\Superglobal; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | // \HttpMessage | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Kambo\HttpMessage\UploadedFile; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Kambo\HttpMessage\Enviroment\Enviroment; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Kambo\HttpMessage\Factories\Enviroment\Interfaces\Factory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * Create instances of UploadedFile object from instance of Enviroment object | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * @package Kambo\HttpMessage\Factories\Enviroment\Superglobal | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  * @author  Bohuslav Simek <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  * @license MIT | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | class FilesFactory implements Factory | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |      * Create instances of UploadedFile object from instance of Enviroment object | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |      * @param Enviroment $enviroment enviroment data | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |      * @return array An associative array containing instances of UploadedFile, if there  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |      *               are no uploads an empty array will be returned: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |      *               [<field name> => <instance of UploadedFile>, ...] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 | 3 |  |     public function create(Enviroment $enviroment) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 | 3 |  |         $files  = $enviroment->getFiles(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 | 3 |  |         $parsed = $this->parseFiles($files); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 | 3 |  |         return $parsed; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |      * Parse files data into instances of UploadedFile | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |      * @param array $files An associative array of items with same structure as $_FILES  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |      * @return array An associative array containing instances of UploadedFile, if there  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |      *               are no uploads an empty array will be returned: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |      *               [<field name> => <instance of UploadedFile>, ...]            | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 | 3 |  |     private function parseFiles($files) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 | 3 |  |         $parsed = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 | 3 |  |         if (!empty($files)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 | 2 |  |             foreach ($files as $field => $file) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 | 2 |  |                 $parsed[$field] = $this->parseFile($file); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 | 2 |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 | 2 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 | 3 |  |         return $parsed; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |      * Parse particular fields in files data into instances of UploadedFile | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |      * @param array $uploadedFile An associative array of items from $_FILES from one single field | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |      * @return array array with instances of UploadedFile | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 62 |  |  |      */ | 
            
                                                        
            
                                    
            
            
                | 63 | 2 |  |     private function parseFile($uploadedFile) | 
            
                                                        
            
                                    
            
            
                | 64 |  |  |     { | 
            
                                                        
            
                                    
            
            
                | 65 | 2 |  |         $item = []; | 
            
                                                        
            
                                    
            
            
                | 66 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 67 | 2 |  |         if (is_array($uploadedFile)) { | 
            
                                                        
            
                                    
            
            
                | 68 | 2 |  |             if (!is_array($uploadedFile['error'])) { | 
            
                                                        
            
                                    
            
            
                | 69 | 1 |  |                 $item[] = new UploadedFile( | 
            
                                                        
            
                                    
            
            
                | 70 | 1 |  |                     $uploadedFile['tmp_name'], | 
            
                                                        
            
                                    
            
            
                | 71 | 1 |  |                     isset($uploadedFile['name']) ? $uploadedFile['name'] : null, | 
            
                                                        
            
                                    
            
            
                | 72 | 1 |  |                     isset($uploadedFile['type']) ? $uploadedFile['type'] : null, | 
            
                                                        
            
                                    
            
            
                | 73 | 1 |  |                     isset($uploadedFile['size']) ? $uploadedFile['size'] : null, | 
            
                                                        
            
                                    
            
            
                | 74 | 1 |  |                     $uploadedFile['error'] | 
            
                                                        
            
                                    
            
            
                | 75 | 1 |  |                 ); | 
            
                                                        
            
                                    
            
            
                | 76 | 1 |  |             } else { | 
            
                                                        
            
                                    
            
            
                | 77 | 1 |  |                 foreach ($uploadedFile['error'] as $fieldIndex => $void) { | 
            
                                                        
            
                                    
            
            
                | 78 | 1 |  |                     $item[] = new UploadedFile( | 
            
                                                        
            
                                    
            
            
                | 79 | 1 |  |                         $uploadedFile['tmp_name'][$fieldIndex], | 
            
                                                        
            
                                    
            
            
                | 80 | 1 |  |                         isset($uploadedFile['name']) ? $uploadedFile['name'][$fieldIndex] : null, | 
            
                                                        
            
                                    
            
            
                | 81 | 1 |  |                         isset($uploadedFile['type']) ? $uploadedFile['type'][$fieldIndex] : null, | 
            
                                                        
            
                                    
            
            
                | 82 | 1 |  |                         isset($uploadedFile['size']) ? $uploadedFile['size'][$fieldIndex] : null, | 
            
                                                        
            
                                    
            
            
                | 83 | 1 |  |                         $uploadedFile['error'][$fieldIndex] | 
            
                                                        
            
                                    
            
            
                | 84 | 1 |  |                     ); | 
            
                                                        
            
                                    
            
            
                | 85 | 1 |  |                 } | 
            
                                                        
            
                                    
            
            
                | 86 |  |  |             } | 
            
                                                        
            
                                    
            
            
                | 87 | 2 |  |         } | 
            
                                                        
            
                                    
            
            
                | 88 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 89 | 2 |  |         return $item; | 
            
                                                        
            
                                    
            
            
                | 90 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 91 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 92 |  |  |  |