1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Validator; |
6
|
|
|
|
7
|
|
|
class RuleParser |
8
|
|
|
{ |
9
|
|
|
const RULES = [ |
10
|
|
|
'singleParam' => [ |
11
|
|
|
'alnum', // (mixed $value); |
12
|
|
|
'base64', // (string $value); |
13
|
|
|
'boolean', // (mixed $value); |
14
|
|
|
'classExists', // (mixed $value); |
15
|
|
|
'defined', // (mixed $constant); |
16
|
|
|
'digit', // (mixed $value); |
17
|
|
|
'directory', // (string $value); |
18
|
|
|
'e164', // (string $value); |
19
|
|
|
'email', // (mixed $value); |
20
|
|
|
'extensionLoaded', // (mixed $value); |
21
|
|
|
'false', // (mixed $value); |
22
|
|
|
'file', // (string $value); |
23
|
|
|
'float', // (mixed $value); |
24
|
|
|
'integer', // (mixed $value); |
25
|
|
|
'integerish', // (mixed $value); |
26
|
|
|
'interfaceExists', // (mixed $value); |
27
|
|
|
'isArray', // (mixed $value); |
28
|
|
|
'isArrayAccessible', // (mixed $value); |
29
|
|
|
'isCallable', // (mixed $value); |
30
|
|
|
'isJsonString', // (mixed $value); |
31
|
|
|
'isObject', // (mixed $value); |
32
|
|
|
'isResource', // (mixed $value); |
33
|
|
|
'isTraversable', // (mixed $value); |
34
|
|
|
'noContent', // (mixed $value); |
35
|
|
|
'notBlank', // (mixed $value); |
36
|
|
|
'notEmpty', // (mixed $value); |
37
|
|
|
'notNull', // (mixed $value); |
38
|
|
|
'null', // (mixed $value); |
39
|
|
|
'numeric', // (mixed $value); |
40
|
|
|
'objectOrClass', // (mixed $value); |
41
|
|
|
'readable', // (string $value); |
42
|
|
|
'scalar', // (mixed $value); |
43
|
|
|
'string', // (mixed $value); |
44
|
|
|
'true', // (mixed $value); |
45
|
|
|
'url', // (mixed $value); |
46
|
|
|
'uuid', // (string $value); |
47
|
|
|
'writeable', // (string $value); |
48
|
|
|
'choice', // (mixed $value, array $choices); |
49
|
|
|
'choicesNotEmpty', // (array $values, array $choices); |
50
|
|
|
'contains', // (mixed $string, string $needle); |
51
|
|
|
'count', // (array|\Countable $countable, int $count); |
52
|
|
|
'date', // (string $value, string $format); |
53
|
|
|
'endsWith', // (mixed $string, string $needle); |
54
|
|
|
'eq', // (mixed $value, mixed $value2); |
55
|
|
|
'greaterOrEqualThan', // (mixed $value, mixed $limit); |
56
|
|
|
'greaterThan', // (mixed $value, mixed $limit); |
57
|
|
|
'implementsInterface', // (mixed $class, string $interfaceName); |
58
|
|
|
'inArray', // (mixed $value, array $choices); |
59
|
|
|
'ip', // (string $value, int $flag = null); |
60
|
|
|
'ipv4', // (string $value, int $flag = null); |
61
|
|
|
'ipv6', // (string $value, int $flag = null); |
62
|
|
|
'isInstanceOf', // (mixed $value, string $className); |
63
|
|
|
'keyExists', // (mixed $value, string|int $key); |
64
|
|
|
'keyIsset', // (mixed $value, string|int $key); |
65
|
|
|
'keyNotExists', // (mixed $value, string|int $key); |
66
|
|
|
'length', // (mixed $value, int $length); |
67
|
|
|
'lessOrEqualThan', // (mixed $value, mixed $limit); |
68
|
|
|
'lessThan', // (mixed $value, mixed $limit); |
69
|
|
|
'max', // (mixed $value, mixed $maxValue); |
70
|
|
|
'maxLength', // (mixed $value, int $maxLength); |
71
|
|
|
'methodExists', // (string $value, mixed $object); |
72
|
|
|
'min', // (mixed $value, mixed $minValue); |
73
|
|
|
'minLength', // (mixed $value, int $minLength); |
74
|
|
|
'notEmptyKey', // (mixed $value, string|int $key); |
75
|
|
|
'notEq', // (mixed $value1, mixed $value2); |
76
|
|
|
'notInArray', // (mixed $value, array $choices); |
77
|
|
|
'notIsInstanceOf', // (mixed $value, string $className); |
78
|
|
|
'notSame', // (mixed $value1, mixed $value2); |
79
|
|
|
'phpVersion', // (string $operator, mixed $version); |
80
|
|
|
'propertiesExist', // (mixed $value, array $properties); |
81
|
|
|
'propertyExists', // (mixed $value, string $property); |
82
|
|
|
'regex', // (mixed $value, string $pattern); |
83
|
|
|
'same', // (mixed $value, mixed $value2); |
84
|
|
|
'satisfy', // (mixed $value, callable $callback); |
85
|
|
|
'startsWith', // (mixed $string, string $needle); |
86
|
|
|
'subclassOf', // (mixed $value, string $className); |
87
|
|
|
], |
88
|
|
|
'multipleParams' => [ |
89
|
|
|
'between', // (mixed $value, mixed $lowerLimit, mixed $upperLimit); |
90
|
|
|
'betweenExclusive', // (mixed $value, mixed $lowerLimit, mixed $upperLimit); |
91
|
|
|
'betweenLength', // (mixed $value, int $minLength, int $maxLength); |
92
|
|
|
'extensionVersion', // (string $extension, string $operator, mixed $version); |
93
|
|
|
'range', // (mixed $value, mixed $minValue, mixed $maxValue); |
94
|
|
|
'version', // (string $version1, string $operator, string $version2); |
95
|
|
|
] |
96
|
|
|
]; |
97
|
|
|
|
98
|
5 |
|
public static function getRule(string $rule, ?string $plainConstrains) |
99
|
|
|
{ |
100
|
5 |
|
$method = key(array_filter( |
101
|
5 |
|
self::RULES, |
102
|
|
|
function (array $availableRules) use ($rule): bool { |
103
|
5 |
|
return in_array($rule, $availableRules); |
104
|
5 |
|
} |
105
|
|
|
)); |
106
|
|
|
|
107
|
5 |
|
return call_user_func_array([self::class, $method], [$plainConstrains]); |
108
|
|
|
} |
109
|
|
|
|
110
|
5 |
|
private static function singleParam(?string $validation) |
111
|
|
|
{ |
112
|
5 |
|
if (preg_match_all("@\((.+)\)@", $validation, $matches)) { |
113
|
2 |
|
$validation = explode(',', $matches[1][0]); |
114
|
|
|
} |
115
|
|
|
|
116
|
5 |
|
return $validation; |
117
|
|
|
} |
118
|
|
|
|
119
|
1 |
|
private static function multipleParams(?string $validation): array |
120
|
|
|
{ |
121
|
|
|
return array_map(function ($rule) { |
122
|
1 |
|
return call_user_func_array([self::class, 'singleParam'], [$rule]); |
123
|
1 |
|
}, explode(',', $validation)); |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|