-
Notifications
You must be signed in to change notification settings - Fork 186
Expand file tree
/
Copy pathAssertion.php
More file actions
1747 lines (1568 loc) · 64.9 KB
/
Assertion.php
File metadata and controls
1747 lines (1568 loc) · 64.9 KB
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 new BSD 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 kontakt@beberlei.de so I can send you a copy immediately.
*/
namespace Assert;
use BadMethodCallException;
/**
* Assert library
*
* @author Benjamin Eberlei <kontakt@beberlei.de>
*
* METHODSTART
* @method static void nullOrEq($value, $value2, $message = null, $propertyPath = null)
* @method static void nullOrSame($value, $value2, $message = null, $propertyPath = null)
* @method static void nullOrNotEq($value1, $value2, $message = null, $propertyPath = null)
* @method static void nullOrNotSame($value1, $value2, $message = null, $propertyPath = null)
* @method static void nullOrInteger($value, $message = null, $propertyPath = null)
* @method static void nullOrFloat($value, $message = null, $propertyPath = null)
* @method static void nullOrDigit($value, $message = null, $propertyPath = null)
* @method static void nullOrDate($value, $message = null, $propertyPath = null)
* @method static void nullOrIntegerish($value, $message = null, $propertyPath = null)
* @method static void nullOrBoolean($value, $message = null, $propertyPath = null)
* @method static void nullOrScalar($value, $message = null, $propertyPath = null)
* @method static void nullOrNotEmpty($value, $message = null, $propertyPath = null)
* @method static void nullOrNoContent($value, $message = null, $propertyPath = null)
* @method static void nullOrNotNull($value, $message = null, $propertyPath = null)
* @method static void nullOrString($value, $message = null, $propertyPath = null)
* @method static void nullOrRegex($value, $pattern, $message = null, $propertyPath = null)
* @method static void nullOrLength($value, $length, $message = null, $propertyPath = null, $encoding = "utf8")
* @method static void nullOrMinLength($value, $minLength, $message = null, $propertyPath = null, $encoding = "utf8")
* @method static void nullOrMaxLength($value, $maxLength, $message = null, $propertyPath = null, $encoding = "utf8")
* @method static void nullOrBetweenLength($value, $minLength, $maxLength, $message = null, $propertyPath = null, $encoding = "utf8")
* @method static void nullOrStartsWith($string, $needle, $message = null, $propertyPath = null, $encoding = "utf8")
* @method static void nullOrEndsWith($string, $needle, $message = null, $propertyPath = null, $encoding = "utf8")
* @method static void nullOrContains($string, $needle, $message = null, $propertyPath = null, $encoding = "utf8")
* @method static void nullOrChoice($value, $choices, $message = null, $propertyPath = null)
* @method static void nullOrInArray($value, $choices, $message = null, $propertyPath = null)
* @method static void nullOrNumeric($value, $message = null, $propertyPath = null)
* @method static void nullOrIsArray($value, $message = null, $propertyPath = null)
* @method static void nullOrIsTraversable($value, $message = null, $propertyPath = null)
* @method static void nullOrIsArrayAccessible($value, $message = null, $propertyPath = null)
* @method static void nullOrKeyExists($value, $key, $message = null, $propertyPath = null)
* @method static void nullOrKeysExist($value, $keys, $message = null, $propertyPath = null)
* @method static void nullOrKeyIsset($value, $key, $message = null, $propertyPath = null)
* @method static void nullOrPropertyExists($value, $key, $message = null, $propertyPath = null)
* @method static void nullOrPropertiesExist($value, $keys, $message = null, $propertyPath = null)
* @method static void nullOrNotEmptyKey($value, $key, $message = null, $propertyPath = null)
* @method static void nullOrNotBlank($value, $message = null, $propertyPath = null)
* @method static void nullOrIsCallable($value, $message = null, $propertyPath = null)
* @method static void nullOrIsInstanceOf($value, $className, $message = null, $propertyPath = null)
* @method static void nullOrNotIsInstanceOf($value, $className, $message = null, $propertyPath = null)
* @method static void nullOrSubclassOf($value, $className, $message = null, $propertyPath = null)
* @method static void nullOrRange($value, $minValue, $maxValue, $message = null, $propertyPath = null)
* @method static void nullOrMin($value, $minValue, $message = null, $propertyPath = null)
* @method static void nullOrMax($value, $maxValue, $message = null, $propertyPath = null)
* @method static void nullOrFile($value, $message = null, $propertyPath = null)
* @method static void nullOrDirectory($value, $message = null, $propertyPath = null)
* @method static void nullOrReadable($value, $message = null, $propertyPath = null)
* @method static void nullOrWriteable($value, $message = null, $propertyPath = null)
* @method static void nullOrEmail($value, $message = null, $propertyPath = null)
* @method static void nullOrUrl($value, $message = null, $propertyPath = null)
* @method static void nullOrAlnum($value, $message = null, $propertyPath = null)
* @method static void nullOrTrue($value, $message = null, $propertyPath = null)
* @method static void nullOrFalse($value, $message = null, $propertyPath = null)
* @method static void nullOrClassExists($value, $message = null, $propertyPath = null)
* @method static void nullOrImplementsInterface($class, $interfaceName, $message = null, $propertyPath = null)
* @method static void nullOrIsJsonString($value, $message = null, $propertyPath = null)
* @method static void nullOrUuid($value, $message = null, $propertyPath = null)
* @method static void nullOrCount($countable, $count, $message = null, $propertyPath = null)
* @method static void nullOrChoicesNotEmpty($values, $choices, $message = null, $propertyPath = null)
* @method static void nullOrMethodExists($value, $object, $message = null, $propertyPath = null)
* @method static void nullOrIsObject($value, $message = null, $propertyPath = null)
* @method static void nullOrUtf8($value, $message = null, $propertyPath = null)
* @method static void allEq($value, $value2, $message = null, $propertyPath = null)
* @method static void allSame($value, $value2, $message = null, $propertyPath = null)
* @method static void allNotEq($value1, $value2, $message = null, $propertyPath = null)
* @method static void allNotSame($value1, $value2, $message = null, $propertyPath = null)
* @method static void allInteger($value, $message = null, $propertyPath = null)
* @method static void allFloat($value, $message = null, $propertyPath = null)
* @method static void allDigit($value, $message = null, $propertyPath = null)
* @method static void allDate($value, $message = null, $propertyPath = null)
* @method static void allIntegerish($value, $message = null, $propertyPath = null)
* @method static void allBoolean($value, $message = null, $propertyPath = null)
* @method static void allScalar($value, $message = null, $propertyPath = null)
* @method static void allNotEmpty($value, $message = null, $propertyPath = null)
* @method static void allNoContent($value, $message = null, $propertyPath = null)
* @method static void allNotNull($value, $message = null, $propertyPath = null)
* @method static void allString($value, $message = null, $propertyPath = null)
* @method static void allRegex($value, $pattern, $message = null, $propertyPath = null)
* @method static void allLength($value, $length, $message = null, $propertyPath = null, $encoding = "utf8")
* @method static void allMinLength($value, $minLength, $message = null, $propertyPath = null, $encoding = "utf8")
* @method static void allMaxLength($value, $maxLength, $message = null, $propertyPath = null, $encoding = "utf8")
* @method static void allBetweenLength($value, $minLength, $maxLength, $message = null, $propertyPath = null, $encoding = "utf8")
* @method static void allStartsWith($string, $needle, $message = null, $propertyPath = null, $encoding = "utf8")
* @method static void allEndsWith($string, $needle, $message = null, $propertyPath = null, $encoding = "utf8")
* @method static void allContains($string, $needle, $message = null, $propertyPath = null, $encoding = "utf8")
* @method static void allChoice($value, $choices, $message = null, $propertyPath = null)
* @method static void allInArray($value, $choices, $message = null, $propertyPath = null)
* @method static void allNumeric($value, $message = null, $propertyPath = null)
* @method static void allIsArray($value, $message = null, $propertyPath = null)
* @method static void allIsTraversable($value, $message = null, $propertyPath = null)
* @method static void allIsArrayAccessible($value, $message = null, $propertyPath = null)
* @method static void allKeyExists($value, $key, $message = null, $propertyPath = null)
* @method static void allKeysExist($value, $keys, $message = null, $propertyPath = null)
* @method static void allKeyIsset($value, $key, $message = null, $propertyPath = null)
* @method static void allPropertyExists($value, $key, $message = null, $propertyPath = null)
* @method static void allPropertiesExist($value, $keys, $message = null, $propertyPath = null)
* @method static void allNotEmptyKey($value, $key, $message = null, $propertyPath = null)
* @method static void allNotBlank($value, $message = null, $propertyPath = null)
* @method static void allIsCallable($value, $message = null, $propertyPath = null)
* @method static void allIsInstanceOf($value, $className, $message = null, $propertyPath = null)
* @method static void allNotIsInstanceOf($value, $className, $message = null, $propertyPath = null)
* @method static void allSubclassOf($value, $className, $message = null, $propertyPath = null)
* @method static void allRange($value, $minValue, $maxValue, $message = null, $propertyPath = null)
* @method static void allMin($value, $minValue, $message = null, $propertyPath = null)
* @method static void allMax($value, $maxValue, $message = null, $propertyPath = null)
* @method static void allFile($value, $message = null, $propertyPath = null)
* @method static void allDirectory($value, $message = null, $propertyPath = null)
* @method static void allReadable($value, $message = null, $propertyPath = null)
* @method static void allWriteable($value, $message = null, $propertyPath = null)
* @method static void allEmail($value, $message = null, $propertyPath = null)
* @method static void allUrl($value, $message = null, $propertyPath = null)
* @method static void allAlnum($value, $message = null, $propertyPath = null)
* @method static void allTrue($value, $message = null, $propertyPath = null)
* @method static void allFalse($value, $message = null, $propertyPath = null)
* @method static void allClassExists($value, $message = null, $propertyPath = null)
* @method static void allImplementsInterface($class, $interfaceName, $message = null, $propertyPath = null)
* @method static void allIsJsonString($value, $message = null, $propertyPath = null)
* @method static void allUuid($value, $message = null, $propertyPath = null)
* @method static void allCount($countable, $count, $message = null, $propertyPath = null)
* @method static void allChoicesNotEmpty($values, $choices, $message = null, $propertyPath = null)
* @method static void allMethodExists($value, $object, $message = null, $propertyPath = null)
* @method static void allIsObject($value, $message = null, $propertyPath = null)
* @method static void allUtf8($value, $message = null, $propertyPath = null)
* METHODEND
*/
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 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_DIRECTORY = 101;
const INVALID_FILE = 102;
const INVALID_READABLE = 103;
const INVALID_WRITEABLE = 104;
const INVALID_CLASS = 105;
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_DATE = 210;
const INVALID_CALLABLE = 211;
const INVALID_KEYS_EXIST = 300;
const INVALID_PROPERTY_EXISTS = 301;
const INVALID_PROPERTIES_EXIST = 302;
const INVALID_UTF8 = 303;
/**
* Exception to throw when an assertion failed.
*
* @var string
*/
static protected $exceptionClass = 'Assert\InvalidArgumentException';
/**
* Helper method that handles building the assertion failure exceptions.
* They are returned from this method so that the stack trace still shows
* the assertions method.
*/
protected static function createException($value, $message, $code, $propertyPath, array $constraints = array())
{
$exceptionClass = static::$exceptionClass;
return new $exceptionClass($message, $code, $propertyPath, $value, $constraints);
}
public static function setExceptionClass($exceptionClass)
{
static::$exceptionClass = $exceptionClass;
}
/**
* Assert that two values are equal (using == ).
*
* @param mixed $value
* @param mixed $value2
* @param string|null $message
* @param string|null $propertyPath
* @return void
* @throws \Assert\AssertionFailedException
*/
public static function eq($value, $value2, $message = null, $propertyPath = null)
{
if ($value != $value2) {
$message = sprintf(
$message ?: 'Value "%s" does not equal expected value "%s".',
self::stringify($value),
self::stringify($value2)
);
throw static::createException($value, $message, static::INVALID_EQ, $propertyPath, array('expected' => $value2));
}
}
/**
* Assert that two values are the same (using ===).
*
* @param mixed $value
* @param mixed $value2
* @param string|null $message
* @param string|null $propertyPath
* @return void
* @throws \Assert\AssertionFailedException
*/
public static function same($value, $value2, $message = null, $propertyPath = null)
{
if ($value !== $value2) {
$message = sprintf(
$message ?: 'Value "%s" is not the same as expected value "%s".',
self::stringify($value),
self::stringify($value2)
);
throw static::createException($value, $message, static::INVALID_SAME, $propertyPath, array('expected' => $value2));
}
}
/**
* Assert that two values are not equal (using == ).
*
* @param mixed $value1
* @param mixed $value2
* @param string|null $message
* @param string|null $propertyPath
* @return void
* @throws \Assert\AssertionFailedException
*/
public static function notEq($value1, $value2, $message = null, $propertyPath = null)
{
if ($value1 == $value2) {
$message = sprintf(
$message ?: 'Value "%s" is equal to expected value "%s".',
self::stringify($value1),
self::stringify($value2)
);
throw static::createException($value1, $message,static::INVALID_NOT_EQ, $propertyPath, array('expected' => $value2));
}
}
/**
* Assert that two values are not the same (using === ).
*
* @param mixed $value1
* @param mixed $value2
* @param string|null $message
* @param string|null $propertyPath
* @return void
* @throws \Assert\AssertionFailedException
*/
public static function notSame($value1, $value2, $message = null, $propertyPath = null)
{
if ($value1 === $value2) {
$message = sprintf(
$message ?: 'Value "%s" is the same as expected value "%s".',
self::stringify($value1),
self::stringify($value2)
);
throw static::createException($value1, $message, static::INVALID_NOT_SAME, $propertyPath, array('expected' => $value2));
}
}
/**
* Assert that value is a php integer.
*
* @param mixed $value
* @param string|null $message
* @param string|null $propertyPath
* @return void
* @throws \Assert\AssertionFailedException
*/
public static function integer($value, $message = null, $propertyPath = null)
{
if ( ! is_int($value)) {
$message = sprintf(
$message ?: 'Value "%s" is not an integer.',
self::stringify($value)
);
throw static::createException($value, $message, static::INVALID_INTEGER, $propertyPath);
}
}
/**
* Assert that value is a php float.
*
* @param mixed $value
* @param string|null $message
* @param string|null $propertyPath
* @return void
* @throws \Assert\AssertionFailedException
*/
public static function float($value, $message = null, $propertyPath = null)
{
if ( ! is_float($value)) {
$message = sprintf(
$message ?: 'Value "%s" is not a float.',
self::stringify($value)
);
throw static::createException($value, $message, static::INVALID_FLOAT, $propertyPath);
}
}
/**
* Validates if an integer or integerish is a digit.
*
* @param mixed $value
* @param string|null $message
* @param string|null $propertyPath
* @return void
* @throws \Assert\AssertionFailedException
*/
public static function digit($value, $message = null, $propertyPath = null)
{
if ( ! ctype_digit((string)$value)) {
$message = sprintf(
$message ?: 'Value "%s" is not a digit.',
self::stringify($value)
);
throw static::createException($value, $message, static::INVALID_DIGIT, $propertyPath);
}
}
/**
* Validates if an string is a date .
*
* @param string $value
* @param string|null $message
* @param string|null $propertyPath
* @return void
* @throws \Assert\AssertionFailedException
*/
public static function date($value, $message=null, $propertyPath=null)
{
if ( strtotime($value) === false )
{
$message = sprintf(
$message ?: 'Value "%s" is not a date.',
self::stringify($value)
);
throw static::createException($value, $message, static::INVALID_DATE, $propertyPath);
}
}
/**
* Assert that value is a php integer'ish.
* @param mixed $value
* @param string|null $message
* @param string|null $propertyPath
* @return void
* @throws \Assert\AssertionFailedException
*/
public static function integerish($value, $message = null, $propertyPath = null)
{
if (is_object($value) || strval(intval($value)) != $value || is_bool($value) || is_null($value)) {
$message = sprintf(
$message ?: 'Value "%s" is not an integer or a number castable to integer.',
self::stringify($value)
);
throw static::createException($value, $message, static::INVALID_INTEGERISH, $propertyPath);
}
}
/**
* Assert that value is php boolean
*
* @param mixed $value
* @param string|null $message
* @param string|null $propertyPath
* @return void
* @throws \Assert\AssertionFailedException
*/
public static function boolean($value, $message = null, $propertyPath = null)
{
if ( ! is_bool($value)) {
$message = sprintf(
$message ?: 'Value "%s" is not a boolean.',
self::stringify($value)
);
throw static::createException($value, $message, static::INVALID_BOOLEAN, $propertyPath);
}
}
/**
* Assert that value is a PHP scalar
*
* @param mixed $value
* @param string|null $message
* @param string|null $propertyPath
* @return void
* @throws \Assert\AssertionFailedException
*/
public static function scalar($value, $message = null, $propertyPath = null)
{
if (!is_scalar($value)) {
$message = sprintf(
$message ?: 'Value "%s" is not a scalar.',
self::stringify($value)
);
throw static::createException($value, $message, static::INVALID_SCALAR, $propertyPath);
}
}
/**
* Assert that value is not empty
*
* @param mixed $value
* @param string|null $message
* @param string|null $propertyPath
* @return void
* @throws \Assert\AssertionFailedException
*/
public static function notEmpty($value, $message = null, $propertyPath = null)
{
if (empty($value)) {
$message = sprintf(
$message ?: 'Value "%s" is empty, but non empty value was expected.',
self::stringify($value)
);
throw static::createException($value, $message, static::VALUE_EMPTY, $propertyPath);
}
}
/**
* Assert that value is empty
*
* @param mixed $value
* @param string|null $message
* @param string|null $propertyPath
* @return void
* @throws \Assert\AssertionFailedException
*/
public static function noContent($value, $message = null, $propertyPath = null)
{
if (!empty($value)) {
$message = sprintf(
$message ?: 'Value "%s" is not empty, but empty value was expected.',
self::stringify($value)
);
throw static::createException($value, $message, static::VALUE_NOT_EMPTY, $propertyPath);
}
}
/**
* Assert that value is not null
*
* @param mixed $value
* @param string|null $message
* @param string|null $propertyPath
* @return void
* @throws \Assert\AssertionFailedException
*/
public static function notNull($value, $message = null, $propertyPath = null)
{
if ($value === null) {
$message = sprintf(
$message ?: 'Value "%s" is null, but non null value was expected.',
self::stringify($value)
);
throw static::createException($value, $message, static::VALUE_NULL, $propertyPath);
}
}
/**
* Assert that value is a string
*
* @param mixed $value
* @param string|null $message
* @param string|null $propertyPath
* @return void
* @throws \Assert\AssertionFailedException
*/
public static function string($value, $message = null, $propertyPath = null)
{
if ( ! is_string($value)) {
$message = sprintf(
$message ?: 'Value "%s" expected to be string, type %s given.',
self::stringify($value),
gettype($value)
);
throw static::createException($value, $message, static::INVALID_STRING, $propertyPath);
}
}
/**
* Assert that value matches a regex
*
* @param mixed $value
* @param string $pattern
* @param string|null $message
* @param string|null $propertyPath
* @return void
* @throws \Assert\AssertionFailedException
*/
public static function regex($value, $pattern, $message = null, $propertyPath = null)
{
static::string($value, $message, $propertyPath);
if ( ! preg_match($pattern, $value)) {
$message = sprintf(
$message ?: 'Value "%s" does not match expression.',
self::stringify($value)
);
throw static::createException($value, $message, static::INVALID_REGEX , $propertyPath, array('pattern' => $pattern));
}
}
/**
* Assert that string has a given length.
*
* @param mixed $value
* @param int $length
* @param string|null $message
* @param string|null $propertyPath
* @param string $encoding
* @return void
* @throws \Assert\AssertionFailedException
*/
public static function length($value, $length, $message = null, $propertyPath = null, $encoding = 'utf8')
{
static::string($value, $message, $propertyPath);
if (mb_strlen($value, $encoding) !== $length) {
$message = sprintf(
$message ?: 'Value "%s" has to be %d exactly characters long, but length is %d.',
self::stringify($value),
$length,
mb_strlen($value, $encoding)
);
$constraints = array('length' => $length, 'encoding' => $encoding);
throw static::createException($value, $message, static::INVALID_LENGTH, $propertyPath, $constraints);
}
}
/**
* Assert that a string is at least $minLength chars long.
*
* @param mixed $value
* @param int $minLength
* @param string|null $message
* @param string|null $propertyPath
* @param string $encoding
* @return void
* @throws \Assert\AssertionFailedException
*/
public static function minLength($value, $minLength, $message = null, $propertyPath = null, $encoding = 'utf8')
{
static::string($value, $message, $propertyPath);
if (mb_strlen($value, $encoding) < $minLength) {
$message = sprintf(
$message ?: 'Value "%s" is too short, it should have more than %d characters, but only has %d characters.',
self::stringify($value),
$minLength,
mb_strlen($value, $encoding)
);
$constraints = array('min_length' => $minLength, 'encoding' => $encoding);
throw static::createException($value, $message, static::INVALID_MIN_LENGTH, $propertyPath, $constraints);
}
}
/**
* Assert that string value is not longer than $maxLength chars.
*
* @param mixed $value
* @param integer $maxLength
* @param string|null $message
* @param string|null $propertyPath
* @param string $encoding
* @return void
* @throws \Assert\AssertionFailedException
*/
public static function maxLength($value, $maxLength, $message = null, $propertyPath = null, $encoding = 'utf8')
{
static::string($value, $message, $propertyPath);
if (mb_strlen($value, $encoding) > $maxLength) {
$message = sprintf(
$message ?: 'Value "%s" is too long, it should have no more than %d characters, but has %d characters.',
self::stringify($value),
$maxLength,
mb_strlen($value, $encoding)
);
$constraints = array('max_length' => $maxLength, 'encoding' => $encoding);
throw static::createException($value, $message, static::INVALID_MAX_LENGTH, $propertyPath, $constraints);
}
}
/**
* Assert that string length is between min,max lengths.
*
* @param mixed $value
* @param integer $minLength
* @param integer $maxLength
* @param string|null $message
* @param string|null $propertyPath
* @param string $encoding
* @return void
* @throws \Assert\AssertionFailedException
*/
public static function betweenLength($value, $minLength, $maxLength, $message = null, $propertyPath = null, $encoding = 'utf8')
{
static::string($value, $message, $propertyPath);
if (mb_strlen($value, $encoding) < $minLength) {
$message = sprintf(
$message ?: 'Value "%s" is too short, it should have more than %d characters, but only has %d characters.',
self::stringify($value),
$minLength,
mb_strlen($value, $encoding)
);
$constraints = array('min_length' => $minLength, 'encoding' => $encoding);
throw static::createException($value, $message, static::INVALID_MIN_LENGTH, $propertyPath, $constraints);
}
if (mb_strlen($value, $encoding) > $maxLength) {
$message = sprintf(
$message ?: 'Value "%s" is too long, it should have no more than %d characters, but has %d characters.',
self::stringify($value),
$maxLength,
mb_strlen($value, $encoding)
);
$constraints = array('max_length' => $maxLength, 'encoding' => $encoding);
throw static::createException($value, $message, static::INVALID_MAX_LENGTH, $propertyPath, $constraints);
}
}
/**
* Assert that string starts with a sequence of chars.
*
* @param mixed $string
* @param string $needle
* @param string|null $message
* @param string|null $propertyPath
* @param string $encoding
* @return void
* @throws \Assert\AssertionFailedException
*/
public static function startsWith($string, $needle, $message = null, $propertyPath = null, $encoding = 'utf8')
{
static::string($string, $message, $propertyPath);
if (mb_strpos($string, $needle, null, $encoding) !== 0) {
$message = sprintf(
$message ?: 'Value "%s" does not start with "%s".',
self::stringify($string),
self::stringify($needle)
);
$constraints = array('needle' => $needle, 'encoding' => $encoding);
throw static::createException($string, $message, static::INVALID_STRING_START, $propertyPath, $constraints);
}
}
/**
* Assert that string ends with a sequence of chars.
*
* @param mixed $string
* @param string $needle
* @param string|null $message
* @param string|null $propertyPath
* @param string $encoding
* @return void
* @throws \Assert\AssertionFailedException
*/
public static function endsWith($string, $needle, $message = null, $propertyPath = null, $encoding = 'utf8')
{
static::string($string, $message, $propertyPath);
$stringPosition = mb_strlen($string, $encoding) - mb_strlen($needle, $encoding);
if (mb_strripos($string, $needle, null, $encoding) !== $stringPosition) {
$message = sprintf(
$message ?: 'Value "%s" does not end with "%s".',
self::stringify($string),
self::stringify($needle)
);
$constraints = array('needle' => $needle, 'encoding' => $encoding);
throw static::createException($string, $message, static::INVALID_STRING_END, $propertyPath, $constraints);
}
}
/**
* Assert that string contains a sequence of chars.
*
* @param mixed $string
* @param string $needle
* @param string|null $message
* @param string|null $propertyPath
* @param string $encoding
* @return void
* @throws \Assert\AssertionFailedException
*/
public static function contains($string, $needle, $message = null, $propertyPath = null, $encoding = 'utf8')
{
static::string($string, $message, $propertyPath);
if (mb_strpos($string, $needle, null, $encoding) === false) {
$message = sprintf(
$message ?: 'Value "%s" does not contain "%s".',
self::stringify($string),
self::stringify($needle)
);
$constraints = array('needle' => $needle, 'encoding' => $encoding);
throw static::createException($string, $message, static::INVALID_STRING_CONTAINS, $propertyPath, $constraints);
}
}
/**
* Assert that value is in array of choices.
*
* @param mixed $value
* @param array $choices
* @param string|null $message
* @param string|null $propertyPath
* @return void
* @throws \Assert\AssertionFailedException
*/
public static function choice($value, array $choices, $message = null, $propertyPath = null)
{
if ( ! in_array($value, $choices, true)) {
$message = sprintf(
$message ?: 'Value "%s" is not an element of the valid values: %s',
self::stringify($value),
implode(", ", array_map('Assert\Assertion::stringify', $choices))
);
throw static::createException($value, $message, static::INVALID_CHOICE, $propertyPath, array('choices' => $choices));
}
}
/**
* Alias of {@see choice()}
*
* @throws \Assert\AssertionFailedException
*/
public static function inArray($value, array $choices, $message = null, $propertyPath = null)
{
static::choice($value, $choices, $message, $propertyPath);
}
/**
* Assert that value is numeric.
*
* @param mixed $value
* @param string|null $message
* @param string|null $propertyPath
* @return void
* @throws \Assert\AssertionFailedException
*/
public static function numeric($value, $message = null, $propertyPath = null)
{
if ( ! is_numeric($value)) {
$message = sprintf(
$message ?: 'Value "%s" is not numeric.',
self::stringify($value)
);
throw static::createException($value, $message, static::INVALID_NUMERIC, $propertyPath);
}
}
/**
* Assert that value is an array.
*
* @param mixed $value
* @param string|null $message
* @param string|null $propertyPath
* @return void
* @throws \Assert\AssertionFailedException
*/
public static function isArray($value, $message = null, $propertyPath = null)
{
if ( ! is_array($value)) {
$message = sprintf(
$message ?: 'Value "%s" is not an array.',
self::stringify($value)
);
throw static::createException($value, $message, static::INVALID_ARRAY, $propertyPath);
}
}
/**
* Assert that value is an array or a traversable object.
*
* @param mixed $value
* @param string|null $message
* @param string|null $propertyPath
* @return void
* @throws \Assert\AssertionFailedException
*/
public static function isTraversable($value, $message = null, $propertyPath = null)
{
if ( ! is_array($value) && ! $value instanceof \Traversable) {
$message = sprintf(
$message ?: 'Value "%s" is not an array and does not implement Traversable.',
self::stringify($value)
);
throw static::createException($value, $message, static::INVALID_TRAVERSABLE, $propertyPath);
}
}
/**
* Assert that value is an array or an array-accessible object.
*
* @param mixed $value
* @param string|null $message
* @param string|null $propertyPath
* @return void
* @throws \Assert\AssertionFailedException
*/
public static function isArrayAccessible($value, $message = null, $propertyPath = null)
{
if ( ! is_array($value) && ! $value instanceof \ArrayAccess) {
$message = sprintf(
$message ?: 'Value "%s" is not an array and does not implement ArrayAccess.',
self::stringify($value)
);
throw static::createException($value, $message, static::INVALID_ARRAY_ACCESSIBLE, $propertyPath);
}
}
/**
* Assert that key exists in an array
*
* @param mixed $value
* @param string|integer $key
* @param string|null $message
* @param string|null $propertyPath
* @return void
* @throws \Assert\AssertionFailedException
*/
public static function keyExists($value, $key, $message = null, $propertyPath = null)
{
static::isArray($value, $message, $propertyPath);
if ( ! array_key_exists($key, $value)) {
$message = sprintf(
$message ?: 'Array does not contain an element with key "%s"',
self::stringify($key)
);
throw static::createException($value, $message, static::INVALID_KEY_EXISTS, $propertyPath, array('key' => $key));
}
}
/**
* Assert that keys exist in array
*
* @param mixed $value
* @param string|integer $key
* @param string|null $message
* @param string|null $propertyPath
* @return void
* @throws \Assert\AssertionFailedException
*/
public static function keysExist($value, $keys, $message = null, $propertyPath = null)
{
static::isArray($value, $message, $propertyPath);
foreach ( $keys as $key ) {
if ( ! array_key_exists($key, $value)) {
$message = $message ?: sprintf(
'Array does not contain an element with key "%s"',
self::stringify($key)
);
throw static::createException($value, $message, static::INVALID_KEYS_EXIST, $propertyPath, array('key' => $key));
}
}
}
/**
* Assert that property exists in array
*
* @param mixed $value
* @param string|integer $key
* @param string|null $message
* @param string|null $propertyPath
* @return void
* @throws \Assert\AssertionFailedException
*/
public static function propertyExists($value, $key, $message = null, $propertyPath = null)
{
static::isObject($value);
if ( ! property_exists($value, $key) && ! isset($value->$key) ) {
$message = $message ?: sprintf(
'Object does not contain an property with key "%s"',
self::stringify($key)
);
throw static::createException($value, $message, static::INVALID_PROPERTY_EXISTS, $propertyPath, array('key' => $key));
}
}
/**
* Assert that properties exists in array
*
* @param mixed $value
* @param array $keys
* @param string|null $message
* @param string|null $propertyPath
* @return void
* @throws \Assert\AssertionFailedException
*/
public static function propertiesExist($value, array $keys, $message = null, $propertyPath = null)
{
static::isObject($value);
foreach ($keys as $key )
{
// Using isset to allow resolution of magically defined properties
if ( ! property_exists($value, $key) && ! isset($value->$key) )
{
$message = $message ?: sprintf(
'Object does not contain an property with key "%s"',
self::stringify($key)
);
throw static::createException($value, $message, static::INVALID_PROPERTIES_EXIST, $propertyPath, array('key' => $key));
}