Total Complexity | 54 |
Total Lines | 328 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like ModelValidator often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use ModelValidator, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
17 | trait ModelValidator |
||
18 | { |
||
19 | protected $_badAttr; |
||
20 | protected $_sendMethod = 'POST'; |
||
21 | |||
22 | protected $_formName; |
||
23 | |||
24 | public $_tokenRequired = false; |
||
25 | protected $_tokenOk = true; |
||
26 | |||
27 | /** |
||
28 | * Initialize validator. Set csrf protection token from request data if available. |
||
29 | * @param bool $csrf |
||
30 | */ |
||
31 | public function initialize($csrf = false) |
||
49 | } |
||
50 | } |
||
51 | |||
52 | /** |
||
53 | * Start validation for defined fields in rules() model method. |
||
54 | * @param array|null $rules |
||
55 | * @return bool |
||
56 | * @throws SyntaxException |
||
57 | */ |
||
58 | public function runValidate(array $rules = null) |
||
59 | { |
||
60 | // skip validation on empty rules |
||
61 | if ($rules === null) { |
||
62 | return true; |
||
63 | } |
||
64 | |||
65 | $success = true; |
||
66 | // list each rule as single one |
||
67 | foreach ($rules as $rule) { |
||
68 | // 0 = field (property) name, 1 = filter name, 2 = filter value |
||
69 | if ($rule[0] === null || $rule[1] === null) { |
||
70 | continue; |
||
71 | } |
||
72 | |||
73 | $propertyName = $rule[0]; |
||
74 | $validationRule = $rule[1]; |
||
75 | $validationValue = null; |
||
76 | if (isset($rule[2])) { |
||
77 | $validationValue = $rule[2]; |
||
78 | } |
||
79 | |||
80 | // check if target field defined as array and make recursive validation |
||
81 | if (Any::isArray($propertyName)) { |
||
82 | $cumulateValidation = true; |
||
83 | foreach ($propertyName as $attrNme) { |
||
84 | // end false condition |
||
85 | if (!$this->validateRecursive($attrNme, $validationRule, $validationValue)) { |
||
86 | $cumulateValidation = false; |
||
87 | } |
||
88 | } |
||
89 | // assign total |
||
90 | $validate = $cumulateValidation; |
||
91 | } else { |
||
92 | $validate = $this->validateRecursive($propertyName, $validationRule, $validationValue); |
||
93 | } |
||
94 | |||
95 | // do not change condition on "true" check's (end-false-condition) |
||
96 | if ($validate === false) { |
||
97 | $success = false; |
||
98 | } |
||
99 | } |
||
100 | |||
101 | return $success; |
||
102 | } |
||
103 | |||
104 | /** |
||
105 | * Try to recursive validate field by defined rules and set result to model properties if validation is successful passed |
||
106 | * @param string|array $propertyName |
||
107 | * @param string $filterName |
||
108 | * @param mixed $filterArgs |
||
109 | * @return bool |
||
110 | * @throws SyntaxException |
||
111 | */ |
||
112 | public function validateRecursive($propertyName, $filterName, $filterArgs = null) |
||
113 | { |
||
114 | // check if we got it from form defined request method |
||
115 | if (App::$Request->getMethod() !== $this->_sendMethod) { |
||
116 | return false; |
||
117 | } |
||
118 | |||
119 | // get field value from user input data |
||
120 | $fieldValue = $this->getFieldValue($propertyName); |
||
121 | |||
122 | $check = false; |
||
123 | // maybe no filter required? |
||
124 | if ($filterName === 'used') { |
||
125 | $check = true; |
||
126 | } elseif (Str::contains('::', $filterName)) { // sounds like a callback class::method::method |
||
127 | // string to array via delimiter :: |
||
128 | $callbackArray = explode('::', $filterName); |
||
129 | // first item is a class name |
||
130 | $class = array_shift($callbackArray); |
||
131 | // last item its a function |
||
132 | $method = array_pop($callbackArray); |
||
133 | // left any items? maybe post-static callbacks? |
||
134 | if (count($callbackArray) > 0) { |
||
135 | foreach ($callbackArray as $obj) { |
||
136 | if (Str::startsWith('$', $obj) && property_exists($class, ltrim($obj, '$'))) { // sounds like a variable |
||
137 | $obj = ltrim($obj, '$'); // trim variable symbol '$' |
||
138 | $class = $class::${$obj}; // make magic :) |
||
139 | } elseif (method_exists($class, $obj)) { // maybe its a function? |
||
140 | $class = $class::$obj; // call function |
||
141 | } else { |
||
142 | throw new SyntaxException('Filter callback execution failed: ' . $filterName); |
||
143 | } |
||
144 | } |
||
145 | } |
||
146 | |||
147 | // check is endpoint method exist |
||
148 | if (method_exists($class, $method)) { |
||
149 | $check = @$class::$method($fieldValue, $filterArgs); |
||
150 | } else { |
||
151 | throw new SyntaxException('Filter callback execution failed: ' . $filterName); |
||
152 | } |
||
153 | } elseif (method_exists('Ffcms\Core\Helper\ModelFilters', $filterName)) { // only full namespace\class path based :( |
||
154 | if ($filterArgs != null) { |
||
155 | $check = ModelFilters::$filterName($fieldValue, $filterArgs); |
||
156 | } else { |
||
157 | $check = ModelFilters::$filterName($fieldValue); |
||
158 | } |
||
159 | } else { |
||
160 | throw new SyntaxException('Filter "' . $filterName . '" is not exist'); |
||
161 | } |
||
162 | |||
163 | // if one from all validation tests is fail - mark as incorrect attribute |
||
164 | if ($check !== true) { |
||
165 | $this->_badAttr[] = $propertyName; |
||
166 | if (App::$Debug) { |
||
167 | App::$Debug->addMessage('Validation failed. Property: ' . $propertyName . ', filter: ' . $filterName, 'warning'); |
||
168 | } |
||
169 | } else { |
||
170 | $field_set_name = $propertyName; |
||
171 | // prevent array-type setting |
||
172 | if (Str::contains('.', $field_set_name)) { |
||
173 | $field_set_name = strstr($field_set_name, '.', true); |
||
174 | } |
||
175 | if (property_exists($this, $field_set_name)) { |
||
176 | if ($propertyName !== $field_set_name) { // array-based property |
||
177 | $dot_path = trim(strstr($propertyName, '.'), '.'); |
||
178 | // prevent throws any exceptions for null and false objects |
||
179 | if (!Any::isArray($this->{$field_set_name})) { |
||
180 | $this->{$field_set_name} = []; |
||
181 | } |
||
182 | |||
183 | // use dot-data provider to compile output array |
||
184 | $dotData = new DotData($this->{$field_set_name}); |
||
185 | $dotData->set($dot_path, $fieldValue); // todo: check me!!! Here can be bug of fail parsing dots and passing path-value |
||
186 | // export data from dot-data lib to model property |
||
187 | $this->{$field_set_name} = $dotData->export(); |
||
188 | } else { // just single property |
||
189 | $this->{$propertyName} = $fieldValue; // refresh model property's from post data |
||
190 | } |
||
191 | } |
||
192 | } |
||
193 | return $check; |
||
194 | } |
||
195 | |||
196 | /** |
||
197 | * Get field value from input POST/GET/AJAX data with defined security level (html - safe html, !secure = fully unescaped) |
||
198 | * @param string $propertyName |
||
199 | * @return array|null|string |
||
200 | * @throws \InvalidArgumentException |
||
201 | */ |
||
202 | private function getFieldValue($propertyName) |
||
235 | } |
||
236 | |||
237 | /** |
||
238 | * Get fail validation attributes as array if exist |
||
239 | * @return null|array |
||
240 | */ |
||
241 | public function getBadAttributes(): ?array |
||
242 | { |
||
243 | return $this->_badAttr; |
||
244 | } |
||
245 | |||
246 | /** |
||
247 | * Set model send method type. Allowed: post, get |
||
248 | * @param string $acceptMethod |
||
249 | */ |
||
250 | final public function setSubmitMethod($acceptMethod) |
||
253 | } |
||
254 | |||
255 | /** |
||
256 | * Get model submit method. Allowed: post, get |
||
257 | * @return string |
||
258 | */ |
||
259 | final public function getSubmitMethod() |
||
260 | { |
||
261 | return $this->_sendMethod; |
||
262 | } |
||
263 | |||
264 | /** |
||
265 | * Check if model get POST-based request as submit of SEND data |
||
266 | * @return bool |
||
267 | * @throws \InvalidArgumentException |
||
268 | */ |
||
269 | final public function send() |
||
276 | } |
||
277 | |||
278 | /** |
||
279 | * @deprecated |
||
280 | * Get input params GET/POST/PUT method |
||
281 | * @param string $param |
||
282 | * @return string|null |
||
283 | */ |
||
284 | public function getInput($param) |
||
285 | { |
||
286 | return $this->getRequest($param, $this->_sendMethod); |
||
287 | } |
||
288 | |||
289 | /** |
||
290 | * @deprecated |
||
291 | * Get uploaded file from user via POST request |
||
292 | * @param string $param |
||
293 | * @return \Symfony\Component\HttpFoundation\File\UploadedFile|null |
||
294 | */ |
||
295 | public function getFile($param) |
||
298 | } |
||
299 | |||
300 | /** |
||
301 | * Get input value based on param path and request method |
||
302 | * @param string $param |
||
303 | * @param string|null $method |
||
304 | * @return mixed |
||
305 | */ |
||
306 | public function getRequest($param, $method = null) |
||
345 | } |
||
346 | } |
||
347 |