|
19 | 19 | use Quantum\Storage\Exceptions\FileUploadException; |
20 | 20 | use Quantum\App\Exceptions\BaseException; |
21 | 21 | use Quantum\Storage\UploadedFile; |
22 | | -use ReflectionException; |
23 | 22 |
|
24 | 23 | /** |
25 | 24 | * Trait File |
@@ -76,37 +75,60 @@ public function getFile(string $key) |
76 | 75 | * Handle files |
77 | 76 | * @param array<string, mixed> $files |
78 | 77 | * @return array<string, UploadedFile|array<UploadedFile>> |
79 | | - * @throws BaseException |
80 | | - * @throws ReflectionException |
81 | 78 | */ |
82 | 79 | public function handleFiles(array $files): array |
83 | 80 | { |
84 | | - if (!count($files)) { |
85 | | - return []; |
86 | | - } |
| 81 | + $formatted = []; |
87 | 82 |
|
88 | | - $key = key($files); |
| 83 | + foreach ($files as $key => $file) { |
| 84 | + if (!is_array($file) || !isset($file['name'])) { |
| 85 | + continue; |
| 86 | + } |
89 | 87 |
|
90 | | - if (!$key) { |
91 | | - return []; |
92 | | - } |
| 88 | + if (!is_array($file['name'])) { |
| 89 | + $formatted[$key] = new UploadedFile($file); |
| 90 | + continue; |
| 91 | + } |
| 92 | + |
| 93 | + if (!$this->isMultiFilePayload($file)) { |
| 94 | + continue; |
| 95 | + } |
93 | 96 |
|
94 | | - if (!is_array($files[$key]['name'])) { |
95 | | - return [$key => new UploadedFile($files[$key])]; |
96 | | - } else { |
97 | | - $formatted = []; |
| 97 | + $types = $file['type']; |
| 98 | + $tmpNames = $file['tmp_name']; |
| 99 | + $errors = $file['error']; |
| 100 | + $sizes = $file['size']; |
| 101 | + $multiFiles = []; |
98 | 102 |
|
99 | | - foreach ($files[$key]['name'] as $index => $name) { |
100 | | - $formatted[$key][$index] = new UploadedFile([ |
| 103 | + foreach ($file['name'] as $index => $name) { |
| 104 | + if (!isset($types[$index], $tmpNames[$index], $errors[$index], $sizes[$index])) { |
| 105 | + continue; |
| 106 | + } |
| 107 | + |
| 108 | + $multiFiles[$index] = new UploadedFile([ |
101 | 109 | 'name' => $name, |
102 | | - 'type' => $files[$key]['type'][$index], |
103 | | - 'tmp_name' => $files[$key]['tmp_name'][$index], |
104 | | - 'error' => $files[$key]['error'][$index], |
105 | | - 'size' => $files[$key]['size'][$index], |
| 110 | + 'type' => $types[$index], |
| 111 | + 'tmp_name' => $tmpNames[$index], |
| 112 | + 'error' => $errors[$index], |
| 113 | + 'size' => $sizes[$index], |
106 | 114 | ]); |
107 | 115 | } |
108 | 116 |
|
109 | | - return $formatted; |
| 117 | + $formatted[$key] = $multiFiles; |
110 | 118 | } |
| 119 | + |
| 120 | + return $formatted; |
| 121 | + } |
| 122 | + |
| 123 | + /** |
| 124 | + * @param array<string, mixed> $file |
| 125 | + */ |
| 126 | + private function isMultiFilePayload(array $file): bool |
| 127 | + { |
| 128 | + return isset($file['type'], $file['tmp_name'], $file['error'], $file['size']) |
| 129 | + && is_array($file['type']) |
| 130 | + && is_array($file['tmp_name']) |
| 131 | + && is_array($file['error']) |
| 132 | + && is_array($file['size']); |
111 | 133 | } |
112 | 134 | } |
0 commit comments