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
|
73 |
|
public function __construct(?FormatterInterface $formatter = null) |
26
|
|
|
{ |
27
|
73 |
|
$this->formatter = $formatter ?? new Formatter(); |
28
|
|
|
} |
29
|
|
|
|
30
|
73 |
|
public function validate(mixed $value, object $rule, ?ValidationContext $context = null): Result |
31
|
|
|
{ |
32
|
73 |
|
if (!$rule instanceof Number) { |
33
|
1 |
|
throw new UnexpectedRuleException(Number::class, $rule); |
34
|
|
|
} |
35
|
|
|
|
36
|
72 |
|
$result = new Result(); |
37
|
|
|
|
38
|
72 |
|
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
|
67 |
|
$pattern = $rule->isAsInteger() ? $rule->getIntegerPattern() : $rule->getNumberPattern(); |
48
|
|
|
|
49
|
67 |
|
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
|
46 |
|
} elseif ($rule->getMin() !== null && $value < $rule->getMin()) { |
56
|
11 |
|
$formattedMessage = $this->formatter->format( |
57
|
11 |
|
$rule->getTooSmallMessage(), |
58
|
11 |
|
['min' => $rule->getMin(), 'attribute' => $context?->getAttribute(), 'value' => $value] |
59
|
|
|
); |
60
|
11 |
|
$result->addError($formattedMessage); |
61
|
38 |
|
} elseif ($rule->getMax() !== null && $value > $rule->getMax()) { |
62
|
5 |
|
$formattedMessage = $this->formatter->format( |
63
|
5 |
|
$rule->getTooBigMessage(), |
64
|
5 |
|
['max' => $rule->getMax(), 'attribute' => $context?->getAttribute(), 'value' => $value] |
65
|
|
|
); |
66
|
5 |
|
$result->addError($formattedMessage); |
67
|
|
|
} |
68
|
|
|
|
69
|
67 |
|
return $result; |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|