| 1 | <?php |
||
| 20 | class FileValidator implements FileValidatorInterface |
||
| 21 | { |
||
| 22 | protected $errors; |
||
| 23 | protected $allowedTypes = [ |
||
| 24 | 'jpeg', |
||
| 25 | 'jpg', |
||
| 26 | 'png', |
||
| 27 | 'gif', |
||
| 28 | ]; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @param UploadedFile $file |
||
| 32 | * |
||
| 33 | * @return bool |
||
| 34 | */ |
||
| 35 | public function validate(UploadedFile $file) |
||
| 36 | { |
||
| 37 | // Check allowed types. |
||
| 38 | $fileExtension = strtolower($file->getClientOriginalExtension()); |
||
| 39 | $guessedFileExtension = strtolower($file->guessExtension()); |
||
| 40 | |||
| 41 | if (!in_array($guessedFileExtension, $this->allowedTypes)) { |
||
| 42 | $this->errors[] = 'Files of "' . $guessedFileExtension . '" type are not allowed'; |
||
| 43 | |||
| 44 | return false; |
||
| 45 | } |
||
| 46 | |||
| 47 | if (!in_array($fileExtension, $this->allowedTypes)) { |
||
| 48 | $this->errors[] = 'Files of "' . $fileExtension . '" type are not allowed'; |
||
| 49 | |||
| 50 | return false; |
||
| 51 | } |
||
| 52 | |||
| 53 | return true; |
||
| 54 | } |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @return array |
||
| 58 | */ |
||
| 59 | public function getAllowedTypes() |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @param array $allowedTypes |
||
| 66 | */ |
||
| 67 | public function setAllowedTypes($allowedTypes) |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @return mixed |
||
| 74 | */ |
||
| 75 | public function getErrors() |
||
| 79 | } |