|
14 | 14 |
|
15 | 15 | use Derafu\DataProcessor\Contract\ValidatorRuleInterface; |
16 | 16 | use Derafu\DataProcessor\Exception\ValidationException; |
17 | | - |
18 | | -// use Psr\Http\Message\UploadedFileInterface; |
| 17 | +use Psr\Http\Message\UploadedFileInterface; |
19 | 18 |
|
20 | 19 | abstract class AbstractFileRule implements ValidatorRuleInterface |
21 | 20 | { |
22 | 21 | protected function validateFileArray(mixed $value): void |
23 | 22 | { |
24 | | - // // Check if PSR-7 UploadedFileInterface is available. |
25 | | - // if (class_exists(UploadedFileInterface::class)) { |
26 | | - // // PSR-7 UploadedFileInterface (single file). |
27 | | - // if ($value instanceof UploadedFileInterface) { |
28 | | - // if ($value->getError() !== UPLOAD_ERR_OK) { |
29 | | - // throw new ValidationException('Uploaded file has error.'); |
30 | | - // } |
31 | | - // return; |
32 | | - // } |
| 23 | + // Case 1 & 2: PSR-7 UploadedFileInterface (if available). |
| 24 | + if (interface_exists(UploadedFileInterface::class)) { |
| 25 | + // Single PSR-7 file. |
| 26 | + if ($value instanceof UploadedFileInterface) { |
| 27 | + if ($value->getError() !== UPLOAD_ERR_OK) { |
| 28 | + throw new ValidationException('Uploaded file has error.'); |
| 29 | + } |
| 30 | + return; |
| 31 | + } |
| 32 | + |
| 33 | + // Indexed array of PSR-7 files. |
| 34 | + if (is_array($value) && isset($value[0]) && $value[0] instanceof UploadedFileInterface) { |
| 35 | + foreach ($value as $file) { |
| 36 | + $this->validateFileArray($file); |
| 37 | + } |
| 38 | + return; |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + if (!is_array($value)) { |
| 43 | + throw new ValidationException('Invalid file format.'); |
| 44 | + } |
33 | 45 |
|
34 | | - // // Array of PSR-7 UploadedFileInterface. |
35 | | - // if (is_array($value) && isset($value[0]) && $value[0] instanceof UploadedFileInterface) { |
36 | | - // foreach ($value as $file) { |
37 | | - // $this->validateFileArray($file); // Recursive. |
38 | | - // } |
39 | | - // return; |
40 | | - // } |
41 | | - // } |
| 46 | + // Case 3: $_FILES simple (single file, tmp_name is a string). |
| 47 | + if (isset($value['tmp_name']) && is_string($value['tmp_name'])) { |
| 48 | + if (!isset($value['error']) || $value['error'] !== UPLOAD_ERR_OK) { |
| 49 | + throw new ValidationException('File has upload error.'); |
| 50 | + } |
| 51 | + if (!file_exists($value['tmp_name'])) { |
| 52 | + throw new ValidationException('File does not exist.'); |
| 53 | + } |
| 54 | + return; |
| 55 | + } |
42 | 56 |
|
43 | | - // // $_FILES simple. |
44 | | - // if (is_array($value) && isset($value['tmp_name'])) { |
45 | | - // if (!is_string($value['tmp_name']) || !file_exists($value['tmp_name'])) { |
46 | | - // throw new ValidationException('File does not exist.'); |
47 | | - // } |
48 | | - // if (!isset($value['error']) || $value['error'] !== UPLOAD_ERR_OK) { |
49 | | - // throw new ValidationException('File has upload error.'); |
50 | | - // } |
51 | | - // return; |
52 | | - // } |
| 57 | + // Case 4a: $_FILES native multiple (tmp_name is an array of paths). |
| 58 | + if (isset($value['tmp_name']) && is_array($value['tmp_name'])) { |
| 59 | + foreach ($value['tmp_name'] as $index => $tmpName) { |
| 60 | + $error = $value['error'][$index] ?? UPLOAD_ERR_NO_FILE; |
| 61 | + if ($error !== UPLOAD_ERR_OK) { |
| 62 | + throw new ValidationException( |
| 63 | + sprintf('File at index %d has upload error.', $index) |
| 64 | + ); |
| 65 | + } |
| 66 | + if (!file_exists($tmpName)) { |
| 67 | + throw new ValidationException( |
| 68 | + sprintf('File at index %d does not exist.', $index) |
| 69 | + ); |
| 70 | + } |
| 71 | + } |
| 72 | + return; |
| 73 | + } |
53 | 74 |
|
54 | | - // // $_FILES multiple. |
55 | | - // if (is_array($value)) { |
56 | | - // foreach ($value as $v) { |
57 | | - // $this->validateFileArray($v); // Recursive. |
58 | | - // } |
59 | | - // return; |
60 | | - // } |
| 75 | + // Case 4b: Normalized multiple (indexed array of single $_FILES-like arrays). |
| 76 | + if (isset($value[0]) && is_array($value[0])) { |
| 77 | + foreach ($value as $v) { |
| 78 | + $this->validateFileArray($v); |
| 79 | + } |
| 80 | + return; |
| 81 | + } |
61 | 82 |
|
62 | | - // // Any other case. |
63 | | - // throw new ValidationException('Invalid file format.'); |
| 83 | + throw new ValidationException('Invalid file format.'); |
64 | 84 | } |
65 | 85 |
|
66 | 86 | protected function parseSize(string $size): int |
|
0 commit comments