|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Check |
|
5
|
|
|
* |
|
6
|
|
|
* Validate a data map against defined methods. |
|
7
|
|
|
* |
|
8
|
|
|
* @package core |
|
9
|
|
|
* @author [email protected] |
|
10
|
|
|
* @copyright Caffeina srl - 2015 - http://caffeina.it |
|
11
|
|
|
*/ |
|
12
|
|
|
|
|
13
|
|
|
class Check { |
|
14
|
|
|
use Module; |
|
15
|
|
|
|
|
16
|
|
|
protected static $methods = [], |
|
17
|
|
|
$errors = []; |
|
18
|
|
|
public static $data = []; |
|
19
|
|
|
|
|
20
|
|
|
public static function valid($rules, $data){ |
|
21
|
|
|
static::$errors = []; |
|
22
|
|
|
Event::triggerOnce('core.check.init'); |
|
23
|
|
|
self::$data = ($data = (array)$data); |
|
24
|
|
|
|
|
25
|
|
|
foreach ((array)$rules as $field_name => $rule) { |
|
26
|
|
|
|
|
27
|
|
|
$current = isset($data[$field_name]) ? $data[$field_name] : null; |
|
28
|
|
|
|
|
29
|
|
|
if (is_callable($rule)){ |
|
30
|
|
|
static::$errors[$field_name] = call_user_func($rule,$current); |
|
31
|
|
|
continue; |
|
32
|
|
|
} elseif (is_string($rule)) { |
|
33
|
|
|
$current_rules = array_flip(preg_split('/\s*\|\s*/', $rule)); |
|
34
|
|
|
} else { |
|
35
|
|
|
$current_rules = (array)$rule; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
static::$errors[$field_name] = true; |
|
39
|
|
|
|
|
40
|
|
|
foreach($current_rules as $method => $message) { |
|
41
|
|
|
|
|
42
|
|
|
$meth_name = strtok($method, ':'); |
|
43
|
|
|
$opts = strtok(':') ?: ''; |
|
44
|
|
|
$opts = $opts ? json_decode("[$opts]") : []; |
|
45
|
|
|
$meth_opts = $opts ? array_merge([$current], $opts) : [$current]; |
|
46
|
|
|
|
|
47
|
|
|
if ( static::$errors[$field_name] !== true ) continue 2; |
|
48
|
|
|
|
|
49
|
|
|
if (empty(static::$methods[$meth_name])) { |
|
50
|
|
|
static::$errors[$field_name] = true; |
|
51
|
|
|
} else { |
|
52
|
|
|
if (call_user_func_array(static::$methods[$meth_name]->validate,$meth_opts)){ |
|
53
|
|
|
static::$errors[$field_name] = true; |
|
54
|
|
|
} else { |
|
55
|
|
|
$arg = []; |
|
56
|
|
|
foreach ($meth_opts as $key => $value) { |
|
57
|
|
|
$arg["arg_$key"] = $value; |
|
58
|
|
|
} |
|
59
|
|
|
static::$errors[$field_name] = Text::render(static::$methods[$meth_name]->message,$arg); |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
self::$data = []; |
|
66
|
|
|
|
|
67
|
|
|
// Clean non-errors |
|
68
|
|
|
static::$errors = array_filter(static::$errors,function($v){ |
|
69
|
|
|
return $v !== true; |
|
70
|
|
|
}); |
|
71
|
|
|
|
|
72
|
|
|
return empty(static::$errors); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
public static function method($name, $definition = null){ |
|
76
|
|
|
if (is_array($name)) { |
|
77
|
|
|
foreach ($name as $method_name => $method_definition){ |
|
78
|
|
|
if (is_callable($method_definition)) $method_definition = ['validate' => $method_definition]; |
|
79
|
|
|
if (empty($method_definition['validate']) || !is_callable($method_definition['validate'])) continue; |
|
80
|
|
|
$method_definition['key'] = "core.check.error.$method_name"; |
|
81
|
|
|
$method_definition['message'] = Filter::with($method_definition['key'],@$method_definition['message']?:'Field not valid.'); |
|
82
|
|
|
static::$methods[$method_name] = (object)$method_definition; |
|
83
|
|
|
} |
|
84
|
|
|
; |
|
85
|
|
|
} else { |
|
86
|
|
|
if (is_callable($definition)) $definition = ['validate' => $definition]; |
|
87
|
|
|
if (empty($definition['validate']) || !is_callable($definition['validate'])) return; |
|
88
|
|
|
$methods['key'] = "core.check.error.$name"; |
|
|
|
|
|
|
89
|
|
|
$methods['message'] = Filter::with($methods['key'],@$methods['message']?:'Field not valid.'); |
|
90
|
|
|
static::$methods[$name] = (object)$definition; |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
public static function errors() { |
|
95
|
|
|
return static::$errors; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
Event::on('core.check.init',function(){ |
|
101
|
|
|
|
|
102
|
|
|
Check::method([ |
|
103
|
|
|
|
|
104
|
|
|
'required' => [ |
|
105
|
|
|
'validate' => function($value) { |
|
106
|
|
|
return (is_numeric($value) && $value==0) || empty($value)?false:true; |
|
107
|
|
|
}, |
|
108
|
|
|
'message' => "This value({{arg_0}}) cannot be empty.", |
|
109
|
|
|
], |
|
110
|
|
|
|
|
111
|
|
|
'alphanumeric' => [ |
|
112
|
|
|
'validate' => function($value) { |
|
113
|
|
|
return preg_match('/^[0-9a-zA-Z]+$/',$value) ? true : false; |
|
114
|
|
|
}, |
|
115
|
|
|
'message' => "Value must be alphanumeric.", |
|
116
|
|
|
], |
|
117
|
|
|
|
|
118
|
|
|
'numeric' => [ |
|
119
|
|
|
'validate' => function($value) { |
|
120
|
|
|
return preg_match('/^\d+$/',$value) ? true : false; |
|
121
|
|
|
}, |
|
122
|
|
|
'message' => "Value must be numeric.", |
|
123
|
|
|
], |
|
124
|
|
|
|
|
125
|
|
|
'email' => [ |
|
126
|
|
|
'validate' => function($value) { |
|
127
|
|
|
return filter_var($value, FILTER_VALIDATE_EMAIL) ? true : false; |
|
128
|
|
|
}, |
|
129
|
|
|
'message' => "This is not a valid email.", |
|
130
|
|
|
], |
|
131
|
|
|
|
|
132
|
|
|
'url' => [ |
|
133
|
|
|
'validate' => function($value) { |
|
134
|
|
|
return filter_var($value, FILTER_VALIDATE_URL) ? true : false; |
|
135
|
|
|
}, |
|
136
|
|
|
'message' => "This is not a valid URL.", |
|
137
|
|
|
], |
|
138
|
|
|
|
|
139
|
|
|
'max' => [ |
|
140
|
|
|
'validate' => function($value,$max) { |
|
141
|
|
|
return $value<=$max ? true : false; |
|
142
|
|
|
}, |
|
143
|
|
|
'message' => "Value must be less than {{arg_1}}.", |
|
144
|
|
|
], |
|
145
|
|
|
|
|
146
|
|
|
'min' => [ |
|
147
|
|
|
'validate' => function($value,$min) { |
|
148
|
|
|
return $value>=$min ? true : false; |
|
149
|
|
|
}, |
|
150
|
|
|
'message' => "Value must be greater than {{arg_1}}.", |
|
151
|
|
|
], |
|
152
|
|
|
|
|
153
|
|
|
'range' => [ |
|
154
|
|
|
'validate' => function($value,$min,$max) { |
|
155
|
|
|
return (($value>=$min)&&($value<=$max)) ? true : false; |
|
156
|
|
|
}, |
|
157
|
|
|
'message' => "This value must be in [{{arg_1}},{{arg_2}}] range.", |
|
158
|
|
|
], |
|
159
|
|
|
|
|
160
|
|
|
'words' => [ |
|
161
|
|
|
'validate' => function($value,$max) { |
|
162
|
|
|
return str_word_count($value)<=$max ? true : false; |
|
163
|
|
|
}, |
|
164
|
|
|
'message' => "Too many words, max count is {{arg_1}}.", |
|
165
|
|
|
], |
|
166
|
|
|
|
|
167
|
|
|
'length' => [ |
|
168
|
|
|
'validate' => function($value,$max) { |
|
169
|
|
|
return strlen($value)<=$max ? true : false; |
|
170
|
|
|
}, |
|
171
|
|
|
'message' => "Too many characters, max count is {{arg_1}}.", |
|
172
|
|
|
], |
|
173
|
|
|
|
|
174
|
|
|
'true' => [ |
|
175
|
|
|
'validate' => function($value) { |
|
176
|
|
|
return !$value ? false : true; |
|
177
|
|
|
}, |
|
178
|
|
|
'message' => "This value must be true.", |
|
179
|
|
|
], |
|
180
|
|
|
|
|
181
|
|
|
'false' => [ |
|
182
|
|
|
'validate' => function($value) { |
|
183
|
|
|
return !$value ?: false; |
|
184
|
|
|
}, |
|
185
|
|
|
'message' => "This value must be false.", |
|
186
|
|
|
], |
|
187
|
|
|
|
|
188
|
|
|
'same_as' => [ |
|
189
|
|
|
'validate' => function($value,$fieldname) { |
|
190
|
|
|
$x = isset(Check::$data[$fieldname]) ? Check::$data[$fieldname] : ''; |
|
191
|
|
|
return ($value==$x) ? true : false; |
|
192
|
|
|
}, |
|
193
|
|
|
'message' => "Field must be equal to {{arg_1}}.", |
|
194
|
|
|
], |
|
195
|
|
|
|
|
196
|
|
|
]); |
|
197
|
|
|
|
|
198
|
|
|
}); |
|
199
|
|
|
|
|
200
|
|
|
|
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArrayis initialized the first time when the foreach loop is entered. You can also see that the value of thebarkey is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.