|
21 | 21 | use function is_string; |
22 | 22 | use function mime_content_type; |
23 | 23 | use function pathinfo; |
| 24 | +use function round; |
24 | 25 | use function str_contains; |
25 | 26 | use function str_ends_with; |
26 | 27 | use function str_replace; |
|
51 | 52 | */ |
52 | 53 | final class FileHandler implements RuleHandlerInterface |
53 | 54 | { |
| 55 | + private const DEFAULT_NOT_EXACT_SIZE_MESSAGES = [ |
| 56 | + 'The size of {property} must be exactly {exactly, number} {exactly, plural, one{byte} other{bytes}}.', |
| 57 | + 'The size of {property} must be exactly {exactly}.', |
| 58 | + ]; |
| 59 | + private const DEFAULT_TOO_SMALL_MESSAGES = [ |
| 60 | + 'The size of {property} cannot be smaller than {limit, number} {limit, plural, one{byte} other{bytes}}.', |
| 61 | + 'The size of {property} cannot be smaller than {limit}.', |
| 62 | + ]; |
| 63 | + private const DEFAULT_TOO_BIG_MESSAGES = [ |
| 64 | + 'The size of {property} cannot be larger than {limit, number} {limit, plural, one{byte} other{bytes}}.', |
| 65 | + 'The size of {property} cannot be larger than {limit}.', |
| 66 | + ]; |
| 67 | + |
54 | 68 | public function validate(mixed $value, RuleInterface $rule, ValidationContext $context): Result |
55 | 69 | { |
56 | 70 | if (!$rule instanceof File) { |
@@ -279,27 +293,148 @@ private function validateSize(array $file, File $rule, ValidationContext $contex |
279 | 293 | } |
280 | 294 |
|
281 | 295 | if ($rule->getSize() !== null && $size !== $rule->getSize()) { |
| 296 | + $ruleSize = $rule->getSize(); |
| 297 | + $sizeParameters = $this->getSizeParameters($ruleSize); |
282 | 298 | $result->addError( |
283 | | - $rule->getNotExactSizeMessage(), |
284 | | - $this->getParameters($context, $file, ['exactly' => $rule->getSize()]), |
| 299 | + $this->getNotExactSizeMessage($rule, $sizeParameters['unit']), |
| 300 | + $this->getParameters( |
| 301 | + $context, |
| 302 | + $file, |
| 303 | + [ |
| 304 | + 'exactly' => $sizeParameters['value'], |
| 305 | + 'exactlyUnit' => $sizeParameters['unit'], |
| 306 | + 'exactlyBytes' => $sizeParameters['bytes'], |
| 307 | + ], |
| 308 | + ), |
285 | 309 | ); |
286 | 310 | } |
287 | 311 |
|
288 | 312 | if ($rule->getMinSize() !== null && $size < $rule->getMinSize()) { |
| 313 | + $minSize = $rule->getMinSize(); |
| 314 | + $sizeParameters = $this->getSizeParameters($minSize); |
289 | 315 | $result->addError( |
290 | | - $rule->getTooSmallMessage(), |
291 | | - $this->getParameters($context, $file, ['limit' => $rule->getMinSize()]), |
| 316 | + $this->getTooSmallMessage($rule, $sizeParameters['unit']), |
| 317 | + $this->getParameters( |
| 318 | + $context, |
| 319 | + $file, |
| 320 | + [ |
| 321 | + 'limit' => $sizeParameters['value'], |
| 322 | + 'limitUnit' => $sizeParameters['unit'], |
| 323 | + 'limitBytes' => $sizeParameters['bytes'], |
| 324 | + ], |
| 325 | + ), |
292 | 326 | ); |
293 | 327 | } |
294 | 328 |
|
295 | 329 | if ($rule->getMaxSize() !== null && $size > $rule->getMaxSize()) { |
| 330 | + $maxSize = $rule->getMaxSize(); |
| 331 | + $sizeParameters = $this->getSizeParameters($maxSize); |
296 | 332 | $result->addError( |
297 | | - $rule->getTooBigMessage(), |
298 | | - $this->getParameters($context, $file, ['limit' => $rule->getMaxSize()]), |
| 333 | + $this->getTooBigMessage($rule, $sizeParameters['unit']), |
| 334 | + $this->getParameters( |
| 335 | + $context, |
| 336 | + $file, |
| 337 | + [ |
| 338 | + 'limit' => $sizeParameters['value'], |
| 339 | + 'limitUnit' => $sizeParameters['unit'], |
| 340 | + 'limitBytes' => $sizeParameters['bytes'], |
| 341 | + ], |
| 342 | + ), |
299 | 343 | ); |
300 | 344 | } |
301 | 345 | } |
302 | 346 |
|
| 347 | + private function getNotExactSizeMessage(File $rule, string $unit): string |
| 348 | + { |
| 349 | + $message = $rule->getNotExactSizeMessage(); |
| 350 | + if (!in_array($message, self::DEFAULT_NOT_EXACT_SIZE_MESSAGES, true)) { |
| 351 | + return $message; |
| 352 | + } |
| 353 | + |
| 354 | + return match ($unit) { |
| 355 | + 'byte' => 'The size of {property} must be exactly {exactly, number} ' |
| 356 | + . '{exactly, plural, one{byte} other{bytes}}.', |
| 357 | + 'KB' => 'The size of {property} must be exactly {exactly, number} KB.', |
| 358 | + 'MB' => 'The size of {property} must be exactly {exactly, number} MB.', |
| 359 | + 'GB' => 'The size of {property} must be exactly {exactly, number} GB.', |
| 360 | + 'TB' => 'The size of {property} must be exactly {exactly, number} TB.', |
| 361 | + 'PB' => 'The size of {property} must be exactly {exactly, number} PB.', |
| 362 | + default => $message, |
| 363 | + }; |
| 364 | + } |
| 365 | + |
| 366 | + private function getTooSmallMessage(File $rule, string $unit): string |
| 367 | + { |
| 368 | + $message = $rule->getTooSmallMessage(); |
| 369 | + if (!in_array($message, self::DEFAULT_TOO_SMALL_MESSAGES, true)) { |
| 370 | + return $message; |
| 371 | + } |
| 372 | + |
| 373 | + return match ($unit) { |
| 374 | + 'byte' => 'The size of {property} cannot be smaller than {limit, number} ' |
| 375 | + . '{limit, plural, one{byte} other{bytes}}.', |
| 376 | + 'KB' => 'The size of {property} cannot be smaller than {limit, number} KB.', |
| 377 | + 'MB' => 'The size of {property} cannot be smaller than {limit, number} MB.', |
| 378 | + 'GB' => 'The size of {property} cannot be smaller than {limit, number} GB.', |
| 379 | + 'TB' => 'The size of {property} cannot be smaller than {limit, number} TB.', |
| 380 | + 'PB' => 'The size of {property} cannot be smaller than {limit, number} PB.', |
| 381 | + default => $message, |
| 382 | + }; |
| 383 | + } |
| 384 | + |
| 385 | + private function getTooBigMessage(File $rule, string $unit): string |
| 386 | + { |
| 387 | + $message = $rule->getTooBigMessage(); |
| 388 | + if (!in_array($message, self::DEFAULT_TOO_BIG_MESSAGES, true)) { |
| 389 | + return $message; |
| 390 | + } |
| 391 | + |
| 392 | + return match ($unit) { |
| 393 | + 'byte' => 'The size of {property} cannot be larger than {limit, number} ' |
| 394 | + . '{limit, plural, one{byte} other{bytes}}.', |
| 395 | + 'KB' => 'The size of {property} cannot be larger than {limit, number} KB.', |
| 396 | + 'MB' => 'The size of {property} cannot be larger than {limit, number} MB.', |
| 397 | + 'GB' => 'The size of {property} cannot be larger than {limit, number} GB.', |
| 398 | + 'TB' => 'The size of {property} cannot be larger than {limit, number} TB.', |
| 399 | + 'PB' => 'The size of {property} cannot be larger than {limit, number} PB.', |
| 400 | + default => $message, |
| 401 | + }; |
| 402 | + } |
| 403 | + |
| 404 | + /** |
| 405 | + * @return array{value: int|float, unit: string, bytes: int} |
| 406 | + */ |
| 407 | + private function getSizeParameters(int $size): array |
| 408 | + { |
| 409 | + if ($size < 1024) { |
| 410 | + return [ |
| 411 | + 'value' => $size, |
| 412 | + 'unit' => 'byte', |
| 413 | + 'bytes' => $size, |
| 414 | + ]; |
| 415 | + } |
| 416 | + |
| 417 | + $value = (float) $size; |
| 418 | + foreach (['KB', 'MB', 'GB', 'TB'] as $unit) { |
| 419 | + $value /= 1024; |
| 420 | + $roundedValue = round($value, 2); |
| 421 | + if ($roundedValue < 1024) { |
| 422 | + return [ |
| 423 | + 'value' => $roundedValue, |
| 424 | + 'unit' => $unit, |
| 425 | + 'bytes' => $size, |
| 426 | + ]; |
| 427 | + } |
| 428 | + } |
| 429 | + |
| 430 | + $value /= 1024; |
| 431 | + return [ |
| 432 | + 'value' => round($value, 2), |
| 433 | + 'unit' => 'PB', |
| 434 | + 'bytes' => $size, |
| 435 | + ]; |
| 436 | + } |
| 437 | + |
303 | 438 | /** |
304 | 439 | * @psalm-param FileData $file |
305 | 440 | */ |
|
0 commit comments