-
Notifications
You must be signed in to change notification settings - Fork 0
/
Rules.php
242 lines (218 loc) · 5.64 KB
/
Rules.php
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
<?php
namespace Mascame\Katina;
/**
* This class is completely based on https://github.com/ptrofimov/matchmaker (Petr Trofimov)
*
* Class Rules
* @package Mascame\Katina
*/
class Rules
{
protected static $rules = [];
/**
* @return array
*/
public static function getRules()
{
return self::$rules;
}
public static function has($key) {
return isset(self::$rules[$key]);
}
public static function get($key)
{
if (is_string($key) && !trim($key)) $key = 'any';
if (! self::has($key)) {
throw new \InvalidArgumentException("Matcher $key not found");
}
return self::$rules[$key];
}
/**
* @param array $rules
*/
public static function setRules($rules)
{
self::$rules = array_merge(self::$rules, $rules);
}
}
// Closures are not allowed to be declared directly in class
Rules::setRules([
'empty' => 'empty',
'nonempty' =>
function ($value) {
return !empty($value);
},
'required' =>
function ($value) {
return !empty($value);
},
'in' =>
function ($value) {
return in_array($value, array_slice(func_get_args(), 1));
},
'mixed' =>
function () {
return true;
},
'any' =>
function () {
return true;
},
/*
* Types
*/
'array' => 'is_array',
'bool' => 'is_bool',
'boolean' => 'is_bool',
'callable' => 'is_callable',
'double' => 'is_double',
'float' => 'is_float',
'int' => 'is_int',
'integer' => 'is_integer',
'long' => 'is_long',
'numeric' => 'is_numeric',
'number' => 'is_numeric',
'object' => 'is_object',
'real' => 'is_real',
'resource' => 'is_resource',
'scalar' => 'is_scalar',
'string' => 'is_string',
/*
* Numbers
*/
'gt' =>
function ($value, $n) {
return $value > $n;
},
'gte' =>
function ($value, $n) {
return $value >= $n;
},
'lt' =>
function ($value, $n) {
return $value < $n;
},
'lte' =>
function ($value, $n) {
return $value <= $n;
},
'negative' =>
function ($value) {
return $value < 0;
},
'positive' =>
function ($value) {
return $value > 0;
},
'between' =>
function ($value, $a, $b) {
return $value >= $a && $value <= $b;
},
/*
* Strings
*/
'alnum' => 'ctype_alnum',
'alpha' => 'ctype_alpha',
'cntrl' => 'ctype_cntrl',
'digit' => 'ctype_digit',
'graph' => 'ctype_graph',
'lower' => 'ctype_lower',
'print' => 'ctype_print',
'punct' => 'ctype_punct',
'space' => 'ctype_space',
'upper' => 'ctype_upper',
'xdigit' => 'ctype_xdigit',
'regexp' =>
function ($value, $regexp) {
return preg_match($regexp, $value);
},
'email' =>
function ($value) {
return filter_var($value, FILTER_VALIDATE_EMAIL);
},
'url' =>
function ($value) {
return filter_var($value, FILTER_VALIDATE_URL);
},
'ip' =>
function ($value) {
return filter_var($value, FILTER_VALIDATE_IP);
},
'length' =>
function ($value, $length) {
return mb_strlen($value, 'utf-8') == $length;
},
'min' =>
function ($value, $min) {
return mb_strlen($value, 'utf-8') >= $min;
},
'max' =>
function ($value, $max) {
return mb_strlen($value, 'utf-8') <= $max;
},
'contains' =>
function ($value, $needle) {
return strpos($value, $needle) !== false;
},
'starts' =>
function ($value, $string) {
return mb_substr($value, 0, mb_strlen($string, 'utf-8'), 'utf-8') == $string;
},
'ends' =>
function ($value, $string) {
return mb_substr($value, -mb_strlen($string, 'utf-8'), 'utf-8') == $string;
},
'json' =>
function ($value) {
return @json_decode($value) !== null;
},
'date' =>
function ($value) {
return strtotime($value) !== false;
},
'imdb' =>
function ($value) {
preg_match("/tt([0-9]+){5,9}/", $value, $matches);
return (sizeof($matches) > 0);
},
/*
* Arrays
*/
'count' =>
function ($value, $count) {
return is_array($value) && count($value) == $count;
},
'keys' =>
function ($value) {
if (!is_array($value)) {
return false;
}
foreach (array_slice(func_get_args(), 1) as $key) {
if (!array_key_exists($key, $value)) {
return false;
}
}
return true;
},
/*
* Objects
*/
'instance' =>
function ($value, $class) {
return is_object($value) && $value instanceof $class;
},
'property' =>
function ($value, $property, $expected) {
return
is_object($value)
&& (property_exists($value, $property) || property_exists($value, '__get'))
&& $value->$property == $expected;
},
'method' =>
function ($value, $method, $expected) {
return
is_object($value)
&& (method_exists($value, $method) || method_exists($value, '__call'))
&& $value->$method() == $expected;
},
]);