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