ArrayRule.php
1 week ago
BooleanRule.php
1 week ago
ConditionalRule.php
1 week ago
DateRule.php
1 week ago
ExistsRule.php
1 week ago
FileRule.php
1 week ago
InRule.php
1 week ago
NotInRule.php
1 week ago
NullableRule.php
1 week ago
NumericRule.php
1 week ago
ObjectRule.php
1 week ago
ProhibitedIfRule.php
1 week ago
ProhibitedRule.php
1 week ago
ProhibitedUnlessRule.php
1 week ago
RequiredIfRule.php
1 week ago
RequiredRule.php
1 week ago
RequiredUnlessRule.php
1 week ago
SameAsRule.php
1 week ago
SometimesRule.php
1 week ago
StringRule.php
1 week ago
UniqueRule.php
1 week ago
FileRule.php
295 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Required rule class. |
| 5 | * |
| 6 | * @package Framework |
| 7 | * @subpackage Validation |
| 8 | * @since 1.0.0 |
| 9 | */ |
| 10 | namespace Kirki\Framework\Validation\Rules; |
| 11 | |
| 12 | \defined('ABSPATH') || exit; |
| 13 | use finfo; |
| 14 | use Kirki\Framework\Filesystem\File as FilesystemFile; |
| 15 | use Kirki\Framework\Filesystem\MimeTypes; |
| 16 | use Kirki\Framework\Supports\Arr; |
| 17 | use Kirki\Framework\Validation\ValidationRule; |
| 18 | use Kirki\Framework\Supports\Str; |
| 19 | use InvalidArgumentException; |
| 20 | /** |
| 21 | * File rule class. |
| 22 | * |
| 23 | * @method $this size(string|int $size) |
| 24 | * @method $this min(string|int $min) |
| 25 | * @method $this max(string|int $max) |
| 26 | * @method $this between(array $between) |
| 27 | * @method $this image() |
| 28 | * @method $this types(array $types) |
| 29 | * @method $this extensions(array $extensions) |
| 30 | */ |
| 31 | class FileRule extends ValidationRule |
| 32 | { |
| 33 | /** |
| 34 | * The rule name. |
| 35 | * |
| 36 | * @var string |
| 37 | * |
| 38 | * @since 1.0.0 |
| 39 | */ |
| 40 | protected string $rule = 'file'; |
| 41 | /** |
| 42 | * The constraints. |
| 43 | * |
| 44 | * @var array |
| 45 | * |
| 46 | * @since 1.0.0 |
| 47 | */ |
| 48 | protected array $constraints = ['size', 'min', 'max', 'between', 'image', 'types', 'extensions']; |
| 49 | /** |
| 50 | * The constructor. |
| 51 | * |
| 52 | * @param array|null $args The arguments. |
| 53 | * |
| 54 | * @return void |
| 55 | * |
| 56 | * @since 1.0.0 |
| 57 | */ |
| 58 | public function __construct($args = null) |
| 59 | { |
| 60 | parent::__construct($args); |
| 61 | } |
| 62 | /** |
| 63 | * Validate the rule. |
| 64 | * |
| 65 | * @return bool |
| 66 | * |
| 67 | * @since 1.0.0 |
| 68 | */ |
| 69 | public function validate() : bool |
| 70 | { |
| 71 | $this->value = $this->make_file($this->value); |
| 72 | if (!$this->value || !$this->value instanceof FilesystemFile || $this->value->getRealPath() === '') { |
| 73 | return $this->fails($this->default_messages['default']); |
| 74 | } |
| 75 | return $this->validate_constraints(function ($passed, $constraint) { |
| 76 | if (!$passed) { |
| 77 | return $this->fails($this->default_messages[$constraint], $this->prepare_placeholder($constraint)); |
| 78 | } |
| 79 | }); |
| 80 | } |
| 81 | /** |
| 82 | * Prepare the placeholder. |
| 83 | * |
| 84 | * @param string $constraint The constraint. |
| 85 | * |
| 86 | * @return array The placeholder. |
| 87 | * |
| 88 | * @since 1.0.0 |
| 89 | */ |
| 90 | protected function prepare_placeholder($constraint) |
| 91 | { |
| 92 | if (\in_array($constraint, ['min', 'max', 'size'])) { |
| 93 | return [$constraint => $this->to_kilobytes($this->get($constraint))]; |
| 94 | } |
| 95 | if ($constraint === 'between') { |
| 96 | return ['min' => $this->to_kilobytes($this->get('between')[0]), 'max' => $this->to_kilobytes($this->get('between')[1])]; |
| 97 | } |
| 98 | return [$constraint => $this->get($constraint)]; |
| 99 | } |
| 100 | /** |
| 101 | * Validate the size constraint. |
| 102 | * |
| 103 | * @return bool |
| 104 | * |
| 105 | * @since 1.0.0 |
| 106 | */ |
| 107 | protected function validate_size() |
| 108 | { |
| 109 | $size = $this->to_kilobytes($this->get('size')); |
| 110 | $filesize = $this->get_file_original_size($this->value); |
| 111 | return $filesize === $size; |
| 112 | } |
| 113 | /** |
| 114 | * Validate the min constraint. |
| 115 | * |
| 116 | * @return bool |
| 117 | * |
| 118 | * @since 1.0.0 |
| 119 | */ |
| 120 | protected function validate_min() |
| 121 | { |
| 122 | $min = $this->to_kilobytes($this->get('min')); |
| 123 | $filesize = $this->get_file_original_size($this->value); |
| 124 | return $filesize >= $min; |
| 125 | } |
| 126 | /** |
| 127 | * Validate the max constraint. |
| 128 | * |
| 129 | * @return bool |
| 130 | * |
| 131 | * @since 1.0.0 |
| 132 | */ |
| 133 | protected function validate_max() |
| 134 | { |
| 135 | $max = $this->to_kilobytes($this->get('max')); |
| 136 | $filesize = $this->get_file_original_size($this->value); |
| 137 | return $filesize <= $max; |
| 138 | } |
| 139 | /** |
| 140 | * Validate the between constraint. |
| 141 | * |
| 142 | * @return bool |
| 143 | * |
| 144 | * @since 1.0.0 |
| 145 | */ |
| 146 | protected function validate_between() |
| 147 | { |
| 148 | [$min, $max] = $this->get('between'); |
| 149 | $min = $this->to_kilobytes($min); |
| 150 | $max = $this->to_kilobytes($max); |
| 151 | $filesize = $this->get_file_original_size($this->value); |
| 152 | return $filesize >= $min && $filesize <= $max; |
| 153 | } |
| 154 | /** |
| 155 | * Validate the image constraint. |
| 156 | * |
| 157 | * @return bool |
| 158 | * |
| 159 | * @since 1.0.0 |
| 160 | */ |
| 161 | protected function validate_image() |
| 162 | { |
| 163 | $mimetype = MimeTypes::make()->get_mimetype($this->value); |
| 164 | $image_mimetypes = MimeTypes::make()->images_mimetypes; |
| 165 | return \in_array($mimetype, $image_mimetypes, \true); |
| 166 | } |
| 167 | /** |
| 168 | * Validate the types constraint. |
| 169 | * |
| 170 | * @return bool |
| 171 | * |
| 172 | * @since 1.0.0 |
| 173 | */ |
| 174 | protected function validate_types() |
| 175 | { |
| 176 | $types = $this->get('types'); |
| 177 | $allowed_mimetypes = MimeTypes::make()->allowed_mimetypes($types); |
| 178 | $mimetype = MimeTypes::make()->get_mimetype($this->value); |
| 179 | return \in_array($mimetype, $allowed_mimetypes, \true); |
| 180 | } |
| 181 | /** |
| 182 | * Validate the extensions constraint. |
| 183 | * |
| 184 | * @return bool |
| 185 | * |
| 186 | * @since 1.0.0 |
| 187 | */ |
| 188 | protected function validate_extensions() |
| 189 | { |
| 190 | $extensions = $this->get('extensions'); |
| 191 | $allowed_extensions = MimeTypes::make()->allowed_extensions($extensions); |
| 192 | $possible_extensions = MimeTypes::make()->guess_extensions($this->value); |
| 193 | return \count(\array_values(\array_intersect($possible_extensions, $allowed_extensions))) > 0; |
| 194 | } |
| 195 | /** |
| 196 | * Convert the file size to kilobytes. |
| 197 | * |
| 198 | * @param string|int $size The file size. |
| 199 | * |
| 200 | * @return int The file size in kilobytes. |
| 201 | * |
| 202 | * @since 1.0.0 |
| 203 | */ |
| 204 | protected function to_kilobytes($size) |
| 205 | { |
| 206 | if (!\is_string($size)) { |
| 207 | return $size; |
| 208 | } |
| 209 | $size = \strtolower(\trim($size)); |
| 210 | $value = (float) $size; |
| 211 | switch (\true) { |
| 212 | case Str::ends_with($size, 'kb'): |
| 213 | return $value * 1; |
| 214 | case Str::ends_with($size, 'mb'): |
| 215 | return $value * 1024; |
| 216 | case Str::ends_with($size, 'gb'): |
| 217 | return $value * 1024 * 1024; |
| 218 | case Str::ends_with($size, 'tb'): |
| 219 | return $value * 1024 * 1024 * 1024; |
| 220 | default: |
| 221 | throw new InvalidArgumentException('Invalid file size: ' . $size); |
| 222 | } |
| 223 | } |
| 224 | /** |
| 225 | * Get the file original size. |
| 226 | * |
| 227 | * @param FilesystemFile $file The file. |
| 228 | * |
| 229 | * @return float The file original size. |
| 230 | * |
| 231 | * @since 1.0.0 |
| 232 | */ |
| 233 | protected function get_file_original_size(FilesystemFile $file) |
| 234 | { |
| 235 | return $file->getSize() / 1024; |
| 236 | } |
| 237 | /** |
| 238 | * Get the file original mimetype. |
| 239 | * |
| 240 | * @param FilesystemFile $file The file. |
| 241 | * |
| 242 | * @return string The file original mimetype. |
| 243 | * |
| 244 | * @since 1.0.0 |
| 245 | */ |
| 246 | protected function get_file_original_mimetype(FilesystemFile $file) |
| 247 | { |
| 248 | $finfo = new finfo(\FILEINFO_MIME_TYPE); |
| 249 | return \strtolower($finfo->file($file->getRealPath())); |
| 250 | } |
| 251 | /** |
| 252 | * Get the file original extension. |
| 253 | * |
| 254 | * @param FilesystemFile $file The file. |
| 255 | * |
| 256 | * @return string The file original extension. |
| 257 | * |
| 258 | * @since 1.0.0 |
| 259 | */ |
| 260 | protected function get_file_original_extension(FilesystemFile $file) |
| 261 | { |
| 262 | return \strtolower($file->getExtension()); |
| 263 | } |
| 264 | /** |
| 265 | * Make the file. |
| 266 | * |
| 267 | * @param string|FilesystemFile $value The file value. |
| 268 | * |
| 269 | * @return FilesystemFile The file. |
| 270 | * |
| 271 | * @since 1.0.0 |
| 272 | */ |
| 273 | protected function make_file($value) |
| 274 | { |
| 275 | if ($value instanceof FilesystemFile) { |
| 276 | return $value; |
| 277 | } |
| 278 | if (\is_string($value)) { |
| 279 | return new FilesystemFile($value, \false); |
| 280 | } |
| 281 | throw new InvalidArgumentException('Invalid file value: ' . $value); |
| 282 | } |
| 283 | /** |
| 284 | * Get the error message. |
| 285 | * |
| 286 | * @return string |
| 287 | * |
| 288 | * @since 1.0.0 |
| 289 | */ |
| 290 | public function messages() |
| 291 | { |
| 292 | return $this->process_messages($this->messages); |
| 293 | } |
| 294 | } |
| 295 |