|
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\Formatter; |
|
10
|
|
|
use Yiisoft\Validator\FormatterInterface; |
|
11
|
|
|
use Yiisoft\Validator\Result; |
|
12
|
|
|
use Yiisoft\Validator\ValidationContext; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Validates that the value is a number. |
|
16
|
|
|
* |
|
17
|
|
|
* The format of the number must match the regular expression specified in {@see Number::$integerPattern} |
|
18
|
|
|
* or {@see Number::$numberPattern}. Optionally, you may configure the {@see Number::min()} and {@see Number::max()} |
|
19
|
|
|
* to ensure the number is within certain range. |
|
20
|
|
|
*/ |
|
21
|
|
|
final class NumberHandler implements RuleHandlerInterface |
|
22
|
|
|
{ |
|
23
|
|
|
private FormatterInterface $formatter; |
|
24
|
|
|
|
|
25
|
76 |
|
public function __construct(?FormatterInterface $formatter = null) |
|
26
|
|
|
{ |
|
27
|
76 |
|
$this->formatter = $formatter ?? new Formatter(); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
76 |
|
public function validate(mixed $value, object $rule, ?ValidationContext $context = null): Result |
|
31
|
|
|
{ |
|
32
|
76 |
|
if (!$rule instanceof Number) { |
|
33
|
1 |
|
throw new UnexpectedRuleException(Number::class, $rule); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
75 |
|
$result = new Result(); |
|
37
|
|
|
|
|
38
|
75 |
|
if (is_bool($value) || !is_scalar($value)) { |
|
39
|
5 |
|
$formattedMessage = $this->formatter->format( |
|
40
|
5 |
|
$rule->isAsInteger() ? 'Value must be an integer.' : 'Value must be a number.', |
|
41
|
5 |
|
['attribute' => $context?->getAttribute(), 'value' => $value] |
|
42
|
|
|
); |
|
43
|
5 |
|
$result->addError($formattedMessage); |
|
44
|
5 |
|
return $result; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
70 |
|
$pattern = $rule->isAsInteger() ? $rule->getIntegerPattern() : $rule->getNumberPattern(); |
|
48
|
|
|
|
|
49
|
70 |
|
if (!preg_match($pattern, NumericHelper::normalize($value))) { |
|
50
|
21 |
|
$formattedMessage = $this->formatter->format( |
|
51
|
21 |
|
$rule->isAsInteger() ? 'Value must be an integer.' : 'Value must be a number.', |
|
52
|
21 |
|
['attribute' => $context?->getAttribute(), 'value' => $value] |
|
53
|
|
|
); |
|
54
|
21 |
|
$result->addError($formattedMessage); |
|
55
|
49 |
|
} elseif ($rule->getMin() !== null && $value < $rule->getMin()) { |
|
56
|
13 |
|
$formattedMessage = $this->formatter->format( |
|
57
|
13 |
|
$rule->getTooSmallMessage(), |
|
58
|
13 |
|
['min' => $rule->getMin(), 'attribute' => $context?->getAttribute(), 'value' => $value] |
|
59
|
|
|
); |
|
60
|
13 |
|
$result->addError($formattedMessage); |
|
61
|
39 |
|
} elseif ($rule->getMax() !== null && $value > $rule->getMax()) { |
|
62
|
6 |
|
$formattedMessage = $this->formatter->format( |
|
63
|
6 |
|
$rule->getTooBigMessage(), |
|
64
|
6 |
|
['max' => $rule->getMax(), 'attribute' => $context?->getAttribute(), 'value' => $value] |
|
65
|
|
|
); |
|
66
|
6 |
|
$result->addError($formattedMessage); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
70 |
|
return $result; |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
|