/
FluidValidator.php
756 lines (684 loc) · 17.7 KB
/
FluidValidator.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
<?php
/**
* @author hollodotme
*/
namespace hollodotme\FluidValidator;
use hollodotme\FluidValidator\Exceptions\CheckMethodNotCallable;
use hollodotme\FluidValidator\Exceptions\InvalidMessageType;
use hollodotme\FluidValidator\Interfaces\CollectsMessages;
use hollodotme\FluidValidator\Interfaces\ProvidesValuesToValidate;
use hollodotme\FluidValidator\MessageCollectors\ScalarListMessageCollector;
use hollodotme\FluidValidator\Validators\StringValidator;
/**
* Class FluidValidator
* @package hollodotme\FluidValidator
* METHODSTART
* @method FluidValidator isString($value, $message)
* @method FluidValidator isStringOrNull($value, $message)
* @method FluidValidator isNonEmptyString($value, $message)
* @method FluidValidator isNonEmptyStringOrNull($value, $message)
* @method FluidValidator isNotEmpty($value, $message)
* @method FluidValidator isNotEmptyOrNull($value, $message)
* @method FluidValidator isArray($value, $message)
* @method FluidValidator isArrayOrNull($value, $message)
* @method FluidValidator isInt($value, $message)
* @method FluidValidator isIntOrNull($value, $message)
* @method FluidValidator isIntInRange($value, array $range, $message)
* @method FluidValidator isIntInRangeOrNull($value, array $range, $message)
* @method FluidValidator isOneStringOf($value, array $list, $message)
* @method FluidValidator isOneStringOfOrNull($value, array $list, $message)
* @method FluidValidator isSubsetOf($values, array $list, $message)
* @method FluidValidator isSubsetOfOrNull($values, array $list, $message)
* @method FluidValidator isUuid($value, $message)
* @method FluidValidator isUuidOrNull($value, $message)
* @method FluidValidator isEqual($value1, $value2, $message)
* @method FluidValidator isNotEqual($value1, $value2, $message)
* @method FluidValidator isSame($value1, $value2, $message)
* @method FluidValidator isNotSame($value1, $value2, $message)
* @method FluidValidator isNull($value, $message)
* @method FluidValidator isNotNull($value, $message)
* @method FluidValidator matchesRegex($value, $regex, $message)
* @method FluidValidator matchesRegexOrNull($value, $regex, $message)
* @method FluidValidator hasLength($value, $length, $message)
* @method FluidValidator hasLengthOrNull($value, $length, $message)
* @method FluidValidator hasMinLength($value, $minLength, $message)
* @method FluidValidator hasMinLengthOrNull($value, $minLength, $message)
* @method FluidValidator hasMaxLength($value, $maxLength, $message)
* @method FluidValidator hasMaxLengthOrNull($value, $maxLength, $message)
* @method FluidValidator counts($values, $count, $message)
* @method FluidValidator countsOrNull($values, $count, $message)
* @method FluidValidator isEmail($value, $message)
* @method FluidValidator isEmailOrNull($value, $message)
* @method FluidValidator isUrl($value, $message)
* @method FluidValidator isUrlNull($value, $message)
* @method FluidValidator isJson($value, $message)
* @method FluidValidator isJsonOrNull($value, $message)
* @method FluidValidator hasKey($values, $key, $message)
* @method FluidValidator hasKeyOrNull($values, $key, $message)
* @method FluidValidator isDate($dateString, $format = 'Y-m-d', $message)
* @method FluidValidator isDateOrNull($dateString, $format = 'Y-m-d', $message)
* @method FluidValidator isTrue($value, $message)
* @method FluidValidator isTrueOrNull($value, $message)
* @method FluidValidator isFalse($value, $message)
* @method FluidValidator isFalseOrNull($value, $message)
* @method FluidValidator ifIsString($value, $continue)
* @method FluidValidator ifIsStringOrNull($value, $continue)
* @method FluidValidator ifIsNonEmptyString($value, $continue)
* @method FluidValidator ifIsNonEmptyStringOrNull($value, $continue)
* @method FluidValidator ifIsNotEmpty($value, $continue)
* @method FluidValidator ifIsNotEmptyOrNull($value, $continue)
* @method FluidValidator ifIsArray($value, $continue)
* @method FluidValidator ifIsArrayOrNull($value, $continue)
* @method FluidValidator ifIsInt($value, $continue)
* @method FluidValidator ifIsIntOrNull($value, $continue)
* @method FluidValidator ifIsIntInRange($value, array $range, $continue)
* @method FluidValidator ifIsIntInRangeOrNull($value, array $range, $continue)
* @method FluidValidator ifIsOneStringOf($value, array $list, $continue)
* @method FluidValidator ifIsOneStringOfOrNull($value, array $list, $continue)
* @method FluidValidator ifIsSubsetOf($values, array $list, $continue)
* @method FluidValidator ifIsSubsetOfOrNull($values, array $list, $continue)
* @method FluidValidator ifIsUuid($value, $continue)
* @method FluidValidator ifIsUuidOrNull($value, $continue)
* @method FluidValidator ifIsEqual($value1, $value2, $continue)
* @method FluidValidator ifIsNotEqual($value1, $value2, $continue)
* @method FluidValidator ifIsSame($value1, $value2, $continue)
* @method FluidValidator ifIsNotSame($value1, $value2, $continue)
* @method FluidValidator ifIsNull($value, $continue)
* @method FluidValidator ifIsNotNull($value, $continue)
* @method FluidValidator ifMatchesRegex($value, $regex, $continue)
* @method FluidValidator ifMatchesRegexOrNull($value, $regex, $continue)
* @method FluidValidator ifHasLength($value, $length, $continue)
* @method FluidValidator ifHasLengthOrNull($value, $length, $continue)
* @method FluidValidator ifHasMinLength($value, $minLength, $continue)
* @method FluidValidator ifHasMinLengthOrNull($value, $minLength, $continue)
* @method FluidValidator ifHasMaxLength($value, $maxLength, $continue)
* @method FluidValidator ifHasMaxLengthOrNull($value, $maxLength, $continue)
* @method FluidValidator ifCounts($values, $count, $continue)
* @method FluidValidator ifCountsOrNull($values, $count, $continue)
* @method FluidValidator ifIsEmail($value, $continue)
* @method FluidValidator ifIsEmailOrNull($value, $continue)
* @method FluidValidator ifIsUrl($value, $continue)
* @method FluidValidator ifIsUrlNull($value, $continue)
* @method FluidValidator ifIsJson($value, $continue)
* @method FluidValidator ifIsJsonOrNull($value, $continue)
* @method FluidValidator ifHasKey($values, $key, $continue)
* @method FluidValidator ifHasKeyOrNull($values, $key, $continue)
* @method FluidValidator ifIsDate($dateString, $format = 'Y-m-d', $continue)
* @method FluidValidator ifIsDateOrNull($dateString, $format = 'Y-m-d', $continue)
* @method FluidValidator ifIsTrue($value, $continue)
* @method FluidValidator ifIsTrueOrNull($value, $continue)
* @method FluidValidator ifIsFalse($value, $continue)
* @method FluidValidator ifIsFalseOrNull($value, $continue)
* METHODEND
*/
class FluidValidator
{
/** @var int */
private $mode;
/** @var ProvidesValuesToValidate|null */
private $dataProvider;
/** @var int */
private $skipCounter;
/** @var bool */
private $passed;
/** @var CollectsMessages */
private $messageCollector;
/**
* @param int $mode
* @param ProvidesValuesToValidate|null $dataProvider
* @param CollectsMessages $messageCollector
*/
public function __construct(
$mode = CheckMode::CONTINUOUS,
ProvidesValuesToValidate $dataProvider = null,
CollectsMessages $messageCollector = null
)
{
$this->mode = $mode;
$this->dataProvider = $dataProvider;
$this->messageCollector = $this->getMessageCollector( $messageCollector );
$this->reset();
}
/**
* @param CollectsMessages|null $messageCollector
*
* @return CollectsMessages
*/
private function getMessageCollector( $messageCollector )
{
if ( $messageCollector instanceof CollectsMessages )
{
return $messageCollector;
}
else
{
return new ScalarListMessageCollector();
}
}
/**
* @return $this
*/
public function reset()
{
$this->skipCounter = 0;
$this->passed = true;
$this->messageCollector->clearMessages();
return $this;
}
/**
* @param bool $expression
* @param int $continue
*
* @return $this
*/
public function checkIf( $expression, $continue )
{
if ( $this->skipCounter > 0 )
{
$this->skipCounter += intval( $continue ) - 1;
}
else
{
if ( !$expression )
{
$this->skipCounter = intval( $continue );
}
}
return $this;
}
/**
* @return bool
*/
public function passed()
{
return $this->passed;
}
/**
* @param int $continue
*
* @return FluidValidator
*/
public function ifPassed( $continue )
{
return $this->checkIf( $this->passed, $continue );
}
/**
* @return bool
*/
public function failed()
{
return !$this->passed;
}
/**
* @return array
*/
public function getMessages()
{
return $this->messageCollector->getMessages();
}
/**
* @param string $name
* @param array $arguments
*
* @throws CheckMethodNotCallable
* @return $this
*/
public function __call( $name, array $arguments )
{
if ( substr( $name, 0, 2 ) == 'if' )
{
$methodName = substr( $name, 2 );
return $this->handleIfMethods( $methodName, $arguments );
}
else
{
return $this->handleCheckMethods( $name, $arguments );
}
}
/**
* @param string $methodName
* @param array $arguments
*
* @throws CheckMethodNotCallable
* @return FluidValidator
*/
private function handleIfMethods( $methodName, array $arguments )
{
$checkMethod = 'check' . ucfirst( preg_replace( "#OrNull$#", '', $methodName ) );
$this->guardCheckMethodIsCallable( $checkMethod );
$continue = array_pop( $arguments );
$checkArguments = $arguments + [ '' ];
$checkResult = call_user_func_array( [ $this, $checkMethod ], $checkArguments );
if ( $this->checkNullCondition( $methodName, $arguments[0] ) )
{
$checkResult = true;
}
return $this->checkIf( $checkResult, $continue );
}
/**
* @param string $methodName
* @param mixed $var
*
* @return bool
*/
private function checkNullCondition( $methodName, $var )
{
$value = $this->getValue( $var );
$optional = (substr( $methodName, -6 ) == 'OrNull');
return ($optional && ($value === null));
}
/**
* @param string $methodName
* @param array $arguments
*
* @throws CheckMethodNotCallable
* @return $this
*/
private function handleCheckMethods( $methodName, array $arguments )
{
$checkMethod = 'check' . ucfirst( preg_replace( "#OrNull$#", '', $methodName ) );
$this->guardCheckMethodIsCallable( $checkMethod );
if ( $this->skipCounter > 0 )
{
$this->skipCounter--;
return $this;
}
elseif ( $this->mode == CheckMode::STOP_ON_FIRST_FAIL && !$this->passed )
{
return $this;
}
else
{
if ( $this->checkNullCondition( $methodName, $arguments[0] ) )
{
return $this;
}
else
{
$message = array_pop( $arguments );
$this->guardMessageIsValid( $message );
$checkResult = call_user_func_array( [ $this, $checkMethod ], $arguments );
if ( !$checkResult )
{
$this->passed = false;
$this->messageCollector->addMessage( $message );
}
}
return $this;
}
}
/**
* @param string $checkMethod
*
* @throws CheckMethodNotCallable
*/
private function guardCheckMethodIsCallable( $checkMethod )
{
$checkMethod = trim( $checkMethod );
if ( $checkMethod == 'check' || !method_exists( $this, $checkMethod ) )
{
throw ( new CheckMethodNotCallable )->withMethodName( $checkMethod );
}
}
/**
* @param mixed $message
*
* @throws InvalidMessageType
*/
private function guardMessageIsValid( $message )
{
if ( !$this->messageCollector->isMessageValid( $message ) )
{
$message = sprintf(
'Invalid message type, check restriction of %s::isMessageValid.',
get_class( $this->messageCollector )
);
throw new InvalidMessageType( $message );
}
}
/**
* @param mixed $var
*
* @return mixed
*/
public function getValue( $var )
{
if ( $this->dataProvider instanceof ProvidesValuesToValidate )
{
return $this->dataProvider->getValueToValidate( $var );
}
else
{
return $var;
}
}
/**
* @param mixed $value
*
* @return bool
*/
protected function checkIsString( $value )
{
return ( new StringValidator() )->isString( $this->getValue( $value ) );
}
/**
* @param mixed $value
*
* @return bool
*/
protected function checkIsNonEmptyString( $value )
{
return ( new StringValidator() )->isNonEmptyString( $this->getValue( $value ) );
}
/**
* @param mixed $value
*
* @return bool
*/
protected function checkIsNotEmpty( $value )
{
return !empty($this->getValue( $value ));
}
/**
* @param mixed $value
*
* @return bool
*/
protected function checkIsArray( $value )
{
return is_array( $this->getValue( $value ) );
}
/**
* @param $value
*
* @return bool
*/
protected function checkIsInt( $value )
{
return ( new StringValidator() )->isInt( $this->getValue( $value ) );
}
/**
* @param mixed $value
* @param array $range
*
* @return bool
*/
protected function checkIsIntInRange( $value, array $range )
{
if ( $this->checkIsInt( $value ) )
{
$val = intval( strval( $this->getValue( $value ) ) );
return in_array( $val, $range, true );
}
else
{
return false;
}
}
/**
* @param mixed $value
* @param array $list
*
* @return bool
*/
protected function checkIsOneStringOf( $value, array $list )
{
if ( $this->checkIsString( $value ) )
{
$val = strval( $this->getValue( $value ) );
return in_array( $val, $list, true );
}
else
{
return false;
}
}
/**
* @param mixed $values
* @param array $list
*
* @return bool
*/
protected function checkIsSubsetOf( $values, array $list )
{
if ( $this->checkIsArray( $values ) )
{
$vals = $this->getValue( $values );
return (count( $vals ) > 0 && count( array_diff( $vals, $list ) ) == 0);
}
else
{
return false;
}
}
/**
* @param mixed $value
*
* @return bool
*/
protected function checkIsUuid( $value )
{
return ( new StringValidator() )->isUuid( $this->getValue( $value ) );
}
/**
* @param mixed $value1
* @param mixed $value2
*
* @return bool
*/
protected function checkIsEqual( $value1, $value2 )
{
return ($this->getValue( $value1 ) == $value2);
}
/**
* @param mixed $value1
* @param mixed $value2
*
* @return bool
*/
protected function checkIsNotEqual( $value1, $value2 )
{
return ($this->getValue( $value1 ) != $value2);
}
/**
* @param mixed $value1
* @param mixed $value2
*
* @return bool
*/
protected function checkIsSame( $value1, $value2 )
{
return ($this->getValue( $value1 ) === $value2);
}
/**
* @param mixed $value1
* @param mixed $value2
*
* @return bool
*/
protected function checkIsNotSame( $value1, $value2 )
{
return ($this->getValue( $value1 ) !== $value2);
}
/**
* @param mixed $value
*
* @return bool
*/
protected function checkIsNull( $value )
{
return ($this->getValue( $value ) === null);
}
/**
* @param mixed $value
*
* @return bool
*/
protected function checkIsNotNull( $value )
{
return ($this->getValue( $value ) !== null);
}
/**
* @param mixed $value
* @param string $regex
*
* @return bool
*/
protected function checkMatchesRegex( $value, $regex )
{
if ( $this->checkIsString( $value ) )
{
$val = strval( $this->getValue( $value ) );
return boolval( preg_match( $regex, $val ) );
}
else
{
return false;
}
}
/**
* @param mixed $value
* @param int $length
*
* @return bool
*/
protected function checkHasLength( $value, $length )
{
if ( $this->checkIsString( $value ) )
{
$val = strval( $this->getValue( $value ) );
return (mb_strlen( $val, '8bit' ) == $length);
}
else
{
return false;
}
}
/**
* @param mixed $value
* @param int $minLength
*
* @return bool
*/
protected function checkHasMinLength( $value, $minLength )
{
if ( $this->checkIsString( $value ) )
{
$val = strval( $this->getValue( $value ) );
return (mb_strlen( $val, '8bit' ) >= $minLength);
}
else
{
return false;
}
}
/**
* @param mixed $value
* @param int $maxLength
*
* @return bool
*/
protected function checkHasMaxLength( $value, $maxLength )
{
if ( $this->checkIsString( $value ) )
{
$val = strval( $this->getValue( $value ) );
return (mb_strlen( $val, '8bit' ) <= $maxLength);
}
else
{
return false;
}
}
/**
* @param mixed $value
* @param int $count
*
* @return bool
*/
protected function checkCounts( $value, $count )
{
if ( $this->checkIsArray( $value ) )
{
return (count( $this->getValue( $value ) ) == $count);
}
else
{
return false;
}
}
/**
* @param mixed $value
*
* @return bool
*/
protected function checkIsEmail( $value )
{
return ( new StringValidator() )->isEmail( $this->getValue( $value ) );
}
/**
* @param mixed $value
*
* @return bool
*/
protected function checkIsUrl( $value )
{
return ( new StringValidator() )->isUrl( $this->getValue( $value ) );
}
/**
* @param mixed $value
*
* @return bool
*/
protected function checkIsJson( $value )
{
return ( new StringValidator() )->isJson( $this->getValue( $value ) );
}
/**
* @param mixed $values
* @param mixed $key
*
* @return bool
*/
protected function checkHasKey( $values, $key )
{
if ( $this->checkIsArray( $values ) )
{
return array_key_exists( $key, $this->getValue( $values ) );
}
else
{
return false;
}
}
/**
* @param string $dateString
* @param string $format
*
* @return bool
*/
protected function checkIsDate( $dateString, $format = 'Y-m-d' )
{
$dateValue = $this->getValue( $dateString );
$dateTime = \DateTime::createFromFormat( $format, $dateValue );
return ($dateTime && ($dateTime->format( $format ) == $dateValue));
}
/**
* @param mixed $var
*
* @return bool
*/
protected function checkIsTrue( $var )
{
return $this->getValue( $var ) === true;
}
/**
* @param mixed $var
*
* @return bool
*/
protected function checkIsFalse( $var )
{
return $this->getValue( $var ) === false;
}
}