@@ -72,7 +72,7 @@ |
||
| 72 | 72 | |
| 73 | 73 | /** |
| 74 | 74 | * @param string $lang |
| 75 | - * @return null |
|
| 75 | + * @return string |
|
| 76 | 76 | */ |
| 77 | 77 | protected function getErrorFilePath(string $lang) |
| 78 | 78 | { |
@@ -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 |
@@ -55,9 +55,9 @@ discard block |
||
| 55 | 55 | // handle input.rule eg (name.required) |
| 56 | 56 | if (preg_match("#^(.+?)\.(.+?)$#", $key, $matches)) { |
| 57 | 57 | // $this->customErrorsWithInputName[name][required] = error message |
| 58 | - $this->customErrorsWithInputName[(string)$matches[1]][(string)$matches[2]] = $value; |
|
| 58 | + $this->customErrorsWithInputName[ (string) $matches[ 1 ] ][ (string) $matches[ 2 ] ] = $value; |
|
| 59 | 59 | } else { |
| 60 | - $this->customErrors[(string)$key] = $value; |
|
| 60 | + $this->customErrors[ (string) $key ] = $value; |
|
| 61 | 61 | } |
| 62 | 62 | } |
| 63 | 63 | } |
@@ -86,9 +86,9 @@ discard block |
||
| 86 | 86 | protected function getDefaultErrorTexts(string $lang = null) |
| 87 | 87 | { |
| 88 | 88 | /* handle default error text file */ |
| 89 | - $default_error_texts = []; |
|
| 90 | - if (file_exists(__DIR__ . "./errors/" . $lang . ".php")) { |
|
| 91 | - $default_error_texts = include(__DIR__ . "./errors/" . $lang . ".php"); |
|
| 89 | + $default_error_texts = [ ]; |
|
| 90 | + if (file_exists(__DIR__."./errors/".$lang.".php")) { |
|
| 91 | + $default_error_texts = include(__DIR__."./errors/".$lang.".php"); |
|
| 92 | 92 | } |
| 93 | 93 | return $default_error_texts; |
| 94 | 94 | } |
@@ -100,7 +100,7 @@ discard block |
||
| 100 | 100 | protected function getCustomErrorTexts(string $lang = null) |
| 101 | 101 | { |
| 102 | 102 | /* handle error text file for custom validators */ |
| 103 | - $custom_error_texts = []; |
|
| 103 | + $custom_error_texts = [ ]; |
|
| 104 | 104 | if (file_exists($this->getErrorFilePath($lang))) |
| 105 | 105 | $custom_error_texts = include($this->getErrorFilePath($lang)); |
| 106 | 106 | return $custom_error_texts; |
@@ -112,8 +112,8 @@ discard block |
||
| 112 | 112 | */ |
| 113 | 113 | protected function handleNaming(string $input_name) |
| 114 | 114 | { |
| 115 | - if (isset($this->namings[$input_name])) { |
|
| 116 | - $named_input = $this->namings[$input_name]; |
|
| 115 | + if (isset($this->namings[ $input_name ])) { |
|
| 116 | + $named_input = $this->namings[ $input_name ]; |
|
| 117 | 117 | } else { |
| 118 | 118 | $named_input = $input_name; |
| 119 | 119 | } |
@@ -128,10 +128,10 @@ discard block |
||
| 128 | 128 | { |
| 129 | 129 | foreach ($params as $key => $param) { |
| 130 | 130 | if (preg_match("#^:([a-zA-Z0-9_]+)$#", $param, $param_type)) { |
| 131 | - if (isset($this->namings[(string)$param_type[1]])) |
|
| 132 | - $params[$key] = $this->namings[(string)$param_type[1]]; |
|
| 131 | + if (isset($this->namings[ (string) $param_type[ 1 ] ])) |
|
| 132 | + $params[ $key ] = $this->namings[ (string) $param_type[ 1 ] ]; |
|
| 133 | 133 | else |
| 134 | - $params[$key] = $param_type[1]; |
|
| 134 | + $params[ $key ] = $param_type[ 1 ]; |
|
| 135 | 135 | } |
| 136 | 136 | } |
| 137 | 137 | return $params; |
@@ -147,7 +147,7 @@ discard block |
||
| 147 | 147 | if ($lang == null) |
| 148 | 148 | $lang = $this->getDefaultLang(); |
| 149 | 149 | |
| 150 | - $error_results = []; |
|
| 150 | + $error_results = [ ]; |
|
| 151 | 151 | $default_error_texts = $this->getDefaultErrorTexts($lang); |
| 152 | 152 | $custom_error_texts = $this->getCustomErrorTexts($lang); |
| 153 | 153 | foreach ($this->errors as $input_name => $results) { |
@@ -156,19 +156,19 @@ discard block |
||
| 156 | 156 | /** |
| 157 | 157 | * if parameters are input name they should be named as well |
| 158 | 158 | */ |
| 159 | - $result['params'] = $this->handleParameterNaming($result['params']); |
|
| 159 | + $result[ 'params' ] = $this->handleParameterNaming($result[ 'params' ]); |
|
| 160 | 160 | // if there is a custom message with input name, apply it |
| 161 | - if (isset($this->customErrorsWithInputName[(string)$input_name][(string)$rule])) { |
|
| 162 | - $error_message = $this->customErrorsWithInputName[(string)$input_name][(string)$rule]; |
|
| 161 | + if (isset($this->customErrorsWithInputName[ (string) $input_name ][ (string) $rule ])) { |
|
| 162 | + $error_message = $this->customErrorsWithInputName[ (string) $input_name ][ (string) $rule ]; |
|
| 163 | 163 | } // if there is a custom message for the rule, apply it |
| 164 | - else if (isset($this->customErrors[(string)$rule])) { |
|
| 165 | - $error_message = $this->customErrors[(string)$rule]; |
|
| 164 | + else if (isset($this->customErrors[ (string) $rule ])) { |
|
| 165 | + $error_message = $this->customErrors[ (string) $rule ]; |
|
| 166 | 166 | } // if there is a custom validator try to fetch from its error file |
| 167 | - else if (isset($custom_error_texts[(string)$rule])) { |
|
| 168 | - $error_message = $custom_error_texts[(string)$rule]; |
|
| 167 | + else if (isset($custom_error_texts[ (string) $rule ])) { |
|
| 168 | + $error_message = $custom_error_texts[ (string) $rule ]; |
|
| 169 | 169 | } // if none try to fetch from default error file |
| 170 | - else if (isset($default_error_texts[(string)$rule])) { |
|
| 171 | - $error_message = $default_error_texts[(string)$rule]; |
|
| 170 | + else if (isset($default_error_texts[ (string) $rule ])) { |
|
| 171 | + $error_message = $default_error_texts[ (string) $rule ]; |
|
| 172 | 172 | } else { |
| 173 | 173 | throw new ValidooException(ValidooException::NO_ERROR_TEXT, $rule); |
| 174 | 174 | } |
@@ -176,10 +176,10 @@ discard block |
||
| 176 | 176 | * handle :params(..) |
| 177 | 177 | */ |
| 178 | 178 | if (preg_match_all("#:params\((.+?)\)#", $error_message, $param_indexes)) |
| 179 | - foreach ($param_indexes[1] as $param_index) { |
|
| 180 | - $error_message = str_replace(":params(" . $param_index . ")", $result['params'][$param_index], $error_message); |
|
| 179 | + foreach ($param_indexes[ 1 ] as $param_index) { |
|
| 180 | + $error_message = str_replace(":params(".$param_index.")", $result[ 'params' ][ $param_index ], $error_message); |
|
| 181 | 181 | } |
| 182 | - $error_results[] = str_replace(":attribute", $named_input, $error_message); |
|
| 182 | + $error_results[ ] = str_replace(":attribute", $named_input, $error_message); |
|
| 183 | 183 | } |
| 184 | 184 | } |
| 185 | 185 | return $error_results; |
@@ -193,8 +193,8 @@ discard block |
||
| 193 | 193 | public function has(string $input_name, string $rule_name = null): bool |
| 194 | 194 | { |
| 195 | 195 | if ($rule_name != null) |
| 196 | - return isset($this->errors[$input_name][$rule_name]); |
|
| 197 | - return isset($this->errors[$input_name]); |
|
| 196 | + return isset($this->errors[ $input_name ][ $rule_name ]); |
|
| 197 | + return isset($this->errors[ $input_name ]); |
|
| 198 | 198 | } |
| 199 | 199 | |
| 200 | 200 | /** |
@@ -214,13 +214,13 @@ discard block |
||
| 214 | 214 | { |
| 215 | 215 | if (preg_match("#^([a-zA-Z0-9_]+)\((.+?)\)$#", $rule, $matches)) { |
| 216 | 216 | return [ |
| 217 | - 'rule' => $matches[1], |
|
| 218 | - 'params' => explode(",", $matches[2]) |
|
| 217 | + 'rule' => $matches[ 1 ], |
|
| 218 | + 'params' => explode(",", $matches[ 2 ]) |
|
| 219 | 219 | ]; |
| 220 | 220 | } |
| 221 | 221 | return [ |
| 222 | 222 | 'rule' => $rule, |
| 223 | - 'params' => [] |
|
| 223 | + 'params' => [ ] |
|
| 224 | 224 | ]; |
| 225 | 225 | } |
| 226 | 226 | |
@@ -234,7 +234,7 @@ discard block |
||
| 234 | 234 | { |
| 235 | 235 | foreach ($params as $key => $param) { |
| 236 | 236 | if (preg_match("#^:([a-zA-Z0-9_]+)$#", $param, $param_type)) { |
| 237 | - $params[$key] = @$inputs[(string)$param_type[1]]; |
|
| 237 | + $params[ $key ] = @$inputs[ (string) $param_type[ 1 ] ]; |
|
| 238 | 238 | } |
| 239 | 239 | } |
| 240 | 240 | return $params; |
@@ -253,10 +253,10 @@ discard block |
||
| 253 | 253 | foreach ($rules as $input => $input_rules) { |
| 254 | 254 | if (is_array($input_rules)) { |
| 255 | 255 | foreach ($input_rules as $rule => $closure) { |
| 256 | - if (!isset($inputs[(string)$input])) |
|
| 256 | + if (!isset($inputs[ (string) $input ])) |
|
| 257 | 257 | $input_value = null; |
| 258 | 258 | else |
| 259 | - $input_value = $inputs[(string)$input]; |
|
| 259 | + $input_value = $inputs[ (string) $input ]; |
|
| 260 | 260 | /** |
| 261 | 261 | * if the key of the $input_rules is numeric that means |
| 262 | 262 | * it's neither an anonymous nor an user function. |
@@ -265,8 +265,8 @@ discard block |
||
| 265 | 265 | $rule = $closure; |
| 266 | 266 | } |
| 267 | 267 | $rule_and_params = static::getParams($rule); |
| 268 | - $params = $real_params = $rule_and_params['params']; |
|
| 269 | - $rule = $rule_and_params['rule']; |
|
| 268 | + $params = $real_params = $rule_and_params[ 'params' ]; |
|
| 269 | + $rule = $rule_and_params[ 'rule' ]; |
|
| 270 | 270 | $params = static::getParamValues($params, $inputs); |
| 271 | 271 | array_unshift($params, $input_value); |
| 272 | 272 | /** |
@@ -289,8 +289,8 @@ discard block |
||
| 289 | 289 | throw new ValidooException(ValidooException::UNKNOWN_RULE, $rule); |
| 290 | 290 | } |
| 291 | 291 | if ($validation == false) { |
| 292 | - $errors[(string)$input][(string)$rule]['result'] = false; |
|
| 293 | - $errors[(string)$input][(string)$rule]['params'] = $real_params; |
|
| 292 | + $errors[ (string) $input ][ (string) $rule ][ 'result' ] = false; |
|
| 293 | + $errors[ (string) $input ][ (string) $rule ][ 'params' ] = $real_params; |
|
| 294 | 294 | } |
| 295 | 295 | } |
| 296 | 296 | } else { |
@@ -333,7 +333,7 @@ discard block |
||
| 333 | 333 | */ |
| 334 | 334 | protected static function integer($input): bool |
| 335 | 335 | { |
| 336 | - return is_int($input) || ($input == (string)(int)$input); |
|
| 336 | + return is_int($input) || ($input == (string) (int) $input); |
|
| 337 | 337 | } |
| 338 | 338 | |
| 339 | 339 | /** |
@@ -342,7 +342,7 @@ discard block |
||
| 342 | 342 | */ |
| 343 | 343 | protected static function float($input): bool |
| 344 | 344 | { |
| 345 | - return is_float($input) || ($input == (string)(float)$input); |
|
| 345 | + return is_float($input) || ($input == (string) (float) $input); |
|
| 346 | 346 | } |
| 347 | 347 | |
| 348 | 348 | /** |
@@ -101,8 +101,9 @@ discard block |
||
| 101 | 101 | { |
| 102 | 102 | /* handle error text file for custom validators */ |
| 103 | 103 | $custom_error_texts = []; |
| 104 | - if (file_exists($this->getErrorFilePath($lang))) |
|
| 105 | - $custom_error_texts = include($this->getErrorFilePath($lang)); |
|
| 104 | + if (file_exists($this->getErrorFilePath($lang))) { |
|
| 105 | + $custom_error_texts = include($this->getErrorFilePath($lang)); |
|
| 106 | + } |
|
| 106 | 107 | return $custom_error_texts; |
| 107 | 108 | } |
| 108 | 109 | |
@@ -128,10 +129,11 @@ discard block |
||
| 128 | 129 | { |
| 129 | 130 | foreach ($params as $key => $param) { |
| 130 | 131 | if (preg_match("#^:([a-zA-Z0-9_]+)$#", $param, $param_type)) { |
| 131 | - if (isset($this->namings[(string)$param_type[1]])) |
|
| 132 | - $params[$key] = $this->namings[(string)$param_type[1]]; |
|
| 133 | - else |
|
| 134 | - $params[$key] = $param_type[1]; |
|
| 132 | + if (isset($this->namings[(string)$param_type[1]])) { |
|
| 133 | + $params[$key] = $this->namings[(string)$param_type[1]]; |
|
| 134 | + } else { |
|
| 135 | + $params[$key] = $param_type[1]; |
|
| 136 | + } |
|
| 135 | 137 | } |
| 136 | 138 | } |
| 137 | 139 | return $params; |
@@ -144,8 +146,9 @@ discard block |
||
| 144 | 146 | */ |
| 145 | 147 | public function getErrors(string $lang = null): array |
| 146 | 148 | { |
| 147 | - if ($lang == null) |
|
| 148 | - $lang = $this->getDefaultLang(); |
|
| 149 | + if ($lang == null) { |
|
| 150 | + $lang = $this->getDefaultLang(); |
|
| 151 | + } |
|
| 149 | 152 | |
| 150 | 153 | $error_results = []; |
| 151 | 154 | $default_error_texts = $this->getDefaultErrorTexts($lang); |
@@ -175,9 +178,10 @@ discard block |
||
| 175 | 178 | /** |
| 176 | 179 | * handle :params(..) |
| 177 | 180 | */ |
| 178 | - if (preg_match_all("#:params\((.+?)\)#", $error_message, $param_indexes)) |
|
| 179 | - foreach ($param_indexes[1] as $param_index) { |
|
| 181 | + if (preg_match_all("#:params\((.+?)\)#", $error_message, $param_indexes)) { |
|
| 182 | + foreach ($param_indexes[1] as $param_index) { |
|
| 180 | 183 | $error_message = str_replace(":params(" . $param_index . ")", $result['params'][$param_index], $error_message); |
| 184 | + } |
|
| 181 | 185 | } |
| 182 | 186 | $error_results[] = str_replace(":attribute", $named_input, $error_message); |
| 183 | 187 | } |
@@ -192,8 +196,9 @@ discard block |
||
| 192 | 196 | */ |
| 193 | 197 | public function has(string $input_name, string $rule_name = null): bool |
| 194 | 198 | { |
| 195 | - if ($rule_name != null) |
|
| 196 | - return isset($this->errors[$input_name][$rule_name]); |
|
| 199 | + if ($rule_name != null) { |
|
| 200 | + return isset($this->errors[$input_name][$rule_name]); |
|
| 201 | + } |
|
| 197 | 202 | return isset($this->errors[$input_name]); |
| 198 | 203 | } |
| 199 | 204 | |
@@ -253,10 +258,11 @@ discard block |
||
| 253 | 258 | foreach ($rules as $input => $input_rules) { |
| 254 | 259 | if (is_array($input_rules)) { |
| 255 | 260 | foreach ($input_rules as $rule => $closure) { |
| 256 | - if (!isset($inputs[(string)$input])) |
|
| 257 | - $input_value = null; |
|
| 258 | - else |
|
| 259 | - $input_value = $inputs[(string)$input]; |
|
| 261 | + if (!isset($inputs[(string)$input])) { |
|
| 262 | + $input_value = null; |
|
| 263 | + } else { |
|
| 264 | + $input_value = $inputs[(string)$input]; |
|
| 265 | + } |
|
| 260 | 266 | /** |
| 261 | 267 | * if the key of the $input_rules is numeric that means |
| 262 | 268 | * it's neither an anonymous nor an user function. |
@@ -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 | } |