@@ -69,16 +69,16 @@ discard block |
||
69 | 69 | * |
70 | 70 | * @return array |
71 | 71 | */ |
72 | - public function validate( $data, array $rules = [] ) |
|
72 | + public function validate($data, array $rules = []) |
|
73 | 73 | { |
74 | - $this->normalizeData( $data ); |
|
75 | - $this->setRules( $rules ); |
|
74 | + $this->normalizeData($data); |
|
75 | + $this->setRules($rules); |
|
76 | 76 | |
77 | - foreach( $this->rules as $attribute => $rules ) { |
|
78 | - foreach( $rules as $rule ) { |
|
79 | - $this->validateAttribute( $rule, $attribute ); |
|
77 | + foreach ($this->rules as $attribute => $rules) { |
|
78 | + foreach ($rules as $rule) { |
|
79 | + $this->validateAttribute($rule, $attribute); |
|
80 | 80 | |
81 | - if( $this->shouldStopValidating( $attribute ))break; |
|
81 | + if ($this->shouldStopValidating($attribute))break; |
|
82 | 82 | } |
83 | 83 | } |
84 | 84 | |
@@ -93,14 +93,14 @@ discard block |
||
93 | 93 | * |
94 | 94 | * @return void |
95 | 95 | */ |
96 | - protected function addError( $attribute, $rule, array $parameters ) |
|
96 | + protected function addError($attribute, $rule, array $parameters) |
|
97 | 97 | { |
98 | - $message = $this->getMessage( $attribute, $rule, $parameters ); |
|
98 | + $message = $this->getMessage($attribute, $rule, $parameters); |
|
99 | 99 | |
100 | - $this->errors[ $attribute ]['errors'][] = $message; |
|
100 | + $this->errors[$attribute]['errors'][] = $message; |
|
101 | 101 | |
102 | - if( !isset( $this->errors[ $attribute ]['value'] )) { |
|
103 | - $this->errors[ $attribute ]['value'] = $this->getValue( $attribute ); |
|
102 | + if (!isset($this->errors[$attribute]['value'])) { |
|
103 | + $this->errors[$attribute]['value'] = $this->getValue($attribute); |
|
104 | 104 | } |
105 | 105 | } |
106 | 106 | |
@@ -112,11 +112,11 @@ discard block |
||
112 | 112 | * |
113 | 113 | * @return void |
114 | 114 | */ |
115 | - protected function addFailure( $attribute, $rule, array $parameters ) |
|
115 | + protected function addFailure($attribute, $rule, array $parameters) |
|
116 | 116 | { |
117 | - $this->addError( $attribute, $rule, $parameters ); |
|
117 | + $this->addError($attribute, $rule, $parameters); |
|
118 | 118 | |
119 | - $this->failedRules[ $attribute ][ $rule ] = $parameters; |
|
119 | + $this->failedRules[$attribute][$rule] = $parameters; |
|
120 | 120 | } |
121 | 121 | |
122 | 122 | /** |
@@ -125,9 +125,9 @@ discard block |
||
125 | 125 | * @param string $attribute |
126 | 126 | * @return string |
127 | 127 | */ |
128 | - protected function getAttributeType( $attribute ) |
|
128 | + protected function getAttributeType($attribute) |
|
129 | 129 | { |
130 | - return $this->hasRule( $attribute, $this->numericRules ) |
|
130 | + return $this->hasRule($attribute, $this->numericRules) |
|
131 | 131 | ? 'numeric' |
132 | 132 | : 'string'; |
133 | 133 | } |
@@ -140,15 +140,15 @@ discard block |
||
140 | 140 | * |
141 | 141 | * @return string|null |
142 | 142 | */ |
143 | - protected function getMessage( $attribute, $rule, array $parameters ) |
|
143 | + protected function getMessage($attribute, $rule, array $parameters) |
|
144 | 144 | { |
145 | - if( in_array( $rule, $this->sizeRules )) { |
|
146 | - return $this->getSizeMessage( $attribute, $rule, $parameters ); |
|
145 | + if (in_array($rule, $this->sizeRules)) { |
|
146 | + return $this->getSizeMessage($attribute, $rule, $parameters); |
|
147 | 147 | } |
148 | 148 | |
149 | - $lowerRule = $this->snakeCase( $rule ); |
|
149 | + $lowerRule = $this->snakeCase($rule); |
|
150 | 150 | |
151 | - return $this->translator( $lowerRule, $rule, $attribute, $parameters ); |
|
151 | + return $this->translator($lowerRule, $rule, $attribute, $parameters); |
|
152 | 152 | } |
153 | 153 | |
154 | 154 | /** |
@@ -159,17 +159,17 @@ discard block |
||
159 | 159 | * |
160 | 160 | * @return array|null |
161 | 161 | */ |
162 | - protected function getRule( $attribute, $rules ) |
|
162 | + protected function getRule($attribute, $rules) |
|
163 | 163 | { |
164 | - if( !array_key_exists( $attribute, $this->rules ))return; |
|
164 | + if (!array_key_exists($attribute, $this->rules))return; |
|
165 | 165 | |
166 | 166 | $rules = (array) $rules; |
167 | 167 | |
168 | - foreach( $this->rules[ $attribute ] as $rule ) { |
|
169 | - list( $rule, $parameters ) = $this->parseRule( $rule ); |
|
168 | + foreach ($this->rules[$attribute] as $rule) { |
|
169 | + list($rule, $parameters) = $this->parseRule($rule); |
|
170 | 170 | |
171 | - if( in_array( $rule, $rules )) { |
|
172 | - return [ $rule, $parameters ]; |
|
171 | + if (in_array($rule, $rules)) { |
|
172 | + return [$rule, $parameters]; |
|
173 | 173 | } |
174 | 174 | } |
175 | 175 | } |
@@ -182,18 +182,18 @@ discard block |
||
182 | 182 | * |
183 | 183 | * @return mixed |
184 | 184 | */ |
185 | - protected function getSize( $attribute, $value ) |
|
185 | + protected function getSize($attribute, $value) |
|
186 | 186 | { |
187 | - $hasNumeric = $this->hasRule( $attribute, $this->numericRules ); |
|
187 | + $hasNumeric = $this->hasRule($attribute, $this->numericRules); |
|
188 | 188 | |
189 | - if( is_numeric( $value ) && $hasNumeric ) { |
|
189 | + if (is_numeric($value) && $hasNumeric) { |
|
190 | 190 | return $value; |
191 | 191 | } |
192 | - elseif( is_array( $value )) { |
|
193 | - return count( $value ); |
|
192 | + elseif (is_array($value)) { |
|
193 | + return count($value); |
|
194 | 194 | } |
195 | 195 | |
196 | - return mb_strlen( $value ); |
|
196 | + return mb_strlen($value); |
|
197 | 197 | } |
198 | 198 | |
199 | 199 | /** |
@@ -204,14 +204,14 @@ discard block |
||
204 | 204 | * |
205 | 205 | * @return string|null |
206 | 206 | */ |
207 | - protected function getSizeMessage( $attribute, $rule, array $parameters ) |
|
207 | + protected function getSizeMessage($attribute, $rule, array $parameters) |
|
208 | 208 | { |
209 | - $lowerRule = $this->snakeCase( $rule ); |
|
210 | - $type = $this->getAttributeType( $attribute ); |
|
209 | + $lowerRule = $this->snakeCase($rule); |
|
210 | + $type = $this->getAttributeType($attribute); |
|
211 | 211 | |
212 | 212 | $lowerRule .= ".{$type}"; |
213 | 213 | |
214 | - return $this->translator( $lowerRule, $rule, $attribute, $parameters ); |
|
214 | + return $this->translator($lowerRule, $rule, $attribute, $parameters); |
|
215 | 215 | } |
216 | 216 | |
217 | 217 | /** |
@@ -221,10 +221,10 @@ discard block |
||
221 | 221 | * |
222 | 222 | * @return mixed |
223 | 223 | */ |
224 | - protected function getValue( $attribute ) |
|
224 | + protected function getValue($attribute) |
|
225 | 225 | { |
226 | - if( isset( $this->data[ $attribute ] )) { |
|
227 | - return $this->data[ $attribute ]; |
|
226 | + if (isset($this->data[$attribute])) { |
|
227 | + return $this->data[$attribute]; |
|
228 | 228 | } |
229 | 229 | } |
230 | 230 | |
@@ -236,9 +236,9 @@ discard block |
||
236 | 236 | * |
237 | 237 | * @return bool |
238 | 238 | */ |
239 | - protected function hasRule( $attribute, $rules ) |
|
239 | + protected function hasRule($attribute, $rules) |
|
240 | 240 | { |
241 | - return !is_null( $this->getRule( $attribute, $rules )); |
|
241 | + return !is_null($this->getRule($attribute, $rules)); |
|
242 | 242 | } |
243 | 243 | |
244 | 244 | /** |
@@ -248,11 +248,11 @@ discard block |
||
248 | 248 | * |
249 | 249 | * @return $this |
250 | 250 | */ |
251 | - protected function normalizeData( $data ) |
|
251 | + protected function normalizeData($data) |
|
252 | 252 | { |
253 | 253 | // If an object was provided, get its public properties |
254 | - if( is_object( $data )) { |
|
255 | - $this->data = get_object_vars( $data ); |
|
254 | + if (is_object($data)) { |
|
255 | + $this->data = get_object_vars($data); |
|
256 | 256 | } |
257 | 257 | else { |
258 | 258 | $this->data = $data; |
@@ -269,13 +269,13 @@ discard block |
||
269 | 269 | * |
270 | 270 | * @return array |
271 | 271 | */ |
272 | - protected function parseParameters( $rule, $parameter ) |
|
272 | + protected function parseParameters($rule, $parameter) |
|
273 | 273 | { |
274 | - if( strtolower( $rule ) == 'regex' ) { |
|
275 | - return [ $parameter ]; |
|
274 | + if (strtolower($rule) == 'regex') { |
|
275 | + return [$parameter]; |
|
276 | 276 | } |
277 | 277 | |
278 | - return str_getcsv( $parameter ); |
|
278 | + return str_getcsv($parameter); |
|
279 | 279 | } |
280 | 280 | |
281 | 281 | /** |
@@ -285,22 +285,22 @@ discard block |
||
285 | 285 | * |
286 | 286 | * @return array |
287 | 287 | */ |
288 | - protected function parseRule( $rule ) |
|
288 | + protected function parseRule($rule) |
|
289 | 289 | { |
290 | 290 | $parameters = []; |
291 | 291 | |
292 | 292 | // example: {rule}:{parameters} |
293 | - if( strpos( $rule, ':' ) !== false ) { |
|
294 | - list( $rule, $parameter ) = explode( ':', $rule, 2 ); |
|
293 | + if (strpos($rule, ':') !== false) { |
|
294 | + list($rule, $parameter) = explode(':', $rule, 2); |
|
295 | 295 | |
296 | 296 | // example: {parameter1,parameter2,...} |
297 | - $parameters = $this->parseParameters( $rule, $parameter ); |
|
297 | + $parameters = $this->parseParameters($rule, $parameter); |
|
298 | 298 | } |
299 | 299 | |
300 | - $rule = ucwords( str_replace( ['-', '_'], ' ', trim( $rule ))); |
|
301 | - $rule = str_replace( ' ', '', $rule ); |
|
300 | + $rule = ucwords(str_replace(['-', '_'], ' ', trim($rule))); |
|
301 | + $rule = str_replace(' ', '', $rule); |
|
302 | 302 | |
303 | - return [ $rule, $parameters ]; |
|
303 | + return [$rule, $parameters]; |
|
304 | 304 | } |
305 | 305 | |
306 | 306 | /** |
@@ -310,9 +310,9 @@ discard block |
||
310 | 310 | * |
311 | 311 | * @return string |
312 | 312 | */ |
313 | - protected function replaceBetween( $message, array $parameters ) |
|
313 | + protected function replaceBetween($message, array $parameters) |
|
314 | 314 | { |
315 | - return str_replace([':min', ':max'], $parameters, $message ); |
|
315 | + return str_replace([':min', ':max'], $parameters, $message); |
|
316 | 316 | } |
317 | 317 | |
318 | 318 | /** |
@@ -322,9 +322,9 @@ discard block |
||
322 | 322 | * |
323 | 323 | * @return string |
324 | 324 | */ |
325 | - protected function replaceMax( $message, array $parameters ) |
|
325 | + protected function replaceMax($message, array $parameters) |
|
326 | 326 | { |
327 | - return str_replace( ':max', $parameters[0], $message ); |
|
327 | + return str_replace(':max', $parameters[0], $message); |
|
328 | 328 | } |
329 | 329 | |
330 | 330 | /** |
@@ -334,9 +334,9 @@ discard block |
||
334 | 334 | * |
335 | 335 | * @return string |
336 | 336 | */ |
337 | - protected function replaceMin( $message, array $parameters ) |
|
337 | + protected function replaceMin($message, array $parameters) |
|
338 | 338 | { |
339 | - return str_replace( ':min', $parameters[0], $message ); |
|
339 | + return str_replace(':min', $parameters[0], $message); |
|
340 | 340 | } |
341 | 341 | |
342 | 342 | /** |
@@ -348,10 +348,10 @@ discard block |
||
348 | 348 | * @return void |
349 | 349 | * @throws InvalidArgumentException |
350 | 350 | */ |
351 | - protected function requireParameterCount( $count, array $parameters, $rule ) |
|
351 | + protected function requireParameterCount($count, array $parameters, $rule) |
|
352 | 352 | { |
353 | - if( count( $parameters ) < $count ) { |
|
354 | - throw new InvalidArgumentException( "Validation rule $rule requires at least $count parameters." ); |
|
353 | + if (count($parameters) < $count) { |
|
354 | + throw new InvalidArgumentException("Validation rule $rule requires at least $count parameters."); |
|
355 | 355 | } |
356 | 356 | } |
357 | 357 | |
@@ -360,10 +360,10 @@ discard block |
||
360 | 360 | * |
361 | 361 | * @return $this |
362 | 362 | */ |
363 | - protected function setRules( array $rules ) |
|
363 | + protected function setRules(array $rules) |
|
364 | 364 | { |
365 | - foreach( $rules as $key => $rule ) { |
|
366 | - $rules[ $key ] = is_string( $rule ) ? explode( '|', $rule ) : $rule; |
|
365 | + foreach ($rules as $key => $rule) { |
|
366 | + $rules[$key] = is_string($rule) ? explode('|', $rule) : $rule; |
|
367 | 367 | } |
368 | 368 | |
369 | 369 | $this->rules = $rules; |
@@ -378,11 +378,11 @@ discard block |
||
378 | 378 | * |
379 | 379 | * @return bool |
380 | 380 | */ |
381 | - protected function shouldStopValidating( $attribute ) |
|
381 | + protected function shouldStopValidating($attribute) |
|
382 | 382 | { |
383 | - return $this->hasRule( $attribute, $this->implicitRules ) |
|
384 | - && isset( $this->failedRules[ $attribute ] ) |
|
385 | - && array_intersect( array_keys( $this->failedRules[ $attribute ] ), $this->implicitRules ); |
|
383 | + return $this->hasRule($attribute, $this->implicitRules) |
|
384 | + && isset($this->failedRules[$attribute]) |
|
385 | + && array_intersect(array_keys($this->failedRules[$attribute]), $this->implicitRules); |
|
386 | 386 | } |
387 | 387 | |
388 | 388 | /** |
@@ -392,12 +392,12 @@ discard block |
||
392 | 392 | * |
393 | 393 | * @return string |
394 | 394 | */ |
395 | - protected function snakeCase( $string ) |
|
395 | + protected function snakeCase($string) |
|
396 | 396 | { |
397 | - if( !ctype_lower( $string )) { |
|
398 | - $string = preg_replace( '/\s+/u', '', $string ); |
|
399 | - $string = preg_replace( '/(.)(?=[A-Z])/u', '$1_', $string ); |
|
400 | - $string = mb_strtolower( $string, 'UTF-8' ); |
|
397 | + if (!ctype_lower($string)) { |
|
398 | + $string = preg_replace('/\s+/u', '', $string); |
|
399 | + $string = preg_replace('/(.)(?=[A-Z])/u', '$1_', $string); |
|
400 | + $string = mb_strtolower($string, 'UTF-8'); |
|
401 | 401 | } |
402 | 402 | |
403 | 403 | return $string; |
@@ -412,20 +412,20 @@ discard block |
||
412 | 412 | * |
413 | 413 | * @return string|null |
414 | 414 | */ |
415 | - protected function translator( $key, $rule, $attribute, array $parameters ) |
|
415 | + protected function translator($key, $rule, $attribute, array $parameters) |
|
416 | 416 | { |
417 | - $strings = glsr_resolve( 'Strings' )->validation(); |
|
417 | + $strings = glsr_resolve('Strings')->validation(); |
|
418 | 418 | |
419 | - $message = isset( $strings[ $key ] ) |
|
420 | - ? $strings[ $key ] |
|
419 | + $message = isset($strings[$key]) |
|
420 | + ? $strings[$key] |
|
421 | 421 | : false; |
422 | 422 | |
423 | - if( !$message )return; |
|
423 | + if (!$message)return; |
|
424 | 424 | |
425 | - $message = str_replace( ':attribute', $attribute, $message ); |
|
425 | + $message = str_replace(':attribute', $attribute, $message); |
|
426 | 426 | |
427 | - if( method_exists( $this, $replacer = "replace{$rule}" )) { |
|
428 | - $message = $this->$replacer( $message, $parameters ); |
|
427 | + if (method_exists($this, $replacer = "replace{$rule}")) { |
|
428 | + $message = $this->$replacer($message, $parameters); |
|
429 | 429 | } |
430 | 430 | |
431 | 431 | return $message; |
@@ -444,11 +444,11 @@ discard block |
||
444 | 444 | * |
445 | 445 | * @return bool |
446 | 446 | */ |
447 | - protected function validateAccepted( $value, $attribute ) |
|
447 | + protected function validateAccepted($value, $attribute) |
|
448 | 448 | { |
449 | 449 | $acceptable = ['yes', 'on', '1', 1, true, 'true']; |
450 | 450 | |
451 | - return $this->validateRequired( $value ) && in_array( $value, $acceptable, true ); |
|
451 | + return $this->validateRequired($value) && in_array($value, $acceptable, true); |
|
452 | 452 | } |
453 | 453 | |
454 | 454 | /** |
@@ -460,20 +460,20 @@ discard block |
||
460 | 460 | * @return void |
461 | 461 | * @throws BadMethodCallException |
462 | 462 | */ |
463 | - protected function validateAttribute( $rule, $attribute ) |
|
463 | + protected function validateAttribute($rule, $attribute) |
|
464 | 464 | { |
465 | - list( $rule, $parameters ) = $this->parseRule( $rule ); |
|
465 | + list($rule, $parameters) = $this->parseRule($rule); |
|
466 | 466 | |
467 | - if( $rule == '' )return; |
|
467 | + if ($rule == '')return; |
|
468 | 468 | |
469 | 469 | $method = "validate{$rule}"; |
470 | 470 | |
471 | - if( !method_exists( $this, $method )) { |
|
472 | - throw new BadMethodCallException( "Method [$method] does not exist." ); |
|
471 | + if (!method_exists($this, $method)) { |
|
472 | + throw new BadMethodCallException("Method [$method] does not exist."); |
|
473 | 473 | } |
474 | 474 | |
475 | - if( !$this->$method( $this->getValue( $attribute ), $attribute, $parameters )) { |
|
476 | - $this->addFailure( $attribute, $rule, $parameters ); |
|
475 | + if (!$this->$method($this->getValue($attribute), $attribute, $parameters)) { |
|
476 | + $this->addFailure($attribute, $rule, $parameters); |
|
477 | 477 | } |
478 | 478 | } |
479 | 479 | |
@@ -485,11 +485,11 @@ discard block |
||
485 | 485 | * |
486 | 486 | * @return bool |
487 | 487 | */ |
488 | - protected function validateBetween( $value, $attribute, array $parameters ) |
|
488 | + protected function validateBetween($value, $attribute, array $parameters) |
|
489 | 489 | { |
490 | - $this->requireParameterCount( 2, $parameters, 'between' ); |
|
490 | + $this->requireParameterCount(2, $parameters, 'between'); |
|
491 | 491 | |
492 | - $size = $this->getSize( $attribute, $value ); |
|
492 | + $size = $this->getSize($attribute, $value); |
|
493 | 493 | |
494 | 494 | return $size >= $parameters[0] && $size <= $parameters[1]; |
495 | 495 | } |
@@ -501,9 +501,9 @@ discard block |
||
501 | 501 | * |
502 | 502 | * @return bool |
503 | 503 | */ |
504 | - protected function validateEmail( $value ) |
|
504 | + protected function validateEmail($value) |
|
505 | 505 | { |
506 | - return filter_var( $value, FILTER_VALIDATE_EMAIL ) !== false; |
|
506 | + return filter_var($value, FILTER_VALIDATE_EMAIL) !== false; |
|
507 | 507 | } |
508 | 508 | |
509 | 509 | /** |
@@ -514,11 +514,11 @@ discard block |
||
514 | 514 | * |
515 | 515 | * @return bool |
516 | 516 | */ |
517 | - protected function validateMax( $value, $attribute, array $parameters ) |
|
517 | + protected function validateMax($value, $attribute, array $parameters) |
|
518 | 518 | { |
519 | - $this->requireParameterCount( 1, $parameters, 'max' ); |
|
519 | + $this->requireParameterCount(1, $parameters, 'max'); |
|
520 | 520 | |
521 | - return $this->getSize( $attribute, $value ) <= $parameters[0]; |
|
521 | + return $this->getSize($attribute, $value) <= $parameters[0]; |
|
522 | 522 | } |
523 | 523 | |
524 | 524 | /** |
@@ -529,11 +529,11 @@ discard block |
||
529 | 529 | * |
530 | 530 | * @return bool |
531 | 531 | */ |
532 | - protected function validateMin( $value, $attribute, array $parameters ) |
|
532 | + protected function validateMin($value, $attribute, array $parameters) |
|
533 | 533 | { |
534 | - $this->requireParameterCount( 1, $parameters, 'min' ); |
|
534 | + $this->requireParameterCount(1, $parameters, 'min'); |
|
535 | 535 | |
536 | - return $this->getSize( $attribute, $value ) >= $parameters[0]; |
|
536 | + return $this->getSize($attribute, $value) >= $parameters[0]; |
|
537 | 537 | } |
538 | 538 | |
539 | 539 | /** |
@@ -543,9 +543,9 @@ discard block |
||
543 | 543 | * |
544 | 544 | * @return bool |
545 | 545 | */ |
546 | - protected function validateNumeric( $value ) |
|
546 | + protected function validateNumeric($value) |
|
547 | 547 | { |
548 | - return is_numeric( $value ); |
|
548 | + return is_numeric($value); |
|
549 | 549 | } |
550 | 550 | |
551 | 551 | /** |
@@ -555,12 +555,12 @@ discard block |
||
555 | 555 | * |
556 | 556 | * @return bool |
557 | 557 | */ |
558 | - protected function validateRequired( $value ) |
|
558 | + protected function validateRequired($value) |
|
559 | 559 | { |
560 | - if( is_string( $value )) { |
|
561 | - $value = trim( $value ); |
|
560 | + if (is_string($value)) { |
|
561 | + $value = trim($value); |
|
562 | 562 | } |
563 | - return is_null( $value ) || empty( $value ) |
|
563 | + return is_null($value) || empty($value) |
|
564 | 564 | ? false |
565 | 565 | : true; |
566 | 566 | } |