-
Notifications
You must be signed in to change notification settings - Fork 187
/
Assertion.php
2797 lines (2479 loc) · 118 KB
/
Assertion.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
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* Assert
*
* LICENSE
*
* This source file is subject to the MIT license that is bundled
* with this package in the file LICENSE.txt.
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to [email protected] so I can send you a copy immediately.
*/
namespace Assert;
use ArrayAccess;
use BadMethodCallException;
use Countable;
use DateTime;
use ReflectionClass;
use ReflectionException;
use ResourceBundle;
use SimpleXMLElement;
use Throwable;
use Traversable;
/**
* Assert library.
*
* @author Benjamin Eberlei <[email protected]>
*
* @method static bool allAlnum(mixed[] $value, string|callable $message = null, string $propertyPath = null) Assert that value is alphanumeric for all values.
* @method static bool allBase64(string[] $value, string|callable $message = null, string $propertyPath = null) Assert that a constant is defined for all values.
* @method static bool allBetween(mixed[] $value, mixed $lowerLimit, mixed $upperLimit, string|callable $message = null, string $propertyPath = null) Assert that a value is greater or equal than a lower limit, and less than or equal to an upper limit for all values.
* @method static bool allBetweenExclusive(mixed[] $value, mixed $lowerLimit, mixed $upperLimit, string|callable $message = null, string $propertyPath = null) Assert that a value is greater than a lower limit, and less than an upper limit for all values.
* @method static bool allBetweenLength(mixed[] $value, int $minLength, int $maxLength, string|callable $message = null, string $propertyPath = null, string $encoding = 'utf8') Assert that string length is between min and max lengths for all values.
* @method static bool allBoolean(mixed[] $value, string|callable $message = null, string $propertyPath = null) Assert that value is php boolean for all values.
* @method static bool allChoice(mixed[] $value, array $choices, string|callable $message = null, string $propertyPath = null) Assert that value is in array of choices for all values.
* @method static bool allChoicesNotEmpty(array[] $values, array $choices, string|callable $message = null, string $propertyPath = null) Determines if the values array has every choice as key and that this choice has content for all values.
* @method static bool allClassExists(mixed[] $value, string|callable $message = null, string $propertyPath = null) Assert that the class exists for all values.
* @method static bool allContains(mixed[] $string, string $needle, string|callable $message = null, string $propertyPath = null, string $encoding = 'utf8') Assert that string contains a sequence of chars for all values.
* @method static bool allCount(array[]|Countable[]|ResourceBundle[]|SimpleXMLElement[] $countable, int $count, string|callable $message = null, string $propertyPath = null) Assert that the count of countable is equal to count for all values.
* @method static bool allDate(string[] $value, string $format, string|callable $message = null, string $propertyPath = null) Assert that date is valid and corresponds to the given format for all values.
* @method static bool allDefined(mixed[] $constant, string|callable $message = null, string $propertyPath = null) Assert that a constant is defined for all values.
* @method static bool allDigit(mixed[] $value, string|callable $message = null, string $propertyPath = null) Validates if an integer or integerish is a digit for all values.
* @method static bool allDirectory(string[] $value, string|callable $message = null, string $propertyPath = null) Assert that a directory exists for all values.
* @method static bool allE164(string[] $value, string|callable $message = null, string $propertyPath = null) Assert that the given string is a valid E164 Phone Number for all values.
* @method static bool allEmail(mixed[] $value, string|callable $message = null, string $propertyPath = null) Assert that value is an email address (using input_filter/FILTER_VALIDATE_EMAIL) for all values.
* @method static bool allEndsWith(mixed[] $string, string $needle, string|callable $message = null, string $propertyPath = null, string $encoding = 'utf8') Assert that string ends with a sequence of chars for all values.
* @method static bool allEq(mixed[] $value, mixed $value2, string|callable $message = null, string $propertyPath = null) Assert that two values are equal (using ==) for all values.
* @method static bool allEqArraySubset(mixed[] $value, mixed $value2, string|callable $message = null, string $propertyPath = null) Assert that the array contains the subset for all values.
* @method static bool allExtensionLoaded(mixed[] $value, string|callable $message = null, string $propertyPath = null) Assert that extension is loaded for all values.
* @method static bool allExtensionVersion(string[] $extension, string $operator, mixed $version, string|callable $message = null, string $propertyPath = null) Assert that extension is loaded and a specific version is installed for all values.
* @method static bool allFalse(mixed[] $value, string|callable $message = null, string $propertyPath = null) Assert that the value is boolean False for all values.
* @method static bool allFile(string[] $value, string|callable $message = null, string $propertyPath = null) Assert that a file exists for all values.
* @method static bool allFloat(mixed[] $value, string|callable $message = null, string $propertyPath = null) Assert that value is a php float for all values.
* @method static bool allGreaterOrEqualThan(mixed[] $value, mixed $limit, string|callable $message = null, string $propertyPath = null) Determines if the value is greater or equal than given limit for all values.
* @method static bool allGreaterThan(mixed[] $value, mixed $limit, string|callable $message = null, string $propertyPath = null) Determines if the value is greater than given limit for all values.
* @method static bool allImplementsInterface(mixed[] $class, string $interfaceName, string|callable $message = null, string $propertyPath = null) Assert that the class implements the interface for all values.
* @method static bool allInArray(mixed[] $value, array $choices, string|callable $message = null, string $propertyPath = null) Assert that value is in array of choices. This is an alias of Assertion::choice() for all values.
* @method static bool allInteger(mixed[] $value, string|callable $message = null, string $propertyPath = null) Assert that value is a php integer for all values.
* @method static bool allIntegerish(mixed[] $value, string|callable $message = null, string $propertyPath = null) Assert that value is a php integer'ish for all values.
* @method static bool allInterfaceExists(mixed[] $value, string|callable $message = null, string $propertyPath = null) Assert that the interface exists for all values.
* @method static bool allIp(string[] $value, int $flag = null, string|callable $message = null, string $propertyPath = null) Assert that value is an IPv4 or IPv6 address for all values.
* @method static bool allIpv4(string[] $value, int $flag = null, string|callable $message = null, string $propertyPath = null) Assert that value is an IPv4 address for all values.
* @method static bool allIpv6(string[] $value, int $flag = null, string|callable $message = null, string $propertyPath = null) Assert that value is an IPv6 address for all values.
* @method static bool allIsArray(mixed[] $value, string|callable $message = null, string $propertyPath = null) Assert that value is an array for all values.
* @method static bool allIsArrayAccessible(mixed[] $value, string|callable $message = null, string $propertyPath = null) Assert that value is an array or an array-accessible object for all values.
* @method static bool allIsCallable(mixed[] $value, string|callable $message = null, string $propertyPath = null) Determines that the provided value is callable for all values.
* @method static bool allIsCountable(array[]|Countable[]|ResourceBundle[]|SimpleXMLElement[] $value, string|callable $message = null, string $propertyPath = null) Assert that value is countable for all values.
* @method static bool allIsInstanceOf(mixed[] $value, string $className, string|callable $message = null, string $propertyPath = null) Assert that value is instance of given class-name for all values.
* @method static bool allIsJsonString(mixed[] $value, string|callable $message = null, string $propertyPath = null) Assert that the given string is a valid json string for all values.
* @method static bool allIsObject(mixed[] $value, string|callable $message = null, string $propertyPath = null) Determines that the provided value is an object for all values.
* @method static bool allIsResource(mixed[] $value, string|callable $message = null, string $propertyPath = null) Assert that value is a resource for all values.
* @method static bool allIsTraversable(mixed[] $value, string|callable $message = null, string $propertyPath = null) Assert that value is an array or a traversable object for all values.
* @method static bool allKeyExists(mixed[] $value, string|int $key, string|callable $message = null, string $propertyPath = null) Assert that key exists in an array for all values.
* @method static bool allKeyIsset(mixed[] $value, string|int $key, string|callable $message = null, string $propertyPath = null) Assert that key exists in an array/array-accessible object using isset() for all values.
* @method static bool allKeyNotExists(mixed[] $value, string|int $key, string|callable $message = null, string $propertyPath = null) Assert that key does not exist in an array for all values.
* @method static bool allLength(mixed[] $value, int $length, string|callable $message = null, string $propertyPath = null, string $encoding = 'utf8') Assert that string has a given length for all values.
* @method static bool allLessOrEqualThan(mixed[] $value, mixed $limit, string|callable $message = null, string $propertyPath = null) Determines if the value is less or equal than given limit for all values.
* @method static bool allLessThan(mixed[] $value, mixed $limit, string|callable $message = null, string $propertyPath = null) Determines if the value is less than given limit for all values.
* @method static bool allMax(mixed[] $value, mixed $maxValue, string|callable $message = null, string $propertyPath = null) Assert that a number is smaller as a given limit for all values.
* @method static bool allMaxCount(array[]|Countable[]|ResourceBundle[]|SimpleXMLElement[] $countable, int $count, string|callable $message = null, string $propertyPath = null) Assert that the countable have at most $count elements for all values.
* @method static bool allMaxLength(mixed[] $value, int $maxLength, string|callable $message = null, string $propertyPath = null, string $encoding = 'utf8') Assert that string value is not longer than $maxLength chars for all values.
* @method static bool allMethodExists(string[] $value, mixed $object, string|callable $message = null, string $propertyPath = null) Determines that the named method is defined in the provided object for all values.
* @method static bool allMin(mixed[] $value, mixed $minValue, string|callable $message = null, string $propertyPath = null) Assert that a value is at least as big as a given limit for all values.
* @method static bool allMinCount(array[]|Countable[]|ResourceBundle[]|SimpleXMLElement[] $countable, int $count, string|callable $message = null, string $propertyPath = null) Assert that the countable have at least $count elements for all values.
* @method static bool allMinLength(mixed[] $value, int $minLength, string|callable $message = null, string $propertyPath = null, string $encoding = 'utf8') Assert that a string is at least $minLength chars long for all values.
* @method static bool allNoContent(mixed[] $value, string|callable $message = null, string $propertyPath = null) Assert that value is empty for all values.
* @method static bool allNotBlank(mixed[] $value, string|callable $message = null, string $propertyPath = null) Assert that value is not blank for all values.
* @method static bool allNotContains(mixed[] $string, string $needle, string|callable $message = null, string $propertyPath = null, string $encoding = 'utf8') Assert that string does not contains a sequence of chars for all values.
* @method static bool allNotEmpty(mixed[] $value, string|callable $message = null, string $propertyPath = null) Assert that value is not empty for all values.
* @method static bool allNotEmptyKey(mixed[] $value, string|int $key, string|callable $message = null, string $propertyPath = null) Assert that key exists in an array/array-accessible object and its value is not empty for all values.
* @method static bool allNotEq(mixed[] $value1, mixed $value2, string|callable $message = null, string $propertyPath = null) Assert that two values are not equal (using ==) for all values.
* @method static bool allNotInArray(mixed[] $value, array $choices, string|callable $message = null, string $propertyPath = null) Assert that value is not in array of choices for all values.
* @method static bool allNotIsInstanceOf(mixed[] $value, string $className, string|callable $message = null, string $propertyPath = null) Assert that value is not instance of given class-name for all values.
* @method static bool allNotNull(mixed[] $value, string|callable $message = null, string $propertyPath = null) Assert that value is not null for all values.
* @method static bool allNotRegex(mixed[] $value, string $pattern, string|callable $message = null, string $propertyPath = null) Assert that value does not match a regex for all values.
* @method static bool allNotSame(mixed[] $value1, mixed $value2, string|callable $message = null, string $propertyPath = null) Assert that two values are not the same (using ===) for all values.
* @method static bool allNull(mixed[] $value, string|callable $message = null, string $propertyPath = null) Assert that value is null for all values.
* @method static bool allNumeric(mixed[] $value, string|callable $message = null, string $propertyPath = null) Assert that value is numeric for all values.
* @method static bool allObjectOrClass(mixed[] $value, string|callable $message = null, string $propertyPath = null) Assert that the value is an object, or a class that exists for all values.
* @method static bool allPhpVersion(string[] $operator, mixed $version, string|callable $message = null, string $propertyPath = null) Assert on PHP version for all values.
* @method static bool allPropertiesExist(mixed[] $value, array $properties, string|callable $message = null, string $propertyPath = null) Assert that the value is an object or class, and that the properties all exist for all values.
* @method static bool allPropertyExists(mixed[] $value, string $property, string|callable $message = null, string $propertyPath = null) Assert that the value is an object or class, and that the property exists for all values.
* @method static bool allRange(mixed[] $value, mixed $minValue, mixed $maxValue, string|callable $message = null, string $propertyPath = null) Assert that value is in range of numbers for all values.
* @method static bool allReadable(string[] $value, string|callable $message = null, string $propertyPath = null) Assert that the value is something readable for all values.
* @method static bool allRegex(mixed[] $value, string $pattern, string|callable $message = null, string $propertyPath = null) Assert that value matches a regex for all values.
* @method static bool allSame(mixed[] $value, mixed $value2, string|callable $message = null, string $propertyPath = null) Assert that two values are the same (using ===) for all values.
* @method static bool allSatisfy(mixed[] $value, callable $callback, string|callable $message = null, string $propertyPath = null) Assert that the provided value is valid according to a callback for all values.
* @method static bool allScalar(mixed[] $value, string|callable $message = null, string $propertyPath = null) Assert that value is a PHP scalar for all values.
* @method static bool allStartsWith(mixed[] $string, string $needle, string|callable $message = null, string $propertyPath = null, string $encoding = 'utf8') Assert that string starts with a sequence of chars for all values.
* @method static bool allString(mixed[] $value, string|callable $message = null, string $propertyPath = null) Assert that value is a string for all values.
* @method static bool allSubclassOf(mixed[] $value, string $className, string|callable $message = null, string $propertyPath = null) Assert that value is subclass of given class-name for all values.
* @method static bool allTrue(mixed[] $value, string|callable $message = null, string $propertyPath = null) Assert that the value is boolean True for all values.
* @method static bool allUniqueValues(array[] $values, string|callable $message = null, string $propertyPath = null) Assert that values in array are unique (using strict equality) for all values.
* @method static bool allUrl(mixed[] $value, string|callable $message = null, string $propertyPath = null) Assert that value is an URL for all values.
* @method static bool allUuid(string[] $value, string|callable $message = null, string $propertyPath = null) Assert that the given string is a valid UUID for all values.
* @method static bool allVersion(string[] $version1, string $operator, string $version2, string|callable $message = null, string $propertyPath = null) Assert comparison of two versions for all values.
* @method static bool allWriteable(string[] $value, string|callable $message = null, string $propertyPath = null) Assert that the value is something writeable for all values.
* @method static bool nullOrAlnum(mixed|null $value, string|callable $message = null, string $propertyPath = null) Assert that value is alphanumeric or that the value is null.
* @method static bool nullOrBase64(string|null $value, string|callable $message = null, string $propertyPath = null) Assert that a constant is defined or that the value is null.
* @method static bool nullOrBetween(mixed|null $value, mixed $lowerLimit, mixed $upperLimit, string|callable $message = null, string $propertyPath = null) Assert that a value is greater or equal than a lower limit, and less than or equal to an upper limit or that the value is null.
* @method static bool nullOrBetweenExclusive(mixed|null $value, mixed $lowerLimit, mixed $upperLimit, string|callable $message = null, string $propertyPath = null) Assert that a value is greater than a lower limit, and less than an upper limit or that the value is null.
* @method static bool nullOrBetweenLength(mixed|null $value, int $minLength, int $maxLength, string|callable $message = null, string $propertyPath = null, string $encoding = 'utf8') Assert that string length is between min and max lengths or that the value is null.
* @method static bool nullOrBoolean(mixed|null $value, string|callable $message = null, string $propertyPath = null) Assert that value is php boolean or that the value is null.
* @method static bool nullOrChoice(mixed|null $value, array $choices, string|callable $message = null, string $propertyPath = null) Assert that value is in array of choices or that the value is null.
* @method static bool nullOrChoicesNotEmpty(array|null $values, array $choices, string|callable $message = null, string $propertyPath = null) Determines if the values array has every choice as key and that this choice has content or that the value is null.
* @method static bool nullOrClassExists(mixed|null $value, string|callable $message = null, string $propertyPath = null) Assert that the class exists or that the value is null.
* @method static bool nullOrContains(mixed|null $string, string $needle, string|callable $message = null, string $propertyPath = null, string $encoding = 'utf8') Assert that string contains a sequence of chars or that the value is null.
* @method static bool nullOrCount(array|Countable|ResourceBundle|SimpleXMLElement|null $countable, int $count, string|callable $message = null, string $propertyPath = null) Assert that the count of countable is equal to count or that the value is null.
* @method static bool nullOrDate(string|null $value, string $format, string|callable $message = null, string $propertyPath = null) Assert that date is valid and corresponds to the given format or that the value is null.
* @method static bool nullOrDefined(mixed|null $constant, string|callable $message = null, string $propertyPath = null) Assert that a constant is defined or that the value is null.
* @method static bool nullOrDigit(mixed|null $value, string|callable $message = null, string $propertyPath = null) Validates if an integer or integerish is a digit or that the value is null.
* @method static bool nullOrDirectory(string|null $value, string|callable $message = null, string $propertyPath = null) Assert that a directory exists or that the value is null.
* @method static bool nullOrE164(string|null $value, string|callable $message = null, string $propertyPath = null) Assert that the given string is a valid E164 Phone Number or that the value is null.
* @method static bool nullOrEmail(mixed|null $value, string|callable $message = null, string $propertyPath = null) Assert that value is an email address (using input_filter/FILTER_VALIDATE_EMAIL) or that the value is null.
* @method static bool nullOrEndsWith(mixed|null $string, string $needle, string|callable $message = null, string $propertyPath = null, string $encoding = 'utf8') Assert that string ends with a sequence of chars or that the value is null.
* @method static bool nullOrEq(mixed|null $value, mixed $value2, string|callable $message = null, string $propertyPath = null) Assert that two values are equal (using ==) or that the value is null.
* @method static bool nullOrEqArraySubset(mixed|null $value, mixed $value2, string|callable $message = null, string $propertyPath = null) Assert that the array contains the subset or that the value is null.
* @method static bool nullOrExtensionLoaded(mixed|null $value, string|callable $message = null, string $propertyPath = null) Assert that extension is loaded or that the value is null.
* @method static bool nullOrExtensionVersion(string|null $extension, string $operator, mixed $version, string|callable $message = null, string $propertyPath = null) Assert that extension is loaded and a specific version is installed or that the value is null.
* @method static bool nullOrFalse(mixed|null $value, string|callable $message = null, string $propertyPath = null) Assert that the value is boolean False or that the value is null.
* @method static bool nullOrFile(string|null $value, string|callable $message = null, string $propertyPath = null) Assert that a file exists or that the value is null.
* @method static bool nullOrFloat(mixed|null $value, string|callable $message = null, string $propertyPath = null) Assert that value is a php float or that the value is null.
* @method static bool nullOrGreaterOrEqualThan(mixed|null $value, mixed $limit, string|callable $message = null, string $propertyPath = null) Determines if the value is greater or equal than given limit or that the value is null.
* @method static bool nullOrGreaterThan(mixed|null $value, mixed $limit, string|callable $message = null, string $propertyPath = null) Determines if the value is greater than given limit or that the value is null.
* @method static bool nullOrImplementsInterface(mixed|null $class, string $interfaceName, string|callable $message = null, string $propertyPath = null) Assert that the class implements the interface or that the value is null.
* @method static bool nullOrInArray(mixed|null $value, array $choices, string|callable $message = null, string $propertyPath = null) Assert that value is in array of choices. This is an alias of Assertion::choice() or that the value is null.
* @method static bool nullOrInteger(mixed|null $value, string|callable $message = null, string $propertyPath = null) Assert that value is a php integer or that the value is null.
* @method static bool nullOrIntegerish(mixed|null $value, string|callable $message = null, string $propertyPath = null) Assert that value is a php integer'ish or that the value is null.
* @method static bool nullOrInterfaceExists(mixed|null $value, string|callable $message = null, string $propertyPath = null) Assert that the interface exists or that the value is null.
* @method static bool nullOrIp(string|null $value, int $flag = null, string|callable $message = null, string $propertyPath = null) Assert that value is an IPv4 or IPv6 address or that the value is null.
* @method static bool nullOrIpv4(string|null $value, int $flag = null, string|callable $message = null, string $propertyPath = null) Assert that value is an IPv4 address or that the value is null.
* @method static bool nullOrIpv6(string|null $value, int $flag = null, string|callable $message = null, string $propertyPath = null) Assert that value is an IPv6 address or that the value is null.
* @method static bool nullOrIsArray(mixed|null $value, string|callable $message = null, string $propertyPath = null) Assert that value is an array or that the value is null.
* @method static bool nullOrIsArrayAccessible(mixed|null $value, string|callable $message = null, string $propertyPath = null) Assert that value is an array or an array-accessible object or that the value is null.
* @method static bool nullOrIsCallable(mixed|null $value, string|callable $message = null, string $propertyPath = null) Determines that the provided value is callable or that the value is null.
* @method static bool nullOrIsCountable(array|Countable|ResourceBundle|SimpleXMLElement|null $value, string|callable $message = null, string $propertyPath = null) Assert that value is countable or that the value is null.
* @method static bool nullOrIsInstanceOf(mixed|null $value, string $className, string|callable $message = null, string $propertyPath = null) Assert that value is instance of given class-name or that the value is null.
* @method static bool nullOrIsJsonString(mixed|null $value, string|callable $message = null, string $propertyPath = null) Assert that the given string is a valid json string or that the value is null.
* @method static bool nullOrIsObject(mixed|null $value, string|callable $message = null, string $propertyPath = null) Determines that the provided value is an object or that the value is null.
* @method static bool nullOrIsResource(mixed|null $value, string|callable $message = null, string $propertyPath = null) Assert that value is a resource or that the value is null.
* @method static bool nullOrIsTraversable(mixed|null $value, string|callable $message = null, string $propertyPath = null) Assert that value is an array or a traversable object or that the value is null.
* @method static bool nullOrKeyExists(mixed|null $value, string|int $key, string|callable $message = null, string $propertyPath = null) Assert that key exists in an array or that the value is null.
* @method static bool nullOrKeyIsset(mixed|null $value, string|int $key, string|callable $message = null, string $propertyPath = null) Assert that key exists in an array/array-accessible object using isset() or that the value is null.
* @method static bool nullOrKeyNotExists(mixed|null $value, string|int $key, string|callable $message = null, string $propertyPath = null) Assert that key does not exist in an array or that the value is null.
* @method static bool nullOrLength(mixed|null $value, int $length, string|callable $message = null, string $propertyPath = null, string $encoding = 'utf8') Assert that string has a given length or that the value is null.
* @method static bool nullOrLessOrEqualThan(mixed|null $value, mixed $limit, string|callable $message = null, string $propertyPath = null) Determines if the value is less or equal than given limit or that the value is null.
* @method static bool nullOrLessThan(mixed|null $value, mixed $limit, string|callable $message = null, string $propertyPath = null) Determines if the value is less than given limit or that the value is null.
* @method static bool nullOrMax(mixed|null $value, mixed $maxValue, string|callable $message = null, string $propertyPath = null) Assert that a number is smaller as a given limit or that the value is null.
* @method static bool nullOrMaxCount(array|Countable|ResourceBundle|SimpleXMLElement|null $countable, int $count, string|callable $message = null, string $propertyPath = null) Assert that the countable have at most $count elements or that the value is null.
* @method static bool nullOrMaxLength(mixed|null $value, int $maxLength, string|callable $message = null, string $propertyPath = null, string $encoding = 'utf8') Assert that string value is not longer than $maxLength chars or that the value is null.
* @method static bool nullOrMethodExists(string|null $value, mixed $object, string|callable $message = null, string $propertyPath = null) Determines that the named method is defined in the provided object or that the value is null.
* @method static bool nullOrMin(mixed|null $value, mixed $minValue, string|callable $message = null, string $propertyPath = null) Assert that a value is at least as big as a given limit or that the value is null.
* @method static bool nullOrMinCount(array|Countable|ResourceBundle|SimpleXMLElement|null $countable, int $count, string|callable $message = null, string $propertyPath = null) Assert that the countable have at least $count elements or that the value is null.
* @method static bool nullOrMinLength(mixed|null $value, int $minLength, string|callable $message = null, string $propertyPath = null, string $encoding = 'utf8') Assert that a string is at least $minLength chars long or that the value is null.
* @method static bool nullOrNoContent(mixed|null $value, string|callable $message = null, string $propertyPath = null) Assert that value is empty or that the value is null.
* @method static bool nullOrNotBlank(mixed|null $value, string|callable $message = null, string $propertyPath = null) Assert that value is not blank or that the value is null.
* @method static bool nullOrNotContains(mixed|null $string, string $needle, string|callable $message = null, string $propertyPath = null, string $encoding = 'utf8') Assert that string does not contains a sequence of chars or that the value is null.
* @method static bool nullOrNotEmpty(mixed|null $value, string|callable $message = null, string $propertyPath = null) Assert that value is not empty or that the value is null.
* @method static bool nullOrNotEmptyKey(mixed|null $value, string|int $key, string|callable $message = null, string $propertyPath = null) Assert that key exists in an array/array-accessible object and its value is not empty or that the value is null.
* @method static bool nullOrNotEq(mixed|null $value1, mixed $value2, string|callable $message = null, string $propertyPath = null) Assert that two values are not equal (using ==) or that the value is null.
* @method static bool nullOrNotInArray(mixed|null $value, array $choices, string|callable $message = null, string $propertyPath = null) Assert that value is not in array of choices or that the value is null.
* @method static bool nullOrNotIsInstanceOf(mixed|null $value, string $className, string|callable $message = null, string $propertyPath = null) Assert that value is not instance of given class-name or that the value is null.
* @method static bool nullOrNotNull(mixed|null $value, string|callable $message = null, string $propertyPath = null) Assert that value is not null or that the value is null.
* @method static bool nullOrNotRegex(mixed|null $value, string $pattern, string|callable $message = null, string $propertyPath = null) Assert that value does not match a regex or that the value is null.
* @method static bool nullOrNotSame(mixed|null $value1, mixed $value2, string|callable $message = null, string $propertyPath = null) Assert that two values are not the same (using ===) or that the value is null.
* @method static bool nullOrNull(mixed|null $value, string|callable $message = null, string $propertyPath = null) Assert that value is null or that the value is null.
* @method static bool nullOrNumeric(mixed|null $value, string|callable $message = null, string $propertyPath = null) Assert that value is numeric or that the value is null.
* @method static bool nullOrObjectOrClass(mixed|null $value, string|callable $message = null, string $propertyPath = null) Assert that the value is an object, or a class that exists or that the value is null.
* @method static bool nullOrPhpVersion(string|null $operator, mixed $version, string|callable $message = null, string $propertyPath = null) Assert on PHP version or that the value is null.
* @method static bool nullOrPropertiesExist(mixed|null $value, array $properties, string|callable $message = null, string $propertyPath = null) Assert that the value is an object or class, and that the properties all exist or that the value is null.
* @method static bool nullOrPropertyExists(mixed|null $value, string $property, string|callable $message = null, string $propertyPath = null) Assert that the value is an object or class, and that the property exists or that the value is null.
* @method static bool nullOrRange(mixed|null $value, mixed $minValue, mixed $maxValue, string|callable $message = null, string $propertyPath = null) Assert that value is in range of numbers or that the value is null.
* @method static bool nullOrReadable(string|null $value, string|callable $message = null, string $propertyPath = null) Assert that the value is something readable or that the value is null.
* @method static bool nullOrRegex(mixed|null $value, string $pattern, string|callable $message = null, string $propertyPath = null) Assert that value matches a regex or that the value is null.
* @method static bool nullOrSame(mixed|null $value, mixed $value2, string|callable $message = null, string $propertyPath = null) Assert that two values are the same (using ===) or that the value is null.
* @method static bool nullOrSatisfy(mixed|null $value, callable $callback, string|callable $message = null, string $propertyPath = null) Assert that the provided value is valid according to a callback or that the value is null.
* @method static bool nullOrScalar(mixed|null $value, string|callable $message = null, string $propertyPath = null) Assert that value is a PHP scalar or that the value is null.
* @method static bool nullOrStartsWith(mixed|null $string, string $needle, string|callable $message = null, string $propertyPath = null, string $encoding = 'utf8') Assert that string starts with a sequence of chars or that the value is null.
* @method static bool nullOrString(mixed|null $value, string|callable $message = null, string $propertyPath = null) Assert that value is a string or that the value is null.
* @method static bool nullOrSubclassOf(mixed|null $value, string $className, string|callable $message = null, string $propertyPath = null) Assert that value is subclass of given class-name or that the value is null.
* @method static bool nullOrTrue(mixed|null $value, string|callable $message = null, string $propertyPath = null) Assert that the value is boolean True or that the value is null.
* @method static bool nullOrUniqueValues(array|null $values, string|callable $message = null, string $propertyPath = null) Assert that values in array are unique (using strict equality) or that the value is null.
* @method static bool nullOrUrl(mixed|null $value, string|callable $message = null, string $propertyPath = null) Assert that value is an URL or that the value is null.
* @method static bool nullOrUuid(string|null $value, string|callable $message = null, string $propertyPath = null) Assert that the given string is a valid UUID or that the value is null.
* @method static bool nullOrVersion(string|null $version1, string $operator, string $version2, string|callable $message = null, string $propertyPath = null) Assert comparison of two versions or that the value is null.
* @method static bool nullOrWriteable(string|null $value, string|callable $message = null, string $propertyPath = null) Assert that the value is something writeable or that the value is null.
*/
class Assertion
{
const INVALID_FLOAT = 9;
const INVALID_INTEGER = 10;
const INVALID_DIGIT = 11;
const INVALID_INTEGERISH = 12;
const INVALID_BOOLEAN = 13;
const VALUE_EMPTY = 14;
const VALUE_NULL = 15;
const VALUE_NOT_NULL = 25;
const INVALID_STRING = 16;
const INVALID_REGEX = 17;
const INVALID_MIN_LENGTH = 18;
const INVALID_MAX_LENGTH = 19;
const INVALID_STRING_START = 20;
const INVALID_STRING_CONTAINS = 21;
const INVALID_CHOICE = 22;
const INVALID_NUMERIC = 23;
const INVALID_ARRAY = 24;
const INVALID_KEY_EXISTS = 26;
const INVALID_NOT_BLANK = 27;
const INVALID_INSTANCE_OF = 28;
const INVALID_SUBCLASS_OF = 29;
const INVALID_RANGE = 30;
const INVALID_ALNUM = 31;
const INVALID_TRUE = 32;
const INVALID_EQ = 33;
const INVALID_SAME = 34;
const INVALID_MIN = 35;
const INVALID_MAX = 36;
const INVALID_LENGTH = 37;
const INVALID_FALSE = 38;
const INVALID_STRING_END = 39;
const INVALID_UUID = 40;
const INVALID_COUNT = 41;
const INVALID_NOT_EQ = 42;
const INVALID_NOT_SAME = 43;
const INVALID_TRAVERSABLE = 44;
const INVALID_ARRAY_ACCESSIBLE = 45;
const INVALID_KEY_ISSET = 46;
const INVALID_VALUE_IN_ARRAY = 47;
const INVALID_E164 = 48;
const INVALID_BASE64 = 49;
const INVALID_NOT_REGEX = 50;
const INVALID_DIRECTORY = 101;
const INVALID_FILE = 102;
const INVALID_READABLE = 103;
const INVALID_WRITEABLE = 104;
const INVALID_CLASS = 105;
const INVALID_INTERFACE = 106;
const INVALID_FILE_NOT_EXISTS = 107;
const INVALID_EMAIL = 201;
const INTERFACE_NOT_IMPLEMENTED = 202;
const INVALID_URL = 203;
const INVALID_NOT_INSTANCE_OF = 204;
const VALUE_NOT_EMPTY = 205;
const INVALID_JSON_STRING = 206;
const INVALID_OBJECT = 207;
const INVALID_METHOD = 208;
const INVALID_SCALAR = 209;
const INVALID_LESS = 210;
const INVALID_LESS_OR_EQUAL = 211;
const INVALID_GREATER = 212;
const INVALID_GREATER_OR_EQUAL = 213;
const INVALID_DATE = 214;
const INVALID_CALLABLE = 215;
const INVALID_KEY_NOT_EXISTS = 216;
const INVALID_SATISFY = 217;
const INVALID_IP = 218;
const INVALID_BETWEEN = 219;
const INVALID_BETWEEN_EXCLUSIVE = 220;
const INVALID_EXTENSION = 222;
const INVALID_CONSTANT = 221;
const INVALID_VERSION = 223;
const INVALID_PROPERTY = 224;
const INVALID_RESOURCE = 225;
const INVALID_COUNTABLE = 226;
const INVALID_MIN_COUNT = 227;
const INVALID_MAX_COUNT = 228;
const INVALID_STRING_NOT_CONTAINS = 229;
const INVALID_UNIQUE_VALUES = 230;
/**
* Exception to throw when an assertion failed.
*
* @var string
*/
protected static $exceptionClass = InvalidArgumentException::class;
/**
* Assert that two values are equal (using ==).
*
* @param mixed $value
* @param mixed $value2
* @param string|callable|null $message
*
* @throws AssertionFailedException
*/
public static function eq($value, $value2, $message = null, ?string $propertyPath = null): bool
{
if ($value != $value2) {
$message = \sprintf(
static::generateMessage($message ?: 'Value "%s" does not equal expected value "%s".'),
static::stringify($value),
static::stringify($value2)
);
throw static::createException($value, $message, static::INVALID_EQ, $propertyPath, ['expected' => $value2]);
}
return true;
}
/**
* Assert that the array contains the subset.
*
* @param mixed $value
* @param mixed $value2
* @param string|callable|null $message
*
* @throws AssertionFailedException
*/
public static function eqArraySubset($value, $value2, $message = null, ?string $propertyPath = null): bool
{
static::isArray($value, $message, $propertyPath);
static::isArray($value2, $message, $propertyPath);
$patched = \array_replace_recursive($value, $value2);
static::eq($patched, $value, $message, $propertyPath);
return true;
}
/**
* Assert that two values are the same (using ===).
*
* @param mixed $value
* @param mixed $value2
* @param string|callable|null $message
* @param string|null $propertyPath
*
* @psalm-template ExpectedType
* @psalm-param ExpectedType $value2
* @psalm-assert =ExpectedType $value
*
* @return bool
*
* @throws AssertionFailedException
*/
public static function same($value, $value2, $message = null, ?string $propertyPath = null): bool
{
if ($value !== $value2) {
$message = \sprintf(
static::generateMessage($message ?: 'Value "%s" is not the same as expected value "%s".'),
static::stringify($value),
static::stringify($value2)
);
throw static::createException($value, $message, static::INVALID_SAME, $propertyPath, ['expected' => $value2]);
}
return true;
}
/**
* Assert that two values are not equal (using ==).
*
* @param mixed $value1
* @param mixed $value2
* @param string|callable|null $message
*
* @throws AssertionFailedException
*/
public static function notEq($value1, $value2, $message = null, ?string $propertyPath = null): bool
{
if ($value1 == $value2) {
$message = \sprintf(
static::generateMessage($message ?: 'Value "%s" was not expected to be equal to value "%s".'),
static::stringify($value1),
static::stringify($value2)
);
throw static::createException($value1, $message, static::INVALID_NOT_EQ, $propertyPath, ['expected' => $value2]);
}
return true;
}
/**
* Assert that two values are not the same (using ===).
*
* @param mixed $value1
* @param mixed $value2
* @param string|callable|null $message
* @param string|null $propertyPath
*
* @psalm-template ExpectedType
* @psalm-param ExpectedType $value2
* @psalm-assert !=ExpectedType $value1
*
* @return bool
*
* @throws AssertionFailedException
*/
public static function notSame($value1, $value2, $message = null, ?string $propertyPath = null): bool
{
if ($value1 === $value2) {
$message = \sprintf(
static::generateMessage($message ?: 'Value "%s" was not expected to be the same as value "%s".'),
static::stringify($value1),
static::stringify($value2)
);
throw static::createException($value1, $message, static::INVALID_NOT_SAME, $propertyPath, ['expected' => $value2]);
}
return true;
}
/**
* Assert that value is not in array of choices.
*
* @param mixed $value
* @param string|callable|null $message
*
* @throws AssertionFailedException
*/
public static function notInArray($value, array $choices, $message = null, ?string $propertyPath = null): bool
{
if (true === \in_array($value, $choices)) {
$message = \sprintf(
static::generateMessage($message ?: 'Value "%s" was not expected to be an element of the values: %s'),
static::stringify($value),
static::stringify($choices)
);
throw static::createException($value, $message, static::INVALID_VALUE_IN_ARRAY, $propertyPath, ['choices' => $choices]);
}
return true;
}
/**
* Assert that value is a php integer.
*
* @param mixed $value
* @param string|callable|null $message
* @param string|null $propertyPath
*
* @psalm-assert int $value
*
* @return bool
*
* @throws AssertionFailedException
*/
public static function integer($value, $message = null, ?string $propertyPath = null): bool
{
if (!\is_int($value)) {
$message = \sprintf(
static::generateMessage($message ?: 'Value "%s" is not an integer.'),
static::stringify($value)
);
throw static::createException($value, $message, static::INVALID_INTEGER, $propertyPath);
}
return true;
}
/**
* Assert that value is a php float.
*
* @param mixed $value
* @param string|callable|null $message
* @param string|null $propertyPath
*
* @psalm-assert float $value
*
* @return bool
*
* @throws AssertionFailedException
*/
public static function float($value, $message = null, ?string $propertyPath = null): bool
{
if (!\is_float($value)) {
$message = \sprintf(
static::generateMessage($message ?: 'Value "%s" is not a float.'),
static::stringify($value)
);
throw static::createException($value, $message, static::INVALID_FLOAT, $propertyPath);
}
return true;
}
/**
* Validates if an integer or integerish is a digit.
*
* @param mixed $value
* @param string|callable|null $message
* @param string|null $propertyPath
*
* @psalm-assert =numeric $value
*
* @return bool
*
* @throws AssertionFailedException
*/
public static function digit($value, $message = null, ?string $propertyPath = null): bool
{
if (!\ctype_digit((string)$value)) {
$message = \sprintf(
static::generateMessage($message ?: 'Value "%s" is not a digit.'),
static::stringify($value)
);
throw static::createException($value, $message, static::INVALID_DIGIT, $propertyPath);
}
return true;
}
/**
* Assert that value is a php integer'ish.
*
* @param mixed $value
* @param string|callable|null $message
*
* @throws AssertionFailedException
*/
public static function integerish($value, $message = null, ?string $propertyPath = null): bool
{
if (
\is_resource($value) ||
\is_object($value) ||
\is_bool($value) ||
\is_null($value) ||
\is_array($value) ||
(\is_string($value) && '' == $value) ||
(
\strval(\intval($value)) !== \strval($value) &&
\strval(\intval($value)) !== \strval(\ltrim($value, '0')) &&
'' !== \strval(\intval($value)) &&
'' !== \strval(\ltrim($value, '0'))
)
) {
$message = \sprintf(
static::generateMessage($message ?: 'Value "%s" is not an integer or a number castable to integer.'),
static::stringify($value)
);
throw static::createException($value, $message, static::INVALID_INTEGERISH, $propertyPath);
}
return true;
}
/**
* Assert that value is php boolean.
*
* @param mixed $value
* @param string|callable|null $message
* @param string|null $propertyPath
*
* @psalm-assert bool $value
*
* @return bool
*
* @throws AssertionFailedException
*/
public static function boolean($value, $message = null, ?string $propertyPath = null): bool
{
if (!\is_bool($value)) {
$message = \sprintf(
static::generateMessage($message ?: 'Value "%s" is not a boolean.'),
static::stringify($value)
);
throw static::createException($value, $message, static::INVALID_BOOLEAN, $propertyPath);
}
return true;
}
/**
* Assert that value is a PHP scalar.
*
* @param mixed $value
* @param string|callable|null $message
* @param string|null $propertyPath
*
* @psalm-assert scalar $value
*
* @return bool
*
* @throws AssertionFailedException
*/
public static function scalar($value, $message = null, ?string $propertyPath = null): bool
{
if (!\is_scalar($value)) {
$message = \sprintf(
static::generateMessage($message ?: 'Value "%s" is not a scalar.'),
static::stringify($value)
);
throw static::createException($value, $message, static::INVALID_SCALAR, $propertyPath);
}
return true;
}
/**
* Assert that value is not empty.
*
* @param mixed $value
* @param string|callable|null $message
* @param string|null $propertyPath
*
* @psalm-assert !empty $value
*
* @return bool
*
* @throws AssertionFailedException
*/
public static function notEmpty($value, $message = null, ?string $propertyPath = null): bool
{
if (empty($value)) {
$message = \sprintf(
static::generateMessage($message ?: 'Value "%s" is empty, but non empty value was expected.'),
static::stringify($value)
);
throw static::createException($value, $message, static::VALUE_EMPTY, $propertyPath);
}
return true;
}
/**
* Assert that value is empty.
*
* @param mixed $value
* @param string|callable|null $message
* @param string|null $propertyPath
*
* @psalm-assert empty $value
*
* @return bool
*
* @throws AssertionFailedException
*/
public static function noContent($value, $message = null, ?string $propertyPath = null): bool
{
if (!empty($value)) {
$message = \sprintf(
static::generateMessage($message ?: 'Value "%s" is not empty, but empty value was expected.'),
static::stringify($value)
);
throw static::createException($value, $message, static::VALUE_NOT_EMPTY, $propertyPath);
}
return true;
}
/**
* Assert that value is null.
*
* @param mixed $value
* @param string|callable|null $message
* @param string|null $propertyPath
*
* @psalm-assert null $value
*
* @return bool
*/
public static function null($value, $message = null, ?string $propertyPath = null): bool
{
if (null !== $value) {
$message = \sprintf(
static::generateMessage($message ?: 'Value "%s" is not null, but null value was expected.'),
static::stringify($value)
);
throw static::createException($value, $message, static::VALUE_NOT_NULL, $propertyPath);
}
return true;
}
/**
* Assert that value is not null.
*
* @param mixed $value
* @param string|callable|null $message
* @param string|null $propertyPath
*
* @psalm-assert !null $value
*
* @return bool
*
* @throws AssertionFailedException
*/
public static function notNull($value, $message = null, ?string $propertyPath = null): bool
{
if (null === $value) {
$message = \sprintf(
static::generateMessage($message ?: 'Value "%s" is null, but non null value was expected.'),
static::stringify($value)
);
throw static::createException($value, $message, static::VALUE_NULL, $propertyPath);
}
return true;
}
/**
* Assert that value is a string.
*
* @param mixed $value
* @param string|callable|null $message
* @param string|null $propertyPath
*
* @psalm-assert string $value
*
* @return bool
*
* @throws AssertionFailedException
*/
public static function string($value, $message = null, ?string $propertyPath = null)
{
if (!\is_string($value)) {
$message = \sprintf(
static::generateMessage($message ?: 'Value "%s" expected to be string, type %s given.'),
static::stringify($value),
\gettype($value)
);
throw static::createException($value, $message, static::INVALID_STRING, $propertyPath);
}
return true;
}
/**
* Assert that value matches a regex.
*
* @param mixed $value
* @param string $pattern
* @param string|callable|null $message
* @param string|null $propertyPath
*
* @psalm-assert =string $value
*
* @return bool
*
* @throws AssertionFailedException
*/
public static function regex($value, $pattern, $message = null, ?string $propertyPath = null): bool
{
static::string($value, $message, $propertyPath);
if (!\preg_match($pattern, $value)) {
$message = \sprintf(
static::generateMessage($message ?: 'Value "%s" does not match expression.'),
static::stringify($value)
);
throw static::createException($value, $message, static::INVALID_REGEX, $propertyPath, ['pattern' => $pattern]);
}
return true;
}
/**
* Assert that value does not match a regex.
*
* @param mixed $value
* @param string $pattern
* @param string|callable|null $message
* @param string|null $propertyPath
*
* @psalm-assert !=string $value
*
* @throws AssertionFailedException
*/
public static function notRegex($value, $pattern, $message = null, ?string $propertyPath = null): bool
{
static::string($value, $message, $propertyPath);
if (\preg_match($pattern, $value)) {
$message = \sprintf(
static::generateMessage($message ?: 'Value "%s" matches expression.'),
static::stringify($value)
);
throw static::createException($value, $message, static::INVALID_NOT_REGEX, $propertyPath, ['pattern' => $pattern]);
}
return true;
}
/**
* Assert that string has a given length.
*
* @param mixed $value
* @param int $length
* @param string|callable|null $message
* @param string|null $propertyPath
* @param string $encoding
*
* @psalm-assert =string $value
*
* @return bool
*
* @throws AssertionFailedException
*/
public static function length($value, $length, $message = null, ?string $propertyPath = null, $encoding = 'utf8'): bool
{
static::string($value, $message, $propertyPath);
if (\mb_strlen($value, $encoding) !== $length) {
$message = \sprintf(
static::generateMessage($message ?: 'Value "%s" has to be %d exactly characters long, but length is %d.'),
static::stringify($value),
$length,
\mb_strlen($value, $encoding)
);
throw static::createException($value, $message, static::INVALID_LENGTH, $propertyPath, ['length' => $length, 'encoding' => $encoding]);
}
return true;
}
/**
* Assert that a string is at least $minLength chars long.
*
* @param mixed $value
* @param int $minLength
* @param string|callable|null $message
* @param string|null $propertyPath
* @param string $encoding
*
* @psalm-assert =string $value
*
* @return bool
*
* @throws AssertionFailedException
*/
public static function minLength($value, $minLength, $message = null, ?string $propertyPath = null, $encoding = 'utf8'): bool
{
static::string($value, $message, $propertyPath);
if (\mb_strlen($value, $encoding) < $minLength) {
$message = \sprintf(
static::generateMessage($message ?: 'Value "%s" is too short, it should have at least %d characters, but only has %d characters.'),
static::stringify($value),
$minLength,
\mb_strlen($value, $encoding)
);
throw static::createException($value, $message, static::INVALID_MIN_LENGTH, $propertyPath, ['min_length' => $minLength, 'encoding' => $encoding]);
}
return true;
}
/**
* Assert that string value is not longer than $maxLength chars.
*
* @param mixed $value
* @param int $maxLength
* @param string|callable|null $message
* @param string|null $propertyPath
* @param string $encoding
*
* @psalm-assert =string $value
*
* @return bool
*
* @throws AssertionFailedException
*/
public static function maxLength($value, $maxLength, $message = null, ?string $propertyPath = null, $encoding = 'utf8'): bool
{
static::string($value, $message, $propertyPath);
if (\mb_strlen($value, $encoding) > $maxLength) {
$message = \sprintf(
static::generateMessage($message ?: 'Value "%s" is too long, it should have no more than %d characters, but has %d characters.'),
static::stringify($value),
$maxLength,
\mb_strlen($value, $encoding)
);
throw static::createException($value, $message, static::INVALID_MAX_LENGTH, $propertyPath, ['max_length' => $maxLength, 'encoding' => $encoding]);
}
return true;
}
/**
* Assert that string length is between min and max lengths.
*
* @param mixed $value
* @param int $minLength
* @param int $maxLength
* @param string|callable|null $message
* @param string|null $propertyPath
* @param string $encoding
*
* @psalm-assert =string $value
*
* @return bool
*
* @throws AssertionFailedException
*/
public static function betweenLength($value, $minLength, $maxLength, $message = null, ?string $propertyPath = null, $encoding = 'utf8'): bool
{
static::string($value, $message, $propertyPath);
static::minLength($value, $minLength, $message, $propertyPath, $encoding);
static::maxLength($value, $maxLength, $message, $propertyPath, $encoding);
return true;
}
/**
* Assert that string starts with a sequence of chars.
*
* @param mixed $string
* @param string $needle
* @param string|callable|null $message
* @param string|null $propertyPath
* @param string $encoding
*
* @psalm-assert =string $string
*
* @return bool
*
* @throws AssertionFailedException
*/
public static function startsWith($string, $needle, $message = null, ?string $propertyPath = null, $encoding = 'utf8'): bool
{
static::string($string, $message, $propertyPath);
if (0 !== \mb_strpos($string, $needle, 0, $encoding)) {
$message = \sprintf(
static::generateMessage($message ?: 'Value "%s" does not start with "%s".'),
static::stringify($string),
static::stringify($needle)
);
throw static::createException($string, $message, static::INVALID_STRING_START, $propertyPath, ['needle' => $needle, 'encoding' => $encoding]);
}
return true;
}
/**
* Assert that string ends with a sequence of chars.
*
* @param mixed $string
* @param string $needle
* @param string|callable|null $message
* @param string|null $propertyPath
* @param string $encoding
*
* @psalm-assert =string $string
*
* @return bool
*
* @throws AssertionFailedException
*/
public static function endsWith($string, $needle, $message = null, ?string $propertyPath = null, $encoding = 'utf8'): bool
{
static::string($string, $message, $propertyPath);
$stringPosition = \mb_strlen($string, $encoding) - \mb_strlen($needle, $encoding);
if (\mb_strripos($string, $needle, 0, $encoding) !== $stringPosition) {
$message = \sprintf(
static::generateMessage($message ?: 'Value "%s" does not end with "%s".'),
static::stringify($string),
static::stringify($needle)
);
throw static::createException($string, $message, static::INVALID_STRING_END, $propertyPath, ['needle' => $needle, 'encoding' => $encoding]);
}
return true;