File.php
120 lines
| 1 | <?php |
| 2 | declare(strict_types=1); |
| 3 | |
| 4 | namespace Give\Framework\ValidationRules\Rules; |
| 5 | |
| 6 | use Closure; |
| 7 | use Give\Framework\Http\Types\UploadedFile; |
| 8 | use Give\Vendors\StellarWP\Validation\Contracts\ValidatesOnFrontEnd; |
| 9 | use Give\Vendors\StellarWP\Validation\Contracts\ValidationRule; |
| 10 | |
| 11 | /** |
| 12 | * @since 2.32.0 |
| 13 | */ |
| 14 | class File implements ValidationRule, ValidatesOnFrontEnd |
| 15 | { |
| 16 | /** |
| 17 | * The size, in bytes, of the uploaded file |
| 18 | * |
| 19 | * @var int |
| 20 | */ |
| 21 | protected $maxSize; |
| 22 | |
| 23 | /** |
| 24 | * @var string[] |
| 25 | */ |
| 26 | protected $allowedMimeTypes; |
| 27 | |
| 28 | /** |
| 29 | * @since 2.32.0 |
| 30 | */ |
| 31 | public static function id(): string |
| 32 | { |
| 33 | return 'file'; |
| 34 | } |
| 35 | |
| 36 | /** |
| 37 | * @since 2.32.0 |
| 38 | */ |
| 39 | public function maxSize(int $maxSize): ValidationRule |
| 40 | { |
| 41 | $this->maxSize = $maxSize; |
| 42 | |
| 43 | return $this; |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * @since 2.32.0 |
| 48 | */ |
| 49 | public function getMaxSize(): int |
| 50 | { |
| 51 | return $this->maxSize ?? wp_max_upload_size(); |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * @since 2.32.0 |
| 56 | */ |
| 57 | public function allowedMimeTypes(array $allowedMimeTypes): ValidationRule |
| 58 | { |
| 59 | $this->allowedMimeTypes = $allowedMimeTypes; |
| 60 | |
| 61 | return $this; |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * @since 2.32.0 |
| 66 | * |
| 67 | * @return string[] |
| 68 | */ |
| 69 | public function getAllowedMimeTypes(): array |
| 70 | { |
| 71 | return $this->allowedMimeTypes ?? get_allowed_mime_types(); |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * @since 2.32.0 |
| 76 | **/ |
| 77 | public function __invoke($value, Closure $fail, string $key, array $values) |
| 78 | { |
| 79 | try { |
| 80 | $file = UploadedFile::fromArray($value); |
| 81 | |
| 82 | if (!$file->isUploadedFile()) { |
| 83 | $fail(sprintf(__('%s must be a valid file.', 'give'), '{field}')); |
| 84 | } |
| 85 | |
| 86 | // check against both the allowed mime types defined by the file rule and the server |
| 87 | if (!in_array($file->getMimeType(), $this->getAllowedMimeTypes(), true) || |
| 88 | !in_array($file->getMimeType(), get_allowed_mime_types(), true)) { |
| 89 | $fail(sprintf(__('%s must be a valid file type.', 'give'), '{field}')); |
| 90 | } |
| 91 | |
| 92 | // check against both the max upload size defined by the file rule and the server |
| 93 | if ($file->getSize() > $this->getMaxSize() || $file->getSize() > wp_max_upload_size()) { |
| 94 | $fail( |
| 95 | sprintf(__('%s must be less than or equal to %d bytes.', 'give'), '{field}', $this->getMaxSize()) |
| 96 | ); |
| 97 | } |
| 98 | |
| 99 | if ($file->getError() !== UPLOAD_ERR_OK) { |
| 100 | $fail(sprintf(__('%s must be a valid file.', 'give'), '{field}')); |
| 101 | } |
| 102 | } catch (\Throwable $e) { |
| 103 | $fail($e->getMessage()); |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * @since 2.32.0 |
| 109 | */ |
| 110 | public static function fromString(string $options = null): ValidationRule |
| 111 | { |
| 112 | return new self(); |
| 113 | } |
| 114 | |
| 115 | public function serializeOption() |
| 116 | { |
| 117 | return null; |
| 118 | } |
| 119 | } |
| 120 |