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