• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

RonasIT / laravel-helpers / 25215527562

01 May 2026 01:12PM UTC coverage: 86.326% (+0.3%) from 85.996%
25215527562

Pull #253

github

web-flow
Merge 15c3381d8 into e7aff560a
Pull Request #253: [252]: add DB type range validation rule

72 of 78 new or added lines in 3 files covered. (92.31%)

1250 of 1448 relevant lines covered (86.33%)

18.37 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

86.96
/src/Rules/DBTypeRangeRule.php
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
    protected const string INTEGER_REGEX = '/^-?\d+$/';
14

15
    protected DBTypeResolverContract $resolver;
16

17
    public function __construct(
18
        protected string $type,
19
    ) {
20
        $this->resolver = app(DBTypeResolverContract::class);
48✔
21
    }
22

23
    public function validate(string $attribute, mixed $value, Closure $fail): void
24
    {
25
        if (is_null($value)) {
48✔
26
            return;
4✔
27
        }
28

29
        if (!$this->resolver->hasType($this->type)) {
44✔
30
            throw new InvalidValidationRuleUsageException(
1✔
31
                message: "db_type_range: Unknown type '{$this->type}' for the {$attribute} field.",
1✔
32
            );
1✔
33
        }
34

35
        $ranges = $this->resolver::ranges();
43✔
36

37
        list($min, $max) = $ranges[$this->type];
43✔
38

NEW
39
        match (true) {
×
40
            $this->resolver->isTypeCategory(DBTypeCategoryEnum::Integer, $this->type) => $this->validateInteger($attribute, $value, $min, $max, $fail),
43✔
41
            $this->resolver->isTypeCategory(DBTypeCategoryEnum::BigInteger, $this->type) => $this->validateBigInteger($attribute, $value, $min, $max, $fail),
19✔
42
            $this->resolver->isTypeCategory(DBTypeCategoryEnum::Float, $this->type) => $this->validateFloat($attribute, $value, $min, $max, $fail),
14✔
43
            $this->resolver->isTypeCategory(DBTypeCategoryEnum::String, $this->type) => $this->validateString($attribute, $value, $min, $max, $fail),
5✔
44
            default => null,
1✔
NEW
45
        };
×
46
    }
47

48
    protected function validateInteger(string $attribute, mixed $value, mixed $min, mixed $max, Closure $fail): void
49
    {
50
        if (!is_numeric($value)) {
24✔
51
            $fail("The {$attribute} must be numeric.");
1✔
52

53
            return;
1✔
54
        }
55

56
        $result = filter_var($value, FILTER_VALIDATE_INT, [
23✔
57
            'options' => [
23✔
58
                'min_range' => $min,
23✔
59
                'max_range' => $max,
23✔
60
            ],
23✔
61
        ]);
23✔
62

63
        if ($result === false) {
23✔
64
            $fail("The {$attribute} must be between {$min} and {$max}.");
10✔
65
        }
66
    }
67

68
    protected function validateBigInteger(string $attribute, mixed $value, mixed $min, mixed $max, Closure $fail): void
69
    {
70
        if (!preg_match(self::INTEGER_REGEX, (string) $value)) {
5✔
NEW
71
            $fail("The {$attribute} must be numeric.");
×
72

NEW
73
            return;
×
74
        }
75

76
        $tooSmall = bccomp((string) $value, (string) $min) === -1;
5✔
77
        $tooBig = bccomp((string) $value, (string) $max) === 1;
5✔
78

79
        if ($tooSmall || $tooBig) {
5✔
80
            $fail("The {$attribute} must be between {$min} and {$max}.");
1✔
81
        }
82
    }
83

84
    protected function validateFloat(string $attribute, mixed $value, mixed $min, mixed $max, Closure $fail): void
85
    {
86
        if (!is_numeric($value)) {
9✔
NEW
87
            $fail("The {$attribute} must be numeric.");
×
88

NEW
89
            return;
×
90
        }
91

92
        $metric = (float) $value;
9✔
93

94
        if ($metric < $min || $metric > $max) {
9✔
95
            $fail("The {$attribute} must be between {$min} and {$max}.");
2✔
96
        }
97
    }
98

99
    protected function validateString(string $attribute, mixed $value, mixed $min, mixed $max, Closure $fail): void
100
    {
101
        if (!is_string($value)) {
4✔
102
            $fail("The {$attribute} must be a string.");
2✔
103

104
            return;
2✔
105
        }
106

107
        $metric = mb_strlen($value);
2✔
108

109
        if ($metric < $min || $metric > $max) {
2✔
110
            $fail("The {$attribute} length must not exceed {$max} characters.");
1✔
111
        }
112
    }
113
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc