1
|
|
|
<?php |
2
|
|
|
namespace Yiisoft\Validator\Rule; |
3
|
|
|
|
4
|
|
|
use Yiisoft\Strings\StringHelper; |
5
|
|
|
use Yiisoft\Validator\DataSetInterface; |
6
|
|
|
use Yiisoft\Validator\Result; |
7
|
|
|
use Yiisoft\Validator\Rule; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* NumberValidator validates that the attribute value is a number. |
11
|
|
|
* |
12
|
|
|
* The format of the number must match the regular expression specified in [[integerPattern]] or [[numberPattern]]. |
13
|
|
|
* Optionally, you may configure the [[max]] and [[min]] properties to ensure the number |
14
|
|
|
* is within certain range. |
15
|
|
|
*/ |
16
|
|
|
class Number extends Rule |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var bool whether the attribute value can only be an integer. Defaults to false. |
20
|
|
|
*/ |
21
|
|
|
private bool $asInteger = false; |
22
|
|
|
/** |
23
|
|
|
* @var int|float upper limit of the number. Defaults to null, meaning no upper limit. |
24
|
|
|
* @see tooBigMessage for the customized message used when the number is too big. |
25
|
|
|
*/ |
26
|
|
|
private $max; |
27
|
|
|
/** |
28
|
|
|
* @var int|float lower limit of the number. Defaults to null, meaning no lower limit. |
29
|
|
|
* @see tooSmallMessage for the customized message used when the number is too small. |
30
|
|
|
*/ |
31
|
|
|
private $min; |
32
|
|
|
/** |
33
|
|
|
* @var string user-defined error message used when the value is bigger than {@link $max}. |
34
|
|
|
*/ |
35
|
|
|
private string $tooBigMessage = 'Value must be no greater than {max}.'; |
36
|
|
|
/** |
37
|
|
|
* @var string user-defined error message used when the value is smaller than {@link $min}. |
38
|
|
|
*/ |
39
|
|
|
private string $tooSmallMessage = 'Value must be no less than {min}.'; |
40
|
|
|
/** |
41
|
|
|
* @var string the regular expression for matching integers. |
42
|
|
|
*/ |
43
|
|
|
private string $integerPattern = '/^\s*[+-]?\d+\s*$/'; |
44
|
|
|
/** |
45
|
|
|
* @var string the regular expression for matching numbers. It defaults to a pattern |
46
|
|
|
* that matches floating numbers with optional exponential part (e.g. -1.23e-10). |
47
|
|
|
*/ |
48
|
|
|
private string $numberPattern = '/^\s*[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?\s*$/'; |
49
|
|
|
|
50
|
13 |
|
private function getNotANumberMessage(array $arguments): string |
51
|
|
|
{ |
52
|
13 |
|
if ($this->asInteger === true) { |
53
|
6 |
|
return $this->formatMessage('Value must be an integer.', $arguments); |
54
|
|
|
} |
55
|
|
|
|
56
|
7 |
|
return $this->formatMessage('Value must be a number.', $arguments); |
57
|
|
|
} |
58
|
|
|
|
59
|
7 |
|
private function getTooBigMessage(array $arguments): string |
60
|
|
|
{ |
61
|
7 |
|
return $this->formatMessage($this->tooBigMessage, $arguments); |
62
|
|
|
} |
63
|
|
|
|
64
|
8 |
|
private function getTooSmallMessage(array $arguments): string |
65
|
|
|
{ |
66
|
8 |
|
return $this->formatMessage($this->tooSmallMessage, $arguments); |
67
|
|
|
} |
68
|
|
|
|
69
|
9 |
|
public function integer(): self |
70
|
|
|
{ |
71
|
9 |
|
$this->asInteger = true; |
72
|
9 |
|
return $this; |
73
|
|
|
} |
74
|
|
|
|
75
|
11 |
|
public function min($value): self |
76
|
|
|
{ |
77
|
11 |
|
$this->min = $value; |
78
|
11 |
|
return $this; |
79
|
|
|
} |
80
|
|
|
|
81
|
11 |
|
public function max($value): self |
82
|
|
|
{ |
83
|
11 |
|
$this->max = $value; |
84
|
11 |
|
return $this; |
85
|
|
|
} |
86
|
|
|
|
87
|
1 |
|
public function tooSmallMessage(string $message): self |
88
|
|
|
{ |
89
|
1 |
|
$this->tooSmallMessage = $message; |
90
|
1 |
|
return $this; |
91
|
|
|
} |
92
|
|
|
|
93
|
30 |
|
protected function validateValue($value, DataSetInterface $dataSet = null): Result |
94
|
|
|
{ |
95
|
30 |
|
$result = new Result(); |
96
|
|
|
|
97
|
30 |
|
if ($this->isNotNumber($value)) { |
98
|
4 |
|
$result->addError($this->getNotANumberMessage(['value' => $value])); |
99
|
4 |
|
return $result; |
100
|
|
|
} |
101
|
|
|
|
102
|
26 |
|
$pattern = $this->asInteger ? $this->integerPattern : $this->numberPattern; |
103
|
|
|
|
104
|
26 |
|
if (!preg_match($pattern, StringHelper::normalizeNumber($value))) { |
105
|
9 |
|
$result->addError($this->getNotANumberMessage(['value' => $value])); |
106
|
23 |
|
} elseif ($this->min !== null && $value < $this->min) { |
107
|
8 |
|
$result->addError($this->getTooSmallMessage(['min' => $this->min])); |
108
|
21 |
|
} elseif ($this->max !== null && $value > $this->max) { |
109
|
7 |
|
$result->addError($this->getTooBigMessage(['max' => $this->max])); |
110
|
|
|
} |
111
|
|
|
|
112
|
26 |
|
return $result; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/* |
116
|
|
|
* @param mixed $value the data value to be checked. |
117
|
|
|
*/ |
118
|
30 |
|
private function isNotNumber($value): bool |
119
|
|
|
{ |
120
|
30 |
|
return is_array($value) |
121
|
30 |
|
|| (is_object($value) && !method_exists($value, '__toString')) |
122
|
30 |
|
|| (!is_object($value) && !is_scalar($value) && $value !== null); |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|