|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Fleetbase\Rules; |
| 4 | + |
| 5 | +use Fleetbase\Support\Utils; |
| 6 | +use Illuminate\Contracts\Validation\Rule; |
| 7 | +use Illuminate\Http\UploadedFile; |
| 8 | +use Illuminate\Support\Str; |
| 9 | + |
| 10 | +/** |
| 11 | + * FileInput Validation Rule. |
| 12 | + * |
| 13 | + * Validates that input is a valid file source (upload, base64, ID, or URL). |
| 14 | + */ |
| 15 | +class FileInput implements Rule |
| 16 | +{ |
| 17 | + /** |
| 18 | + * The validation error message. |
| 19 | + */ |
| 20 | + protected string $message = 'The :attribute must be a valid file upload, base64 string, file ID, or URL.'; |
| 21 | + |
| 22 | + /** |
| 23 | + * Determine if the validation rule passes. |
| 24 | + * |
| 25 | + * @param string $attribute |
| 26 | + */ |
| 27 | + public function passes($attribute, $value): bool |
| 28 | + { |
| 29 | + // Check for UploadedFile |
| 30 | + if ($value instanceof UploadedFile) { |
| 31 | + return $value->isValid(); |
| 32 | + } |
| 33 | + |
| 34 | + if (is_string($value)) { |
| 35 | + // Check for public ID |
| 36 | + if (Utils::isPublicId($value)) { |
| 37 | + return true; |
| 38 | + } |
| 39 | + |
| 40 | + // Check for Base64 data URI |
| 41 | + if (Str::startsWith($value, 'data:image') || Str::startsWith($value, 'data:application')) { |
| 42 | + return true; |
| 43 | + } |
| 44 | + |
| 45 | + // Check for valid URL |
| 46 | + if (filter_var($value, FILTER_VALIDATE_URL)) { |
| 47 | + return true; |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + return false; |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * Get the validation error message. |
| 56 | + */ |
| 57 | + public function message(): string |
| 58 | + { |
| 59 | + return $this->message; |
| 60 | + } |
| 61 | +} |
0 commit comments