1 | <?php /** MicroValidator */ |
||
21 | class Validator |
||
22 | { |
||
23 | /** @var array $validators supported validations */ |
||
24 | protected static $validators = [ |
||
25 | 'required' => 'RequiredValidator', |
||
26 | 'captcha' => 'CaptchaValidator', |
||
27 | 'boolean' => 'BooleanValidator', |
||
28 | 'compare' => 'CompareValidator', |
||
29 | 'string' => 'StringValidator', |
||
30 | 'regexp' => 'RegexpValidator', |
||
31 | 'number' => 'NumberValidator', |
||
32 | 'unique' => 'UniqueValidator', |
||
33 | 'range' => 'RangeValidator', |
||
34 | 'email' => 'EmailValidator', |
||
35 | 'url' => 'UrlValidator', |
||
36 | 'file' => 'FileValidator' |
||
37 | ]; |
||
38 | /** @var array $errors errors summary */ |
||
39 | public $errors = []; |
||
40 | /** @var array $elements validation elements */ |
||
41 | public $elements = []; |
||
42 | /** @var array $params validation parameters */ |
||
43 | public $params = []; |
||
44 | /** @var IContainer $container Container */ |
||
45 | protected $container; |
||
46 | /** @var array $rule current rule */ |
||
47 | protected $rule = []; |
||
48 | |||
49 | /** |
||
50 | * Constructor validator object |
||
51 | * |
||
52 | * @access public |
||
53 | * |
||
54 | * @param array $params configuration array |
||
55 | * |
||
56 | * @result void |
||
57 | */ |
||
58 | public function __construct(array $params) |
||
63 | |||
64 | /** |
||
65 | * Running validation process |
||
66 | * |
||
67 | * @access public |
||
68 | * |
||
69 | * @param IFormModel $model model |
||
70 | * @param bool $client run on client side? |
||
71 | * |
||
72 | * @return bool|string |
||
73 | * @throws Exception |
||
74 | */ |
||
75 | public function run($model, $client = false) |
||
76 | { |
||
77 | $elements = explode(',', str_replace(' ', '', array_shift($this->rule))); |
||
78 | $name = array_shift($this->rule); |
||
79 | |||
80 | $className = $this->getValidatorClass($name); |
||
81 | if (!$className) { |
||
82 | if (function_exists($name)) { |
||
83 | foreach ($elements AS $element) { |
||
84 | if (property_exists($model, $element)) { |
||
85 | $model->$element = call_user_func($name, $model->$element); |
||
86 | } |
||
87 | } |
||
88 | |||
89 | return true; |
||
90 | } elseif (method_exists($model, $name)) { |
||
91 | foreach ($elements AS $element) { |
||
92 | if (property_exists($model, $element) && !call_user_func([$model, $name], $element)) { |
||
93 | return false; |
||
94 | } |
||
95 | } |
||
96 | |||
97 | return true; |
||
98 | } else { |
||
99 | throw new Exception('Validator ' . $name . ' not defined.'); |
||
100 | } |
||
101 | } |
||
102 | |||
103 | /** @var IValidator $valid */ |
||
104 | $valid = new $className(['container' => $this->container, 'params' => $this->rule]); |
||
105 | $valid->elements = $elements; |
||
|
|||
106 | |||
107 | if ($client && method_exists($valid, 'client')) { |
||
108 | $result = $this->clientValidate($valid, $model); |
||
109 | } else { |
||
110 | $result = $valid->validate($model); |
||
111 | } |
||
112 | |||
113 | if ($valid->errors) { |
||
114 | $this->errors[] = $valid->errors; |
||
115 | } |
||
116 | |||
117 | return $result; |
||
118 | } |
||
119 | |||
120 | /** |
||
121 | * @param $name |
||
122 | * @return bool|string |
||
123 | */ |
||
124 | protected function getValidatorClass($name) |
||
136 | |||
137 | /** |
||
138 | * Client validation making |
||
139 | * |
||
140 | * @access public |
||
141 | * |
||
142 | * @param IValidator $validator |
||
143 | * @param IFormModel $model |
||
144 | * |
||
145 | * @return string |
||
146 | */ |
||
147 | public function clientValidate(IValidator $validator, IFormModel $model) |
||
163 | |||
164 | /** |
||
165 | * Get errors after run validation |
||
166 | * |
||
167 | * @access public |
||
168 | * @return array |
||
169 | */ |
||
170 | public function getErrors() |
||
174 | |||
175 | /** |
||
176 | * Check is empty property |
||
177 | * |
||
178 | * @access protected |
||
179 | * |
||
180 | * @param mixed $value check value is empty |
||
181 | * @param bool $trim run trim? |
||
182 | * |
||
183 | * @return bool |
||
184 | */ |
||
185 | protected function isEmpty($value, $trim = false) |
||
189 | } |
||
190 |
If you access a property on an interface, you most likely code against a concrete implementation of the interface.
Available Fixes
Adding an additional type check:
Changing the type hint: