|
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 InvalidArgumentException; |
|
17
|
|
|
use Maslosoft\Addendum\Interfaces\AnnotatedInterface; |
|
18
|
|
|
use Maslosoft\Mangan\Interfaces\Validators\ValidatorInterface; |
|
19
|
|
|
use Maslosoft\Mangan\Meta\ManganMeta; |
|
20
|
|
|
use Maslosoft\Mangan\Validators\Traits\AllowEmpty; |
|
21
|
|
|
use Maslosoft\Mangan\Validators\Traits\Messages; |
|
22
|
|
|
use Maslosoft\Mangan\Validators\Traits\OnScenario; |
|
23
|
|
|
use Maslosoft\Mangan\Validators\Traits\Safe; |
|
24
|
|
|
use Maslosoft\Mangan\Validators\Traits\SkipOnError; |
|
25
|
|
|
use Maslosoft\Mangan\Validators\Traits\Strict; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* CompareValidator |
|
29
|
|
|
* |
|
30
|
|
|
* @author Piotr Maselkowski <pmaselkowski at gmail.com> |
|
31
|
|
|
*/ |
|
32
|
|
|
class CompareValidator implements ValidatorInterface |
|
33
|
|
|
{ |
|
34
|
|
|
|
|
35
|
|
|
use AllowEmpty, |
|
36
|
|
|
Messages, |
|
37
|
|
|
Strict, |
|
38
|
|
|
OnScenario, |
|
39
|
|
|
Safe, |
|
40
|
|
|
SkipOnError; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @var string the name of the attribute to be compared with |
|
44
|
|
|
*/ |
|
45
|
|
|
public $compareAttribute; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @var string the constant value to be compared with |
|
49
|
|
|
*/ |
|
50
|
|
|
public $compareValue; |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @var string the operator for comparison. Defaults to '='. |
|
54
|
|
|
* The followings are valid operators: |
|
55
|
|
|
* <ul> |
|
56
|
|
|
* <li>'=' or '==': validates to see if the two values are equal. If {@link strict} is true, the comparison |
|
57
|
|
|
* will be done in strict mode (i.e. checking value type as well).</li> |
|
58
|
|
|
* <li>'!=': validates to see if the two values are NOT equal. If {@link strict} is true, the comparison |
|
59
|
|
|
* will be done in strict mode (i.e. checking value type as well).</li> |
|
60
|
|
|
* <li>'>': validates to see if the value being validated is greater than the value being compared with.</li> |
|
61
|
|
|
* <li>'>=': validates to see if the value being validated is greater than or equal to the value being compared with.</li> |
|
62
|
|
|
* <li>'<': validates to see if the value being validated is less than the value being compared with.</li> |
|
63
|
|
|
* <li>'<=': validates to see if the value being validated is less than or equal to the value being compared with.</li> |
|
64
|
|
|
* </ul> |
|
65
|
|
|
*/ |
|
66
|
|
|
public $operator = '='; |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @Label('{attribute} must be repeated exactly') |
|
70
|
|
|
* @var string |
|
71
|
|
|
*/ |
|
72
|
|
|
public $msgRepeat = ''; |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @Label('{attribute} must not be equal to "{compareValue}"') |
|
76
|
|
|
* @var string |
|
77
|
|
|
*/ |
|
78
|
|
|
public $msgEq = ''; |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* @Label('{attribute} must be greater than "{compareValue}"') |
|
82
|
|
|
* @var string |
|
83
|
|
|
*/ |
|
84
|
|
|
public $msgGt = ''; |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* @Label('{attribute} must be greater than or equal to "{compareValue}"') |
|
88
|
|
|
* @var string |
|
89
|
|
|
*/ |
|
90
|
|
|
public $msgGte = ''; |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* @Label('{attribute} must be less than "{compareValue}"') |
|
94
|
|
|
* @var string |
|
95
|
|
|
*/ |
|
96
|
|
|
public $msgLt = ''; |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* @Label('{attribute} must be less than or equal to "{compareValue}"') |
|
100
|
|
|
* @var string |
|
101
|
|
|
*/ |
|
102
|
|
|
public $msgLte = ''; |
|
103
|
|
|
|
|
104
|
14 |
|
public function isValid(AnnotatedInterface $model, $attribute) |
|
105
|
|
|
{ |
|
106
|
14 |
|
$value = $model->$attribute; |
|
107
|
14 |
|
if ($this->allowEmpty && empty($value)) |
|
108
|
|
|
{ |
|
109
|
|
|
return true; |
|
110
|
|
|
} |
|
111
|
14 |
|
$label = ManganMeta::create($model)->field($attribute)->label; |
|
112
|
|
|
|
|
113
|
14 |
|
if ($this->compareValue !== null) |
|
114
|
|
|
{ |
|
115
|
12 |
|
$compareLabel = $compareValue = $this->compareValue; |
|
116
|
|
|
} |
|
117
|
|
|
else |
|
118
|
|
|
{ |
|
119
|
2 |
|
$compareAttribute = $this->compareAttribute === null ? $attribute . '_repeat' : $this->compareAttribute; |
|
120
|
2 |
|
$compareValue = $model->$compareAttribute; |
|
121
|
2 |
|
$compareLabel = ManganMeta::create($model)->field($compareAttribute)->label; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
14 |
|
switch ($this->operator) |
|
125
|
|
|
{ |
|
126
|
14 |
|
case '=': |
|
127
|
10 |
|
case '==': |
|
128
|
4 |
|
if (($this->strict && $value !== $compareValue) || (!$this->strict && $value != $compareValue)) |
|
129
|
|
|
{ |
|
130
|
2 |
|
$this->addError('msgRepeat', ['{attribute}' => $compareLabel]); |
|
131
|
2 |
|
return false; |
|
132
|
|
|
} |
|
133
|
2 |
|
break; |
|
134
|
10 |
|
case '!=': |
|
135
|
2 |
|
if (($this->strict && $value === $compareValue) || (!$this->strict && $value == $compareValue)) |
|
136
|
|
|
{ |
|
137
|
1 |
|
$this->addError('msgEq', ['{attribute}' => $label, '{compareValue}' => $compareValue]); |
|
138
|
1 |
|
return false; |
|
139
|
|
|
} |
|
140
|
1 |
|
break; |
|
141
|
8 |
|
case '>': |
|
142
|
2 |
|
if ($value <= $compareValue) |
|
143
|
|
|
{ |
|
144
|
1 |
|
$this->addError('msgGt', ['{attribute}' => $label, '{compareValue}' => $compareValue]); |
|
145
|
1 |
|
return false; |
|
146
|
|
|
} |
|
147
|
1 |
|
break; |
|
148
|
6 |
|
case '>=': |
|
149
|
2 |
|
if ($value < $compareValue) |
|
150
|
|
|
{ |
|
151
|
1 |
|
$this->addError('msgGte', ['{attribute}' => $label, '{compareValue}' => $compareValue]); |
|
152
|
1 |
|
return false; |
|
153
|
|
|
} |
|
154
|
1 |
|
break; |
|
155
|
4 |
|
case '<': |
|
156
|
2 |
|
if ($value >= $compareValue) |
|
157
|
|
|
{ |
|
158
|
1 |
|
$this->addError('msgLt', ['{attribute}' => $label, '{compareValue}' => $compareValue]); |
|
159
|
1 |
|
return false; |
|
160
|
|
|
} |
|
161
|
1 |
|
break; |
|
162
|
2 |
|
case '<=': |
|
163
|
2 |
|
if ($value > $compareValue) |
|
164
|
|
|
{ |
|
165
|
1 |
|
$this->addError('msgLte', ['{attribute}' => $label, '{compareValue}' => $compareValue]); |
|
166
|
1 |
|
return false; |
|
167
|
|
|
} |
|
168
|
1 |
|
break; |
|
169
|
|
|
default: |
|
170
|
|
|
$msg = sprintf('Invalid operator `%s` on attribute `%s` of model `%s`', $this->operator, $attribute, get_class($model)); |
|
171
|
|
|
throw new InvalidArgumentException($msg); |
|
172
|
|
|
} |
|
173
|
7 |
|
return true; |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
} |
|
177
|
|
|
|