|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace RonasIT\Support\Rules; |
| 4 | + |
| 5 | +use Closure; |
| 6 | +use Illuminate\Contracts\Validation\ValidationRule; |
| 7 | +use RonasIT\Support\Contracts\DBTypeResolverContract; |
| 8 | +use RonasIT\Support\Enums\DBTypeCategoryEnum; |
| 9 | +use RonasIT\Support\Exceptions\InvalidValidationRuleUsageException; |
| 10 | + |
| 11 | +class DBTypeRangeRule implements ValidationRule |
| 12 | +{ |
| 13 | + private const string INTEGER_PATTERN = '/^-?\d+$/'; |
| 14 | + |
| 15 | + protected DBTypeResolverContract $resolver; |
| 16 | + |
| 17 | + public function __construct( |
| 18 | + protected string $type, |
| 19 | + ) { |
| 20 | + $this->resolver = app(DBTypeResolverContract::class); |
| 21 | + } |
| 22 | + |
| 23 | + public function validate(string $attribute, mixed $value, Closure $fail): void |
| 24 | + { |
| 25 | + if (is_null($value)) { |
| 26 | + return; |
| 27 | + } |
| 28 | + |
| 29 | + if (!$this->resolver->hasType($this->type)) { |
| 30 | + throw new InvalidValidationRuleUsageException( |
| 31 | + message: "db_type_range: Unknown type '{$this->type}' for the {$attribute} field.", |
| 32 | + ); |
| 33 | + } |
| 34 | + |
| 35 | + list($min, $max) = $this->resolver->getRange($this->type); |
| 36 | + |
| 37 | + match ($this->resolver->getTypeCategory($this->type)) { |
| 38 | + DBTypeCategoryEnum::Integer => $this->validateInteger($attribute, $value, $min, $max, $fail), |
| 39 | + DBTypeCategoryEnum::Float => $this->validateFloat($attribute, $value, $min, $max, $fail), |
| 40 | + DBTypeCategoryEnum::String => $this->validateString($attribute, $value, $max, $fail), |
| 41 | + default => null, |
| 42 | + }; |
| 43 | + } |
| 44 | + |
| 45 | + protected function validateInteger(string $attribute, mixed $value, mixed $min, mixed $max, Closure $fail): void |
| 46 | + { |
| 47 | + if (!is_scalar($value) || !preg_match(self::INTEGER_PATTERN, (string) $value)) { |
| 48 | + $fail("The {$attribute} must be an integer."); |
| 49 | + |
| 50 | + return; |
| 51 | + } |
| 52 | + |
| 53 | + $tooSmall = bccomp((string) $value, (string) $min) === -1; |
| 54 | + $tooBig = bccomp((string) $value, (string) $max) === 1; |
| 55 | + |
| 56 | + if ($tooSmall || $tooBig) { |
| 57 | + $fail("The {$attribute} must be between {$min} and {$max}."); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + protected function validateFloat(string $attribute, mixed $value, mixed $min, mixed $max, Closure $fail): void |
| 62 | + { |
| 63 | + if (!is_numeric($value)) { |
| 64 | + $fail("The {$attribute} must be numeric."); |
| 65 | + |
| 66 | + return; |
| 67 | + } |
| 68 | + |
| 69 | + if ((float) $value < $min || (float) $value > $max) { |
| 70 | + $fail("The {$attribute} must be between {$min} and {$max}."); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + protected function validateString(string $attribute, mixed $value, mixed $max, Closure $fail): void |
| 75 | + { |
| 76 | + if (!is_string($value)) { |
| 77 | + $fail("The {$attribute} must be a string."); |
| 78 | + |
| 79 | + return; |
| 80 | + } |
| 81 | + |
| 82 | + if (mb_strlen($value) > $max) { |
| 83 | + $fail("The {$attribute} length must not exceed {$max} characters."); |
| 84 | + } |
| 85 | + } |
| 86 | +} |
0 commit comments