-
Notifications
You must be signed in to change notification settings - Fork 0
/
Validate.php
384 lines (314 loc) · 7.62 KB
/
Validate.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
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
384
<?php
/**
* Validate
*
* Validation class with localisation possibility
*
* @author Dwayne Walsh
* @copyright (c) 2018, Dwayne Walsh
* @link https://github.com/DwayneWalsh
* @since 2019.01.02
*/
class Validate {
protected $numMin = 0,
$numMax = 0,
$lengthMin = 0,
$lengthMax = 0,
$equal = '',
$name = null,
$field = null;
public $patterns = array(
'words' => '[\p{L}\s]+',
'tel' => '[0-9+\s()-]+',
'filename' => '[\p{L}\s0-9-_!%&()=\[\]#@,.;+]+\.[A-Za-z0-9]{2,4}',
'folder' => '[\p{L}\s0-9-_!%&()=\[\]#@,.;+]+',
'address' => '[\p{L}0-9\s.,()°-]+'
);
public $responses = array(
'empty' => "{{field}} must not be empty",
'badFormat' => "{{field}} is invalid",
'length' => "{{field}} must be between {{lenMin}} and {{lenMax}} characters long",
'minMax' => "{{field}} must be between {{numMin}} and {{numMax}}",
'int' => "{{field}} must be an integer",
'float' => "{{field}} must be a float",
'alpha' => "{{field}} must only contain letters (a-z)",
'alphanum' => "{{field}} must only contain letters (a-z) and numbers (0-9)",
'whiteSpace' => "{{field}} cannot contain spaces",
'url' => "{{field}} must be an URL",
'uri' => "{{field}} must be an URI",
'bool' => "{{field}} must be a boolean (true-false)",
'email' => "{{field}} must be a valid email",
'equal' => "{{field2}} must match {{field}}"
);
public function __construct($ruleSet = null)
{
$this->setNewRuleSet($ruleSet);
}
protected $errors = array();
private $parsed = false;
/** Magic methods */
public function __get($var)
{
return $this->{$var};
}
public function __set($name, $var)
{
$this->{$name} = $var;
}
/**
* Set field name for display in messages
*
* @param string $name
* @return this
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Set field name
*
* @param string $name
* @return this
*/
public function setField($name)
{
$this->field = $name;
return $this;
}
/**
* Value of the field
*
* @param mixed $value
* @return this
*/
public function setValue($value)
{
$this->value = $value;
return $this;
}
/**
* Not Empty
*
* @return this
*/
public function notEmpty()
{
if(empty($this->value)) {
$this->addError($this->responses['empty']);
}
return $this;
}
/**
* No white space
*
* @return this
*/
public function noWhiteSpace()
{
if(preg_match('/\s/',$this->value)) {
$this->addError($this->responses['whiteSpace']);
}
return $this;
}
/**
* Equal
* Checks if two values match
*
* @param mixed $value
* @return this
*/
public function equal($name, $value, $customName = null)
{
$this->equal = $customName ? $customName : $name;
if($this->value != $value) {
$this->addError($this->responses['equal']);
}
return $this;
}
/**
* Length
* For string lenth
*
* @param number $min
* @param number $max
* @return this
*/
public function length($min, $max)
{
$this->lengthMin = $min;
$this->lengthMax = $max;
if(strlen($this->value) < $this->lengthMin || strlen($this->value) > $this->lengthMax) {
$this->addError($this->responses['length']);
}
return $this;
}
/**
* Min Max
* For numeric values
*
* @param number $min
* @param number $max
* @return this
*/
public function minMax($min, $max)
{
$this->numMin = $min;
$this->numMax = $max;
if($this->value < $this->numMin || $this->value > $this->numMax) {
$this->addError($this->responses['minMax']);
}
return $this;
}
public function int()
{
if(!filter_var($this->value, FILTER_VALIDATE_INT)) {
$this->addError($this->responses['int']);
}
return $this;
}
public function float()
{
if(!filter_var($this->value, FILTER_VALIDATE_FLOAT)) {
$this->addError($this->responses['float']);
}
return $this;
}
public function alpha()
{
if(!filter_var($this->value, FILTER_VALIDATE_REGEXP, array('options' => array('regexp' => "/^[a-zA-Z]+$/")))) {
$this->addError($this->responses['alpha']);
}
return $this;
}
public function alphanum()
{
if(!filter_var($this->value, FILTER_VALIDATE_REGEXP, array('options' => array('regexp' => "/^[a-zA-Z0-9]+$/")))) {
$this->addError($this->responses['alphanum']);
}
return $this;
}
public function url()
{
if(!filter_var($this->value, FILTER_VALIDATE_REGEXP, array('options' => array('regexp' => "/^[a-zA-Z0-9]+$/")))) {
$this->addError($this->responses['url']);
}
return $this;
}
public function uri()
{
if(!filter_var($this->value, FILTER_VALIDATE_REGEXP, array('options' => array('regexp' => "/^[A-Za-z0-9-\/_]+$/")))) {
$this->addError($this->responses['uri']);
}
return $this;
}
public function bool()
{
if(!filter_var($this->value, FILTER_VALIDATE_BOOLEAN)) {
$this->addError($this->responses['bool']);
}
return $this;
}
public function email()
{
if(!filter_var($this->value, FILTER_VALIDATE_EMAIL)) {
$this->addError($this->responses['email']);
}
return $this;
}
/**
* Checks if array or other pattern
*
* @param string $name
* @return this
*/
private function pattern($name)
{
if($name == 'array') {
if(!is_array($this->value)){
$this->addError($this->responses['array']);
}
} else {
$regex = '/^('.$this->patterns[$name].')$/u';
if($this->value != '' && !preg_match($regex, $this->value)){
$this->addError($this->responses['badFormat']);
}
}
return $this;
}
/**
* Checks value for the specific pattern
*
* @param string $pattern
* @return this
*/
public function customPattern($pattern){
$regex = '/^('.$pattern.')$/u';
if($this->value != '' && !preg_match($regex, $this->value) &&
ini_get("display_errors") != false || ini_get("display_errors") != 0 || ini_get("display_errors") != 'off'
){
throw new \Exception("Custom pattern \"{$pattern}\" is invalid.");
}
return $this;
}
public function addError($error)
{
/** Change ex. password_repeat to Password Repeat */
$name = $this->name;
if(!$name) {
$name = str_replace('_', ' ', $this->field);
$name = str_replace('-', ' ', $name);
$name = ucwords($name);
}
$error = str_replace("{{field}}", $name, $error);
$error = str_replace("{{lenMin}}", $this->lengthMin, $error);
$error = str_replace("{{lenMax}}", $this->lengthMax, $error);
$error = str_replace("{{numMin}}", $this->numMin, $error);
$error = str_replace("{{numMax}}", $this->numMax, $error);
$error = str_replace("{{field2}}", $this->equal, $error);
$this->errors[$this->field][] = $error;
}
public function getErrors($callback = null)
{
if(!$callback) {
return $this->errors;
}
return call_user_func($callback, $this->errors);
}
public function custom($callback)
{
return call_user_func($callback, $this);
}
public function failed()
{
return count($this->errors) != 0 ? true : false;
}
private function setNewRuleSet($ruleSet = null)
{
if($ruleSet) {
if(is_array($ruleSet)) {
$missing_keys = [];
foreach ($this->responses as $key => $value) {
if(!array_key_exists($key, $ruleSet)) {
array_push($missing_keys, $key);
}
}
if(count($missing_keys)) {
throw new \Exception("New rule has missing keys: '".implode("', '", $missing_keys)."'");
}
$this->responses = $ruleSet;
} else {
throw new \Exception("New rule set must be type of array");
}
$this->responses = $ruleSet;
}
}
public function getFirstError()
{
if(count($this->errors)) {
return $this->errors[key($this->errors)][0];
}
return '';
}
}