@@ -20,32 +20,32 @@ discard block |
||
20 | 20 | /** |
21 | 21 | * @var array |
22 | 22 | */ |
23 | - protected $data = []; |
|
23 | + protected $data = [ ]; |
|
24 | 24 | |
25 | 25 | /** |
26 | 26 | * @var array |
27 | 27 | */ |
28 | - protected $ruleGroups = []; |
|
28 | + protected $ruleGroups = [ ]; |
|
29 | 29 | |
30 | 30 | /** |
31 | 31 | * @var array |
32 | 32 | */ |
33 | - protected $messages = []; |
|
33 | + protected $messages = [ ]; |
|
34 | 34 | |
35 | 35 | /** |
36 | 36 | * @var array |
37 | 37 | */ |
38 | - protected static $forceRules = ['Required', 'RequiredIf', 'RequiredWith', 'RequiredUnless', 'RequiredWithout']; |
|
38 | + protected static $forceRules = [ 'Required', 'RequiredIf', 'RequiredWith', 'RequiredUnless', 'RequiredWithout' ]; |
|
39 | 39 | |
40 | 40 | /** |
41 | 41 | * @var array |
42 | 42 | */ |
43 | - protected static $extensions = []; |
|
43 | + protected static $extensions = [ ]; |
|
44 | 44 | |
45 | 45 | /** |
46 | 46 | * @var array |
47 | 47 | */ |
48 | - protected static $extensionTemplates = []; |
|
48 | + protected static $extensionTemplates = [ ]; |
|
49 | 49 | |
50 | 50 | /** |
51 | 51 | * Validator constructor. |
@@ -55,7 +55,7 @@ discard block |
||
55 | 55 | * @param array $customMessages |
56 | 56 | * @param string $file |
57 | 57 | */ |
58 | - public function __construct(array $data, array $rules, array $customMessages = [], $file = __DIR__.'/messages/en.php') |
|
58 | + public function __construct(array $data, array $rules, array $customMessages = [ ], $file = __DIR__.'/messages/en.php') |
|
59 | 59 | { |
60 | 60 | $this->data = $data; |
61 | 61 | $this->parseRules($rules); |
@@ -72,11 +72,11 @@ discard block |
||
72 | 72 | { |
73 | 73 | $name = self::formatRuleName($name); |
74 | 74 | |
75 | - self::$extensions[$name] = $callback; |
|
75 | + self::$extensions[ $name ] = $callback; |
|
76 | 76 | |
77 | - $isForce && self::$forceRules[] = $name; |
|
77 | + $isForce && self::$forceRules[ ] = $name; |
|
78 | 78 | |
79 | - !empty($message) && (static::$extensionTemplates[$name] = $message); |
|
79 | + !empty($message) && (static::$extensionTemplates[ $name ] = $message); |
|
80 | 80 | } |
81 | 81 | |
82 | 82 | /** |
@@ -89,14 +89,14 @@ discard block |
||
89 | 89 | $value = $this->getField($field); |
90 | 90 | foreach ($rules as $rule => $parameters) { |
91 | 91 | if (!$this->runValidateRule($field, $value, $rule, $parameters)) { |
92 | - $this->messages[$field][$rule] = $this->buildMessage($rule, $field, $parameters); |
|
92 | + $this->messages[ $field ][ $rule ] = $this->buildMessage($rule, $field, $parameters); |
|
93 | 93 | } |
94 | 94 | } |
95 | 95 | } elseif ($forceRules = array_intersect(self::$forceRules, array_keys($rules))) { |
96 | 96 | $value = null; |
97 | 97 | foreach ($forceRules as $rule) { |
98 | - if (!$this->runValidateRule($field, null, $rule, $rules[$rule])) { |
|
99 | - $this->messages[$field][$rule] = $this->buildMessage($rule, $field, $rules[$rule]); |
|
98 | + if (!$this->runValidateRule($field, null, $rule, $rules[ $rule ])) { |
|
99 | + $this->messages[ $field ][ $rule ] = $this->buildMessage($rule, $field, $rules[ $rule ]); |
|
100 | 100 | } |
101 | 101 | } |
102 | 102 | } |
@@ -134,13 +134,13 @@ discard block |
||
134 | 134 | */ |
135 | 135 | protected function parseRules(array $ruleGroups) |
136 | 136 | { |
137 | - $map = []; |
|
137 | + $map = [ ]; |
|
138 | 138 | foreach ($ruleGroups as $field => $rules) { |
139 | 139 | foreach (explode('|', $rules) as $rule) { |
140 | 140 | list($rule, $parameters) = explode(':', (false === strpos($rule, ':') ? ($rule.':') : $rule), 2); |
141 | - !isset($map[$rule]) && $map[$rule] = self::formatRuleName($rule); |
|
142 | - $rule = $map[$rule]; |
|
143 | - $this->ruleGroups[$field][$rule] = ('' === $parameters ? [] : explode(',', $parameters)); |
|
141 | + !isset($map[ $rule ]) && $map[ $rule ] = self::formatRuleName($rule); |
|
142 | + $rule = $map[ $rule ]; |
|
143 | + $this->ruleGroups[ $field ][ $rule ] = ('' === $parameters ? [ ] : explode(',', $parameters)); |
|
144 | 144 | } |
145 | 145 | } |
146 | 146 | unset($map); |
@@ -156,7 +156,7 @@ discard block |
||
156 | 156 | return implode( |
157 | 157 | '', |
158 | 158 | array_map( |
159 | - function ($value) { |
|
159 | + function($value) { |
|
160 | 160 | return ucfirst($value); |
161 | 161 | }, |
162 | 162 | explode('_', $name) |
@@ -176,13 +176,13 @@ discard block |
||
176 | 176 | if (!array_key_exists($item, $this->data)) { |
177 | 177 | return false; |
178 | 178 | } |
179 | - $value = $this->data[$item]; |
|
179 | + $value = $this->data[ $item ]; |
|
180 | 180 | |
181 | 181 | foreach ($field as $item) { |
182 | 182 | if (!array_key_exists($item, $value)) { |
183 | 183 | return false; |
184 | 184 | } |
185 | - $value = $value[$item]; |
|
185 | + $value = $value[ $item ]; |
|
186 | 186 | } |
187 | 187 | |
188 | 188 | return true; |
@@ -197,9 +197,9 @@ discard block |
||
197 | 197 | { |
198 | 198 | $field = explode('.', $field); |
199 | 199 | $item = array_shift($field); |
200 | - $value = $this->data[$item]; |
|
200 | + $value = $this->data[ $item ]; |
|
201 | 201 | foreach ($field as $item) { |
202 | - $value = $value[$item]; |
|
202 | + $value = $value[ $item ]; |
|
203 | 203 | } |
204 | 204 | |
205 | 205 | return $value; |
@@ -213,9 +213,9 @@ discard block |
||
213 | 213 | * |
214 | 214 | * @return bool |
215 | 215 | */ |
216 | - protected function runValidateRule($field, $value, $rule, array $parameters = []) |
|
216 | + protected function runValidateRule($field, $value, $rule, array $parameters = [ ]) |
|
217 | 217 | { |
218 | - $callback = array_key_exists($rule, self::$extensions) ? self::$extensions[$rule] : [$this, "validate{$rule}"]; |
|
218 | + $callback = array_key_exists($rule, self::$extensions) ? self::$extensions[ $rule ] : [ $this, "validate{$rule}" ]; |
|
219 | 219 | |
220 | 220 | return (bool) call_user_func($callback, $field, $value, $parameters, $this); |
221 | 221 | } |
@@ -21,7 +21,7 @@ discard block |
||
21 | 21 | */ |
22 | 22 | public function validateAccept($field, $value, array $parameters, Validator $validator) |
23 | 23 | { |
24 | - return in_array(strtolower($value), ['yes', 'on', '1', 1, true], true); |
|
24 | + return in_array(strtolower($value), [ 'yes', 'on', '1', 1, true ], true); |
|
25 | 25 | } |
26 | 26 | |
27 | 27 | /** |
@@ -73,8 +73,8 @@ discard block |
||
73 | 73 | */ |
74 | 74 | public function validateSize($field, $value, array $parameters, Validator $validator) |
75 | 75 | { |
76 | - $size = filter_var($parameters[0], FILTER_VALIDATE_INT); |
|
77 | - false === $size && $size = filter_var($parameters[0], FILTER_VALIDATE_FLOAT); |
|
76 | + $size = filter_var($parameters[ 0 ], FILTER_VALIDATE_INT); |
|
77 | + false === $size && $size = filter_var($parameters[ 0 ], FILTER_VALIDATE_FLOAT); |
|
78 | 78 | |
79 | 79 | return $this->getSize($field, $value) === $size; |
80 | 80 | } |
@@ -102,7 +102,7 @@ discard block |
||
102 | 102 | */ |
103 | 103 | public function validateBoolean($field, $value, array $parameters, Validator $validator) |
104 | 104 | { |
105 | - return in_array($value, [true, false, 0, 1, '0', '1'], true); |
|
105 | + return in_array($value, [ true, false, 0, 1, '0', '1' ], true); |
|
106 | 106 | } |
107 | 107 | |
108 | 108 | /** |
@@ -115,7 +115,7 @@ discard block |
||
115 | 115 | */ |
116 | 116 | public function validateConfirm($field, $value, array $parameters, Validator $validator) |
117 | 117 | { |
118 | - return $value === $this->data[$parameters[0]]; |
|
118 | + return $value === $this->data[ $parameters[ 0 ] ]; |
|
119 | 119 | } |
120 | 120 | |
121 | 121 | /** |
@@ -169,7 +169,7 @@ discard block |
||
169 | 169 | */ |
170 | 170 | public function validateRequiredWith($field, $value, array $parameters, Validator $validator) |
171 | 171 | { |
172 | - return !is_null($value) || !array_key_exists($parameters[0], $this->data); |
|
172 | + return !is_null($value) || !array_key_exists($parameters[ 0 ], $this->data); |
|
173 | 173 | } |
174 | 174 | |
175 | 175 | /** |
@@ -182,7 +182,7 @@ discard block |
||
182 | 182 | */ |
183 | 183 | public function validateRequiredWithout($field, $value, array $parameters, Validator $validator) |
184 | 184 | { |
185 | - return !is_null($value) || array_key_exists($parameters[0], $this->data); |
|
185 | + return !is_null($value) || array_key_exists($parameters[ 0 ], $this->data); |
|
186 | 186 | } |
187 | 187 | |
188 | 188 | /** |
@@ -198,7 +198,7 @@ discard block |
||
198 | 198 | $otherField = array_shift($parameters); |
199 | 199 | |
200 | 200 | return !is_null($value) || ( |
201 | - !array_key_exists($otherField, $this->data) || false === array_search($this->data[$otherField], $parameters) |
|
201 | + !array_key_exists($otherField, $this->data) || false === array_search($this->data[ $otherField ], $parameters) |
|
202 | 202 | ); |
203 | 203 | } |
204 | 204 | |
@@ -215,7 +215,7 @@ discard block |
||
215 | 215 | $otherField = array_shift($parameters); |
216 | 216 | |
217 | 217 | return !is_null($value) || ( |
218 | - !array_key_exists($otherField, $this->data) || false !== array_search($this->data[$otherField], $parameters) |
|
218 | + !array_key_exists($otherField, $this->data) || false !== array_search($this->data[ $otherField ], $parameters) |
|
219 | 219 | ); |
220 | 220 | } |
221 | 221 | |
@@ -267,7 +267,7 @@ discard block |
||
267 | 267 | */ |
268 | 268 | public function validateMin($field, $value, array $parameters, Validator $validator) |
269 | 269 | { |
270 | - return $this->getSize($field, $value) >= $parameters[0]; |
|
270 | + return $this->getSize($field, $value) >= $parameters[ 0 ]; |
|
271 | 271 | } |
272 | 272 | |
273 | 273 | /** |
@@ -280,7 +280,7 @@ discard block |
||
280 | 280 | */ |
281 | 281 | public function validateMax($field, $value, array $parameters, Validator $validator) |
282 | 282 | { |
283 | - return $this->getSize($field, $value) <= $parameters[0]; |
|
283 | + return $this->getSize($field, $value) <= $parameters[ 0 ]; |
|
284 | 284 | } |
285 | 285 | |
286 | 286 | /** |
@@ -294,25 +294,25 @@ discard block |
||
294 | 294 | public function validateRange($field, $value, array $parameters, Validator $validator) |
295 | 295 | { |
296 | 296 | $size = $this->getSize($field, $value); |
297 | - if (!isset($parameters[0])) { |
|
297 | + if (!isset($parameters[ 0 ])) { |
|
298 | 298 | return false; |
299 | 299 | } |
300 | - if (isset($parameters[1])) { |
|
301 | - if ('' === $parameters[0]) { |
|
302 | - if ('' === $parameters[1]) { |
|
300 | + if (isset($parameters[ 1 ])) { |
|
301 | + if ('' === $parameters[ 0 ]) { |
|
302 | + if ('' === $parameters[ 1 ]) { |
|
303 | 303 | return false; |
304 | 304 | } |
305 | 305 | |
306 | - return $size <= $parameters[1]; |
|
306 | + return $size <= $parameters[ 1 ]; |
|
307 | 307 | } |
308 | - if ('' === $parameters[1]) { |
|
309 | - return $size >= $parameters[0]; |
|
308 | + if ('' === $parameters[ 1 ]) { |
|
309 | + return $size >= $parameters[ 0 ]; |
|
310 | 310 | } |
311 | 311 | |
312 | - return $size >= $parameters[0] && $size <= $parameters[1]; |
|
312 | + return $size >= $parameters[ 0 ] && $size <= $parameters[ 1 ]; |
|
313 | 313 | } |
314 | 314 | |
315 | - return '' === $parameters[0] ? false : ($size >= $parameters[0]); |
|
315 | + return '' === $parameters[ 0 ] ? false : ($size >= $parameters[ 0 ]); |
|
316 | 316 | } |
317 | 317 | |
318 | 318 | /** |
@@ -325,7 +325,7 @@ discard block |
||
325 | 325 | */ |
326 | 326 | public function validateRegex($field, $value, array $parameters, Validator $validator) |
327 | 327 | { |
328 | - return (bool) preg_match($parameters[0], $value); |
|
328 | + return (bool) preg_match($parameters[ 0 ], $value); |
|
329 | 329 | } |
330 | 330 | |
331 | 331 | /** |
@@ -364,7 +364,7 @@ discard block |
||
364 | 364 | */ |
365 | 365 | public function validateDateFormat($field, $value, array $parameters, Validator $validator) |
366 | 366 | { |
367 | - return !(bool) date_parse_from_format($parameters[0], $value)['error_count']; |
|
367 | + return !(bool) date_parse_from_format($parameters[ 0 ], $value)[ 'error_count' ]; |
|
368 | 368 | } |
369 | 369 | |
370 | 370 | /** |
@@ -377,7 +377,7 @@ discard block |
||
377 | 377 | */ |
378 | 378 | public function validateDateBefore($field, $value, array $parameters, Validator $validator) |
379 | 379 | { |
380 | - return strtotime($value) < strtotime($parameters[0]); |
|
380 | + return strtotime($value) < strtotime($parameters[ 0 ]); |
|
381 | 381 | } |
382 | 382 | |
383 | 383 | /** |
@@ -390,7 +390,7 @@ discard block |
||
390 | 390 | */ |
391 | 391 | public function validateDateAfter($field, $value, array $parameters, Validator $validator) |
392 | 392 | { |
393 | - return strtotime($value) > strtotime($parameters[0]); |
|
393 | + return strtotime($value) > strtotime($parameters[ 0 ]); |
|
394 | 394 | } |
395 | 395 | |
396 | 396 | /** |
@@ -418,7 +418,7 @@ discard block |
||
418 | 418 | { |
419 | 419 | $specifyField = array_shift($parameters); |
420 | 420 | |
421 | - return array_key_exists($specifyField, $this->data) && $value !== $this->data[$specifyField]; |
|
421 | + return array_key_exists($specifyField, $this->data) && $value !== $this->data[ $specifyField ]; |
|
422 | 422 | } |
423 | 423 | |
424 | 424 | /** |
@@ -430,7 +430,7 @@ discard block |
||
430 | 430 | public function getSize($field, $value) |
431 | 431 | { |
432 | 432 | switch (true) { |
433 | - case isset($this->ruleGroups[$field]['String']) && is_string($value): |
|
433 | + case isset($this->ruleGroups[ $field ][ 'String' ]) && is_string($value): |
|
434 | 434 | return strlen($value); |
435 | 435 | case is_array($value): |
436 | 436 | return count($value); |
@@ -11,15 +11,15 @@ discard block |
||
11 | 11 | |
12 | 12 | trait MessagesAttributes |
13 | 13 | { |
14 | - protected $templates = []; |
|
14 | + protected $templates = [ ]; |
|
15 | 15 | |
16 | - protected function loadMessageTemplate($file, array $customMessage = []) |
|
16 | + protected function loadMessageTemplate($file, array $customMessage = [ ]) |
|
17 | 17 | { |
18 | 18 | $this->templates = require $file; |
19 | 19 | $this->templates = array_merge($this->templates, self::$extensionTemplates); |
20 | 20 | |
21 | 21 | foreach ($customMessage as $rule => $template) { |
22 | - $this->templates[self::formatRuleName($rule)] = $template; |
|
22 | + $this->templates[ self::formatRuleName($rule) ] = $template; |
|
23 | 23 | } |
24 | 24 | } |
25 | 25 | |
@@ -30,12 +30,12 @@ discard block |
||
30 | 30 | * |
31 | 31 | * @return string |
32 | 32 | */ |
33 | - protected function buildMessage($rule, $field, array $parameters = []) |
|
33 | + protected function buildMessage($rule, $field, array $parameters = [ ]) |
|
34 | 34 | { |
35 | - if (!isset($this->templates[$rule])) { |
|
35 | + if (!isset($this->templates[ $rule ])) { |
|
36 | 36 | return "{$field} field check failed"; |
37 | 37 | } |
38 | - array_unshift($parameters, "{$field} {$this->templates[$rule]}"); |
|
38 | + array_unshift($parameters, "{$field} {$this->templates[ $rule ]}"); |
|
39 | 39 | |
40 | 40 | try { |
41 | 41 | return sprintf(...$parameters); |