1
|
|
|
<?php |
2
|
|
|
namespace PhpBoot\Validator; |
3
|
|
|
use PhpBoot\Annotation\EntityContainerBuilder; |
4
|
|
|
use PhpBoot\Utils\TypeHint; |
5
|
|
|
|
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Validator |
9
|
|
|
* |
10
|
|
|
* ** usage: ** |
11
|
|
|
* $v = new Validator(); |
12
|
|
|
* $v->rule('required|integer|in:1,2,3', 'fieldName'); |
13
|
|
|
* |
14
|
|
|
* ** rules: ** |
15
|
|
|
* |
16
|
|
|
* required - Required field |
17
|
|
|
* equals - Field must match another field (email/password confirmation) |
18
|
|
|
* different - Field must be different than another field |
19
|
|
|
* accepted - Checkbox or Radio must be accepted (yes, on, 1, true) |
20
|
|
|
* numeric - Must be numeric |
21
|
|
|
* integer - Must be integer number |
22
|
|
|
* boolean - Must be boolean |
23
|
|
|
* array - Must be array |
24
|
|
|
* length - String must be certain length |
25
|
|
|
* lengthBetween - String must be between given lengths |
26
|
|
|
* lengthMin - String must be greater than given length |
27
|
|
|
* lengthMax - String must be less than given length |
28
|
|
|
* min - Minimum |
29
|
|
|
* max - Maximum |
30
|
|
|
* in - Performs in_array check on given array values |
31
|
|
|
* notIn - Negation of in rule (not in array of values) |
32
|
|
|
* ip - Valid IP address |
33
|
|
|
* email - Valid email address |
34
|
|
|
* url - Valid URL |
35
|
|
|
* urlActive - Valid URL with active DNS record |
36
|
|
|
* alpha - Alphabetic characters only |
37
|
|
|
* alphaNum - Alphabetic and numeric characters only |
38
|
|
|
* slug - URL slug characters (a-z, 0-9, -, _) |
39
|
|
|
* regex - Field matches given regex pattern |
40
|
|
|
* date - Field is a valid date |
41
|
|
|
* dateFormat - Field is a valid date in the given format |
42
|
|
|
* dateBefore - Field is a valid date and is before the given date |
43
|
|
|
* dateAfter - Field is a valid date and is after the given date |
44
|
|
|
* contains - Field is a string and contains the given string |
45
|
|
|
* creditCard - Field is a valid credit card number |
46
|
|
|
* instanceOf - Field contains an instance of the given class |
47
|
|
|
* optional - Value does not need to be included in data array. If it is however, it must pass validation. |
48
|
|
|
*/ |
49
|
|
|
class Validator extends \Valitron\Validator |
50
|
|
|
{ |
51
|
|
|
/** |
52
|
|
|
* @param callable|string $rule |
53
|
|
|
* @param array|string $fields |
54
|
|
|
* @return $this |
55
|
|
|
*/ |
56
|
11 |
|
public function rule($rule, $fields) |
57
|
|
|
{ |
58
|
11 |
|
if(is_string($rule)){ |
59
|
11 |
|
$rules = explode('|', $rule); |
60
|
11 |
|
foreach ($rules as $r){ |
61
|
11 |
|
$params = explode(':', trim($r)); |
62
|
11 |
|
$rule = $params[0]; |
63
|
11 |
|
$params = isset($params[1])?explode(',', $params[1]):[]; |
64
|
11 |
|
if($rule == 'in' || $rule == 'notIn'){ |
65
|
1 |
|
$params = [$params]; |
66
|
1 |
|
} |
67
|
11 |
|
call_user_func_array([$this, 'parent::rule'], array_merge([$rule, $fields], $params)); |
68
|
|
|
|
69
|
11 |
|
} |
70
|
11 |
|
return $this; |
71
|
|
|
} |
72
|
|
|
parent::rule($rule, $fields); |
73
|
|
|
return $this; |
74
|
|
|
} |
75
|
11 |
|
public function hasRule($name, $field) |
76
|
|
|
{ |
77
|
11 |
|
return parent::hasRule($name, $field); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
// /** |
|
|
|
|
81
|
|
|
// * Validate that a field matches a specified type |
82
|
|
|
// * |
83
|
|
|
// * @param string $field |
84
|
|
|
// * @param mixed $value |
85
|
|
|
// * @param array $params |
86
|
|
|
// * @internal param array $fields |
87
|
|
|
// * @return bool |
88
|
|
|
// */ |
89
|
|
|
// protected function validateType($field, $value, $params) |
90
|
|
|
// { |
91
|
|
|
// $type = $params[0]; |
92
|
|
|
// if(TypeHint::isArray($type)){ |
93
|
|
|
// $type = TypeHint::getArrayType($type); |
94
|
|
|
// if(!$this->validateArray($field, $value)){ |
95
|
|
|
// return false; |
96
|
|
|
// } |
97
|
|
|
// foreach ($value as $k=>$v){ |
98
|
|
|
// if(!$this->validateType($field.'.'.$k, $v, [$type])){ |
99
|
|
|
// return false; |
100
|
|
|
// } |
101
|
|
|
// } |
102
|
|
|
// return true; |
103
|
|
|
// } |
104
|
|
|
// if(TypeHint::isScalarType($type)){ |
105
|
|
|
// if($type == 'mixed'){ |
106
|
|
|
// return true; |
107
|
|
|
// }else{ |
108
|
|
|
// return call_user_func("is_$type", $value); |
109
|
|
|
// } |
110
|
|
|
// }else{ |
111
|
|
|
// //TODO class validate |
112
|
|
|
// $metas = new EntityContainerBuilder($type); |
113
|
|
|
// $metas = $metas->getPropertyMetas(); |
114
|
|
|
// |
115
|
|
|
// } |
116
|
|
|
// } |
117
|
|
|
|
118
|
|
|
} |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.