|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
/** |
|
4
|
|
|
* Caridea |
|
5
|
|
|
* |
|
6
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); you may not |
|
7
|
|
|
* use this file except in compliance with the License. You may obtain a copy of |
|
8
|
|
|
* the License at |
|
9
|
|
|
* |
|
10
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
|
11
|
|
|
* |
|
12
|
|
|
* Unless required by applicable law or agreed to in writing, software |
|
13
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
|
14
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
|
15
|
|
|
* License for the specific language governing permissions and limitations under |
|
16
|
|
|
* the License. |
|
17
|
|
|
* |
|
18
|
|
|
* @copyright 2015-2016 LibreWorks contributors |
|
19
|
|
|
* @license http://opensource.org/licenses/Apache-2.0 Apache 2.0 License |
|
20
|
|
|
*/ |
|
21
|
|
|
namespace Caridea\Validate\Rule; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Compares scalar values to some operand. |
|
25
|
|
|
* |
|
26
|
|
|
* @copyright 2015-2016 LibreWorks contributors |
|
27
|
|
|
* @license http://opensource.org/licenses/Apache-2.0 Apache 2.0 License |
|
28
|
|
|
*/ |
|
29
|
|
|
class Compare implements \Caridea\Validate\Rule |
|
30
|
|
|
{ |
|
31
|
|
|
/** |
|
32
|
|
|
* @var string The operator type |
|
33
|
|
|
*/ |
|
34
|
|
|
private $operator; |
|
35
|
|
|
/** |
|
36
|
|
|
* @var mixed The comparison value |
|
37
|
|
|
*/ |
|
38
|
|
|
private $operand; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Creates a new CompareRule. |
|
42
|
|
|
* |
|
43
|
|
|
* @param string $operator The operator type |
|
44
|
|
|
* @param mixed $operand Optional comparison value |
|
45
|
|
|
*/ |
|
46
|
9 |
|
protected function __construct(string $operator, $operand = null) |
|
47
|
|
|
{ |
|
48
|
9 |
|
$this->operator = $operator; |
|
49
|
9 |
|
$this->operand = $operand; |
|
50
|
9 |
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Validates the provided value. |
|
54
|
|
|
* |
|
55
|
|
|
* @param mixed $value A value to validate against the rule |
|
56
|
|
|
* @param array|object $data The dataset which contains this field |
|
57
|
|
|
* @return array An array of error codes or null if validation succeeded |
|
58
|
|
|
*/ |
|
59
|
9 |
|
public function apply($value, $data = []) |
|
60
|
|
|
{ |
|
61
|
9 |
|
if ("eqf" === $this->operator) { |
|
62
|
1 |
|
return $value === $this->access($data, $this->operand) ? |
|
63
|
1 |
|
null : ['FIELDS_NOT_EQUAL']; |
|
64
|
|
|
} |
|
65
|
8 |
|
if (!is_scalar($value)) { |
|
66
|
8 |
|
return ['FORMAT_ERROR']; |
|
67
|
|
|
} |
|
68
|
8 |
|
switch ($this->operator) { |
|
69
|
8 |
|
case "in": |
|
70
|
1 |
|
return in_array($value, $this->operand, true) ? null : ['NOT_ALLOWED_VALUE']; |
|
71
|
7 |
|
case "lt": |
|
72
|
1 |
|
return $value > $this->operand ? ['TOO_HIGH'] : null; |
|
73
|
6 |
|
case "gt": |
|
74
|
1 |
|
return $value < $this->operand ? ['TOO_LOW'] : null; |
|
75
|
5 |
|
case "bt": |
|
76
|
1 |
|
if ($value > $this->operand[1]) { |
|
77
|
1 |
|
return ['TOO_HIGH']; |
|
78
|
1 |
|
} elseif ($value < $this->operand[0]) { |
|
79
|
1 |
|
return ['TOO_LOW']; |
|
80
|
|
|
} |
|
81
|
1 |
|
return null; |
|
82
|
4 |
|
case "int": |
|
83
|
1 |
|
return is_int($value) || ctype_digit(ltrim((string)$value, '-+')) ? |
|
84
|
1 |
|
null : ['NOT_INTEGER']; |
|
85
|
3 |
|
case "+int": |
|
86
|
1 |
|
return (is_int($value) || ctype_digit(ltrim((string)$value, '-+'))) && |
|
87
|
1 |
|
((int) $value) > 0 ? null : ['NOT_POSITIVE_INTEGER']; |
|
88
|
2 |
|
case "float": |
|
89
|
1 |
|
return is_float($value) || ($value === (string)(float)$value) ? |
|
90
|
1 |
|
null : ['NOT_DECIMAL']; |
|
91
|
1 |
|
case "+float": |
|
92
|
1 |
|
if (is_float($value)) { |
|
93
|
1 |
|
return $value <= 0 ? ['NOT_POSITIVE_DECIMAL'] : null; |
|
94
|
1 |
|
} elseif ($value === (string)(float)$value) { |
|
95
|
1 |
|
return ((float) $value) <= 0 ? ['NOT_POSITIVE_DECIMAL'] : null; |
|
96
|
|
|
} |
|
97
|
1 |
|
return ['NOT_POSITIVE_DECIMAL']; |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Gets a field from the values. |
|
103
|
|
|
* |
|
104
|
|
|
* This can be overridden to access by other means (e.g. object properties, |
|
105
|
|
|
* getter methods). |
|
106
|
|
|
* |
|
107
|
|
|
* @param mixed $values The values |
|
108
|
|
|
* @param string $field The field to access |
|
109
|
|
|
* @return mixed The accessed value |
|
110
|
|
|
*/ |
|
111
|
|
|
protected function access($values, string $field) |
|
112
|
|
|
{ |
|
113
|
|
|
return isset($values[$field]) ? $values[$field] : null; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* Gets a rule that matches a value against a list of accepted values. |
|
118
|
|
|
* |
|
119
|
|
|
* @param array $values The accepted values |
|
120
|
|
|
* @return \Caridea\Validate\Rule\Compare the created rule |
|
121
|
|
|
*/ |
|
122
|
1 |
|
public static function oneOf(array $values): Compare |
|
123
|
|
|
{ |
|
124
|
1 |
|
return new Compare('in', $values); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* Gets a rule that requires numbers to be no greater than a limit. |
|
129
|
|
|
* |
|
130
|
|
|
* @param int|float $value The maximum value |
|
131
|
|
|
* @return \Caridea\Validate\Rule\Compare the created rule |
|
132
|
|
|
*/ |
|
133
|
1 |
|
public static function max($value): Compare |
|
134
|
|
|
{ |
|
135
|
1 |
|
return new Compare('lt', $value); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* Gets a rule that requires numbers to be no less than a limit. |
|
140
|
|
|
* |
|
141
|
|
|
* @param int|float $value The minimum value |
|
142
|
|
|
* @return \Caridea\Validate\Rule\Compare the created rule |
|
143
|
|
|
*/ |
|
144
|
1 |
|
public static function min($value): Compare |
|
145
|
|
|
{ |
|
146
|
1 |
|
return new Compare('gt', $value); |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* Gets a rule that requires numbers to be in a given range. |
|
151
|
|
|
* |
|
152
|
|
|
* @param int|float $min The minimum value, inclusive |
|
153
|
|
|
* @param int|float $max The maximum value, inclusive |
|
154
|
|
|
* @return \Caridea\Validate\Rule\Compare the created rule |
|
155
|
|
|
*/ |
|
156
|
1 |
|
public static function between($min, $max): Compare |
|
157
|
|
|
{ |
|
158
|
1 |
|
$value = $min > $max ? [$max, $min] : [$min, $max]; |
|
159
|
1 |
|
return new Compare('bt', $value); |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
/** |
|
163
|
|
|
* Gets a rule that matches integers and strings with integer values. |
|
164
|
|
|
* |
|
165
|
|
|
* @return \Caridea\Validate\Rule\Compare the created rule |
|
166
|
|
|
*/ |
|
167
|
1 |
|
public static function integer(): Compare |
|
168
|
|
|
{ |
|
169
|
1 |
|
return new Compare('int'); |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
/** |
|
173
|
|
|
* Gets a rule that matches positive integers |
|
174
|
|
|
* |
|
175
|
|
|
* @return \Caridea\Validate\Rule\Compare the created rule |
|
176
|
|
|
*/ |
|
177
|
1 |
|
public static function positiveInteger(): Compare |
|
178
|
|
|
{ |
|
179
|
1 |
|
return new Compare('+int'); |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
/** |
|
183
|
|
|
* Gets a rule that matches floats and strings with float values. |
|
184
|
|
|
* |
|
185
|
|
|
* @return \Caridea\Validate\Rule\Compare the created rule |
|
186
|
|
|
*/ |
|
187
|
1 |
|
public static function decimal(): Compare |
|
188
|
|
|
{ |
|
189
|
1 |
|
return new Compare('float'); |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
/** |
|
193
|
|
|
* Gets a rule that matches positive floats |
|
194
|
|
|
* |
|
195
|
|
|
* @return \Caridea\Validate\Rule\Compare the created rule |
|
196
|
|
|
*/ |
|
197
|
1 |
|
public static function positiveDecimal(): Compare |
|
198
|
|
|
{ |
|
199
|
1 |
|
return new Compare('+float'); |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
/** |
|
203
|
|
|
* Gets a rule that compares two fields for equality |
|
204
|
|
|
* |
|
205
|
|
|
* @param string $field The other field whose value will be compared |
|
206
|
|
|
* @return \Caridea\Validate\Rule\Compare the created rule |
|
207
|
|
|
*/ |
|
208
|
|
|
public static function equalToField(string $field): Compare |
|
209
|
|
|
{ |
|
210
|
|
|
return new Compare('eqf', $field); |
|
211
|
|
|
} |
|
212
|
|
|
} |
|
213
|
|
|
|