|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This software package is licensed under AGPL or Commercial license. |
|
5
|
|
|
* |
|
6
|
|
|
* @package maslosoft/mangan |
|
7
|
|
|
* @licence AGPL or Commercial |
|
8
|
|
|
* @copyright Copyright (c) Piotr Masełkowski <[email protected]> |
|
9
|
|
|
* @copyright Copyright (c) Maslosoft |
|
10
|
|
|
* @copyright Copyright (c) Others as mentioned in code |
|
11
|
|
|
* @link https://maslosoft.com/mangan/ |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
namespace Maslosoft\Mangan\Validators\BuiltIn; |
|
15
|
|
|
|
|
16
|
|
|
use Maslosoft\Addendum\Interfaces\AnnotatedInterface; |
|
17
|
|
|
use Maslosoft\Mangan\Interfaces\Validators\ValidatorInterface; |
|
18
|
|
|
use Maslosoft\Mangan\Meta\ManganMeta; |
|
19
|
|
|
use Maslosoft\Mangan\Validators\Traits\AllowEmpty; |
|
20
|
|
|
use Maslosoft\Mangan\Validators\Traits\Messages; |
|
21
|
|
|
use Maslosoft\Mangan\Validators\Traits\OnScenario; |
|
22
|
|
|
use Maslosoft\Mangan\Validators\Traits\Safe; |
|
23
|
|
|
use Maslosoft\Mangan\Validators\Traits\SkipOnError; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* NumberValidator |
|
27
|
|
|
* |
|
28
|
|
|
* @author Piotr Maselkowski <pmaselkowski at gmail.com> |
|
29
|
|
|
*/ |
|
30
|
|
|
class NumberValidator implements ValidatorInterface |
|
31
|
|
|
{ |
|
32
|
|
|
|
|
33
|
|
|
use AllowEmpty, |
|
34
|
|
|
Messages, |
|
35
|
|
|
OnScenario, |
|
36
|
|
|
Safe, |
|
37
|
|
|
SkipOnError; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Whether the attribute value can only be an integer. Defaults to false. |
|
41
|
|
|
* @var boolean |
|
42
|
|
|
*/ |
|
43
|
|
|
public $integerOnly = false; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Upper limit of the number. Defaults to null, meaning no upper limit. |
|
47
|
|
|
* @var integer|float |
|
48
|
|
|
*/ |
|
49
|
|
|
public $max = NULL; |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Lower limit of the number. Defaults to null, meaning no lower limit. |
|
53
|
|
|
* @var integer|float |
|
54
|
|
|
*/ |
|
55
|
|
|
public $min = NULL; |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Number must be greater than. Defaults to null, meaning no constraint. |
|
59
|
|
|
* @var integer|float |
|
60
|
|
|
*/ |
|
61
|
|
|
public $gt = null; |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Number must be lesser than. Defaults to null, meaning no constraint. |
|
65
|
|
|
* @var integer|float |
|
66
|
|
|
*/ |
|
67
|
|
|
public $lt= null; |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Deprecated: Use `msgTooSmall` instead |
|
71
|
|
|
* @var string user-defined error message used when the value is too big. |
|
72
|
|
|
* @deprecated Use `msgTooSmall` instead |
|
73
|
|
|
*/ |
|
74
|
|
|
public $tooBig = NULL; |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Deprecated: Use `msgTooBig` instead |
|
78
|
|
|
* @var string user-defined error message used when the value is too small. |
|
79
|
|
|
* @deprecated Use `msgTooBig` instead |
|
80
|
|
|
*/ |
|
81
|
|
|
public $tooSmall = NULL; |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Custom message to show if value is not number |
|
85
|
|
|
* @Label('{attribute} must be a number') |
|
86
|
|
|
* @var string |
|
87
|
|
|
*/ |
|
88
|
|
|
public $msgNumber = ''; |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Custom message to show if value is not integer when integer checking is |
|
92
|
|
|
* enabled |
|
93
|
|
|
* @Label('{attribute} must be an integer') |
|
94
|
|
|
* @var string |
|
95
|
|
|
*/ |
|
96
|
|
|
public $msgInteger = ''; |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Custom message to show if value is over maximum |
|
100
|
|
|
* @Label('{attribute} is too small (minimum is {min})') |
|
101
|
|
|
* @var string |
|
102
|
|
|
*/ |
|
103
|
|
|
public $msgTooSmall = ''; |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* Custom message to show if value is under minimum |
|
107
|
|
|
* @Label('{attribute} is too big (maximum is {max})') |
|
108
|
|
|
* @var string |
|
109
|
|
|
*/ |
|
110
|
|
|
public $msgTooBig = ''; |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* Custom message to show if value must be greater than required |
|
114
|
|
|
* @Label('{attribute} must be greater than {gt}') |
|
115
|
|
|
* @var string |
|
116
|
|
|
*/ |
|
117
|
|
|
public $msgGt = ''; |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* Custom message to show if value is lesser than required |
|
121
|
|
|
* @Label('{attribute} must be lesser than {lt}') |
|
122
|
|
|
* @var string |
|
123
|
|
|
*/ |
|
124
|
|
|
public $msgLt = ''; |
|
125
|
|
|
|
|
126
|
11 |
|
public function isValid(AnnotatedInterface $model, $attribute) |
|
127
|
|
|
{ |
|
128
|
|
|
// For lt/gt values exactly zero, the `allowEmpty` **must** be `false` |
|
129
|
|
|
// or validation would not be performed at all. |
|
130
|
11 |
|
if($this->lt === 0 || $this->gt === 0 || $this->lt === 0.0 || $this->gt === 0.0) |
|
131
|
|
|
{ |
|
132
|
4 |
|
$this->allowEmpty = false; |
|
133
|
|
|
} |
|
134
|
11 |
|
$value = $model->$attribute; |
|
135
|
11 |
|
if ($this->allowEmpty && empty($value)) |
|
136
|
|
|
{ |
|
137
|
|
|
return true; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
11 |
|
$label = ManganMeta::create($model)->field($attribute)->label; |
|
141
|
11 |
|
if (!is_scalar($value)) |
|
142
|
|
|
{ |
|
143
|
1 |
|
$this->addError('msgNumber', ['{attribute}' => $label]); |
|
144
|
1 |
|
return false; |
|
145
|
|
|
} |
|
146
|
11 |
|
if (!is_numeric($value)) |
|
147
|
|
|
{ |
|
148
|
1 |
|
$this->addError('msgNumber', ['{attribute}' => $label]); |
|
149
|
1 |
|
return false; |
|
150
|
|
|
} |
|
151
|
11 |
|
if ($this->integerOnly) |
|
152
|
|
|
{ |
|
153
|
|
|
|
|
154
|
2 |
|
if (false === filter_var($value, FILTER_VALIDATE_INT)) |
|
155
|
|
|
{ |
|
156
|
1 |
|
$this->addError('msgInteger', ['{attribute}' => $label]); |
|
157
|
2 |
|
return false; |
|
158
|
|
|
} |
|
159
|
|
|
} |
|
160
|
|
|
else |
|
161
|
|
|
{ |
|
162
|
9 |
|
if (false === filter_var($value, FILTER_VALIDATE_FLOAT)) |
|
163
|
|
|
{ |
|
164
|
|
|
$this->addError('msgNumber', ['{attribute}' => $label]); |
|
165
|
|
|
return false; |
|
166
|
|
|
} |
|
167
|
|
|
} |
|
168
|
10 |
|
if ($this->min !== null && $value < $this->min) |
|
169
|
|
|
{ |
|
170
|
1 |
|
if (!empty($this->tooSmall)) |
|
171
|
|
|
{ |
|
172
|
|
|
$this->addError($this->tooSmall, ['{min}' => $this->min, '{attribute}' => $label]); |
|
173
|
|
|
return false; |
|
174
|
|
|
} |
|
175
|
1 |
|
$this->addError('msgTooSmall', ['{min}' => $this->min, '{attribute}' => $label]); |
|
176
|
1 |
|
return false; |
|
177
|
|
|
} |
|
178
|
9 |
|
if ($this->max !== null && $value > $this->max) |
|
179
|
|
|
{ |
|
180
|
1 |
|
if (!empty($this->tooBig)) |
|
181
|
|
|
{ |
|
182
|
|
|
$this->addError($this->tooBig, ['{max}' => $this->max, '{attribute}' => $label]); |
|
183
|
|
|
} |
|
184
|
1 |
|
$this->addError('msgTooBig', ['{max}' => $this->max, '{attribute}' => $label]); |
|
185
|
1 |
|
return false; |
|
186
|
|
|
} |
|
187
|
8 |
|
if ($this->gt !== null && !($value > $this->gt)) |
|
188
|
|
|
{ |
|
189
|
1 |
|
$this->addError('msgGt', ['{gt}' => $this->gt, '{attribute}' => $label]); |
|
190
|
1 |
|
return false; |
|
191
|
|
|
} |
|
192
|
7 |
|
if ($this->lt !== null && !($value < $this->lt)) |
|
193
|
|
|
{ |
|
194
|
1 |
|
$this->addError('msgLt', ['{lt}' => $this->lt, '{attribute}' => $label]); |
|
195
|
1 |
|
return false; |
|
196
|
|
|
} |
|
197
|
6 |
|
return true; |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
} |
|
201
|
|
|
|