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 |
||
160 | class Assertion |
||
161 | { |
||
162 | const INVALID_FLOAT = 9; |
||
163 | const INVALID_INTEGER = 10; |
||
164 | const INVALID_DIGIT = 11; |
||
165 | const INVALID_INTEGERISH = 12; |
||
166 | const INVALID_BOOLEAN = 13; |
||
167 | const VALUE_EMPTY = 14; |
||
168 | const VALUE_NULL = 15; |
||
169 | const INVALID_STRING = 16; |
||
170 | const INVALID_REGEX = 17; |
||
171 | const INVALID_MIN_LENGTH = 18; |
||
172 | const INVALID_MAX_LENGTH = 19; |
||
173 | const INVALID_STRING_START = 20; |
||
174 | const INVALID_STRING_CONTAINS = 21; |
||
175 | const INVALID_CHOICE = 22; |
||
176 | const INVALID_NUMERIC = 23; |
||
177 | const INVALID_ARRAY = 24; |
||
178 | const INVALID_KEY_EXISTS = 26; |
||
179 | const INVALID_NOT_BLANK = 27; |
||
180 | const INVALID_INSTANCE_OF = 28; |
||
181 | const INVALID_SUBCLASS_OF = 29; |
||
182 | const INVALID_RANGE = 30; |
||
183 | const INVALID_ALNUM = 31; |
||
184 | const INVALID_TRUE = 32; |
||
185 | const INVALID_EQ = 33; |
||
186 | const INVALID_SAME = 34; |
||
187 | const INVALID_MIN = 35; |
||
188 | const INVALID_MAX = 36; |
||
189 | const INVALID_LENGTH = 37; |
||
190 | const INVALID_FALSE = 38; |
||
191 | const INVALID_STRING_END = 39; |
||
192 | const INVALID_UUID = 40; |
||
193 | const INVALID_COUNT = 41; |
||
194 | const INVALID_NOT_EQ = 42; |
||
195 | const INVALID_NOT_SAME = 43; |
||
196 | const INVALID_TRAVERSABLE = 44; |
||
197 | const INVALID_ARRAY_ACCESSIBLE = 45; |
||
198 | const INVALID_KEY_ISSET = 46; |
||
199 | const INVALID_VALUE_IN_ARRAY = 47; |
||
200 | const INVALID_DIRECTORY = 101; |
||
201 | const INVALID_FILE = 102; |
||
202 | const INVALID_READABLE = 103; |
||
203 | const INVALID_WRITEABLE = 104; |
||
204 | const INVALID_CLASS = 105; |
||
205 | const INVALID_INTERFACE = 106; |
||
206 | const INVALID_EMAIL = 201; |
||
207 | const INTERFACE_NOT_IMPLEMENTED = 202; |
||
208 | const INVALID_URL = 203; |
||
209 | const INVALID_NOT_INSTANCE_OF = 204; |
||
210 | const VALUE_NOT_EMPTY = 205; |
||
211 | const INVALID_JSON_STRING = 206; |
||
212 | const INVALID_OBJECT = 207; |
||
213 | const INVALID_METHOD = 208; |
||
214 | const INVALID_SCALAR = 209; |
||
215 | const INVALID_LESS = 210; |
||
216 | const INVALID_LESS_OR_EQUAL = 211; |
||
217 | const INVALID_GREATER = 212; |
||
218 | const INVALID_GREATER_OR_EQUAL = 213; |
||
219 | const INVALID_DATE = 214; |
||
220 | const INVALID_CALLABLE = 215; |
||
221 | const INVALID_KEY_NOT_EXISTS = 216; |
||
222 | const INVALID_SATISFY = 217; |
||
223 | const INVALID_IP = 218; |
||
224 | |||
225 | /** |
||
226 | * Exception to throw when an assertion failed. |
||
227 | * |
||
228 | * @var string |
||
229 | */ |
||
230 | static protected $exceptionClass = 'Assert\InvalidArgumentException'; |
||
231 | |||
232 | /** |
||
233 | * Helper method that handles building the assertion failure exceptions. |
||
234 | * They are returned from this method so that the stack trace still shows |
||
235 | * the assertions method. |
||
236 | */ |
||
237 | protected static function createException($value, $message, $code, $propertyPath, array $constraints = array()) |
||
242 | |||
243 | /** |
||
244 | * Assert that two values are equal (using == ). |
||
245 | * |
||
246 | * @param mixed $value |
||
247 | * @param mixed $value2 |
||
248 | * @param string|null $message |
||
249 | * @param string|null $propertyPath |
||
250 | * @return void |
||
251 | * @throws \Assert\AssertionFailedException |
||
252 | */ |
||
253 | public static function eq($value, $value2, $message = null, $propertyPath = null) |
||
265 | |||
266 | /** |
||
267 | * Assert that two values are the same (using ===). |
||
268 | * |
||
269 | * @param mixed $value |
||
270 | * @param mixed $value2 |
||
271 | * @param string|null $message |
||
272 | * @param string|null $propertyPath |
||
273 | * @return void |
||
274 | * @throws \Assert\AssertionFailedException |
||
275 | */ |
||
276 | public static function same($value, $value2, $message = null, $propertyPath = null) |
||
288 | |||
289 | /** |
||
290 | * Assert that two values are not equal (using == ). |
||
291 | * |
||
292 | * @param mixed $value1 |
||
293 | * @param mixed $value2 |
||
294 | * @param string|null $message |
||
295 | * @param string|null $propertyPath |
||
296 | * @return void |
||
297 | * @throws \Assert\AssertionFailedException |
||
298 | */ |
||
299 | public static function notEq($value1, $value2, $message = null, $propertyPath = null) |
||
310 | |||
311 | /** |
||
312 | * Assert that two values are not the same (using === ). |
||
313 | * |
||
314 | * @param mixed $value1 |
||
315 | * @param mixed $value2 |
||
316 | * @param string|null $message |
||
317 | * @param string|null $propertyPath |
||
318 | * @return void |
||
319 | * @throws \Assert\AssertionFailedException |
||
320 | */ |
||
321 | public static function notSame($value1, $value2, $message = null, $propertyPath = null) |
||
332 | |||
333 | /** |
||
334 | * Assert that value is not in array of choices. |
||
335 | * |
||
336 | * @param mixed $value |
||
337 | * @param array $choices |
||
338 | * @param string|null $message |
||
339 | * @param string|null $propertyPath |
||
340 | * @return void |
||
341 | * @throws \Assert\AssertionFailedException |
||
342 | */ |
||
343 | public static function notInArray($value, array $choices, $message = null, $propertyPath = null) |
||
354 | |||
355 | /** |
||
356 | * Assert that value is a php integer. |
||
357 | * |
||
358 | * @param mixed $value |
||
359 | * @param string|null $message |
||
360 | * @param string|null $propertyPath |
||
361 | * @return void |
||
362 | * @throws \Assert\AssertionFailedException |
||
363 | */ |
||
364 | public static function integer($value, $message = null, $propertyPath = null) |
||
375 | |||
376 | /** |
||
377 | * Assert that value is a php float. |
||
378 | * |
||
379 | * @param mixed $value |
||
380 | * @param string|null $message |
||
381 | * @param string|null $propertyPath |
||
382 | * @return void |
||
383 | * @throws \Assert\AssertionFailedException |
||
384 | */ |
||
385 | public static function float($value, $message = null, $propertyPath = null) |
||
396 | |||
397 | /** |
||
398 | * Validates if an integer or integerish is a digit. |
||
399 | * |
||
400 | * @param mixed $value |
||
401 | * @param string|null $message |
||
402 | * @param string|null $propertyPath |
||
403 | * @return void |
||
404 | * @throws \Assert\AssertionFailedException |
||
405 | */ |
||
406 | public static function digit($value, $message = null, $propertyPath = null) |
||
417 | |||
418 | /** |
||
419 | * Assert that value is a php integer'ish. |
||
420 | * |
||
421 | * @param mixed $value |
||
422 | * @param string|null $message |
||
423 | * @param string|null $propertyPath |
||
424 | * @return void |
||
425 | * @throws \Assert\AssertionFailedException |
||
426 | */ |
||
427 | public static function integerish($value, $message = null, $propertyPath = null) |
||
438 | |||
439 | /** |
||
440 | * Assert that value is php boolean |
||
441 | * |
||
442 | * @param mixed $value |
||
443 | * @param string|null $message |
||
444 | * @param string|null $propertyPath |
||
445 | * @return void |
||
446 | * @throws \Assert\AssertionFailedException |
||
447 | */ |
||
448 | public static function boolean($value, $message = null, $propertyPath = null) |
||
459 | |||
460 | /** |
||
461 | * Assert that value is a PHP scalar |
||
462 | * |
||
463 | * @param mixed $value |
||
464 | * @param string|null $message |
||
465 | * @param string|null $propertyPath |
||
466 | * @return void |
||
467 | * @throws \Assert\AssertionFailedException |
||
468 | */ |
||
469 | public static function scalar($value, $message = null, $propertyPath = null) |
||
480 | |||
481 | /** |
||
482 | * Assert that value is not empty |
||
483 | * |
||
484 | * @param mixed $value |
||
485 | * @param string|null $message |
||
486 | * @param string|null $propertyPath |
||
487 | * @return void |
||
488 | * @throws \Assert\AssertionFailedException |
||
489 | */ |
||
490 | public static function notEmpty($value, $message = null, $propertyPath = null) |
||
501 | |||
502 | /** |
||
503 | * Assert that value is empty |
||
504 | * |
||
505 | * @param mixed $value |
||
506 | * @param string|null $message |
||
507 | * @param string|null $propertyPath |
||
508 | * @return void |
||
509 | * @throws \Assert\AssertionFailedException |
||
510 | */ |
||
511 | public static function noContent($value, $message = null, $propertyPath = null) |
||
522 | |||
523 | /** |
||
524 | * Assert that value is not null |
||
525 | * |
||
526 | * @param mixed $value |
||
527 | * @param string|null $message |
||
528 | * @param string|null $propertyPath |
||
529 | * @return void |
||
530 | * @throws \Assert\AssertionFailedException |
||
531 | */ |
||
532 | public static function notNull($value, $message = null, $propertyPath = null) |
||
543 | |||
544 | /** |
||
545 | * Assert that value is a string |
||
546 | * |
||
547 | * @param mixed $value |
||
548 | * @param string|null $message |
||
549 | * @param string|null $propertyPath |
||
550 | * @return void |
||
551 | * @throws \Assert\AssertionFailedException |
||
552 | */ |
||
553 | public static function string($value, $message = null, $propertyPath = null) |
||
565 | |||
566 | /** |
||
567 | * Assert that value matches a regex |
||
568 | * |
||
569 | * @param mixed $value |
||
570 | * @param string $pattern |
||
571 | * @param string|null $message |
||
572 | * @param string|null $propertyPath |
||
573 | * @return void |
||
574 | * @throws \Assert\AssertionFailedException |
||
575 | */ |
||
576 | public static function regex($value, $pattern, $message = null, $propertyPath = null) |
||
589 | |||
590 | /** |
||
591 | * Assert that string has a given length. |
||
592 | * |
||
593 | * @param mixed $value |
||
594 | * @param int $length |
||
595 | * @param string|null $message |
||
596 | * @param string|null $propertyPath |
||
597 | * @param string $encoding |
||
598 | * @return void |
||
599 | * @throws \Assert\AssertionFailedException |
||
600 | */ |
||
601 | public static function length($value, $length, $message = null, $propertyPath = null, $encoding = 'utf8') |
||
617 | |||
618 | /** |
||
619 | * Assert that a string is at least $minLength chars long. |
||
620 | * |
||
621 | * @param mixed $value |
||
622 | * @param int $minLength |
||
623 | * @param string|null $message |
||
624 | * @param string|null $propertyPath |
||
625 | * @param string $encoding |
||
626 | * @return void |
||
627 | * @throws \Assert\AssertionFailedException |
||
628 | */ |
||
629 | public static function minLength($value, $minLength, $message = null, $propertyPath = null, $encoding = 'utf8') |
||
645 | |||
646 | /** |
||
647 | * Assert that string value is not longer than $maxLength chars. |
||
648 | * |
||
649 | * @param mixed $value |
||
650 | * @param integer $maxLength |
||
651 | * @param string|null $message |
||
652 | * @param string|null $propertyPath |
||
653 | * @param string $encoding |
||
654 | * @return void |
||
655 | * @throws \Assert\AssertionFailedException |
||
656 | */ |
||
657 | public static function maxLength($value, $maxLength, $message = null, $propertyPath = null, $encoding = 'utf8') |
||
673 | |||
674 | /** |
||
675 | * Assert that string length is between min,max lengths. |
||
676 | * |
||
677 | * @param mixed $value |
||
678 | * @param integer $minLength |
||
679 | * @param integer $maxLength |
||
680 | * @param string|null $message |
||
681 | * @param string|null $propertyPath |
||
682 | * @param string $encoding |
||
683 | * @return void |
||
684 | * @throws \Assert\AssertionFailedException |
||
685 | */ |
||
686 | public static function betweenLength($value, $minLength, $maxLength, $message = null, $propertyPath = null, $encoding = 'utf8') |
||
714 | |||
715 | /** |
||
716 | * Assert that string starts with a sequence of chars. |
||
717 | * |
||
718 | * @param mixed $string |
||
719 | * @param string $needle |
||
720 | * @param string|null $message |
||
721 | * @param string|null $propertyPath |
||
722 | * @param string $encoding |
||
723 | * @return void |
||
724 | * @throws \Assert\AssertionFailedException |
||
725 | */ |
||
726 | public static function startsWith($string, $needle, $message = null, $propertyPath = null, $encoding = 'utf8') |
||
741 | |||
742 | /** |
||
743 | * Assert that string ends with a sequence of chars. |
||
744 | * |
||
745 | * @param mixed $string |
||
746 | * @param string $needle |
||
747 | * @param string|null $message |
||
748 | * @param string|null $propertyPath |
||
749 | * @param string $encoding |
||
750 | * @return void |
||
751 | * @throws \Assert\AssertionFailedException |
||
752 | */ |
||
753 | public static function endsWith($string, $needle, $message = null, $propertyPath = null, $encoding = 'utf8') |
||
770 | |||
771 | /** |
||
772 | * Assert that string contains a sequence of chars. |
||
773 | * |
||
774 | * @param mixed $string |
||
775 | * @param string $needle |
||
776 | * @param string|null $message |
||
777 | * @param string|null $propertyPath |
||
778 | * @param string $encoding |
||
779 | * @return void |
||
780 | * @throws \Assert\AssertionFailedException |
||
781 | */ |
||
782 | public static function contains($string, $needle, $message = null, $propertyPath = null, $encoding = 'utf8') |
||
797 | |||
798 | /** |
||
799 | * Assert that value is in array of choices. |
||
800 | * |
||
801 | * @param mixed $value |
||
802 | * @param array $choices |
||
803 | * @param string|null $message |
||
804 | * @param string|null $propertyPath |
||
805 | * @return void |
||
806 | * @throws \Assert\AssertionFailedException |
||
807 | */ |
||
808 | public static function choice($value, array $choices, $message = null, $propertyPath = null) |
||
820 | |||
821 | /** |
||
822 | * Alias of {@see choice()} |
||
823 | * |
||
824 | * @throws \Assert\AssertionFailedException |
||
825 | */ |
||
826 | public static function inArray($value, array $choices, $message = null, $propertyPath = null) |
||
830 | |||
831 | /** |
||
832 | * Assert that value is numeric. |
||
833 | * |
||
834 | * @param mixed $value |
||
835 | * @param string|null $message |
||
836 | * @param string|null $propertyPath |
||
837 | * @return void |
||
838 | * @throws \Assert\AssertionFailedException |
||
839 | */ |
||
840 | public static function numeric($value, $message = null, $propertyPath = null) |
||
851 | |||
852 | /** |
||
853 | * Assert that value is an array. |
||
854 | * |
||
855 | * @param mixed $value |
||
856 | * @param string|null $message |
||
857 | * @param string|null $propertyPath |
||
858 | * @return void |
||
859 | * @throws \Assert\AssertionFailedException |
||
860 | */ |
||
861 | public static function isArray($value, $message = null, $propertyPath = null) |
||
872 | |||
873 | /** |
||
874 | * Assert that value is an array or a traversable object. |
||
875 | * |
||
876 | * @param mixed $value |
||
877 | * @param string|null $message |
||
878 | * @param string|null $propertyPath |
||
879 | * @return void |
||
880 | * @throws \Assert\AssertionFailedException |
||
881 | */ |
||
882 | public static function isTraversable($value, $message = null, $propertyPath = null) |
||
893 | |||
894 | /** |
||
895 | * Assert that value is an array or an array-accessible object. |
||
896 | * |
||
897 | * @param mixed $value |
||
898 | * @param string|null $message |
||
899 | * @param string|null $propertyPath |
||
900 | * @return void |
||
901 | * @throws \Assert\AssertionFailedException |
||
902 | */ |
||
903 | public static function isArrayAccessible($value, $message = null, $propertyPath = null) |
||
914 | |||
915 | /** |
||
916 | * Assert that key exists in an array |
||
917 | * |
||
918 | * @param mixed $value |
||
919 | * @param string|integer $key |
||
920 | * @param string|null $message |
||
921 | * @param string|null $propertyPath |
||
922 | * @return void |
||
923 | * @throws \Assert\AssertionFailedException |
||
924 | */ |
||
925 | public static function keyExists($value, $key, $message = null, $propertyPath = null) |
||
938 | |||
939 | /** |
||
940 | * Assert that key does not exist in an array |
||
941 | * |
||
942 | * @param mixed $value |
||
943 | * @param string|integer $key |
||
944 | * @param string|null $message |
||
945 | * @param string|null $propertyPath |
||
946 | * @return void |
||
947 | * @throws \Assert\AssertionFailedException |
||
948 | */ |
||
949 | public static function keyNotExists($value, $key, $message = null, $propertyPath = null) |
||
962 | |||
963 | /** |
||
964 | * Assert that key exists in an array/array-accessible object using isset() |
||
965 | * |
||
966 | * @param mixed $value |
||
967 | * @param string|integer $key |
||
968 | * @param string|null $message |
||
969 | * @param string|null $propertyPath |
||
970 | * @return void |
||
971 | * @throws \Assert\AssertionFailedException |
||
972 | */ |
||
973 | public static function keyIsset($value, $key, $message = null, $propertyPath = null) |
||
986 | |||
987 | /** |
||
988 | * Assert that key exists in an array/array-accessible object and it's value is not empty. |
||
989 | * |
||
990 | * @param mixed $value |
||
991 | * @param string|integer $key |
||
992 | * @param string|null $message |
||
993 | * @param string|null $propertyPath |
||
994 | * @return void |
||
995 | * @throws \Assert\AssertionFailedException |
||
996 | */ |
||
997 | public static function notEmptyKey($value, $key, $message = null, $propertyPath = null) |
||
1002 | |||
1003 | /** |
||
1004 | * Assert that value is not blank |
||
1005 | * |
||
1006 | * @param mixed $value |
||
1007 | * @param string|null $message |
||
1008 | * @param string|null $propertyPath |
||
1009 | * @return void |
||
1010 | * @throws \Assert\AssertionFailedException |
||
1011 | */ |
||
1012 | public static function notBlank($value, $message = null, $propertyPath = null) |
||
1023 | |||
1024 | /** |
||
1025 | * Assert that value is instance of given class-name. |
||
1026 | * |
||
1027 | * @param mixed $value |
||
1028 | * @param string $className |
||
1029 | * @param string|null $message |
||
1030 | * @param string|null $propertyPath |
||
1031 | * @return void |
||
1032 | * @throws \Assert\AssertionFailedException |
||
1033 | */ |
||
1034 | public static function isInstanceOf($value, $className, $message = null, $propertyPath = null) |
||
1046 | |||
1047 | /** |
||
1048 | * Assert that value is not instance of given class-name. |
||
1049 | * |
||
1050 | * @param mixed $value |
||
1051 | * @param string $className |
||
1052 | * @param string|null $message |
||
1053 | * @param string|null $propertyPath |
||
1054 | * @return void |
||
1055 | * @throws \Assert\AssertionFailedException |
||
1056 | */ |
||
1057 | public static function notIsInstanceOf($value, $className, $message = null, $propertyPath = null) |
||
1069 | |||
1070 | /** |
||
1071 | * Assert that value is subclass of given class-name. |
||
1072 | * |
||
1073 | * @param mixed $value |
||
1074 | * @param string $className |
||
1075 | * @param string|null $message |
||
1076 | * @param string|null $propertyPath |
||
1077 | * @return void |
||
1078 | * @throws \Assert\AssertionFailedException |
||
1079 | */ |
||
1080 | public static function subclassOf($value, $className, $message = null, $propertyPath = null) |
||
1092 | |||
1093 | /** |
||
1094 | * Assert that value is in range of numbers. |
||
1095 | * |
||
1096 | * @param mixed $value |
||
1097 | * @param integer $minValue |
||
1098 | * @param integer $maxValue |
||
1099 | * @param string|null $message |
||
1100 | * @param string|null $propertyPath |
||
1101 | * @return void |
||
1102 | * @throws \Assert\AssertionFailedException |
||
1103 | */ |
||
1104 | public static function range($value, $minValue, $maxValue, $message = null, $propertyPath = null) |
||
1119 | |||
1120 | /** |
||
1121 | * Assert that a value is at least as big as a given limit |
||
1122 | * |
||
1123 | * @param mixed $value |
||
1124 | * @param mixed $minValue |
||
1125 | * @param string|null $message |
||
1126 | * @param string|null $propertyPath |
||
1127 | * @return void |
||
1128 | * @throws \Assert\AssertionFailedException |
||
1129 | */ |
||
1130 | public static function min($value, $minValue, $message = null, $propertyPath = null) |
||
1144 | |||
1145 | /** |
||
1146 | * Assert that a number is smaller as a given limit |
||
1147 | * |
||
1148 | * @param mixed $value |
||
1149 | * @param mixed $maxValue |
||
1150 | * @param string|null $message |
||
1151 | * @param string|null $propertyPath |
||
1152 | * @return void |
||
1153 | * @throws \Assert\AssertionFailedException |
||
1154 | */ |
||
1155 | public static function max($value, $maxValue, $message = null, $propertyPath = null) |
||
1169 | |||
1170 | /** |
||
1171 | * Assert that a file exists |
||
1172 | * |
||
1173 | * @param string $value |
||
1174 | * @param string|null $message |
||
1175 | * @param string|null $propertyPath |
||
1176 | * @return void |
||
1177 | * @throws \Assert\AssertionFailedException |
||
1178 | */ |
||
1179 | public static function file($value, $message = null, $propertyPath = null) |
||
1193 | |||
1194 | /** |
||
1195 | * Assert that a directory exists |
||
1196 | * |
||
1197 | * @param string $value |
||
1198 | * @param string|null $message |
||
1199 | * @param string|null $propertyPath |
||
1200 | * @return void |
||
1201 | * @throws \Assert\AssertionFailedException |
||
1202 | */ |
||
1203 | public static function directory($value, $message = null, $propertyPath = null) |
||
1216 | |||
1217 | /** |
||
1218 | * Assert that the value is something readable |
||
1219 | * |
||
1220 | * @param string $value |
||
1221 | * @param string|null $message |
||
1222 | * @param string|null $propertyPath |
||
1223 | * @return void |
||
1224 | * @throws \Assert\AssertionFailedException |
||
1225 | */ |
||
1226 | public static function readable($value, $message = null, $propertyPath = null) |
||
1239 | |||
1240 | /** |
||
1241 | * Assert that the value is something writeable |
||
1242 | * |
||
1243 | * @param string $value |
||
1244 | * @param string|null $message |
||
1245 | * @param string|null $propertyPath |
||
1246 | * @return void |
||
1247 | * @throws \Assert\AssertionFailedException |
||
1248 | */ |
||
1249 | public static function writeable($value, $message = null, $propertyPath = null) |
||
1262 | |||
1263 | /** |
||
1264 | * Assert that value is an email adress (using input_filter/FILTER_VALIDATE_EMAIL). |
||
1265 | * |
||
1266 | * @param mixed $value |
||
1267 | * @param string|null $message |
||
1268 | * @param string|null $propertyPath |
||
1269 | * @return void |
||
1270 | * @throws \Assert\AssertionFailedException |
||
1271 | */ |
||
1272 | public static function email($value, $message = null, $propertyPath = null) |
||
1297 | |||
1298 | /** |
||
1299 | * Assert that value is an URL. |
||
1300 | * |
||
1301 | * This code snipped was taken from the Symfony project and modified to the special demands of this method. |
||
1302 | * |
||
1303 | * @param mixed $value |
||
1304 | * @param string|null $message |
||
1305 | * @param string|null $propertyPath |
||
1306 | * @return void |
||
1307 | * @throws \Assert\AssertionFailedException |
||
1308 | * |
||
1309 | * |
||
1310 | * @link https://github.com/symfony/Validator/blob/master/Constraints/UrlValidator.php |
||
1311 | * @link https://github.com/symfony/Validator/blob/master/Constraints/Url.php |
||
1312 | */ |
||
1313 | public static function url($value, $message = null, $propertyPath = null) |
||
1346 | |||
1347 | /** |
||
1348 | * Assert that value is alphanumeric. |
||
1349 | * |
||
1350 | * @param mixed $value |
||
1351 | * @param string|null $message |
||
1352 | * @param string|null $propertyPath |
||
1353 | * @return void |
||
1354 | * @throws \Assert\AssertionFailedException |
||
1355 | */ |
||
1356 | public static function alnum($value, $message = null, $propertyPath = null) |
||
1369 | |||
1370 | /** |
||
1371 | * Assert that the value is boolean True. |
||
1372 | * |
||
1373 | * @param mixed $value |
||
1374 | * @param string|null $message |
||
1375 | * @param string|null $propertyPath |
||
1376 | * @return void |
||
1377 | * @throws \Assert\AssertionFailedException |
||
1378 | */ |
||
1379 | public static function true($value, $message = null, $propertyPath = null) |
||
1390 | |||
1391 | /** |
||
1392 | * Assert that the value is boolean False. |
||
1393 | * |
||
1394 | * @param mixed $value |
||
1395 | * @param string|null $message |
||
1396 | * @param string|null $propertyPath |
||
1397 | * @return void |
||
1398 | * @throws \Assert\AssertionFailedException |
||
1399 | */ |
||
1400 | public static function false($value, $message = null, $propertyPath = null) |
||
1411 | |||
1412 | /** |
||
1413 | * Assert that the class exists. |
||
1414 | * |
||
1415 | * @param mixed $value |
||
1416 | * @param string|null $message |
||
1417 | * @param string|null $propertyPath |
||
1418 | * @return void |
||
1419 | * @throws \Assert\AssertionFailedException |
||
1420 | */ |
||
1421 | public static function classExists($value, $message = null, $propertyPath = null) |
||
1432 | |||
1433 | /** |
||
1434 | * Assert that the interface exists. |
||
1435 | * |
||
1436 | * @param mixed $value |
||
1437 | * @param string|null $message |
||
1438 | * @param string|null $propertyPath |
||
1439 | * @return void |
||
1440 | * @throws \Assert\AssertionFailedException |
||
1441 | */ |
||
1442 | public static function interfaceExists($value, $message = null, $propertyPath = null) |
||
1453 | |||
1454 | /** |
||
1455 | * Assert that the class implements the interface |
||
1456 | * |
||
1457 | * @param mixed $class |
||
1458 | * @param string $interfaceName |
||
1459 | * @param string|null $message |
||
1460 | * @param string|null $propertyPath |
||
1461 | * @return void |
||
1462 | * @throws \Assert\AssertionFailedException |
||
1463 | */ |
||
1464 | public static function implementsInterface($class, $interfaceName, $message = null, $propertyPath = null) |
||
1477 | |||
1478 | /** |
||
1479 | * Assert that the given string is a valid json string. |
||
1480 | * |
||
1481 | * NOTICE: |
||
1482 | * Since this does a json_decode to determine its validity |
||
1483 | * you probably should consider, when using the variable |
||
1484 | * content afterwards, just to decode and check for yourself instead |
||
1485 | * of using this assertion. |
||
1486 | * |
||
1487 | * @param mixed $value |
||
1488 | * @param string|null $message |
||
1489 | * @param string|null $propertyPath |
||
1490 | * @return void |
||
1491 | * @throws \Assert\AssertionFailedException |
||
1492 | */ |
||
1493 | public static function isJsonString($value, $message = null, $propertyPath = null) |
||
1504 | |||
1505 | /** |
||
1506 | * Assert that the given string is a valid UUID |
||
1507 | * |
||
1508 | * Uses code from {@link https://github.com/ramsey/uuid} that is MIT licensed. |
||
1509 | * |
||
1510 | * @param string $value |
||
1511 | * @param string|null $message |
||
1512 | * @param string|null $propertyPath |
||
1513 | * @return void |
||
1514 | * @throws \Assert\AssertionFailedException |
||
1515 | */ |
||
1516 | public static function uuid($value, $message = null, $propertyPath = null) |
||
1533 | |||
1534 | /** |
||
1535 | * Assert that the count of countable is equal to count. |
||
1536 | * |
||
1537 | * @param array|\Countable $countable |
||
1538 | * @param int $count |
||
1539 | * @param string $message |
||
1540 | * @param string $propertyPath |
||
1541 | * @return void |
||
1542 | * @throws \Assert\AssertionFailedException |
||
1543 | */ |
||
1544 | public static function count($countable, $count, $message = null, $propertyPath = null) |
||
1555 | |||
1556 | /** |
||
1557 | * static call handler to implement: |
||
1558 | * - "null or assertion" delegation |
||
1559 | * - "all" delegation |
||
1560 | */ |
||
1561 | public static function __callStatic($method, $args) |
||
1597 | |||
1598 | /** |
||
1599 | * Determines if the values array has every choice as key and that this choice has content. |
||
1600 | * |
||
1601 | * @param array $values |
||
1602 | * @param array $choices |
||
1603 | * @param null $message |
||
1604 | * @param null $propertyPath |
||
1605 | */ |
||
1606 | public static function choicesNotEmpty(array $values, array $choices, $message = null, $propertyPath = null) |
||
1615 | |||
1616 | /** |
||
1617 | * Determines that the named method is defined in the provided object. |
||
1618 | * |
||
1619 | * @param string $value |
||
1620 | * @param mixed $object |
||
1621 | * @param null $message |
||
1622 | * @param null $propertyPath |
||
1623 | * |
||
1624 | * @throws |
||
1625 | */ |
||
1626 | public static function methodExists($value, $object, $message = null, $propertyPath = null) |
||
1639 | |||
1640 | /** |
||
1641 | * Determines that the provided value is an object. |
||
1642 | * |
||
1643 | * @param mixed $value |
||
1644 | * @param null $message |
||
1645 | * @param null $propertyPath |
||
1646 | */ |
||
1647 | public static function isObject($value, $message = null, $propertyPath = null) |
||
1659 | |||
1660 | /** |
||
1661 | * Determines if the value is less than given limit. |
||
1662 | * |
||
1663 | * @param mixed $value |
||
1664 | * @param mixed $limit |
||
1665 | * @param null $message |
||
1666 | * @param null $propertyPath |
||
1667 | */ |
||
1668 | public static function lessThan($value, $limit, $message = null, $propertyPath = null) |
||
1680 | |||
1681 | /** |
||
1682 | * Determines if the value is less or than given limit. |
||
1683 | * |
||
1684 | * @param mixed $value |
||
1685 | * @param mixed $limit |
||
1686 | * @param null $message |
||
1687 | * @param null $propertyPath |
||
1688 | */ |
||
1689 | public static function lessOrEqualThan($value, $limit, $message = null, $propertyPath = null) |
||
1701 | |||
1702 | /** |
||
1703 | * Determines if the value is greater than given limit. |
||
1704 | * |
||
1705 | * @param mixed $value |
||
1706 | * @param mixed $limit |
||
1707 | * @param null $message |
||
1708 | * @param null $propertyPath |
||
1709 | */ |
||
1710 | public static function greaterThan($value, $limit, $message = null, $propertyPath = null) |
||
1722 | |||
1723 | /** |
||
1724 | * Determines if the value is greater or equal than given limit. |
||
1725 | * |
||
1726 | * @param mixed $value |
||
1727 | * @param mixed $limit |
||
1728 | * @param null $message |
||
1729 | * @param null $propertyPath |
||
1730 | */ |
||
1731 | public static function greaterOrEqualThan($value, $limit, $message = null, $propertyPath = null) |
||
1743 | |||
1744 | /** |
||
1745 | * Assert that date is valid and corresponds to the given format. |
||
1746 | * |
||
1747 | * @param string $value |
||
1748 | * @param string $format supports all of the options date(), except for the following: |
||
1749 | * N, w, W, t, L, o, B, a, A, g, h, I, O, P, Z, c, r. |
||
1750 | * @param string|null $message |
||
1751 | * @param string|null $propertyPath |
||
1752 | * |
||
1753 | * @link http://php.net/manual/function.date.php#refsect1-function.date-parameters |
||
1754 | */ |
||
1755 | public static function date($value, $format, $message = null, $propertyPath = null) |
||
1772 | |||
1773 | /** |
||
1774 | * Determines that the provided value is callable. |
||
1775 | * |
||
1776 | * @param mixed $value |
||
1777 | * @param null $message |
||
1778 | * @param null $propertyPath |
||
1779 | */ |
||
1780 | public static function isCallable($value, $message = null, $propertyPath = null) |
||
1791 | |||
1792 | /** |
||
1793 | * Assert that the provided value is valid according to a callback. |
||
1794 | * |
||
1795 | * If the callback returns `false` the assertion will fail. |
||
1796 | * |
||
1797 | * @param mixed $value |
||
1798 | * @param callable $callback |
||
1799 | * @param string|null $message |
||
1800 | * @param string|null $propertyPath |
||
1801 | */ |
||
1802 | public static function satisfy($value, $callback, $message = null, $propertyPath = null) |
||
1815 | |||
1816 | /** |
||
1817 | * Assert that value is an IPv4 or IPv6 address |
||
1818 | * (using input_filter/FILTER_VALIDATE_IP). |
||
1819 | * |
||
1820 | * @param string $value |
||
1821 | * @param null|int $flag |
||
1822 | * @param string|null $message |
||
1823 | * @param string|null $propertyPath |
||
1824 | * |
||
1825 | * @link http://php.net/manual/filter.filters.flags.php |
||
1826 | */ |
||
1827 | public static function ip($value, $flag = null, $message = null, $propertyPath = null) |
||
1838 | |||
1839 | /** |
||
1840 | * Assert that value is an IPv4 address |
||
1841 | * (using input_filter/FILTER_VALIDATE_IP). |
||
1842 | * |
||
1843 | * @param string $value |
||
1844 | * @param null|int $flag |
||
1845 | * @param string|null $message |
||
1846 | * @param string|null $propertyPath |
||
1847 | * |
||
1848 | * @link http://php.net/manual/filter.filters.flags.php |
||
1849 | */ |
||
1850 | public static function ipv4($value, $flag = null, $message = null, $propertyPath = null) |
||
1854 | |||
1855 | /** |
||
1856 | * Assert that value is an IPv6 address |
||
1857 | * (using input_filter/FILTER_VALIDATE_IP). |
||
1858 | * |
||
1859 | * @param string $value |
||
1860 | * @param null|int $flag |
||
1861 | * @param string|null $message |
||
1862 | * @param string|null $propertyPath |
||
1863 | * |
||
1864 | * @link http://php.net/manual/filter.filters.flags.php |
||
1865 | */ |
||
1866 | public static function ipv6($value, $flag = null, $message = null, $propertyPath = null) |
||
1870 | |||
1871 | /** |
||
1872 | * Make a string version of a value. |
||
1873 | * |
||
1874 | * @param mixed $value |
||
1875 | * @return string |
||
1876 | */ |
||
1877 | protected static function stringify($value) |
||
1911 | } |
||
1912 | |||
1913 |