@@ -20,13 +20,13 @@ discard block |
||
20 | 20 | { |
21 | 21 | |
22 | 22 | /** @var array */ |
23 | - private $errors = []; |
|
23 | + private $errors = [ ]; |
|
24 | 24 | /** @var array */ |
25 | - private $namings = []; |
|
25 | + private $namings = [ ]; |
|
26 | 26 | /** @var array */ |
27 | - private $customErrorsWithInputName = []; |
|
27 | + private $customErrorsWithInputName = [ ]; |
|
28 | 28 | /** @var array */ |
29 | - private $customErrors = []; |
|
29 | + private $customErrors = [ ]; |
|
30 | 30 | |
31 | 31 | /** |
32 | 32 | * Constructor is not allowed because Validoo uses its own |
@@ -58,14 +58,14 @@ discard block |
||
58 | 58 | if (!is_array($input_rules)) |
59 | 59 | throw new ValidooException(ValidooException::ARRAY_EXPECTED, $input); |
60 | 60 | |
61 | - if (in_array("onlyifset", $input_rules) && !isset($inputs[$input])) |
|
61 | + if (in_array("onlyifset", $input_rules) && !isset($inputs[ $input ])) |
|
62 | 62 | continue; |
63 | 63 | |
64 | 64 | foreach ($input_rules as $rule => $closure) { |
65 | - if (!isset($inputs[$input])) { |
|
65 | + if (!isset($inputs[ $input ])) { |
|
66 | 66 | $input_value = null; |
67 | 67 | } else { |
68 | - $input_value = $inputs[$input]; |
|
68 | + $input_value = $inputs[ $input ]; |
|
69 | 69 | } |
70 | 70 | if (is_numeric($rule)) { |
71 | 71 | $rule = $closure; |
@@ -74,14 +74,14 @@ discard block |
||
74 | 74 | continue; |
75 | 75 | |
76 | 76 | $rule_and_params = self::getParams($rule); |
77 | - $params = $real_params = $rule_and_params['params']; |
|
78 | - $rule = $rule_and_params['rule']; |
|
77 | + $params = $real_params = $rule_and_params[ 'params' ]; |
|
78 | + $rule = $rule_and_params[ 'rule' ]; |
|
79 | 79 | $params = self::getParamValues($params, $inputs); |
80 | 80 | array_unshift($params, $input_value); |
81 | 81 | |
82 | 82 | if (false == self::doValidation($closure, $params, $rule)) { |
83 | - $errors[$input][$rule]['result'] = false; |
|
84 | - $errors[$input][$rule]['params'] = $real_params; |
|
83 | + $errors[ $input ][ $rule ][ 'result' ] = false; |
|
84 | + $errors[ $input ][ $rule ][ 'params' ] = $real_params; |
|
85 | 85 | } |
86 | 86 | } |
87 | 87 | |
@@ -124,13 +124,13 @@ discard block |
||
124 | 124 | { |
125 | 125 | if (preg_match("#^([\w]+)\((.+?)\)$#", $rule, $matches)) { |
126 | 126 | return [ |
127 | - 'rule' => $matches[1], |
|
128 | - 'params' => explode(',', $matches[2]) |
|
127 | + 'rule' => $matches[ 1 ], |
|
128 | + 'params' => explode(',', $matches[ 2 ]) |
|
129 | 129 | ]; |
130 | 130 | } |
131 | 131 | return [ |
132 | 132 | 'rule' => $rule, |
133 | - 'params' => [] |
|
133 | + 'params' => [ ] |
|
134 | 134 | ]; |
135 | 135 | } |
136 | 136 | |
@@ -145,7 +145,7 @@ discard block |
||
145 | 145 | { |
146 | 146 | foreach ($params as $key => $param) { |
147 | 147 | if (preg_match("#^:([\w]+)$#", $param, $param_type)) { |
148 | - $params[$key] = @$inputs[(string)$param_type[1]]; |
|
148 | + $params[ $key ] = @$inputs[ (string) $param_type[ 1 ] ]; |
|
149 | 149 | } |
150 | 150 | } |
151 | 151 | return $params; |
@@ -193,7 +193,7 @@ discard block |
||
193 | 193 | */ |
194 | 194 | protected static function integer($input): bool |
195 | 195 | { |
196 | - return is_int($input) || ($input == (string)(int)$input); |
|
196 | + return is_int($input) || ($input == (string) (int) $input); |
|
197 | 197 | } |
198 | 198 | |
199 | 199 | /** |
@@ -202,7 +202,7 @@ discard block |
||
202 | 202 | */ |
203 | 203 | protected static function float($input): bool |
204 | 204 | { |
205 | - return is_float($input) || ($input == (string)(float)$input); |
|
205 | + return is_float($input) || ($input == (string) (float) $input); |
|
206 | 206 | } |
207 | 207 | |
208 | 208 | /** |
@@ -307,9 +307,9 @@ discard block |
||
307 | 307 | // handle input.rule eg (name.required) |
308 | 308 | if (preg_match("#^(.+?)\.(.+?)$#", $key, $matches)) { |
309 | 309 | // $this->customErrorsWithInputName[name][required] = error message |
310 | - $this->customErrorsWithInputName[(string)$matches[1]][(string)$matches[2]] = $value; |
|
310 | + $this->customErrorsWithInputName[ (string) $matches[ 1 ] ][ (string) $matches[ 2 ] ] = $value; |
|
311 | 311 | } else { |
312 | - $this->customErrors[(string)$key] = $value; |
|
312 | + $this->customErrors[ (string) $key ] = $value; |
|
313 | 313 | } |
314 | 314 | } |
315 | 315 | } |
@@ -325,7 +325,7 @@ discard block |
||
325 | 325 | $lang = $this->getDefaultLang(); |
326 | 326 | } |
327 | 327 | |
328 | - $error_results = []; |
|
328 | + $error_results = [ ]; |
|
329 | 329 | $default_error_texts = $this->getDefaultErrorTexts($lang); |
330 | 330 | |
331 | 331 | foreach ($this->errors as $input_name => $results) { |
@@ -334,19 +334,19 @@ discard block |
||
334 | 334 | /** |
335 | 335 | * if parameters are input name they should be named as well |
336 | 336 | */ |
337 | - $result['params'] = $this->handleParameterNaming($result['params']); |
|
337 | + $result[ 'params' ] = $this->handleParameterNaming($result[ 'params' ]); |
|
338 | 338 | // if there is a custom message with input name, apply it |
339 | - if (isset($this->customErrorsWithInputName[(string)$input_name][(string)$rule])) { |
|
340 | - $error_message = $this->customErrorsWithInputName[(string)$input_name][(string)$rule]; |
|
339 | + if (isset($this->customErrorsWithInputName[ (string) $input_name ][ (string) $rule ])) { |
|
340 | + $error_message = $this->customErrorsWithInputName[ (string) $input_name ][ (string) $rule ]; |
|
341 | 341 | } // if there is a custom message for the rule, apply it |
342 | - else if (isset($this->customErrors[(string)$rule])) { |
|
343 | - $error_message = $this->customErrors[(string)$rule]; |
|
342 | + else if (isset($this->customErrors[ (string) $rule ])) { |
|
343 | + $error_message = $this->customErrors[ (string) $rule ]; |
|
344 | 344 | } // if there is a custom validator try to fetch from its error file |
345 | - else if (isset($custom_error_texts[(string)$rule])) { |
|
346 | - $error_message = $custom_error_texts[(string)$rule]; |
|
345 | + else if (isset($custom_error_texts[ (string) $rule ])) { |
|
346 | + $error_message = $custom_error_texts[ (string) $rule ]; |
|
347 | 347 | } // if none try to fetch from default error file |
348 | - else if (isset($default_error_texts[(string)$rule])) { |
|
349 | - $error_message = $default_error_texts[(string)$rule]; |
|
348 | + else if (isset($default_error_texts[ (string) $rule ])) { |
|
349 | + $error_message = $default_error_texts[ (string) $rule ]; |
|
350 | 350 | } else { |
351 | 351 | throw new ValidooException(ValidooException::NO_ERROR_TEXT, $rule); |
352 | 352 | } |
@@ -354,11 +354,11 @@ discard block |
||
354 | 354 | * handle :params(..) |
355 | 355 | */ |
356 | 356 | if (preg_match_all("#:params\((.+?)\)#", $error_message, $param_indexes)) { |
357 | - foreach ($param_indexes[1] as $param_index) { |
|
358 | - $error_message = str_replace(':params(' . $param_index . ')', $result['params'][$param_index], $error_message); |
|
357 | + foreach ($param_indexes[ 1 ] as $param_index) { |
|
358 | + $error_message = str_replace(':params('.$param_index.')', $result[ 'params' ][ $param_index ], $error_message); |
|
359 | 359 | } |
360 | 360 | } |
361 | - $error_results[] = str_replace(':attribute', $named_input, $error_message); |
|
361 | + $error_results[ ] = str_replace(':attribute', $named_input, $error_message); |
|
362 | 362 | } |
363 | 363 | } |
364 | 364 | |
@@ -387,13 +387,13 @@ discard block |
||
387 | 387 | protected function getDefaultErrorTexts(string $lang = null) |
388 | 388 | { |
389 | 389 | /* handle default error text file */ |
390 | - $default_error_texts = []; |
|
391 | - if (file_exists(__DIR__ . '/errors/' . $lang . '.php')) { |
|
390 | + $default_error_texts = [ ]; |
|
391 | + if (file_exists(__DIR__.'/errors/'.$lang.'.php')) { |
|
392 | 392 | /** @noinspection PhpIncludeInspection */ |
393 | - $default_error_texts = include __DIR__ . '/errors/' . $lang . '.php'; |
|
393 | + $default_error_texts = include __DIR__.'/errors/'.$lang.'.php'; |
|
394 | 394 | } |
395 | - if (file_exists(__DIR__ . '/errors/' . $lang . '.json')) { |
|
396 | - $default_error_texts = json_decode(file_get_contents(__DIR__ . '/errors/' . $lang . '.json'), true); |
|
395 | + if (file_exists(__DIR__.'/errors/'.$lang.'.json')) { |
|
396 | + $default_error_texts = json_decode(file_get_contents(__DIR__.'/errors/'.$lang.'.json'), true); |
|
397 | 397 | } |
398 | 398 | |
399 | 399 | |
@@ -406,8 +406,8 @@ discard block |
||
406 | 406 | */ |
407 | 407 | protected function handleNaming(string $input_name) |
408 | 408 | { |
409 | - if (isset($this->namings[$input_name])) { |
|
410 | - $named_input = $this->namings[$input_name]; |
|
409 | + if (isset($this->namings[ $input_name ])) { |
|
410 | + $named_input = $this->namings[ $input_name ]; |
|
411 | 411 | } else { |
412 | 412 | $named_input = $input_name; |
413 | 413 | } |
@@ -423,10 +423,10 @@ discard block |
||
423 | 423 | { |
424 | 424 | foreach ($params as $key => $param) { |
425 | 425 | if (preg_match("#^:([\w]+)$#", $param, $param_type)) { |
426 | - if (isset($this->namings[(string)$param_type[1]])) { |
|
427 | - $params[$key] = $this->namings[(string)$param_type[1]]; |
|
426 | + if (isset($this->namings[ (string) $param_type[ 1 ] ])) { |
|
427 | + $params[ $key ] = $this->namings[ (string) $param_type[ 1 ] ]; |
|
428 | 428 | } else { |
429 | - $params[$key] = $param_type[1]; |
|
429 | + $params[ $key ] = $param_type[ 1 ]; |
|
430 | 430 | } |
431 | 431 | } |
432 | 432 | } |
@@ -442,10 +442,10 @@ discard block |
||
442 | 442 | public function has(string $input_name, string $rule_name = null): bool |
443 | 443 | { |
444 | 444 | if (null !== $rule_name) { |
445 | - return isset($this->errors[$input_name][$rule_name]); |
|
445 | + return isset($this->errors[ $input_name ][ $rule_name ]); |
|
446 | 446 | } |
447 | 447 | |
448 | - return isset($this->errors[$input_name]); |
|
448 | + return isset($this->errors[ $input_name ]); |
|
449 | 449 | } |
450 | 450 | |
451 | 451 | /** |
@@ -52,14 +52,17 @@ discard block |
||
52 | 52 | $errors = null; |
53 | 53 | foreach ($rules as $input => $input_rules) { |
54 | 54 | |
55 | - if (is_string($input_rules)) |
|
56 | - $input_rules = explode("|", $input_rules); |
|
55 | + if (is_string($input_rules)) { |
|
56 | + $input_rules = explode("|", $input_rules); |
|
57 | + } |
|
57 | 58 | |
58 | - if (!is_array($input_rules)) |
|
59 | - throw new ValidooException(ValidooException::ARRAY_EXPECTED, $input); |
|
59 | + if (!is_array($input_rules)) { |
|
60 | + throw new ValidooException(ValidooException::ARRAY_EXPECTED, $input); |
|
61 | + } |
|
60 | 62 | |
61 | - if (in_array("onlyifset", $input_rules) && !isset($inputs[$input])) |
|
62 | - continue; |
|
63 | + if (in_array("onlyifset", $input_rules) && !isset($inputs[$input])) { |
|
64 | + continue; |
|
65 | + } |
|
63 | 66 | |
64 | 67 | foreach ($input_rules as $rule => $closure) { |
65 | 68 | if (!isset($inputs[$input])) { |
@@ -70,8 +73,9 @@ discard block |
||
70 | 73 | if (is_numeric($rule)) { |
71 | 74 | $rule = $closure; |
72 | 75 | } |
73 | - if ('onlyifset' == $rule) |
|
74 | - continue; |
|
76 | + if ('onlyifset' == $rule) { |
|
77 | + continue; |
|
78 | + } |
|
75 | 79 | |
76 | 80 | $rule_and_params = self::getParams($rule); |
77 | 81 | $params = $real_params = $rule_and_params['params']; |
@@ -157,10 +161,12 @@ discard block |
||
157 | 161 | */ |
158 | 162 | protected static function required($input = null): bool |
159 | 163 | { |
160 | - if (is_string($input)) |
|
161 | - $input = trim($input); |
|
162 | - if (is_numeric($input)) |
|
163 | - return true; |
|
164 | + if (is_string($input)) { |
|
165 | + $input = trim($input); |
|
166 | + } |
|
167 | + if (is_numeric($input)) { |
|
168 | + return true; |
|
169 | + } |
|
164 | 170 | |
165 | 171 | return (null !== $input && !empty($input)); |
166 | 172 | } |
@@ -20,7 +20,7 @@ |
||
20 | 20 | static::UNKNOWN_RULE => "Unknown Rule: :param", |
21 | 21 | static::ARRAY_EXPECTED => "Rules are expected to Array. Input Name: :param" |
22 | 22 | ]; |
23 | - parent::__construct(str_replace(":param", $param, static::$error_messages[$code]), $code); |
|
23 | + parent::__construct(str_replace(":param", $param, static::$error_messages[ $code ]), $code); |
|
24 | 24 | } |
25 | 25 | |
26 | 26 | } |