-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathValidator.php
More file actions
383 lines (324 loc) · 9.95 KB
/
Validator.php
File metadata and controls
383 lines (324 loc) · 9.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
<?php
namespace UniMapper;
use UniMapper\Entity\Reflection\Property\Option\Computed;
/**
* Validates entity
*/
class Validator
{
const FILLED = "isFilled",
URL = "isUrl",
IP = "isIp",
EMAIL = "isEmail";
/** @var array $rules Set of rules and conditions */
protected $rules = [];
/** @var array $errors Failed important rules */
protected $errors = [];
/** @var array $warnings Failed rules with minor severity */
protected $warnings = [];
/** @var \UniMapper\Validator $parent Parent validator */
protected $parent = [];
/** @var Entity\Reflection\Property $property */
protected $property;
/** @var \UniMapper\Entity $entity */
protected $entity;
/** @var string $child Child property name */
protected $child;
public function __construct(Entity $entity, Validator $parent = null)
{
$this->parent = $parent;
$this->entity = $entity;
}
public function getParent()
{
return $this->parent;
}
public function getProperty()
{
return $this->property;
}
public function getEntity()
{
return $this->entity;
}
/**
* Add validation condition
*
* @param mixed $validation Callable or some default validation method name
*
* @return \UniMapper\Validator
*/
public function addCondition($validation)
{
if (!$this->property) {
throw new Exception\InvalidArgumentException(
"Condition can be called only on properties!"
);
}
$condition = new Validator\Condition(
$this->_getValidation($validation),
$this
);
$this->rules[] = $condition;
return $condition->getValidator();
}
public function endCondition()
{
if ($this->parent) {
return $this->parent;
}
return $this;
}
/**
* Add permanent error manually
*
* @param string $message
* @param integer $severity
* @param array $indexes Failed indexes, has effect on collections only
*
* @return \UniMapper\Validator
*/
public function addError($message, $severity = Validator\Rule::ERROR,
array $indexes = []
) {
$rule = new Validator\Rule(
$this->entity,
function () {
return false;
},
$message,
$this->property,
$severity,
$this->child
);
$rule->setChildFailed($indexes);
$this->rules[] = $rule;
return $this;
}
/**
* Set property which is validation configured on
*
* @param string $name Property name in entity
* @param string $child Child property name
*
* @return \UniMapper\Validator
*
* @throws \Exception
*/
public function on($name, $child = null)
{
$reflection = $this->entity->getReflection();
if (!$reflection->hasProperty($name)) {
throw new Exception\InvalidArgumentException(
"Unknown property '" . $name . "'!"
);
}
$this->property = $reflection->getProperty($name);
if ($this->property->hasOption(Computed::KEY)) {
throw new Exception\InvalidArgumentException(
"Validation can not be used on computed property!"
);
}
if ($child
&& (!$this->property->getType() === Entity\Reflection\Property::TYPE_ENTITY
&& !$this->property->getType() === Entity\Reflection\Property::TYPE_COLLECTION)
) {
throw new Exception\InvalidArgumentException(
"Child validation can be used only on entities and collections!"
);
}
$this->child = $child;
return $this->endCondition();
}
/**
* Start to configure validations on entity itself
*
* @return \UniMapper\Validator
*/
public function onEntity()
{
$this->property = null;
$this->child = null;
return $this->endCondition();
}
/**
* Addd validation rule
*
* @param mixed $validation Callable or some default validation method name
* @param string $message
* @param integer $severity
*
* @return \UniMapper\Validator
*/
public function addRule($validation, $message, $severity = Validator\Rule::ERROR)
{
$this->rules[] = new Validator\Rule(
$this->entity,
$this->_getValidation($validation),
$message,
$this->property,
$severity,
$this->child
);
return $this;
}
private function _getValidation($definition)
{
if (is_string($definition) && method_exists(__CLASS__, $definition)) {
return [__CLASS__, $definition];
}
return $definition;
}
/**
* Run all validations
*
* @param integer $failOn Severity level
*
* @return boolean
*/
public function validate($failOn = Validator\Rule::ERROR)
{
$this->warnings = [];
$this->errors = [];
foreach ($this->rules as $rule) {
if ($rule instanceof Validator\Condition) {
// Condition
$condition = $rule;
if ($condition->validate()) {
// Conditions rules
if (!$condition->getValidator()->validate($failOn)) {
$this->errors = array_merge(
$this->errors,
$rule->getValidator()->getErrors()
);
}
$this->warnings = array_merge(
$this->warnings,
$rule->getValidator()->getWarnings()
);
}
} else {
// Rule
if (!$rule->validate()) {
if ($rule->getSeverity() <= $failOn) {
$this->errors[] = $rule;
} else {
$this->warnings[] = $rule;
}
}
}
}
// Run nested validators - every entity may have its own validator
foreach ($this->entity->getData() as $propertyName => $value) {
if ($value instanceof Entity) {
$validator = $value->getValidator();
if (!$validator->validate($failOn)) {
foreach ($validator->getErrors() as $error) {
$rule = clone $error;
$rule->setPath(
array_merge([$propertyName], $rule->getPath())
);
$this->errors[] = $rule;
}
}
foreach ($validator->getWarnings() as $warning) {
$rule = clone $warning;
$rule->setPath(
array_merge([$propertyName], $rule->getPath())
);
$this->warnings[] = $rule;
}
}
}
return count($this->errors) === 0;
}
public function getErrors()
{
return $this->errors;
}
public function getWarnings()
{
return $this->warnings;
}
/**
* Get messages
*
* @param integer $minSeverity
* @param callable $factory
*
* @return array
*/
public function getMessages(
$minSeverity = Validator\Rule::DEBUG,
callable $factory = null
) {
if ($factory === null) {
$factory = function ($text, $severity, $path) {
return new Validator\Message($text, $severity, $path);
};
}
$messages = [];
foreach (array_merge($this->errors, $this->warnings) as $rule) {
if ($rule->getSeverity() <= $minSeverity) {
$failedIndexes = $rule->getFailedChildIndexes();
if ($rule->getProperty() !== null
&& $rule->getProperty()->getType() === Entity\Reflection\Property::TYPE_COLLECTION
&& !empty($failedIndexes)
) {
foreach ($failedIndexes as $index) {
$path = $rule->getPath();
if (count($path) > 1) {
$leaf = array_pop($path);
$path = array_merge($path, [$index, $leaf]);
} else {
$path[] = $index;
}
$messages[] = $factory(
$rule->getMessage(),
$rule->getSeverity(),
$path
);
}
} else {
$messages[] = $factory(
$rule->getMessage(),
$rule->getSeverity(),
$rule->getPath()
);
}
}
}
return $messages;
}
public static function isTraversable($value)
{
return is_array($value) || is_object($value);
}
public static function isUrl($value)
{
return filter_var($value, FILTER_VALIDATE_URL);
}
public static function isFilled($value)
{
if (self::isTraversable($value)) {
return count($value) > 0;
}
return !empty($value);
}
public static function isEmail($value)
{
return filter_var($value, FILTER_VALIDATE_EMAIL);
}
public static function isIp($value)
{
return filter_var($value, FILTER_VALIDATE_IP);
}
public static function isIpv4($value)
{
return filter_var($value, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4);
}
public static function isIpv6($value)
{
return filter_var($value, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6);
}
}