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 |
||
173 | class Assertion |
||
174 | { |
||
175 | const INVALID_FLOAT = 9; |
||
176 | const INVALID_INTEGER = 10; |
||
177 | const INVALID_DIGIT = 11; |
||
178 | const INVALID_INTEGERISH = 12; |
||
179 | const INVALID_BOOLEAN = 13; |
||
180 | const VALUE_EMPTY = 14; |
||
181 | const VALUE_NULL = 15; |
||
182 | const VALUE_NOT_NULL = 25; |
||
183 | const INVALID_STRING = 16; |
||
184 | const INVALID_REGEX = 17; |
||
185 | const INVALID_MIN_LENGTH = 18; |
||
186 | const INVALID_MAX_LENGTH = 19; |
||
187 | const INVALID_STRING_START = 20; |
||
188 | const INVALID_STRING_CONTAINS = 21; |
||
189 | const INVALID_CHOICE = 22; |
||
190 | const INVALID_NUMERIC = 23; |
||
191 | const INVALID_ARRAY = 24; |
||
192 | const INVALID_KEY_EXISTS = 26; |
||
193 | const INVALID_NOT_BLANK = 27; |
||
194 | const INVALID_INSTANCE_OF = 28; |
||
195 | const INVALID_SUBCLASS_OF = 29; |
||
196 | const INVALID_RANGE = 30; |
||
197 | const INVALID_ALNUM = 31; |
||
198 | const INVALID_TRUE = 32; |
||
199 | const INVALID_EQ = 33; |
||
200 | const INVALID_SAME = 34; |
||
201 | const INVALID_MIN = 35; |
||
202 | const INVALID_MAX = 36; |
||
203 | const INVALID_LENGTH = 37; |
||
204 | const INVALID_FALSE = 38; |
||
205 | const INVALID_STRING_END = 39; |
||
206 | const INVALID_UUID = 40; |
||
207 | const INVALID_COUNT = 41; |
||
208 | const INVALID_NOT_EQ = 42; |
||
209 | const INVALID_NOT_SAME = 43; |
||
210 | const INVALID_TRAVERSABLE = 44; |
||
211 | const INVALID_ARRAY_ACCESSIBLE = 45; |
||
212 | const INVALID_KEY_ISSET = 46; |
||
213 | const INVALID_VALUE_IN_ARRAY = 47; |
||
214 | const INVALID_E164 = 48; |
||
215 | const INVALID_DIRECTORY = 101; |
||
216 | const INVALID_FILE = 102; |
||
217 | const INVALID_READABLE = 103; |
||
218 | const INVALID_WRITEABLE = 104; |
||
219 | const INVALID_CLASS = 105; |
||
220 | const INVALID_INTERFACE = 106; |
||
221 | const INVALID_EMAIL = 201; |
||
222 | const INTERFACE_NOT_IMPLEMENTED = 202; |
||
223 | const INVALID_URL = 203; |
||
224 | const INVALID_NOT_INSTANCE_OF = 204; |
||
225 | const VALUE_NOT_EMPTY = 205; |
||
226 | const INVALID_JSON_STRING = 206; |
||
227 | const INVALID_OBJECT = 207; |
||
228 | const INVALID_METHOD = 208; |
||
229 | const INVALID_SCALAR = 209; |
||
230 | const INVALID_LESS = 210; |
||
231 | const INVALID_LESS_OR_EQUAL = 211; |
||
232 | const INVALID_GREATER = 212; |
||
233 | const INVALID_GREATER_OR_EQUAL = 213; |
||
234 | const INVALID_DATE = 214; |
||
235 | const INVALID_CALLABLE = 215; |
||
236 | const INVALID_KEY_NOT_EXISTS = 216; |
||
237 | const INVALID_SATISFY = 217; |
||
238 | const INVALID_IP = 218; |
||
239 | const INVALID_BETWEEN = 219; |
||
240 | const INVALID_BETWEEN_EXCLUSIVE = 220; |
||
241 | const INVALID_EXTENSION = 222; |
||
242 | const INVALID_CONSTANT = 221; |
||
243 | |||
244 | /** |
||
245 | * Exception to throw when an assertion failed. |
||
246 | * |
||
247 | * @var string |
||
248 | */ |
||
249 | protected static $exceptionClass = 'Assert\InvalidArgumentException'; |
||
250 | |||
251 | /** |
||
252 | * Helper method that handles building the assertion failure exceptions. |
||
253 | * They are returned from this method so that the stack trace still shows |
||
254 | * the assertions method. |
||
255 | * |
||
256 | * @param mixed $value |
||
257 | * @param string $message |
||
258 | * @param int $code |
||
259 | * @param string|null $propertyPath |
||
260 | * @param array $constraints |
||
261 | * |
||
262 | * @return mixed |
||
|
|||
263 | */ |
||
264 | protected static function createException($value, $message, $code, $propertyPath, array $constraints = array()) |
||
269 | |||
270 | /** |
||
271 | * Assert that two values are equal (using == ). |
||
272 | * |
||
273 | * @param mixed $value |
||
274 | * @param mixed $value2 |
||
275 | * @param string|null $message |
||
276 | * @param string|null $propertyPath |
||
277 | * @return bool |
||
278 | * @throws \Assert\AssertionFailedException |
||
279 | */ |
||
280 | public static function eq($value, $value2, $message = null, $propertyPath = null) |
||
294 | |||
295 | /** |
||
296 | * Assert that two values are the same (using ===). |
||
297 | * |
||
298 | * @param mixed $value |
||
299 | * @param mixed $value2 |
||
300 | * @param string|null $message |
||
301 | * @param string|null $propertyPath |
||
302 | * @return bool |
||
303 | * @throws \Assert\AssertionFailedException |
||
304 | */ |
||
305 | public static function same($value, $value2, $message = null, $propertyPath = null) |
||
319 | |||
320 | /** |
||
321 | * Assert that two values are not equal (using == ). |
||
322 | * |
||
323 | * @param mixed $value1 |
||
324 | * @param mixed $value2 |
||
325 | * @param string|null $message |
||
326 | * @param string|null $propertyPath |
||
327 | * @return bool |
||
328 | * @throws \Assert\AssertionFailedException |
||
329 | */ |
||
330 | public static function notEq($value1, $value2, $message = null, $propertyPath = null) |
||
343 | |||
344 | /** |
||
345 | * Assert that two values are not the same (using === ). |
||
346 | * |
||
347 | * @param mixed $value1 |
||
348 | * @param mixed $value2 |
||
349 | * @param string|null $message |
||
350 | * @param string|null $propertyPath |
||
351 | * @return bool |
||
352 | * @throws \Assert\AssertionFailedException |
||
353 | */ |
||
354 | public static function notSame($value1, $value2, $message = null, $propertyPath = null) |
||
367 | |||
368 | /** |
||
369 | * Assert that value is not in array of choices. |
||
370 | * |
||
371 | * @param mixed $value |
||
372 | * @param array $choices |
||
373 | * @param string|null $message |
||
374 | * @param string|null $propertyPath |
||
375 | * @return bool |
||
376 | * @throws \Assert\AssertionFailedException |
||
377 | */ |
||
378 | public static function notInArray($value, array $choices, $message = null, $propertyPath = null) |
||
391 | |||
392 | /** |
||
393 | * Assert that value is a php integer. |
||
394 | * |
||
395 | * @param mixed $value |
||
396 | * @param string|null $message |
||
397 | * @param string|null $propertyPath |
||
398 | * @return bool |
||
399 | * @throws \Assert\AssertionFailedException |
||
400 | */ |
||
401 | public static function integer($value, $message = null, $propertyPath = null) |
||
414 | |||
415 | /** |
||
416 | * Assert that value is a php float. |
||
417 | * |
||
418 | * @param mixed $value |
||
419 | * @param string|null $message |
||
420 | * @param string|null $propertyPath |
||
421 | * @return bool |
||
422 | * @throws \Assert\AssertionFailedException |
||
423 | */ |
||
424 | public static function float($value, $message = null, $propertyPath = null) |
||
437 | |||
438 | /** |
||
439 | * Validates if an integer or integerish is a digit. |
||
440 | * |
||
441 | * @param mixed $value |
||
442 | * @param string|null $message |
||
443 | * @param string|null $propertyPath |
||
444 | * @return bool |
||
445 | * @throws \Assert\AssertionFailedException |
||
446 | */ |
||
447 | public static function digit($value, $message = null, $propertyPath = null) |
||
460 | |||
461 | /** |
||
462 | * Assert that value is a php integer'ish. |
||
463 | * |
||
464 | * @param mixed $value |
||
465 | * @param string|null $message |
||
466 | * @param string|null $propertyPath |
||
467 | * @return bool |
||
468 | * @throws \Assert\AssertionFailedException |
||
469 | */ |
||
470 | public static function integerish($value, $message = null, $propertyPath = null) |
||
483 | |||
484 | /** |
||
485 | * Assert that value is php boolean |
||
486 | * |
||
487 | * @param mixed $value |
||
488 | * @param string|null $message |
||
489 | * @param string|null $propertyPath |
||
490 | * @return bool |
||
491 | * @throws \Assert\AssertionFailedException |
||
492 | */ |
||
493 | public static function boolean($value, $message = null, $propertyPath = null) |
||
506 | |||
507 | /** |
||
508 | * Assert that value is a PHP scalar |
||
509 | * |
||
510 | * @param mixed $value |
||
511 | * @param string|null $message |
||
512 | * @param string|null $propertyPath |
||
513 | * @return bool |
||
514 | * @throws \Assert\AssertionFailedException |
||
515 | */ |
||
516 | public static function scalar($value, $message = null, $propertyPath = null) |
||
529 | |||
530 | /** |
||
531 | * Assert that value is not empty |
||
532 | * |
||
533 | * @param mixed $value |
||
534 | * @param string|null $message |
||
535 | * @param string|null $propertyPath |
||
536 | * @return bool |
||
537 | * @throws \Assert\AssertionFailedException |
||
538 | */ |
||
539 | public static function notEmpty($value, $message = null, $propertyPath = null) |
||
552 | |||
553 | /** |
||
554 | * Assert that value is empty |
||
555 | * |
||
556 | * @param mixed $value |
||
557 | * @param string|null $message |
||
558 | * @param string|null $propertyPath |
||
559 | * @return bool |
||
560 | * @throws \Assert\AssertionFailedException |
||
561 | */ |
||
562 | public static function noContent($value, $message = null, $propertyPath = null) |
||
575 | |||
576 | /** |
||
577 | * Assert that value is null |
||
578 | * |
||
579 | * @param mixed $value |
||
580 | * @param string|null $message |
||
581 | * @param string|null $propertyPath |
||
582 | * @return bool |
||
583 | * @throws \Assert\AssertionFailedException |
||
584 | */ |
||
585 | public static function null($value, $message = null, $propertyPath = null) |
||
598 | |||
599 | /** |
||
600 | * Assert that value is not null |
||
601 | * |
||
602 | * @param mixed $value |
||
603 | * @param string|null $message |
||
604 | * @param string|null $propertyPath |
||
605 | * @return bool |
||
606 | * @throws \Assert\AssertionFailedException |
||
607 | */ |
||
608 | public static function notNull($value, $message = null, $propertyPath = null) |
||
621 | |||
622 | /** |
||
623 | * Assert that value is a string |
||
624 | * |
||
625 | * @param mixed $value |
||
626 | * @param string|null $message |
||
627 | * @param string|null $propertyPath |
||
628 | * @return bool |
||
629 | * @throws \Assert\AssertionFailedException |
||
630 | */ |
||
631 | public static function string($value, $message = null, $propertyPath = null) |
||
645 | |||
646 | /** |
||
647 | * Assert that value matches a regex |
||
648 | * |
||
649 | * @param mixed $value |
||
650 | * @param string $pattern |
||
651 | * @param string|null $message |
||
652 | * @param string|null $propertyPath |
||
653 | * @return bool |
||
654 | * @throws \Assert\AssertionFailedException |
||
655 | */ |
||
656 | public static function regex($value, $pattern, $message = null, $propertyPath = null) |
||
671 | |||
672 | /** |
||
673 | * Assert that string has a given length. |
||
674 | * |
||
675 | * @param mixed $value |
||
676 | * @param int $length |
||
677 | * @param string|null $message |
||
678 | * @param string|null $propertyPath |
||
679 | * @param string $encoding |
||
680 | * @return bool |
||
681 | * @throws \Assert\AssertionFailedException |
||
682 | */ |
||
683 | public static function length($value, $length, $message = null, $propertyPath = null, $encoding = 'utf8') |
||
701 | |||
702 | /** |
||
703 | * Assert that a string is at least $minLength chars long. |
||
704 | * |
||
705 | * @param mixed $value |
||
706 | * @param int $minLength |
||
707 | * @param string|null $message |
||
708 | * @param string|null $propertyPath |
||
709 | * @param string $encoding |
||
710 | * @return bool |
||
711 | * @throws \Assert\AssertionFailedException |
||
712 | */ |
||
713 | public static function minLength($value, $minLength, $message = null, $propertyPath = null, $encoding = 'utf8') |
||
731 | |||
732 | /** |
||
733 | * Assert that string value is not longer than $maxLength chars. |
||
734 | * |
||
735 | * @param mixed $value |
||
736 | * @param integer $maxLength |
||
737 | * @param string|null $message |
||
738 | * @param string|null $propertyPath |
||
739 | * @param string $encoding |
||
740 | * @return bool |
||
741 | * @throws \Assert\AssertionFailedException |
||
742 | */ |
||
743 | public static function maxLength($value, $maxLength, $message = null, $propertyPath = null, $encoding = 'utf8') |
||
761 | |||
762 | /** |
||
763 | * Assert that string length is between min,max lengths. |
||
764 | * |
||
765 | * @param mixed $value |
||
766 | * @param integer $minLength |
||
767 | * @param integer $maxLength |
||
768 | * @param string|null $message |
||
769 | * @param string|null $propertyPath |
||
770 | * @param string $encoding |
||
771 | * @return bool |
||
772 | * @throws \Assert\AssertionFailedException |
||
773 | */ |
||
774 | public static function betweenLength($value, $minLength, $maxLength, $message = null, $propertyPath = null, $encoding = 'utf8') |
||
804 | |||
805 | /** |
||
806 | * Assert that string starts with a sequence of chars. |
||
807 | * |
||
808 | * @param mixed $string |
||
809 | * @param string $needle |
||
810 | * @param string|null $message |
||
811 | * @param string|null $propertyPath |
||
812 | * @param string $encoding |
||
813 | * @return bool |
||
814 | * @throws \Assert\AssertionFailedException |
||
815 | */ |
||
816 | public static function startsWith($string, $needle, $message = null, $propertyPath = null, $encoding = 'utf8') |
||
833 | |||
834 | /** |
||
835 | * Assert that string ends with a sequence of chars. |
||
836 | * |
||
837 | * @param mixed $string |
||
838 | * @param string $needle |
||
839 | * @param string|null $message |
||
840 | * @param string|null $propertyPath |
||
841 | * @param string $encoding |
||
842 | * @return bool |
||
843 | * @throws \Assert\AssertionFailedException |
||
844 | */ |
||
845 | public static function endsWith($string, $needle, $message = null, $propertyPath = null, $encoding = 'utf8') |
||
864 | |||
865 | /** |
||
866 | * Assert that string contains a sequence of chars. |
||
867 | * |
||
868 | * @param mixed $string |
||
869 | * @param string $needle |
||
870 | * @param string|null $message |
||
871 | * @param string|null $propertyPath |
||
872 | * @param string $encoding |
||
873 | * @return bool |
||
874 | * @throws \Assert\AssertionFailedException |
||
875 | */ |
||
876 | public static function contains($string, $needle, $message = null, $propertyPath = null, $encoding = 'utf8') |
||
893 | |||
894 | /** |
||
895 | * Assert that value is in array of choices. |
||
896 | * |
||
897 | * @param mixed $value |
||
898 | * @param array $choices |
||
899 | * @param string|null $message |
||
900 | * @param string|null $propertyPath |
||
901 | * @return bool |
||
902 | * @throws \Assert\AssertionFailedException |
||
903 | */ |
||
904 | public static function choice($value, array $choices, $message = null, $propertyPath = null) |
||
918 | |||
919 | /** |
||
920 | * Alias of {@see choice()} |
||
921 | * |
||
922 | * @param mixed $value |
||
923 | * @param array $choices |
||
924 | * @param string|null $message |
||
925 | * @param string|null $propertyPath |
||
926 | * @return bool |
||
927 | */ |
||
928 | public static function inArray($value, array $choices, $message = null, $propertyPath = null) |
||
932 | |||
933 | /** |
||
934 | * Assert that value is numeric. |
||
935 | * |
||
936 | * @param mixed $value |
||
937 | * @param string|null $message |
||
938 | * @param string|null $propertyPath |
||
939 | * @return bool |
||
940 | * @throws \Assert\AssertionFailedException |
||
941 | */ |
||
942 | public static function numeric($value, $message = null, $propertyPath = null) |
||
955 | |||
956 | /** |
||
957 | * Assert that value is an array. |
||
958 | * |
||
959 | * @param mixed $value |
||
960 | * @param string|null $message |
||
961 | * @param string|null $propertyPath |
||
962 | * @return bool |
||
963 | * @throws \Assert\AssertionFailedException |
||
964 | */ |
||
965 | public static function isArray($value, $message = null, $propertyPath = null) |
||
978 | |||
979 | /** |
||
980 | * Assert that value is an array or a traversable object. |
||
981 | * |
||
982 | * @param mixed $value |
||
983 | * @param string|null $message |
||
984 | * @param string|null $propertyPath |
||
985 | * @return bool |
||
986 | * @throws \Assert\AssertionFailedException |
||
987 | */ |
||
988 | public static function isTraversable($value, $message = null, $propertyPath = null) |
||
1001 | |||
1002 | /** |
||
1003 | * Assert that value is an array or an array-accessible object. |
||
1004 | * |
||
1005 | * @param mixed $value |
||
1006 | * @param string|null $message |
||
1007 | * @param string|null $propertyPath |
||
1008 | * @return bool |
||
1009 | * @throws \Assert\AssertionFailedException |
||
1010 | */ |
||
1011 | public static function isArrayAccessible($value, $message = null, $propertyPath = null) |
||
1024 | |||
1025 | /** |
||
1026 | * Assert that key exists in an array |
||
1027 | * |
||
1028 | * @param mixed $value |
||
1029 | * @param string|integer $key |
||
1030 | * @param string|null $message |
||
1031 | * @param string|null $propertyPath |
||
1032 | * @return bool |
||
1033 | * @throws \Assert\AssertionFailedException |
||
1034 | */ |
||
1035 | public static function keyExists($value, $key, $message = null, $propertyPath = null) |
||
1050 | |||
1051 | /** |
||
1052 | * Assert that key does not exist in an array |
||
1053 | * |
||
1054 | * @param mixed $value |
||
1055 | * @param string|integer $key |
||
1056 | * @param string|null $message |
||
1057 | * @param string|null $propertyPath |
||
1058 | * @return bool |
||
1059 | * @throws \Assert\AssertionFailedException |
||
1060 | */ |
||
1061 | public static function keyNotExists($value, $key, $message = null, $propertyPath = null) |
||
1076 | |||
1077 | /** |
||
1078 | * Assert that key exists in an array/array-accessible object using isset() |
||
1079 | * |
||
1080 | * @param mixed $value |
||
1081 | * @param string|integer $key |
||
1082 | * @param string|null $message |
||
1083 | * @param string|null $propertyPath |
||
1084 | * @return bool |
||
1085 | * @throws \Assert\AssertionFailedException |
||
1086 | */ |
||
1087 | public static function keyIsset($value, $key, $message = null, $propertyPath = null) |
||
1102 | |||
1103 | /** |
||
1104 | * Assert that key exists in an array/array-accessible object and its value is not empty. |
||
1105 | * |
||
1106 | * @param mixed $value |
||
1107 | * @param string|integer $key |
||
1108 | * @param string|null $message |
||
1109 | * @param string|null $propertyPath |
||
1110 | * @return bool |
||
1111 | * @throws \Assert\AssertionFailedException |
||
1112 | */ |
||
1113 | public static function notEmptyKey($value, $key, $message = null, $propertyPath = null) |
||
1120 | |||
1121 | /** |
||
1122 | * Assert that value is not blank |
||
1123 | * |
||
1124 | * @param mixed $value |
||
1125 | * @param string|null $message |
||
1126 | * @param string|null $propertyPath |
||
1127 | * @return bool |
||
1128 | * @throws \Assert\AssertionFailedException |
||
1129 | */ |
||
1130 | public static function notBlank($value, $message = null, $propertyPath = null) |
||
1143 | |||
1144 | /** |
||
1145 | * Assert that value is instance of given class-name. |
||
1146 | * |
||
1147 | * @param mixed $value |
||
1148 | * @param string $className |
||
1149 | * @param string|null $message |
||
1150 | * @param string|null $propertyPath |
||
1151 | * @return bool |
||
1152 | * @throws \Assert\AssertionFailedException |
||
1153 | */ |
||
1154 | public static function isInstanceOf($value, $className, $message = null, $propertyPath = null) |
||
1168 | |||
1169 | /** |
||
1170 | * Assert that value is not instance of given class-name. |
||
1171 | * |
||
1172 | * @param mixed $value |
||
1173 | * @param string $className |
||
1174 | * @param string|null $message |
||
1175 | * @param string|null $propertyPath |
||
1176 | * @return bool |
||
1177 | * @throws \Assert\AssertionFailedException |
||
1178 | */ |
||
1179 | public static function notIsInstanceOf($value, $className, $message = null, $propertyPath = null) |
||
1193 | |||
1194 | /** |
||
1195 | * Assert that value is subclass of given class-name. |
||
1196 | * |
||
1197 | * @param mixed $value |
||
1198 | * @param string $className |
||
1199 | * @param string|null $message |
||
1200 | * @param string|null $propertyPath |
||
1201 | * @return bool |
||
1202 | * @throws \Assert\AssertionFailedException |
||
1203 | */ |
||
1204 | public static function subclassOf($value, $className, $message = null, $propertyPath = null) |
||
1218 | |||
1219 | /** |
||
1220 | * Assert that value is in range of numbers. |
||
1221 | * |
||
1222 | * @param mixed $value |
||
1223 | * @param integer $minValue |
||
1224 | * @param integer $maxValue |
||
1225 | * @param string|null $message |
||
1226 | * @param string|null $propertyPath |
||
1227 | * @return bool |
||
1228 | * @throws \Assert\AssertionFailedException |
||
1229 | */ |
||
1230 | public static function range($value, $minValue, $maxValue, $message = null, $propertyPath = null) |
||
1247 | |||
1248 | /** |
||
1249 | * Assert that a value is at least as big as a given limit |
||
1250 | * |
||
1251 | * @param mixed $value |
||
1252 | * @param mixed $minValue |
||
1253 | * @param string|null $message |
||
1254 | * @param string|null $propertyPath |
||
1255 | * @return bool |
||
1256 | * @throws \Assert\AssertionFailedException |
||
1257 | */ |
||
1258 | public static function min($value, $minValue, $message = null, $propertyPath = null) |
||
1274 | |||
1275 | /** |
||
1276 | * Assert that a number is smaller as a given limit |
||
1277 | * |
||
1278 | * @param mixed $value |
||
1279 | * @param mixed $maxValue |
||
1280 | * @param string|null $message |
||
1281 | * @param string|null $propertyPath |
||
1282 | * @return bool |
||
1283 | * @throws \Assert\AssertionFailedException |
||
1284 | */ |
||
1285 | public static function max($value, $maxValue, $message = null, $propertyPath = null) |
||
1301 | |||
1302 | /** |
||
1303 | * Assert that a file exists |
||
1304 | * |
||
1305 | * @param string $value |
||
1306 | * @param string|null $message |
||
1307 | * @param string|null $propertyPath |
||
1308 | * @return bool |
||
1309 | * @throws \Assert\AssertionFailedException |
||
1310 | */ |
||
1311 | public static function file($value, $message = null, $propertyPath = null) |
||
1327 | |||
1328 | /** |
||
1329 | * Assert that a directory exists |
||
1330 | * |
||
1331 | * @param string $value |
||
1332 | * @param string|null $message |
||
1333 | * @param string|null $propertyPath |
||
1334 | * @return bool |
||
1335 | * @throws \Assert\AssertionFailedException |
||
1336 | */ |
||
1337 | public static function directory($value, $message = null, $propertyPath = null) |
||
1352 | |||
1353 | /** |
||
1354 | * Assert that the value is something readable |
||
1355 | * |
||
1356 | * @param string $value |
||
1357 | * @param string|null $message |
||
1358 | * @param string|null $propertyPath |
||
1359 | * @return bool |
||
1360 | * @throws \Assert\AssertionFailedException |
||
1361 | */ |
||
1362 | public static function readable($value, $message = null, $propertyPath = null) |
||
1377 | |||
1378 | /** |
||
1379 | * Assert that the value is something writeable |
||
1380 | * |
||
1381 | * @param string $value |
||
1382 | * @param string|null $message |
||
1383 | * @param string|null $propertyPath |
||
1384 | * @return bool |
||
1385 | * @throws \Assert\AssertionFailedException |
||
1386 | */ |
||
1387 | public static function writeable($value, $message = null, $propertyPath = null) |
||
1402 | |||
1403 | /** |
||
1404 | * Assert that value is an email adress (using input_filter/FILTER_VALIDATE_EMAIL). |
||
1405 | * |
||
1406 | * @param mixed $value |
||
1407 | * @param string|null $message |
||
1408 | * @param string|null $propertyPath |
||
1409 | * @return bool |
||
1410 | * @throws \Assert\AssertionFailedException |
||
1411 | */ |
||
1412 | public static function email($value, $message = null, $propertyPath = null) |
||
1439 | |||
1440 | /** |
||
1441 | * Assert that value is an URL. |
||
1442 | * |
||
1443 | * This code snipped was taken from the Symfony project and modified to the special demands of this method. |
||
1444 | * |
||
1445 | * @param mixed $value |
||
1446 | * @param string|null $message |
||
1447 | * @param string|null $propertyPath |
||
1448 | * @return bool |
||
1449 | * @throws \Assert\AssertionFailedException |
||
1450 | * |
||
1451 | * |
||
1452 | * @link https://github.com/symfony/Validator/blob/master/Constraints/UrlValidator.php |
||
1453 | * @link https://github.com/symfony/Validator/blob/master/Constraints/Url.php |
||
1454 | */ |
||
1455 | public static function url($value, $message = null, $propertyPath = null) |
||
1490 | |||
1491 | /** |
||
1492 | * Assert that value is alphanumeric. |
||
1493 | * |
||
1494 | * @param mixed $value |
||
1495 | * @param string|null $message |
||
1496 | * @param string|null $propertyPath |
||
1497 | * @return bool |
||
1498 | * @throws \Assert\AssertionFailedException |
||
1499 | */ |
||
1500 | public static function alnum($value, $message = null, $propertyPath = null) |
||
1515 | |||
1516 | /** |
||
1517 | * Assert that the value is boolean True. |
||
1518 | * |
||
1519 | * @param mixed $value |
||
1520 | * @param string|null $message |
||
1521 | * @param string|null $propertyPath |
||
1522 | * @return bool |
||
1523 | * @throws \Assert\AssertionFailedException |
||
1524 | */ |
||
1525 | public static function true($value, $message = null, $propertyPath = null) |
||
1538 | |||
1539 | /** |
||
1540 | * Assert that the value is boolean False. |
||
1541 | * |
||
1542 | * @param mixed $value |
||
1543 | * @param string|null $message |
||
1544 | * @param string|null $propertyPath |
||
1545 | * @return bool |
||
1546 | * @throws \Assert\AssertionFailedException |
||
1547 | */ |
||
1548 | public static function false($value, $message = null, $propertyPath = null) |
||
1561 | |||
1562 | /** |
||
1563 | * Assert that the class exists. |
||
1564 | * |
||
1565 | * @param mixed $value |
||
1566 | * @param string|null $message |
||
1567 | * @param string|null $propertyPath |
||
1568 | * @return bool |
||
1569 | * @throws \Assert\AssertionFailedException |
||
1570 | */ |
||
1571 | public static function classExists($value, $message = null, $propertyPath = null) |
||
1584 | |||
1585 | /** |
||
1586 | * Assert that the interface exists. |
||
1587 | * |
||
1588 | * @param mixed $value |
||
1589 | * @param string|null $message |
||
1590 | * @param string|null $propertyPath |
||
1591 | * @return bool |
||
1592 | * @throws \Assert\AssertionFailedException |
||
1593 | */ |
||
1594 | public static function interfaceExists($value, $message = null, $propertyPath = null) |
||
1607 | |||
1608 | /** |
||
1609 | * Assert that the class implements the interface |
||
1610 | * |
||
1611 | * @param mixed $class |
||
1612 | * @param string $interfaceName |
||
1613 | * @param string|null $message |
||
1614 | * @param string|null $propertyPath |
||
1615 | * @return bool |
||
1616 | * @throws \Assert\AssertionFailedException |
||
1617 | */ |
||
1618 | public static function implementsInterface($class, $interfaceName, $message = null, $propertyPath = null) |
||
1633 | |||
1634 | /** |
||
1635 | * Assert that the given string is a valid json string. |
||
1636 | * |
||
1637 | * NOTICE: |
||
1638 | * Since this does a json_decode to determine its validity |
||
1639 | * you probably should consider, when using the variable |
||
1640 | * content afterwards, just to decode and check for yourself instead |
||
1641 | * of using this assertion. |
||
1642 | * |
||
1643 | * @param mixed $value |
||
1644 | * @param string|null $message |
||
1645 | * @param string|null $propertyPath |
||
1646 | * @return bool |
||
1647 | * @throws \Assert\AssertionFailedException |
||
1648 | */ |
||
1649 | public static function isJsonString($value, $message = null, $propertyPath = null) |
||
1662 | |||
1663 | /** |
||
1664 | * Assert that the given string is a valid UUID |
||
1665 | * |
||
1666 | * Uses code from {@link https://github.com/ramsey/uuid} that is MIT licensed. |
||
1667 | * |
||
1668 | * @param string $value |
||
1669 | * @param string|null $message |
||
1670 | * @param string|null $propertyPath |
||
1671 | * @return bool |
||
1672 | * @throws \Assert\AssertionFailedException |
||
1673 | */ |
||
1674 | public static function uuid($value, $message = null, $propertyPath = null) |
||
1693 | |||
1694 | /** |
||
1695 | * Assert that the given string is a valid E164 Phone Number |
||
1696 | * |
||
1697 | * @link https://en.wikipedia.org/wiki/E.164 |
||
1698 | * |
||
1699 | * @param string $value |
||
1700 | * @param string|null $message |
||
1701 | * @param string|null $propertyPath |
||
1702 | * @return bool |
||
1703 | * @throws \Assert\AssertionFailedException |
||
1704 | */ |
||
1705 | public static function e164($value, $message = null, $propertyPath = null) |
||
1718 | |||
1719 | /** |
||
1720 | * Assert that the count of countable is equal to count. |
||
1721 | * |
||
1722 | * @param array|\Countable $countable |
||
1723 | * @param int $count |
||
1724 | * @param string $message |
||
1725 | * @param string $propertyPath |
||
1726 | * @return bool |
||
1727 | * @throws \Assert\AssertionFailedException |
||
1728 | */ |
||
1729 | public static function count($countable, $count, $message = null, $propertyPath = null) |
||
1742 | |||
1743 | /** |
||
1744 | * static call handler to implement: |
||
1745 | * - "null or assertion" delegation |
||
1746 | * - "all" delegation |
||
1747 | * |
||
1748 | * @param string $method |
||
1749 | * @param array $args |
||
1750 | * |
||
1751 | * @return bool|mixed |
||
1752 | */ |
||
1753 | public static function __callStatic($method, $args) |
||
1789 | |||
1790 | /** |
||
1791 | * Determines if the values array has every choice as key and that this choice has content. |
||
1792 | * |
||
1793 | * @param array $values |
||
1794 | * @param array $choices |
||
1795 | * @param null $message |
||
1796 | * @param null $propertyPath |
||
1797 | * @return bool |
||
1798 | */ |
||
1799 | public static function choicesNotEmpty(array $values, array $choices, $message = null, $propertyPath = null) |
||
1809 | |||
1810 | /** |
||
1811 | * Determines that the named method is defined in the provided object. |
||
1812 | * |
||
1813 | * @param string $value |
||
1814 | * @param mixed $object |
||
1815 | * @param null $message |
||
1816 | * @param null $propertyPath |
||
1817 | * @return bool |
||
1818 | * @throws |
||
1819 | */ |
||
1820 | public static function methodExists($value, $object, $message = null, $propertyPath = null) |
||
1835 | |||
1836 | /** |
||
1837 | * Determines that the provided value is an object. |
||
1838 | * |
||
1839 | * @param mixed $value |
||
1840 | * @param null $message |
||
1841 | * @param null $propertyPath |
||
1842 | * @return bool |
||
1843 | */ |
||
1844 | public static function isObject($value, $message = null, $propertyPath = null) |
||
1857 | |||
1858 | /** |
||
1859 | * Determines if the value is less than given limit. |
||
1860 | * |
||
1861 | * @param mixed $value |
||
1862 | * @param mixed $limit |
||
1863 | * @param null $message |
||
1864 | * @param null $propertyPath |
||
1865 | * @return bool |
||
1866 | */ |
||
1867 | public static function lessThan($value, $limit, $message = null, $propertyPath = null) |
||
1881 | |||
1882 | /** |
||
1883 | * Determines if the value is less or than given limit. |
||
1884 | * |
||
1885 | * @param mixed $value |
||
1886 | * @param mixed $limit |
||
1887 | * @param null $message |
||
1888 | * @param null $propertyPath |
||
1889 | * @return bool |
||
1890 | */ |
||
1891 | public static function lessOrEqualThan($value, $limit, $message = null, $propertyPath = null) |
||
1905 | |||
1906 | /** |
||
1907 | * Determines if the value is greater than given limit. |
||
1908 | * |
||
1909 | * @param mixed $value |
||
1910 | * @param mixed $limit |
||
1911 | * @param null $message |
||
1912 | * @param null $propertyPath |
||
1913 | * @return bool |
||
1914 | */ |
||
1915 | public static function greaterThan($value, $limit, $message = null, $propertyPath = null) |
||
1929 | |||
1930 | /** |
||
1931 | * Determines if the value is greater or equal than given limit. |
||
1932 | * |
||
1933 | * @param mixed $value |
||
1934 | * @param mixed $limit |
||
1935 | * @param null $message |
||
1936 | * @param null $propertyPath |
||
1937 | * @return bool |
||
1938 | */ |
||
1939 | public static function greaterOrEqualThan($value, $limit, $message = null, $propertyPath = null) |
||
1953 | |||
1954 | /** |
||
1955 | * Assert that a value is greater or equal than a lower limit, and less than or equal to an upper limit. |
||
1956 | * |
||
1957 | * @param mixed $value |
||
1958 | * @param mixed $lowerLimit |
||
1959 | * @param mixed $upperLimit |
||
1960 | * @param string $message |
||
1961 | * @param string $propertyPath |
||
1962 | * @return bool |
||
1963 | */ |
||
1964 | public static function between($value, $lowerLimit, $upperLimit, $message = null, $propertyPath = null) |
||
1979 | |||
1980 | /** |
||
1981 | * Assert that a value is greater than a lower limit, and less than an upper limit. |
||
1982 | * |
||
1983 | * @param mixed $value |
||
1984 | * @param mixed $lowerLimit |
||
1985 | * @param mixed $upperLimit |
||
1986 | * @param string $message |
||
1987 | * @param string $propertyPath |
||
1988 | * @return bool |
||
1989 | */ |
||
1990 | public static function betweenExclusive($value, $lowerLimit, $upperLimit, $message = null, $propertyPath = null) |
||
2005 | |||
2006 | /** |
||
2007 | * Assert that extension is loaded. |
||
2008 | * |
||
2009 | * @param mixed $value |
||
2010 | * @param string|null $message |
||
2011 | * @param string|null $propertyPath |
||
2012 | * @return bool |
||
2013 | * @throws \Assert\AssertionFailedException |
||
2014 | */ |
||
2015 | public static function extensionLoaded($value, $message = null, $propertyPath = null) |
||
2028 | |||
2029 | /** |
||
2030 | * Assert that date is valid and corresponds to the given format. |
||
2031 | * |
||
2032 | * @param string $value |
||
2033 | * @param string $format supports all of the options date(), except for the following: |
||
2034 | * N, w, W, t, L, o, B, a, A, g, h, I, O, P, Z, c, r. |
||
2035 | * @param string|null $message |
||
2036 | * @param string|null $propertyPath |
||
2037 | * @return bool |
||
2038 | * |
||
2039 | * @link http://php.net/manual/function.date.php#refsect1-function.date-parameters |
||
2040 | */ |
||
2041 | public static function date($value, $format, $message = null, $propertyPath = null) |
||
2060 | |||
2061 | /** |
||
2062 | * Determines that the provided value is callable. |
||
2063 | * |
||
2064 | * @param mixed $value |
||
2065 | * @param null $message |
||
2066 | * @param null $propertyPath |
||
2067 | * @return bool |
||
2068 | */ |
||
2069 | public static function isCallable($value, $message = null, $propertyPath = null) |
||
2082 | |||
2083 | /** |
||
2084 | * Assert that the provided value is valid according to a callback. |
||
2085 | * |
||
2086 | * If the callback returns `false` the assertion will fail. |
||
2087 | * |
||
2088 | * @param mixed $value |
||
2089 | * @param callable $callback |
||
2090 | * @param string|null $message |
||
2091 | * @param string|null $propertyPath |
||
2092 | * @return bool |
||
2093 | */ |
||
2094 | public static function satisfy($value, $callback, $message = null, $propertyPath = null) |
||
2109 | |||
2110 | /** |
||
2111 | * Assert that value is an IPv4 or IPv6 address |
||
2112 | * (using input_filter/FILTER_VALIDATE_IP). |
||
2113 | * |
||
2114 | * @param string $value |
||
2115 | * @param null|int $flag |
||
2116 | * @param string|null $message |
||
2117 | * @param string|null $propertyPath |
||
2118 | * @return bool |
||
2119 | * |
||
2120 | * @link http://php.net/manual/filter.filters.flags.php |
||
2121 | */ |
||
2122 | public static function ip($value, $flag = null, $message = null, $propertyPath = null) |
||
2135 | |||
2136 | /** |
||
2137 | * Assert that value is an IPv4 address |
||
2138 | * (using input_filter/FILTER_VALIDATE_IP). |
||
2139 | * |
||
2140 | * @param string $value |
||
2141 | * @param null|int $flag |
||
2142 | * @param string|null $message |
||
2143 | * @param string|null $propertyPath |
||
2144 | * @return bool |
||
2145 | * |
||
2146 | * @link http://php.net/manual/filter.filters.flags.php |
||
2147 | */ |
||
2148 | public static function ipv4($value, $flag = null, $message = null, $propertyPath = null) |
||
2154 | |||
2155 | /** |
||
2156 | * Assert that value is an IPv6 address |
||
2157 | * (using input_filter/FILTER_VALIDATE_IP). |
||
2158 | * |
||
2159 | * @param string $value |
||
2160 | * @param null|int $flag |
||
2161 | * @param string|null $message |
||
2162 | * @param string|null $propertyPath |
||
2163 | * @return bool |
||
2164 | * |
||
2165 | * @link http://php.net/manual/filter.filters.flags.php |
||
2166 | */ |
||
2167 | public static function ipv6($value, $flag = null, $message = null, $propertyPath = null) |
||
2173 | |||
2174 | /** |
||
2175 | * Make a string version of a value. |
||
2176 | * |
||
2177 | * @param mixed $value |
||
2178 | * @return string |
||
2179 | */ |
||
2180 | protected static function stringify($value) |
||
2214 | |||
2215 | /** |
||
2216 | * Assert that a constant is defined. |
||
2217 | * |
||
2218 | * @param mixed $constant |
||
2219 | * @param string|null $message |
||
2220 | * @param string|null $propertyPath |
||
2221 | * @return bool |
||
2222 | * @throws \Assert\AssertionFailedException |
||
2223 | */ |
||
2224 | public static function defined($constant, $message = null, $propertyPath = null) |
||
2234 | } |
||
2235 |
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.