Passed
Push — master ( 5c2907...37dc07 )
by Sergei
24:35 queued 21:56
created

NumberHandler::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 0
c 1
b 0
f 0
dl 0
loc 2
ccs 1
cts 1
cp 1
rs 10
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Validator\Rule;
6
7
use Yiisoft\Strings\NumericHelper;
8
use Yiisoft\Validator\Exception\UnexpectedRuleException;
9
use Yiisoft\Validator\Result;
10
use Yiisoft\Validator\RuleHandlerInterface;
11
use Yiisoft\Validator\ValidationContext;
12
13
/**
14
 * Validates that the value is a number.
15
 *
16
 * The format of the number must match the regular expression specified in {@see Number::$integerPattern}
17
 * or {@see Number::$numberPattern}. Optionally, you may configure the {@see Number::min()} and {@see Number::max()}
18
 * to ensure the number is within certain range.
19
 */
20
final class NumberHandler implements RuleHandlerInterface
21
{
22 111
    public function validate(mixed $value, object $rule, ValidationContext $context): Result
23
    {
24 111
        if (!$rule instanceof Number) {
25 1
            throw new UnexpectedRuleException(Number::class, $rule);
26
        }
27
28 110
        $result = new Result();
29
30 110
        if (is_bool($value) || !is_scalar($value)) {
31 12
            $result->addError(
32 12
                $rule->isAsInteger() ? 'Value must be an integer.' : 'Value must be a number.',
33
                [
34 12
                    'attribute' => $context->getAttribute(),
35
                    'value' => $value,
36
                ],
37
            );
38 12
            return $result;
39
        }
40
41 98
        $pattern = $rule->isAsInteger() ? $rule->getIntegerPattern() : $rule->getNumberPattern();
42
43 98
        if (!preg_match($pattern, NumericHelper::normalize($value))) {
44 23
            $result->addError(
45 23
                $rule->isAsInteger() ? 'Value must be an integer.' : 'Value must be a number.',
46
                [
47 23
                    'attribute' => $context->getAttribute(),
48
                    'value' => $value,
49
                ],
50
            );
51 75
        } elseif ($rule->getMin() !== null && $value < $rule->getMin()) {
52 34
            $result->addError(
53 34
                $rule->getTooSmallMessage(),
54
                [
55 34
                    'min' => $rule->getMin(),
56 34
                    'attribute' => $context->getAttribute(),
57
                    'value' => $value,
58
                ],
59
            );
60 53
        } elseif ($rule->getMax() !== null && $value > $rule->getMax()) {
61 17
            $result->addError(
62 17
                $rule->getTooBigMessage(),
63
                [
64 17
                    'max' => $rule->getMax(),
65 17
                    'attribute' => $context->getAttribute(),
66
                    'value' => $value,
67
                ],
68
            );
69
        }
70
71 98
        return $result;
72
    }
73
}
74