Completed
Push — develop ( ec1aaf...0b0716 )
by Paul
02:09
created
src/Validator.php 2 patches
Spacing   +114 added lines, -114 removed lines patch added patch discarded remove patch
@@ -69,16 +69,16 @@  discard block
 block discarded – undo
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( $attribute, $rule );
77
+		foreach ($this->rules as $attribute => $rules) {
78
+			foreach ($rules as $rule) {
79
+				$this->validateAttribute($attribute, $rule);
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
444 444
 	 *
445 445
 	 * @return bool
446 446
 	 */
447
-	protected function validateAccepted( $attribute, $value )
447
+	protected function validateAccepted($attribute, $value)
448 448
 	{
449 449
 		$acceptable = ['yes', 'on', '1', 1, true, 'true'];
450 450
 
451
-		return $this->validateRequired( $attribute, $value ) && in_array( $value, $acceptable, true );
451
+		return $this->validateRequired($attribute, $value) && in_array($value, $acceptable, true);
452 452
 	}
453 453
 
454 454
 	/**
@@ -460,26 +460,26 @@  discard block
 block discarded – undo
460 460
 	 * @return void
461 461
 	 * @throws BadMethodCallException
462 462
 	 */
463
-	protected function validateAttribute( $attribute, $rule )
463
+	protected function validateAttribute($attribute, $rule)
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
-		$value = $this->getValue( $attribute );
469
+		$value = $this->getValue($attribute);
470 470
 
471 471
 		// is the value filled or is the attribute required?
472 472
 		// - removed $validatable assignment
473
-		$this->validateRequired( $attribute, $value ) || in_array( $rule, $this->implicitRules );
473
+		$this->validateRequired($attribute, $value) || in_array($rule, $this->implicitRules);
474 474
 
475 475
 		$method = "validate{$rule}";
476 476
 
477
-		if( !method_exists( $this, $method )) {
478
-			throw new BadMethodCallException( "Method [$method] does not exist." );
477
+		if (!method_exists($this, $method)) {
478
+			throw new BadMethodCallException("Method [$method] does not exist.");
479 479
 		}
480 480
 
481
-		if( !$this->$method( $attribute, $value, $parameters )) {
482
-			$this->addFailure( $attribute, $rule, $parameters );
481
+		if (!$this->$method($attribute, $value, $parameters)) {
482
+			$this->addFailure($attribute, $rule, $parameters);
483 483
 		}
484 484
 	}
485 485
 
@@ -491,11 +491,11 @@  discard block
 block discarded – undo
491 491
 	 *
492 492
 	 * @return bool
493 493
 	 */
494
-	protected function validateBetween( $attribute, $value, array $parameters )
494
+	protected function validateBetween($attribute, $value, array $parameters)
495 495
 	{
496
-		$this->requireParameterCount( 2, $parameters, 'between' );
496
+		$this->requireParameterCount(2, $parameters, 'between');
497 497
 
498
-		$size = $this->getSize( $attribute, $value );
498
+		$size = $this->getSize($attribute, $value);
499 499
 
500 500
 		return $size >= $parameters[0] && $size <= $parameters[1];
501 501
 	}
@@ -508,9 +508,9 @@  discard block
 block discarded – undo
508 508
 	 *
509 509
 	 * @return bool
510 510
 	 */
511
-	protected function validateEmail( $attribute, $value )
511
+	protected function validateEmail($attribute, $value)
512 512
 	{
513
-		return filter_var( $value, FILTER_VALIDATE_EMAIL ) !== false;
513
+		return filter_var($value, FILTER_VALIDATE_EMAIL) !== false;
514 514
 	}
515 515
 
516 516
 	/**
@@ -521,11 +521,11 @@  discard block
 block discarded – undo
521 521
 	 *
522 522
 	 * @return bool
523 523
 	 */
524
-	protected function validateMax( $attribute, $value, array $parameters )
524
+	protected function validateMax($attribute, $value, array $parameters)
525 525
 	{
526
-		$this->requireParameterCount( 1, $parameters, 'max' );
526
+		$this->requireParameterCount(1, $parameters, 'max');
527 527
 
528
-		return $this->getSize( $attribute, $value ) <= $parameters[0];
528
+		return $this->getSize($attribute, $value) <= $parameters[0];
529 529
 	}
530 530
 
531 531
 	/**
@@ -536,11 +536,11 @@  discard block
 block discarded – undo
536 536
 	 *
537 537
 	 * @return bool
538 538
 	 */
539
-	protected function validateMin( $attribute, $value, array $parameters )
539
+	protected function validateMin($attribute, $value, array $parameters)
540 540
 	{
541
-		$this->requireParameterCount( 1, $parameters, 'min' );
541
+		$this->requireParameterCount(1, $parameters, 'min');
542 542
 
543
-		return $this->getSize( $attribute, $value ) >= $parameters[0];
543
+		return $this->getSize($attribute, $value) >= $parameters[0];
544 544
 	}
545 545
 
546 546
 	/**
@@ -551,9 +551,9 @@  discard block
 block discarded – undo
551 551
 	 *
552 552
 	 * @return bool
553 553
 	 */
554
-	protected function validateNumeric( $attribute, $value )
554
+	protected function validateNumeric($attribute, $value)
555 555
 	{
556
-		return is_numeric( $value );
556
+		return is_numeric($value);
557 557
 	}
558 558
 
559 559
 	/**
@@ -564,11 +564,11 @@  discard block
 block discarded – undo
564 564
 	 *
565 565
 	 * @return bool
566 566
 	 */
567
-	protected function validateRequired( $attribute, $value )
567
+	protected function validateRequired($attribute, $value)
568 568
 	{
569
-		return is_null( $value )
570
-			|| ( is_string( $value ) && trim( $value ) === '' )
571
-			|| ( is_array( $value ) && count( $value ) < 1 )
569
+		return is_null($value)
570
+			|| (is_string($value) && trim($value) === '')
571
+			|| (is_array($value) && count($value) < 1)
572 572
 			? false
573 573
 			: true;
574 574
 	}
Please login to merge, or discard this patch.
Braces   +14 added lines, -8 removed lines patch added patch discarded remove patch
@@ -78,7 +78,9 @@  discard block
 block discarded – undo
78 78
 			foreach( $rules as $rule ) {
79 79
 				$this->validateAttribute( $attribute, $rule );
80 80
 
81
-				if( $this->shouldStopValidating( $attribute ))break;
81
+				if( $this->shouldStopValidating( $attribute )) {
82
+					break;
83
+				}
82 84
 			}
83 85
 		}
84 86
 
@@ -161,7 +163,9 @@  discard block
 block discarded – undo
161 163
 	 */
162 164
 	protected function getRule( $attribute, $rules )
163 165
 	{
164
-		if( !array_key_exists( $attribute, $this->rules ))return;
166
+		if( !array_key_exists( $attribute, $this->rules )) {
167
+			return;
168
+		}
165 169
 
166 170
 		$rules = (array) $rules;
167 171
 
@@ -188,8 +192,7 @@  discard block
 block discarded – undo
188 192
 
189 193
 		if( is_numeric( $value ) && $hasNumeric ) {
190 194
 			return $value;
191
-		}
192
-		elseif( is_array( $value )) {
195
+		} elseif( is_array( $value )) {
193 196
 			return count( $value );
194 197
 		}
195 198
 
@@ -253,8 +256,7 @@  discard block
 block discarded – undo
253 256
 		// If an object was provided, get its public properties
254 257
 		if( is_object( $data )) {
255 258
 			$this->data = get_object_vars( $data );
256
-		}
257
-		else {
259
+		} else {
258 260
 			$this->data = $data;
259 261
 		}
260 262
 
@@ -420,7 +422,9 @@  discard block
 block discarded – undo
420 422
 			? $strings[ $key ]
421 423
 			: false;
422 424
 
423
-		if( !$message )return;
425
+		if( !$message ) {
426
+			return;
427
+		}
424 428
 
425 429
 		$message = str_replace( ':attribute', $attribute, $message );
426 430
 
@@ -464,7 +468,9 @@  discard block
 block discarded – undo
464 468
 	{
465 469
 		list( $rule, $parameters ) = $this->parseRule( $rule );
466 470
 
467
-		if( $rule == '' )return;
471
+		if( $rule == '' ) {
472
+			return;
473
+		}
468 474
 
469 475
 		$value = $this->getValue( $attribute );
470 476
 
Please login to merge, or discard this patch.