Complex classes like Assertion often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Assertion, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
179 | class Assertion |
||
180 | { |
||
181 | const INVALID_FLOAT = 9; |
||
182 | const INVALID_INTEGER = 10; |
||
183 | const INVALID_DIGIT = 11; |
||
184 | const INVALID_INTEGERISH = 12; |
||
185 | const INVALID_BOOLEAN = 13; |
||
186 | const VALUE_EMPTY = 14; |
||
187 | const VALUE_NULL = 15; |
||
188 | const VALUE_NOT_NULL = 25; |
||
189 | const INVALID_STRING = 16; |
||
190 | const INVALID_REGEX = 17; |
||
191 | const INVALID_MIN_LENGTH = 18; |
||
192 | const INVALID_MAX_LENGTH = 19; |
||
193 | const INVALID_STRING_START = 20; |
||
194 | const INVALID_STRING_CONTAINS = 21; |
||
195 | const INVALID_CHOICE = 22; |
||
196 | const INVALID_NUMERIC = 23; |
||
197 | const INVALID_ARRAY = 24; |
||
198 | const INVALID_KEY_EXISTS = 26; |
||
199 | const INVALID_NOT_BLANK = 27; |
||
200 | const INVALID_INSTANCE_OF = 28; |
||
201 | const INVALID_SUBCLASS_OF = 29; |
||
202 | const INVALID_RANGE = 30; |
||
203 | const INVALID_ALNUM = 31; |
||
204 | const INVALID_TRUE = 32; |
||
205 | const INVALID_EQ = 33; |
||
206 | const INVALID_SAME = 34; |
||
207 | const INVALID_MIN = 35; |
||
208 | const INVALID_MAX = 36; |
||
209 | const INVALID_LENGTH = 37; |
||
210 | const INVALID_FALSE = 38; |
||
211 | const INVALID_STRING_END = 39; |
||
212 | const INVALID_UUID = 40; |
||
213 | const INVALID_COUNT = 41; |
||
214 | const INVALID_NOT_EQ = 42; |
||
215 | const INVALID_NOT_SAME = 43; |
||
216 | const INVALID_TRAVERSABLE = 44; |
||
217 | const INVALID_ARRAY_ACCESSIBLE = 45; |
||
218 | const INVALID_KEY_ISSET = 46; |
||
219 | const INVALID_VALUE_IN_ARRAY = 47; |
||
220 | const INVALID_E164 = 48; |
||
221 | const INVALID_DIRECTORY = 101; |
||
222 | const INVALID_FILE = 102; |
||
223 | const INVALID_READABLE = 103; |
||
224 | const INVALID_WRITEABLE = 104; |
||
225 | const INVALID_CLASS = 105; |
||
226 | const INVALID_INTERFACE = 106; |
||
227 | const INVALID_EMAIL = 201; |
||
228 | const INTERFACE_NOT_IMPLEMENTED = 202; |
||
229 | const INVALID_URL = 203; |
||
230 | const INVALID_NOT_INSTANCE_OF = 204; |
||
231 | const VALUE_NOT_EMPTY = 205; |
||
232 | const INVALID_JSON_STRING = 206; |
||
233 | const INVALID_OBJECT = 207; |
||
234 | const INVALID_METHOD = 208; |
||
235 | const INVALID_SCALAR = 209; |
||
236 | const INVALID_LESS = 210; |
||
237 | const INVALID_LESS_OR_EQUAL = 211; |
||
238 | const INVALID_GREATER = 212; |
||
239 | const INVALID_GREATER_OR_EQUAL = 213; |
||
240 | const INVALID_DATE = 214; |
||
241 | const INVALID_CALLABLE = 215; |
||
242 | const INVALID_KEY_NOT_EXISTS = 216; |
||
243 | const INVALID_SATISFY = 217; |
||
244 | const INVALID_IP = 218; |
||
245 | const INVALID_BETWEEN = 219; |
||
246 | const INVALID_BETWEEN_EXCLUSIVE = 220; |
||
247 | const INVALID_EXTENSION = 222; |
||
248 | const INVALID_CONSTANT = 221; |
||
249 | const INVALID_VERSION = 223; |
||
250 | |||
251 | /** |
||
252 | * Exception to throw when an assertion failed. |
||
253 | * |
||
254 | * @var string |
||
255 | */ |
||
256 | protected static $exceptionClass = 'Assert\InvalidArgumentException'; |
||
257 | |||
258 | /** |
||
259 | * Helper method that handles building the assertion failure exceptions. |
||
260 | * They are returned from this method so that the stack trace still shows |
||
261 | * the assertions method. |
||
262 | * |
||
263 | * @param mixed $value |
||
264 | * @param string|callable $message |
||
265 | * @param int $code |
||
266 | * @param string|null $propertyPath |
||
267 | * @param array $constraints |
||
268 | * |
||
269 | * @return mixed |
||
|
|||
270 | */ |
||
271 | protected static function createException($value, $message, $code, $propertyPath = null, array $constraints = array()) |
||
277 | |||
278 | /** |
||
279 | * Assert that two values are equal (using == ). |
||
280 | * |
||
281 | * @param mixed $value |
||
282 | * @param mixed $value2 |
||
283 | * @param string|callable|null $message |
||
284 | * @param string|null $propertyPath |
||
285 | * |
||
286 | * @return bool |
||
287 | * |
||
288 | * @throws \Assert\AssertionFailedException |
||
289 | */ |
||
290 | public static function eq($value, $value2, $message = null, $propertyPath = null) |
||
304 | |||
305 | /** |
||
306 | * Assert that two values are the same (using ===). |
||
307 | * |
||
308 | * @param mixed $value |
||
309 | * @param mixed $value2 |
||
310 | * @param string|callable|null $message |
||
311 | * @param string|null $propertyPath |
||
312 | * |
||
313 | * @return bool |
||
314 | * |
||
315 | * @throws \Assert\AssertionFailedException |
||
316 | */ |
||
317 | public static function same($value, $value2, $message = null, $propertyPath = null) |
||
331 | |||
332 | /** |
||
333 | * Assert that two values are not equal (using == ). |
||
334 | * |
||
335 | * @param mixed $value1 |
||
336 | * @param mixed $value2 |
||
337 | * @param string|callable|null $message |
||
338 | * @param string|null $propertyPath |
||
339 | * |
||
340 | * @return bool |
||
341 | * |
||
342 | * @throws \Assert\AssertionFailedException |
||
343 | */ |
||
344 | public static function notEq($value1, $value2, $message = null, $propertyPath = null) |
||
357 | |||
358 | /** |
||
359 | * Assert that two values are not the same (using === ). |
||
360 | * |
||
361 | * @param mixed $value1 |
||
362 | * @param mixed $value2 |
||
363 | * @param string|callable|null $message |
||
364 | * @param string|null $propertyPath |
||
365 | * |
||
366 | * @return bool |
||
367 | * |
||
368 | * @throws \Assert\AssertionFailedException |
||
369 | */ |
||
370 | public static function notSame($value1, $value2, $message = null, $propertyPath = null) |
||
383 | |||
384 | /** |
||
385 | * Assert that value is not in array of choices. |
||
386 | * |
||
387 | * @param mixed $value |
||
388 | * @param array $choices |
||
389 | * @param string|callable|null $message |
||
390 | * @param string|null $propertyPath |
||
391 | * |
||
392 | * @return bool |
||
393 | * |
||
394 | * @throws \Assert\AssertionFailedException |
||
395 | */ |
||
396 | public static function notInArray($value, array $choices, $message = null, $propertyPath = null) |
||
409 | |||
410 | /** |
||
411 | * Assert that value is a php integer. |
||
412 | * |
||
413 | * @param mixed $value |
||
414 | * @param string|callable|null $message |
||
415 | * @param string|null $propertyPath |
||
416 | * |
||
417 | * @return bool |
||
418 | * |
||
419 | * @throws \Assert\AssertionFailedException |
||
420 | */ |
||
421 | public static function integer($value, $message = null, $propertyPath = null) |
||
434 | |||
435 | /** |
||
436 | * Assert that value is a php float. |
||
437 | * |
||
438 | * @param mixed $value |
||
439 | * @param string|callable|null $message |
||
440 | * @param string|null $propertyPath |
||
441 | * |
||
442 | * @return bool |
||
443 | * |
||
444 | * @throws \Assert\AssertionFailedException |
||
445 | */ |
||
446 | public static function float($value, $message = null, $propertyPath = null) |
||
459 | |||
460 | /** |
||
461 | * Validates if an integer or integerish is a digit. |
||
462 | * |
||
463 | * @param mixed $value |
||
464 | * @param string|callable|null $message |
||
465 | * @param string|null $propertyPath |
||
466 | * |
||
467 | * @return bool |
||
468 | * |
||
469 | * @throws \Assert\AssertionFailedException |
||
470 | */ |
||
471 | public static function digit($value, $message = null, $propertyPath = null) |
||
484 | |||
485 | /** |
||
486 | * Assert that value is a php integer'ish. |
||
487 | * |
||
488 | * @param mixed $value |
||
489 | * @param string|callable|null $message |
||
490 | * @param string|null $propertyPath |
||
491 | * |
||
492 | * @return bool |
||
493 | * |
||
494 | * @throws \Assert\AssertionFailedException |
||
495 | */ |
||
496 | public static function integerish($value, $message = null, $propertyPath = null) |
||
509 | |||
510 | /** |
||
511 | * Assert that value is php boolean. |
||
512 | * |
||
513 | * @param mixed $value |
||
514 | * @param string|callable|null $message |
||
515 | * @param string|null $propertyPath |
||
516 | * |
||
517 | * @return bool |
||
518 | * |
||
519 | * @throws \Assert\AssertionFailedException |
||
520 | */ |
||
521 | public static function boolean($value, $message = null, $propertyPath = null) |
||
534 | |||
535 | /** |
||
536 | * Assert that value is a PHP scalar. |
||
537 | * |
||
538 | * @param mixed $value |
||
539 | * @param string|callable|null $message |
||
540 | * @param string|null $propertyPath |
||
541 | * |
||
542 | * @return bool |
||
543 | * |
||
544 | * @throws \Assert\AssertionFailedException |
||
545 | */ |
||
546 | public static function scalar($value, $message = null, $propertyPath = null) |
||
559 | |||
560 | /** |
||
561 | * Assert that value is not empty. |
||
562 | * |
||
563 | * @param mixed $value |
||
564 | * @param string|callable|null $message |
||
565 | * @param string|null $propertyPath |
||
566 | * |
||
567 | * @return bool |
||
568 | * |
||
569 | * @throws \Assert\AssertionFailedException |
||
570 | */ |
||
571 | public static function notEmpty($value, $message = null, $propertyPath = null) |
||
584 | |||
585 | /** |
||
586 | * Assert that value is empty. |
||
587 | * |
||
588 | * @param mixed $value |
||
589 | * @param string|callable|null $message |
||
590 | * @param string|null $propertyPath |
||
591 | * |
||
592 | * @return bool |
||
593 | * |
||
594 | * @throws \Assert\AssertionFailedException |
||
595 | */ |
||
596 | public static function noContent($value, $message = null, $propertyPath = null) |
||
609 | |||
610 | /** |
||
611 | * Assert that value is null. |
||
612 | * |
||
613 | * @param mixed $value |
||
614 | * @param string|callable|null $message |
||
615 | * @param string|null $propertyPath |
||
616 | * |
||
617 | * @return bool |
||
618 | * |
||
619 | * @throws \Assert\AssertionFailedException |
||
620 | */ |
||
621 | public static function null($value, $message = null, $propertyPath = null) |
||
634 | |||
635 | /** |
||
636 | * Assert that value is not null. |
||
637 | * |
||
638 | * @param mixed $value |
||
639 | * @param string|callable|null $message |
||
640 | * @param string|null $propertyPath |
||
641 | * |
||
642 | * @return bool |
||
643 | * |
||
644 | * @throws \Assert\AssertionFailedException |
||
645 | */ |
||
646 | public static function notNull($value, $message = null, $propertyPath = null) |
||
659 | |||
660 | /** |
||
661 | * Assert that value is a string. |
||
662 | * |
||
663 | * @param mixed $value |
||
664 | * @param string|callable|null $message |
||
665 | * @param string|null $propertyPath |
||
666 | * |
||
667 | * @return bool |
||
668 | * |
||
669 | * @throws \Assert\AssertionFailedException |
||
670 | */ |
||
671 | public static function string($value, $message = null, $propertyPath = null) |
||
685 | |||
686 | /** |
||
687 | * Assert that value matches a regex. |
||
688 | * |
||
689 | * @param mixed $value |
||
690 | * @param string $pattern |
||
691 | * @param string|callable|null $message |
||
692 | * @param string|null $propertyPath |
||
693 | * |
||
694 | * @return bool |
||
695 | * |
||
696 | * @throws \Assert\AssertionFailedException |
||
697 | */ |
||
698 | public static function regex($value, $pattern, $message = null, $propertyPath = null) |
||
713 | |||
714 | /** |
||
715 | * Assert that string has a given length. |
||
716 | * |
||
717 | * @param mixed $value |
||
718 | * @param int $length |
||
719 | * @param string|callable|null $message |
||
720 | * @param string|null $propertyPath |
||
721 | * @param string $encoding |
||
722 | * |
||
723 | * @return bool |
||
724 | * |
||
725 | * @throws \Assert\AssertionFailedException |
||
726 | */ |
||
727 | public static function length($value, $length, $message = null, $propertyPath = null, $encoding = 'utf8') |
||
745 | |||
746 | /** |
||
747 | * Assert that a string is at least $minLength chars long. |
||
748 | * |
||
749 | * @param mixed $value |
||
750 | * @param int $minLength |
||
751 | * @param string|callable|null $message |
||
752 | * @param string|null $propertyPath |
||
753 | * @param string $encoding |
||
754 | * |
||
755 | * @return bool |
||
756 | * |
||
757 | * @throws \Assert\AssertionFailedException |
||
758 | */ |
||
759 | public static function minLength($value, $minLength, $message = null, $propertyPath = null, $encoding = 'utf8') |
||
777 | |||
778 | /** |
||
779 | * Assert that string value is not longer than $maxLength chars. |
||
780 | * |
||
781 | * @param mixed $value |
||
782 | * @param int $maxLength |
||
783 | * @param string|callable|null $message |
||
784 | * @param string|null $propertyPath |
||
785 | * @param string $encoding |
||
786 | * |
||
787 | * @return bool |
||
788 | * |
||
789 | * @throws \Assert\AssertionFailedException |
||
790 | */ |
||
791 | public static function maxLength($value, $maxLength, $message = null, $propertyPath = null, $encoding = 'utf8') |
||
809 | |||
810 | /** |
||
811 | * Assert that string length is between min,max lengths. |
||
812 | * |
||
813 | * @param mixed $value |
||
814 | * @param int $minLength |
||
815 | * @param int $maxLength |
||
816 | * @param string|callable|null $message |
||
817 | * @param string|null $propertyPath |
||
818 | * @param string $encoding |
||
819 | * |
||
820 | * @return bool |
||
821 | * |
||
822 | * @throws \Assert\AssertionFailedException |
||
823 | */ |
||
824 | public static function betweenLength($value, $minLength, $maxLength, $message = null, $propertyPath = null, $encoding = 'utf8') |
||
832 | |||
833 | /** |
||
834 | * Assert that string starts with a sequence of chars. |
||
835 | * |
||
836 | * @param mixed $string |
||
837 | * @param string $needle |
||
838 | * @param string|callable|null $message |
||
839 | * @param string|null $propertyPath |
||
840 | * @param string $encoding |
||
841 | * |
||
842 | * @return bool |
||
843 | * |
||
844 | * @throws \Assert\AssertionFailedException |
||
845 | */ |
||
846 | public static function startsWith($string, $needle, $message = null, $propertyPath = null, $encoding = 'utf8') |
||
863 | |||
864 | /** |
||
865 | * Assert that string ends with a sequence of chars. |
||
866 | * |
||
867 | * @param mixed $string |
||
868 | * @param string $needle |
||
869 | * @param string|callable|null $message |
||
870 | * @param string|null $propertyPath |
||
871 | * @param string $encoding |
||
872 | * |
||
873 | * @return bool |
||
874 | * |
||
875 | * @throws \Assert\AssertionFailedException |
||
876 | */ |
||
877 | public static function endsWith($string, $needle, $message = null, $propertyPath = null, $encoding = 'utf8') |
||
896 | |||
897 | /** |
||
898 | * Assert that string contains a sequence of chars. |
||
899 | * |
||
900 | * @param mixed $string |
||
901 | * @param string $needle |
||
902 | * @param string|callable|null $message |
||
903 | * @param string|null $propertyPath |
||
904 | * @param string $encoding |
||
905 | * |
||
906 | * @return bool |
||
907 | * |
||
908 | * @throws \Assert\AssertionFailedException |
||
909 | */ |
||
910 | public static function contains($string, $needle, $message = null, $propertyPath = null, $encoding = 'utf8') |
||
927 | |||
928 | /** |
||
929 | * Assert that value is in array of choices. |
||
930 | * |
||
931 | * @param mixed $value |
||
932 | * @param array $choices |
||
933 | * @param string|callable|null $message |
||
934 | * @param string|null $propertyPath |
||
935 | * |
||
936 | * @return bool |
||
937 | * |
||
938 | * @throws \Assert\AssertionFailedException |
||
939 | */ |
||
940 | public static function choice($value, array $choices, $message = null, $propertyPath = null) |
||
954 | |||
955 | /** |
||
956 | * Assert that value is in array of choices. |
||
957 | * |
||
958 | * This is an alias of {@see choice()}. |
||
959 | * |
||
960 | * @aliasOf choice() |
||
961 | * |
||
962 | * @param mixed $value |
||
963 | * @param array $choices |
||
964 | * @param string|callable|null $message |
||
965 | * @param string|null $propertyPath |
||
966 | * |
||
967 | * @return bool |
||
968 | */ |
||
969 | public static function inArray($value, array $choices, $message = null, $propertyPath = null) |
||
973 | |||
974 | /** |
||
975 | * Assert that value is numeric. |
||
976 | * |
||
977 | * @param mixed $value |
||
978 | * @param string|callable|null $message |
||
979 | * @param string|null $propertyPath |
||
980 | * |
||
981 | * @return bool |
||
982 | * |
||
983 | * @throws \Assert\AssertionFailedException |
||
984 | */ |
||
985 | public static function numeric($value, $message = null, $propertyPath = null) |
||
998 | |||
999 | /** |
||
1000 | * Assert that value is an array. |
||
1001 | * |
||
1002 | * @param mixed $value |
||
1003 | * @param string|callable|null $message |
||
1004 | * @param string|null $propertyPath |
||
1005 | * |
||
1006 | * @return bool |
||
1007 | * |
||
1008 | * @throws \Assert\AssertionFailedException |
||
1009 | */ |
||
1010 | public static function isArray($value, $message = null, $propertyPath = null) |
||
1023 | |||
1024 | /** |
||
1025 | * Assert that value is an array or a traversable object. |
||
1026 | * |
||
1027 | * @param mixed $value |
||
1028 | * @param string|callable|null $message |
||
1029 | * @param string|null $propertyPath |
||
1030 | * |
||
1031 | * @return bool |
||
1032 | * |
||
1033 | * @throws \Assert\AssertionFailedException |
||
1034 | */ |
||
1035 | public static function isTraversable($value, $message = null, $propertyPath = null) |
||
1048 | |||
1049 | /** |
||
1050 | * Assert that value is an array or an array-accessible object. |
||
1051 | * |
||
1052 | * @param mixed $value |
||
1053 | * @param string|callable|null $message |
||
1054 | * @param string|null $propertyPath |
||
1055 | * |
||
1056 | * @return bool |
||
1057 | * |
||
1058 | * @throws \Assert\AssertionFailedException |
||
1059 | */ |
||
1060 | public static function isArrayAccessible($value, $message = null, $propertyPath = null) |
||
1073 | |||
1074 | /** |
||
1075 | * Assert that key exists in an array. |
||
1076 | * |
||
1077 | * @param mixed $value |
||
1078 | * @param string|int $key |
||
1079 | * @param string|callable|null $message |
||
1080 | * @param string|null $propertyPath |
||
1081 | * |
||
1082 | * @return bool |
||
1083 | * |
||
1084 | * @throws \Assert\AssertionFailedException |
||
1085 | */ |
||
1086 | public static function keyExists($value, $key, $message = null, $propertyPath = null) |
||
1101 | |||
1102 | /** |
||
1103 | * Assert that key does not exist in an array. |
||
1104 | * |
||
1105 | * @param mixed $value |
||
1106 | * @param string|int $key |
||
1107 | * @param string|callable|null $message |
||
1108 | * @param string|null $propertyPath |
||
1109 | * |
||
1110 | * @return bool |
||
1111 | * |
||
1112 | * @throws \Assert\AssertionFailedException |
||
1113 | */ |
||
1114 | public static function keyNotExists($value, $key, $message = null, $propertyPath = null) |
||
1129 | |||
1130 | /** |
||
1131 | * Assert that key exists in an array/array-accessible object using isset(). |
||
1132 | * |
||
1133 | * @param mixed $value |
||
1134 | * @param string|int $key |
||
1135 | * @param string|callable|null $message |
||
1136 | * @param string|null $propertyPath |
||
1137 | * |
||
1138 | * @return bool |
||
1139 | * |
||
1140 | * @throws \Assert\AssertionFailedException |
||
1141 | */ |
||
1142 | public static function keyIsset($value, $key, $message = null, $propertyPath = null) |
||
1157 | |||
1158 | /** |
||
1159 | * Assert that key exists in an array/array-accessible object and its value is not empty. |
||
1160 | * |
||
1161 | * @param mixed $value |
||
1162 | * @param string|int $key |
||
1163 | * @param string|callable|null $message |
||
1164 | * @param string|null $propertyPath |
||
1165 | * |
||
1166 | * @return bool |
||
1167 | * |
||
1168 | * @throws \Assert\AssertionFailedException |
||
1169 | */ |
||
1170 | public static function notEmptyKey($value, $key, $message = null, $propertyPath = null) |
||
1177 | |||
1178 | /** |
||
1179 | * Assert that value is not blank. |
||
1180 | * |
||
1181 | * @param mixed $value |
||
1182 | * @param string|callable|null $message |
||
1183 | * @param string|null $propertyPath |
||
1184 | * |
||
1185 | * @return bool |
||
1186 | * |
||
1187 | * @throws \Assert\AssertionFailedException |
||
1188 | */ |
||
1189 | public static function notBlank($value, $message = null, $propertyPath = null) |
||
1202 | |||
1203 | /** |
||
1204 | * Assert that value is instance of given class-name. |
||
1205 | * |
||
1206 | * @param mixed $value |
||
1207 | * @param string $className |
||
1208 | * @param string|callable|null $message |
||
1209 | * @param string|null $propertyPath |
||
1210 | * |
||
1211 | * @return bool |
||
1212 | * |
||
1213 | * @throws \Assert\AssertionFailedException |
||
1214 | */ |
||
1215 | public static function isInstanceOf($value, $className, $message = null, $propertyPath = null) |
||
1229 | |||
1230 | /** |
||
1231 | * Assert that value is not instance of given class-name. |
||
1232 | * |
||
1233 | * @param mixed $value |
||
1234 | * @param string $className |
||
1235 | * @param string|callable|null $message |
||
1236 | * @param string|null $propertyPath |
||
1237 | * |
||
1238 | * @return bool |
||
1239 | * |
||
1240 | * @throws \Assert\AssertionFailedException |
||
1241 | */ |
||
1242 | public static function notIsInstanceOf($value, $className, $message = null, $propertyPath = null) |
||
1256 | |||
1257 | /** |
||
1258 | * Assert that value is subclass of given class-name. |
||
1259 | * |
||
1260 | * @param mixed $value |
||
1261 | * @param string $className |
||
1262 | * @param string|callable|null $message |
||
1263 | * @param string|null $propertyPath |
||
1264 | * |
||
1265 | * @return bool |
||
1266 | * |
||
1267 | * @throws \Assert\AssertionFailedException |
||
1268 | */ |
||
1269 | public static function subclassOf($value, $className, $message = null, $propertyPath = null) |
||
1283 | |||
1284 | /** |
||
1285 | * Assert that value is in range of numbers. |
||
1286 | * |
||
1287 | * @param mixed $value |
||
1288 | * @param mixed $minValue |
||
1289 | * @param mixed $maxValue |
||
1290 | * @param string|callable|null $message |
||
1291 | * @param string|null $propertyPath |
||
1292 | * |
||
1293 | * @return bool |
||
1294 | * |
||
1295 | * @throws \Assert\AssertionFailedException |
||
1296 | */ |
||
1297 | public static function range($value, $minValue, $maxValue, $message = null, $propertyPath = null) |
||
1314 | |||
1315 | /** |
||
1316 | * Assert that a value is at least as big as a given limit. |
||
1317 | * |
||
1318 | * @param mixed $value |
||
1319 | * @param mixed $minValue |
||
1320 | * @param string|callable|null $message |
||
1321 | * @param string|null $propertyPath |
||
1322 | * |
||
1323 | * @return bool |
||
1324 | * |
||
1325 | * @throws \Assert\AssertionFailedException |
||
1326 | */ |
||
1327 | public static function min($value, $minValue, $message = null, $propertyPath = null) |
||
1343 | |||
1344 | /** |
||
1345 | * Assert that a number is smaller as a given limit. |
||
1346 | * |
||
1347 | * @param mixed $value |
||
1348 | * @param mixed $maxValue |
||
1349 | * @param string|callable|null $message |
||
1350 | * @param string|null $propertyPath |
||
1351 | * |
||
1352 | * @return bool |
||
1353 | * |
||
1354 | * @throws \Assert\AssertionFailedException |
||
1355 | */ |
||
1356 | public static function max($value, $maxValue, $message = null, $propertyPath = null) |
||
1372 | |||
1373 | /** |
||
1374 | * Assert that a file exists. |
||
1375 | * |
||
1376 | * @param string $value |
||
1377 | * @param string|callable|null $message |
||
1378 | * @param string|null $propertyPath |
||
1379 | * |
||
1380 | * @return bool |
||
1381 | * |
||
1382 | * @throws \Assert\AssertionFailedException |
||
1383 | */ |
||
1384 | public static function file($value, $message = null, $propertyPath = null) |
||
1400 | |||
1401 | /** |
||
1402 | * Assert that a directory exists. |
||
1403 | * |
||
1404 | * @param string $value |
||
1405 | * @param string|callable|null $message |
||
1406 | * @param string|null $propertyPath |
||
1407 | * |
||
1408 | * @return bool |
||
1409 | * |
||
1410 | * @throws \Assert\AssertionFailedException |
||
1411 | */ |
||
1412 | public static function directory($value, $message = null, $propertyPath = null) |
||
1427 | |||
1428 | /** |
||
1429 | * Assert that the value is something readable. |
||
1430 | * |
||
1431 | * @param string $value |
||
1432 | * @param string|callable|null $message |
||
1433 | * @param string|null $propertyPath |
||
1434 | * |
||
1435 | * @return bool |
||
1436 | * |
||
1437 | * @throws \Assert\AssertionFailedException |
||
1438 | */ |
||
1439 | public static function readable($value, $message = null, $propertyPath = null) |
||
1454 | |||
1455 | /** |
||
1456 | * Assert that the value is something writeable. |
||
1457 | * |
||
1458 | * @param string $value |
||
1459 | * @param string|callable|null $message |
||
1460 | * @param string|null $propertyPath |
||
1461 | * |
||
1462 | * @return bool |
||
1463 | * |
||
1464 | * @throws \Assert\AssertionFailedException |
||
1465 | */ |
||
1466 | public static function writeable($value, $message = null, $propertyPath = null) |
||
1481 | |||
1482 | /** |
||
1483 | * Assert that value is an email adress (using input_filter/FILTER_VALIDATE_EMAIL). |
||
1484 | * |
||
1485 | * @param mixed $value |
||
1486 | * @param string|callable|null $message |
||
1487 | * @param string|null $propertyPath |
||
1488 | * |
||
1489 | * @return bool |
||
1490 | * |
||
1491 | * @throws \Assert\AssertionFailedException |
||
1492 | */ |
||
1493 | public static function email($value, $message = null, $propertyPath = null) |
||
1520 | |||
1521 | /** |
||
1522 | * Assert that value is an URL. |
||
1523 | * |
||
1524 | * This code snipped was taken from the Symfony project and modified to the special demands of this method. |
||
1525 | * |
||
1526 | * @param mixed $value |
||
1527 | * @param string|callable|null $message |
||
1528 | * @param string|null $propertyPath |
||
1529 | * |
||
1530 | * @return bool |
||
1531 | * |
||
1532 | * @throws \Assert\AssertionFailedException |
||
1533 | * |
||
1534 | * @see https://github.com/symfony/Validator/blob/master/Constraints/UrlValidator.php |
||
1535 | * @see https://github.com/symfony/Validator/blob/master/Constraints/Url.php |
||
1536 | */ |
||
1537 | public static function url($value, $message = null, $propertyPath = null) |
||
1572 | |||
1573 | /** |
||
1574 | * Assert that value is alphanumeric. |
||
1575 | * |
||
1576 | * @param mixed $value |
||
1577 | * @param string|callable|null $message |
||
1578 | * @param string|null $propertyPath |
||
1579 | * |
||
1580 | * @return bool |
||
1581 | * |
||
1582 | * @throws \Assert\AssertionFailedException |
||
1583 | */ |
||
1584 | public static function alnum($value, $message = null, $propertyPath = null) |
||
1599 | |||
1600 | /** |
||
1601 | * Assert that the value is boolean True. |
||
1602 | * |
||
1603 | * @param mixed $value |
||
1604 | * @param string|callable|null $message |
||
1605 | * @param string|null $propertyPath |
||
1606 | * |
||
1607 | * @return bool |
||
1608 | * |
||
1609 | * @throws \Assert\AssertionFailedException |
||
1610 | */ |
||
1611 | public static function true($value, $message = null, $propertyPath = null) |
||
1624 | |||
1625 | /** |
||
1626 | * Assert that the value is boolean False. |
||
1627 | * |
||
1628 | * @param mixed $value |
||
1629 | * @param string|callable|null $message |
||
1630 | * @param string|null $propertyPath |
||
1631 | * |
||
1632 | * @return bool |
||
1633 | * |
||
1634 | * @throws \Assert\AssertionFailedException |
||
1635 | */ |
||
1636 | public static function false($value, $message = null, $propertyPath = null) |
||
1649 | |||
1650 | /** |
||
1651 | * Assert that the class exists. |
||
1652 | * |
||
1653 | * @param mixed $value |
||
1654 | * @param string|callable|null $message |
||
1655 | * @param string|null $propertyPath |
||
1656 | * |
||
1657 | * @return bool |
||
1658 | * |
||
1659 | * @throws \Assert\AssertionFailedException |
||
1660 | */ |
||
1661 | public static function classExists($value, $message = null, $propertyPath = null) |
||
1674 | |||
1675 | /** |
||
1676 | * Assert that the interface exists. |
||
1677 | * |
||
1678 | * @param mixed $value |
||
1679 | * @param string|callable|null $message |
||
1680 | * @param string|null $propertyPath |
||
1681 | * |
||
1682 | * @return bool |
||
1683 | * |
||
1684 | * @throws \Assert\AssertionFailedException |
||
1685 | */ |
||
1686 | public static function interfaceExists($value, $message = null, $propertyPath = null) |
||
1699 | |||
1700 | /** |
||
1701 | * Assert that the class implements the interface. |
||
1702 | * |
||
1703 | * @param mixed $class |
||
1704 | * @param string $interfaceName |
||
1705 | * @param string|callable|null $message |
||
1706 | * @param string|null $propertyPath |
||
1707 | * |
||
1708 | * @return bool |
||
1709 | * |
||
1710 | * @throws \Assert\AssertionFailedException |
||
1711 | */ |
||
1712 | public static function implementsInterface($class, $interfaceName, $message = null, $propertyPath = null) |
||
1727 | |||
1728 | /** |
||
1729 | * Assert that the given string is a valid json string. |
||
1730 | * |
||
1731 | * NOTICE: |
||
1732 | * Since this does a json_decode to determine its validity |
||
1733 | * you probably should consider, when using the variable |
||
1734 | * content afterwards, just to decode and check for yourself instead |
||
1735 | * of using this assertion. |
||
1736 | * |
||
1737 | * @param mixed $value |
||
1738 | * @param string|callable|null $message |
||
1739 | * @param string|null $propertyPath |
||
1740 | * |
||
1741 | * @return bool |
||
1742 | * |
||
1743 | * @throws \Assert\AssertionFailedException |
||
1744 | */ |
||
1745 | public static function isJsonString($value, $message = null, $propertyPath = null) |
||
1758 | |||
1759 | /** |
||
1760 | * Assert that the given string is a valid UUID. |
||
1761 | * |
||
1762 | * Uses code from {@link https://github.com/ramsey/uuid} that is MIT licensed. |
||
1763 | * |
||
1764 | * @param string $value |
||
1765 | * @param string|callable|null $message |
||
1766 | * @param string|null $propertyPath |
||
1767 | * |
||
1768 | * @return bool |
||
1769 | * |
||
1770 | * @throws \Assert\AssertionFailedException |
||
1771 | */ |
||
1772 | public static function uuid($value, $message = null, $propertyPath = null) |
||
1791 | |||
1792 | /** |
||
1793 | * Assert that the given string is a valid E164 Phone Number. |
||
1794 | * |
||
1795 | * @see https://en.wikipedia.org/wiki/E.164 |
||
1796 | * |
||
1797 | * @param string $value |
||
1798 | * @param string|callable|null $message |
||
1799 | * @param string|null $propertyPath |
||
1800 | * |
||
1801 | * @return bool |
||
1802 | * |
||
1803 | * @throws \Assert\AssertionFailedException |
||
1804 | */ |
||
1805 | public static function e164($value, $message = null, $propertyPath = null) |
||
1818 | |||
1819 | /** |
||
1820 | * Assert that the count of countable is equal to count. |
||
1821 | * |
||
1822 | * @param array|\Countable $countable |
||
1823 | * @param int $count |
||
1824 | * @param string|null $message |
||
1825 | * @param string|null $propertyPath |
||
1826 | * |
||
1827 | * @return bool |
||
1828 | * |
||
1829 | * @throws \Assert\AssertionFailedException |
||
1830 | */ |
||
1831 | public static function count($countable, $count, $message = null, $propertyPath = null) |
||
1844 | |||
1845 | /** |
||
1846 | * static call handler to implement: |
||
1847 | * - "null or assertion" delegation |
||
1848 | * - "all" delegation. |
||
1849 | * |
||
1850 | * @param string $method |
||
1851 | * @param array $args |
||
1852 | * |
||
1853 | * @return bool|mixed |
||
1854 | */ |
||
1855 | public static function __callStatic($method, $args) |
||
1891 | |||
1892 | /** |
||
1893 | * Determines if the values array has every choice as key and that this choice has content. |
||
1894 | * |
||
1895 | * @param array $values |
||
1896 | * @param array $choices |
||
1897 | * @param string|callable|null $message |
||
1898 | * @param string|null $propertyPath |
||
1899 | * |
||
1900 | * @return bool |
||
1901 | */ |
||
1902 | public static function choicesNotEmpty(array $values, array $choices, $message = null, $propertyPath = null) |
||
1912 | |||
1913 | /** |
||
1914 | * Determines that the named method is defined in the provided object. |
||
1915 | * |
||
1916 | * @param string $value |
||
1917 | * @param mixed $object |
||
1918 | * @param string|callable|null $message |
||
1919 | * @param string|null $propertyPath |
||
1920 | * |
||
1921 | * @return bool |
||
1922 | * |
||
1923 | * @throws |
||
1924 | */ |
||
1925 | public static function methodExists($value, $object, $message = null, $propertyPath = null) |
||
1940 | |||
1941 | /** |
||
1942 | * Determines that the provided value is an object. |
||
1943 | * |
||
1944 | * @param mixed $value |
||
1945 | * @param string|callable|null $message |
||
1946 | * @param string|null $propertyPath |
||
1947 | * |
||
1948 | * @return bool |
||
1949 | */ |
||
1950 | public static function isObject($value, $message = null, $propertyPath = null) |
||
1963 | |||
1964 | /** |
||
1965 | * Determines if the value is less than given limit. |
||
1966 | * |
||
1967 | * @param mixed $value |
||
1968 | * @param mixed $limit |
||
1969 | * @param string|callable|null $message |
||
1970 | * @param string|null $propertyPath |
||
1971 | * |
||
1972 | * @return bool |
||
1973 | */ |
||
1974 | public static function lessThan($value, $limit, $message = null, $propertyPath = null) |
||
1988 | |||
1989 | /** |
||
1990 | * Determines if the value is less or than given limit. |
||
1991 | * |
||
1992 | * @param mixed $value |
||
1993 | * @param mixed $limit |
||
1994 | * @param string|callable|null $message |
||
1995 | * @param string|null $propertyPath |
||
1996 | * |
||
1997 | * @return bool |
||
1998 | */ |
||
1999 | public static function lessOrEqualThan($value, $limit, $message = null, $propertyPath = null) |
||
2013 | |||
2014 | /** |
||
2015 | * Determines if the value is greater than given limit. |
||
2016 | * |
||
2017 | * @param mixed $value |
||
2018 | * @param mixed $limit |
||
2019 | * @param string|callable|null $message |
||
2020 | * @param string|null $propertyPath |
||
2021 | * |
||
2022 | * @return bool |
||
2023 | */ |
||
2024 | public static function greaterThan($value, $limit, $message = null, $propertyPath = null) |
||
2038 | |||
2039 | /** |
||
2040 | * Determines if the value is greater or equal than given limit. |
||
2041 | * |
||
2042 | * @param mixed $value |
||
2043 | * @param mixed $limit |
||
2044 | * @param string|callable|null $message |
||
2045 | * @param string|null $propertyPath |
||
2046 | * |
||
2047 | * @return bool |
||
2048 | */ |
||
2049 | public static function greaterOrEqualThan($value, $limit, $message = null, $propertyPath = null) |
||
2063 | |||
2064 | /** |
||
2065 | * Assert that a value is greater or equal than a lower limit, and less than or equal to an upper limit. |
||
2066 | * |
||
2067 | * @param mixed $value |
||
2068 | * @param mixed $lowerLimit |
||
2069 | * @param mixed $upperLimit |
||
2070 | * @param string $message |
||
2071 | * @param string $propertyPath |
||
2072 | * |
||
2073 | * @return bool |
||
2074 | */ |
||
2075 | public static function between($value, $lowerLimit, $upperLimit, $message = null, $propertyPath = null) |
||
2090 | |||
2091 | /** |
||
2092 | * Assert that a value is greater than a lower limit, and less than an upper limit. |
||
2093 | * |
||
2094 | * @param mixed $value |
||
2095 | * @param mixed $lowerLimit |
||
2096 | * @param mixed $upperLimit |
||
2097 | * @param string $message |
||
2098 | * @param string $propertyPath |
||
2099 | * |
||
2100 | * @return bool |
||
2101 | */ |
||
2102 | public static function betweenExclusive($value, $lowerLimit, $upperLimit, $message = null, $propertyPath = null) |
||
2117 | |||
2118 | /** |
||
2119 | * Assert that extension is loaded. |
||
2120 | * |
||
2121 | * @param mixed $value |
||
2122 | * @param string|callable|null $message |
||
2123 | * @param string|null $propertyPath |
||
2124 | * |
||
2125 | * @return bool |
||
2126 | * |
||
2127 | * @throws \Assert\AssertionFailedException |
||
2128 | */ |
||
2129 | public static function extensionLoaded($value, $message = null, $propertyPath = null) |
||
2142 | |||
2143 | /** |
||
2144 | * Assert that date is valid and corresponds to the given format. |
||
2145 | * |
||
2146 | * @param string $value |
||
2147 | * @param string $format supports all of the options date(), except for the following: |
||
2148 | * N, w, W, t, L, o, B, a, A, g, h, I, O, P, Z, c, r |
||
2149 | * @param string|callable|null $message |
||
2150 | * @param string|null $propertyPath |
||
2151 | * |
||
2152 | * @return bool |
||
2153 | * |
||
2154 | * @see http://php.net/manual/function.date.php#refsect1-function.date-parameters |
||
2155 | */ |
||
2156 | public static function date($value, $format, $message = null, $propertyPath = null) |
||
2175 | |||
2176 | /** |
||
2177 | * Assert comparison of two versions. |
||
2178 | * |
||
2179 | * @param string $version1 |
||
2180 | * @param string $operator |
||
2181 | * @param string $version2 |
||
2182 | * @param string|callable|null $message |
||
2183 | * @param string|null $propertyPath |
||
2184 | * |
||
2185 | * @return bool |
||
2186 | * |
||
2187 | * @throws \Assert\AssertionFailedException |
||
2188 | */ |
||
2189 | public static function version($version1, $operator, $version2, $message = null, $propertyPath = null) |
||
2206 | |||
2207 | /** |
||
2208 | * Assert on PHP version. |
||
2209 | * |
||
2210 | * @param string $operator |
||
2211 | * @param mixed $version |
||
2212 | * @param string|callable|null $message |
||
2213 | * @param string|null $propertyPath |
||
2214 | * |
||
2215 | * @return bool |
||
2216 | * |
||
2217 | * @throws \Assert\AssertionFailedException |
||
2218 | */ |
||
2219 | public static function phpVersion($operator, $version, $message = null, $propertyPath = null) |
||
2225 | |||
2226 | /** |
||
2227 | * Assert that extension is loaded and a specific version is installed. |
||
2228 | * |
||
2229 | * @param string $extension |
||
2230 | * @param string $operator |
||
2231 | * @param mixed $version |
||
2232 | * @param string|callable|null $message |
||
2233 | * @param string|null $propertyPath |
||
2234 | * |
||
2235 | * @return bool |
||
2236 | * |
||
2237 | * @throws \Assert\AssertionFailedException |
||
2238 | */ |
||
2239 | public static function extensionVersion($extension, $operator, $version, $message = null, $propertyPath = null) |
||
2245 | |||
2246 | /** |
||
2247 | * Determines that the provided value is callable. |
||
2248 | * |
||
2249 | * @param mixed $value |
||
2250 | * @param string|callable|null $message |
||
2251 | * @param string|null $propertyPath |
||
2252 | * |
||
2253 | * @return bool |
||
2254 | */ |
||
2255 | public static function isCallable($value, $message = null, $propertyPath = null) |
||
2268 | |||
2269 | /** |
||
2270 | * Assert that the provided value is valid according to a callback. |
||
2271 | * |
||
2272 | * If the callback returns `false` the assertion will fail. |
||
2273 | * |
||
2274 | * @param mixed $value |
||
2275 | * @param callable $callback |
||
2276 | * @param string|callable|null $message |
||
2277 | * @param string|null $propertyPath |
||
2278 | * |
||
2279 | * @return bool |
||
2280 | */ |
||
2281 | public static function satisfy($value, $callback, $message = null, $propertyPath = null) |
||
2296 | |||
2297 | /** |
||
2298 | * Assert that value is an IPv4 or IPv6 address |
||
2299 | * (using input_filter/FILTER_VALIDATE_IP). |
||
2300 | * |
||
2301 | * @param string $value |
||
2302 | * @param null|int $flag |
||
2303 | * @param string|callable|null $message |
||
2304 | * @param string|null $propertyPath |
||
2305 | * |
||
2306 | * @return bool |
||
2307 | * |
||
2308 | * @see http://php.net/manual/filter.filters.flags.php |
||
2309 | */ |
||
2310 | public static function ip($value, $flag = null, $message = null, $propertyPath = null) |
||
2323 | |||
2324 | /** |
||
2325 | * Assert that value is an IPv4 address |
||
2326 | * (using input_filter/FILTER_VALIDATE_IP). |
||
2327 | * |
||
2328 | * @param string $value |
||
2329 | * @param null|int $flag |
||
2330 | * @param string|callable|null $message |
||
2331 | * @param string|null $propertyPath |
||
2332 | * |
||
2333 | * @return bool |
||
2334 | * |
||
2335 | * @see http://php.net/manual/filter.filters.flags.php |
||
2336 | */ |
||
2337 | public static function ipv4($value, $flag = null, $message = null, $propertyPath = null) |
||
2343 | |||
2344 | /** |
||
2345 | * Assert that value is an IPv6 address |
||
2346 | * (using input_filter/FILTER_VALIDATE_IP). |
||
2347 | * |
||
2348 | * @param string $value |
||
2349 | * @param null|int $flag |
||
2350 | * @param string|callable|null $message |
||
2351 | * @param string|null $propertyPath |
||
2352 | * |
||
2353 | * @return bool |
||
2354 | * |
||
2355 | * @see http://php.net/manual/filter.filters.flags.php |
||
2356 | */ |
||
2357 | public static function ipv6($value, $flag = null, $message = null, $propertyPath = null) |
||
2363 | |||
2364 | /** |
||
2365 | * Make a string version of a value. |
||
2366 | * |
||
2367 | * @param mixed $value |
||
2368 | * |
||
2369 | * @return string |
||
2370 | */ |
||
2371 | protected static function stringify($value) |
||
2405 | |||
2406 | /** |
||
2407 | * Assert that a constant is defined. |
||
2408 | * |
||
2409 | * @param mixed $constant |
||
2410 | * @param string|callable|null $message |
||
2411 | * @param string|null $propertyPath |
||
2412 | * |
||
2413 | * @return bool |
||
2414 | * |
||
2415 | * @throws \Assert\AssertionFailedException |
||
2416 | */ |
||
2417 | public static function defined($constant, $message = null, $propertyPath = null) |
||
2427 | |||
2428 | /** |
||
2429 | * Generate the message. |
||
2430 | * |
||
2431 | * @param string|callable|null $message |
||
2432 | * |
||
2433 | * @return string |
||
2434 | */ |
||
2435 | protected static function generateMessage($message = null) |
||
2457 | } |
||
2458 |
This check looks for the generic type
array
as a return type and suggests a more specific type. This type is inferred from the actual code.