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 |
||
171 | class Assertion |
||
172 | { |
||
173 | const INVALID_FLOAT = 9; |
||
174 | const INVALID_INTEGER = 10; |
||
175 | const INVALID_DIGIT = 11; |
||
176 | const INVALID_INTEGERISH = 12; |
||
177 | const INVALID_BOOLEAN = 13; |
||
178 | const VALUE_EMPTY = 14; |
||
179 | const VALUE_NULL = 15; |
||
180 | const VALUE_NOT_NULL = 25; |
||
181 | const INVALID_STRING = 16; |
||
182 | const INVALID_REGEX = 17; |
||
183 | const INVALID_MIN_LENGTH = 18; |
||
184 | const INVALID_MAX_LENGTH = 19; |
||
185 | const INVALID_STRING_START = 20; |
||
186 | const INVALID_STRING_CONTAINS = 21; |
||
187 | const INVALID_CHOICE = 22; |
||
188 | const INVALID_NUMERIC = 23; |
||
189 | const INVALID_ARRAY = 24; |
||
190 | const INVALID_KEY_EXISTS = 26; |
||
191 | const INVALID_NOT_BLANK = 27; |
||
192 | const INVALID_INSTANCE_OF = 28; |
||
193 | const INVALID_SUBCLASS_OF = 29; |
||
194 | const INVALID_RANGE = 30; |
||
195 | const INVALID_ALNUM = 31; |
||
196 | const INVALID_TRUE = 32; |
||
197 | const INVALID_EQ = 33; |
||
198 | const INVALID_SAME = 34; |
||
199 | const INVALID_MIN = 35; |
||
200 | const INVALID_MAX = 36; |
||
201 | const INVALID_LENGTH = 37; |
||
202 | const INVALID_FALSE = 38; |
||
203 | const INVALID_STRING_END = 39; |
||
204 | const INVALID_UUID = 40; |
||
205 | const INVALID_COUNT = 41; |
||
206 | const INVALID_NOT_EQ = 42; |
||
207 | const INVALID_NOT_SAME = 43; |
||
208 | const INVALID_TRAVERSABLE = 44; |
||
209 | const INVALID_ARRAY_ACCESSIBLE = 45; |
||
210 | const INVALID_KEY_ISSET = 46; |
||
211 | const INVALID_VALUE_IN_ARRAY = 47; |
||
212 | const INVALID_E164 = 48; |
||
213 | const INVALID_DIRECTORY = 101; |
||
214 | const INVALID_FILE = 102; |
||
215 | const INVALID_READABLE = 103; |
||
216 | const INVALID_WRITEABLE = 104; |
||
217 | const INVALID_CLASS = 105; |
||
218 | const INVALID_INTERFACE = 106; |
||
219 | const INVALID_EMAIL = 201; |
||
220 | const INTERFACE_NOT_IMPLEMENTED = 202; |
||
221 | const INVALID_URL = 203; |
||
222 | const INVALID_NOT_INSTANCE_OF = 204; |
||
223 | const VALUE_NOT_EMPTY = 205; |
||
224 | const INVALID_JSON_STRING = 206; |
||
225 | const INVALID_OBJECT = 207; |
||
226 | const INVALID_METHOD = 208; |
||
227 | const INVALID_SCALAR = 209; |
||
228 | const INVALID_LESS = 210; |
||
229 | const INVALID_LESS_OR_EQUAL = 211; |
||
230 | const INVALID_GREATER = 212; |
||
231 | const INVALID_GREATER_OR_EQUAL = 213; |
||
232 | const INVALID_DATE = 214; |
||
233 | const INVALID_CALLABLE = 215; |
||
234 | const INVALID_KEY_NOT_EXISTS = 216; |
||
235 | const INVALID_SATISFY = 217; |
||
236 | const INVALID_IP = 218; |
||
237 | const INVALID_BETWEEN = 219; |
||
238 | const INVALID_BETWEEN_EXCLUSIVE = 220; |
||
239 | const INVALID_CONSTANT = 221; |
||
240 | |||
241 | /** |
||
242 | * Exception to throw when an assertion failed. |
||
243 | * |
||
244 | * @var string |
||
245 | */ |
||
246 | protected static $exceptionClass = 'Assert\InvalidArgumentException'; |
||
247 | |||
248 | /** |
||
249 | * Helper method that handles building the assertion failure exceptions. |
||
250 | * They are returned from this method so that the stack trace still shows |
||
251 | * the assertions method. |
||
252 | * |
||
253 | * @param mixed $value |
||
254 | * @param string $message |
||
255 | * @param int $code |
||
256 | * @param string|null $propertyPath |
||
257 | * @param array $constraints |
||
258 | * |
||
259 | * @return mixed |
||
|
|||
260 | */ |
||
261 | protected static function createException($value, $message, $code, $propertyPath, array $constraints = array()) |
||
262 | { |
||
263 | $exceptionClass = static::$exceptionClass; |
||
264 | return new $exceptionClass($message, $code, $propertyPath, $value, $constraints); |
||
265 | } |
||
266 | |||
267 | /** |
||
268 | * Assert that two values are equal (using == ). |
||
269 | * |
||
270 | * @param mixed $value |
||
271 | * @param mixed $value2 |
||
272 | * @param string|null $message |
||
273 | * @param string|null $propertyPath |
||
274 | * @return bool |
||
275 | * @throws \Assert\AssertionFailedException |
||
276 | */ |
||
277 | public static function eq($value, $value2, $message = null, $propertyPath = null) |
||
291 | |||
292 | /** |
||
293 | * Assert that two values are the same (using ===). |
||
294 | * |
||
295 | * @param mixed $value |
||
296 | * @param mixed $value2 |
||
297 | * @param string|null $message |
||
298 | * @param string|null $propertyPath |
||
299 | * @return bool |
||
300 | * @throws \Assert\AssertionFailedException |
||
301 | */ |
||
302 | public static function same($value, $value2, $message = null, $propertyPath = null) |
||
316 | |||
317 | /** |
||
318 | * Assert that two values are not equal (using == ). |
||
319 | * |
||
320 | * @param mixed $value1 |
||
321 | * @param mixed $value2 |
||
322 | * @param string|null $message |
||
323 | * @param string|null $propertyPath |
||
324 | * @return bool |
||
325 | * @throws \Assert\AssertionFailedException |
||
326 | */ |
||
327 | public static function notEq($value1, $value2, $message = null, $propertyPath = null) |
||
340 | |||
341 | /** |
||
342 | * Assert that two values are not the same (using === ). |
||
343 | * |
||
344 | * @param mixed $value1 |
||
345 | * @param mixed $value2 |
||
346 | * @param string|null $message |
||
347 | * @param string|null $propertyPath |
||
348 | * @return bool |
||
349 | * @throws \Assert\AssertionFailedException |
||
350 | */ |
||
351 | public static function notSame($value1, $value2, $message = null, $propertyPath = null) |
||
364 | |||
365 | /** |
||
366 | * Assert that value is not in array of choices. |
||
367 | * |
||
368 | * @param mixed $value |
||
369 | * @param array $choices |
||
370 | * @param string|null $message |
||
371 | * @param string|null $propertyPath |
||
372 | * @return bool |
||
373 | * @throws \Assert\AssertionFailedException |
||
374 | */ |
||
375 | public static function notInArray($value, array $choices, $message = null, $propertyPath = null) |
||
388 | |||
389 | /** |
||
390 | * Assert that value is a php integer. |
||
391 | * |
||
392 | * @param mixed $value |
||
393 | * @param string|null $message |
||
394 | * @param string|null $propertyPath |
||
395 | * @return bool |
||
396 | * @throws \Assert\AssertionFailedException |
||
397 | */ |
||
398 | public static function integer($value, $message = null, $propertyPath = null) |
||
411 | |||
412 | /** |
||
413 | * Assert that value is a php float. |
||
414 | * |
||
415 | * @param mixed $value |
||
416 | * @param string|null $message |
||
417 | * @param string|null $propertyPath |
||
418 | * @return bool |
||
419 | * @throws \Assert\AssertionFailedException |
||
420 | */ |
||
421 | public static function float($value, $message = null, $propertyPath = null) |
||
434 | |||
435 | /** |
||
436 | * Validates if an integer or integerish is a digit. |
||
437 | * |
||
438 | * @param mixed $value |
||
439 | * @param string|null $message |
||
440 | * @param string|null $propertyPath |
||
441 | * @return bool |
||
442 | * @throws \Assert\AssertionFailedException |
||
443 | */ |
||
444 | public static function digit($value, $message = null, $propertyPath = null) |
||
457 | |||
458 | /** |
||
459 | * Assert that value is a php integer'ish. |
||
460 | * |
||
461 | * @param mixed $value |
||
462 | * @param string|null $message |
||
463 | * @param string|null $propertyPath |
||
464 | * @return bool |
||
465 | * @throws \Assert\AssertionFailedException |
||
466 | */ |
||
467 | public static function integerish($value, $message = null, $propertyPath = null) |
||
480 | |||
481 | /** |
||
482 | * Assert that value is php boolean |
||
483 | * |
||
484 | * @param mixed $value |
||
485 | * @param string|null $message |
||
486 | * @param string|null $propertyPath |
||
487 | * @return bool |
||
488 | * @throws \Assert\AssertionFailedException |
||
489 | */ |
||
490 | public static function boolean($value, $message = null, $propertyPath = null) |
||
503 | |||
504 | /** |
||
505 | * Assert that value is a PHP scalar |
||
506 | * |
||
507 | * @param mixed $value |
||
508 | * @param string|null $message |
||
509 | * @param string|null $propertyPath |
||
510 | * @return bool |
||
511 | * @throws \Assert\AssertionFailedException |
||
512 | */ |
||
513 | public static function scalar($value, $message = null, $propertyPath = null) |
||
526 | |||
527 | /** |
||
528 | * Assert that value is not empty |
||
529 | * |
||
530 | * @param mixed $value |
||
531 | * @param string|null $message |
||
532 | * @param string|null $propertyPath |
||
533 | * @return bool |
||
534 | * @throws \Assert\AssertionFailedException |
||
535 | */ |
||
536 | public static function notEmpty($value, $message = null, $propertyPath = null) |
||
549 | |||
550 | /** |
||
551 | * Assert that value is empty |
||
552 | * |
||
553 | * @param mixed $value |
||
554 | * @param string|null $message |
||
555 | * @param string|null $propertyPath |
||
556 | * @return bool |
||
557 | * @throws \Assert\AssertionFailedException |
||
558 | */ |
||
559 | public static function noContent($value, $message = null, $propertyPath = null) |
||
572 | |||
573 | /** |
||
574 | * Assert that value is null |
||
575 | * |
||
576 | * @param mixed $value |
||
577 | * @param string|null $message |
||
578 | * @param string|null $propertyPath |
||
579 | * @return bool |
||
580 | * @throws \Assert\AssertionFailedException |
||
581 | */ |
||
582 | public static function null($value, $message = null, $propertyPath = null) |
||
595 | |||
596 | /** |
||
597 | * Assert that value is not null |
||
598 | * |
||
599 | * @param mixed $value |
||
600 | * @param string|null $message |
||
601 | * @param string|null $propertyPath |
||
602 | * @return bool |
||
603 | * @throws \Assert\AssertionFailedException |
||
604 | */ |
||
605 | public static function notNull($value, $message = null, $propertyPath = null) |
||
618 | |||
619 | /** |
||
620 | * Assert that value is a string |
||
621 | * |
||
622 | * @param mixed $value |
||
623 | * @param string|null $message |
||
624 | * @param string|null $propertyPath |
||
625 | * @return bool |
||
626 | * @throws \Assert\AssertionFailedException |
||
627 | */ |
||
628 | public static function string($value, $message = null, $propertyPath = null) |
||
642 | |||
643 | /** |
||
644 | * Assert that value matches a regex |
||
645 | * |
||
646 | * @param mixed $value |
||
647 | * @param string $pattern |
||
648 | * @param string|null $message |
||
649 | * @param string|null $propertyPath |
||
650 | * @return bool |
||
651 | * @throws \Assert\AssertionFailedException |
||
652 | */ |
||
653 | public static function regex($value, $pattern, $message = null, $propertyPath = null) |
||
668 | |||
669 | /** |
||
670 | * Assert that string has a given length. |
||
671 | * |
||
672 | * @param mixed $value |
||
673 | * @param int $length |
||
674 | * @param string|null $message |
||
675 | * @param string|null $propertyPath |
||
676 | * @param string $encoding |
||
677 | * @return bool |
||
678 | * @throws \Assert\AssertionFailedException |
||
679 | */ |
||
680 | public static function length($value, $length, $message = null, $propertyPath = null, $encoding = 'utf8') |
||
698 | |||
699 | /** |
||
700 | * Assert that a string is at least $minLength chars long. |
||
701 | * |
||
702 | * @param mixed $value |
||
703 | * @param int $minLength |
||
704 | * @param string|null $message |
||
705 | * @param string|null $propertyPath |
||
706 | * @param string $encoding |
||
707 | * @return bool |
||
708 | * @throws \Assert\AssertionFailedException |
||
709 | */ |
||
710 | public static function minLength($value, $minLength, $message = null, $propertyPath = null, $encoding = 'utf8') |
||
728 | |||
729 | /** |
||
730 | * Assert that string value is not longer than $maxLength chars. |
||
731 | * |
||
732 | * @param mixed $value |
||
733 | * @param integer $maxLength |
||
734 | * @param string|null $message |
||
735 | * @param string|null $propertyPath |
||
736 | * @param string $encoding |
||
737 | * @return bool |
||
738 | * @throws \Assert\AssertionFailedException |
||
739 | */ |
||
740 | public static function maxLength($value, $maxLength, $message = null, $propertyPath = null, $encoding = 'utf8') |
||
758 | |||
759 | /** |
||
760 | * Assert that string length is between min,max lengths. |
||
761 | * |
||
762 | * @param mixed $value |
||
763 | * @param integer $minLength |
||
764 | * @param integer $maxLength |
||
765 | * @param string|null $message |
||
766 | * @param string|null $propertyPath |
||
767 | * @param string $encoding |
||
768 | * @return bool |
||
769 | * @throws \Assert\AssertionFailedException |
||
770 | */ |
||
771 | public static function betweenLength($value, $minLength, $maxLength, $message = null, $propertyPath = null, $encoding = 'utf8') |
||
801 | |||
802 | /** |
||
803 | * Assert that string starts with a sequence of chars. |
||
804 | * |
||
805 | * @param mixed $string |
||
806 | * @param string $needle |
||
807 | * @param string|null $message |
||
808 | * @param string|null $propertyPath |
||
809 | * @param string $encoding |
||
810 | * @return bool |
||
811 | * @throws \Assert\AssertionFailedException |
||
812 | */ |
||
813 | public static function startsWith($string, $needle, $message = null, $propertyPath = null, $encoding = 'utf8') |
||
830 | |||
831 | /** |
||
832 | * Assert that string ends with a sequence of chars. |
||
833 | * |
||
834 | * @param mixed $string |
||
835 | * @param string $needle |
||
836 | * @param string|null $message |
||
837 | * @param string|null $propertyPath |
||
838 | * @param string $encoding |
||
839 | * @return bool |
||
840 | * @throws \Assert\AssertionFailedException |
||
841 | */ |
||
842 | public static function endsWith($string, $needle, $message = null, $propertyPath = null, $encoding = 'utf8') |
||
861 | |||
862 | /** |
||
863 | * Assert that string contains a sequence of chars. |
||
864 | * |
||
865 | * @param mixed $string |
||
866 | * @param string $needle |
||
867 | * @param string|null $message |
||
868 | * @param string|null $propertyPath |
||
869 | * @param string $encoding |
||
870 | * @return bool |
||
871 | * @throws \Assert\AssertionFailedException |
||
872 | */ |
||
873 | public static function contains($string, $needle, $message = null, $propertyPath = null, $encoding = 'utf8') |
||
890 | |||
891 | /** |
||
892 | * Assert that value is in array of choices. |
||
893 | * |
||
894 | * @param mixed $value |
||
895 | * @param array $choices |
||
896 | * @param string|null $message |
||
897 | * @param string|null $propertyPath |
||
898 | * @return bool |
||
899 | * @throws \Assert\AssertionFailedException |
||
900 | */ |
||
901 | public static function choice($value, array $choices, $message = null, $propertyPath = null) |
||
915 | |||
916 | /** |
||
917 | * Alias of {@see choice()} |
||
918 | * |
||
919 | * @param mixed $value |
||
920 | * @param array $choices |
||
921 | * @param string|null $message |
||
922 | * @param string|null $propertyPath |
||
923 | * @return bool |
||
924 | */ |
||
925 | public static function inArray($value, array $choices, $message = null, $propertyPath = null) |
||
929 | |||
930 | /** |
||
931 | * Assert that value is numeric. |
||
932 | * |
||
933 | * @param mixed $value |
||
934 | * @param string|null $message |
||
935 | * @param string|null $propertyPath |
||
936 | * @return bool |
||
937 | * @throws \Assert\AssertionFailedException |
||
938 | */ |
||
939 | public static function numeric($value, $message = null, $propertyPath = null) |
||
952 | |||
953 | /** |
||
954 | * Assert that value is an array. |
||
955 | * |
||
956 | * @param mixed $value |
||
957 | * @param string|null $message |
||
958 | * @param string|null $propertyPath |
||
959 | * @return bool |
||
960 | * @throws \Assert\AssertionFailedException |
||
961 | */ |
||
962 | public static function isArray($value, $message = null, $propertyPath = null) |
||
975 | |||
976 | /** |
||
977 | * Assert that value is an array or a traversable object. |
||
978 | * |
||
979 | * @param mixed $value |
||
980 | * @param string|null $message |
||
981 | * @param string|null $propertyPath |
||
982 | * @return bool |
||
983 | * @throws \Assert\AssertionFailedException |
||
984 | */ |
||
985 | public static function isTraversable($value, $message = null, $propertyPath = null) |
||
998 | |||
999 | /** |
||
1000 | * Assert that value is an array or an array-accessible object. |
||
1001 | * |
||
1002 | * @param mixed $value |
||
1003 | * @param string|null $message |
||
1004 | * @param string|null $propertyPath |
||
1005 | * @return bool |
||
1006 | * @throws \Assert\AssertionFailedException |
||
1007 | */ |
||
1008 | public static function isArrayAccessible($value, $message = null, $propertyPath = null) |
||
1021 | |||
1022 | /** |
||
1023 | * Assert that key exists in an array |
||
1024 | * |
||
1025 | * @param mixed $value |
||
1026 | * @param string|integer $key |
||
1027 | * @param string|null $message |
||
1028 | * @param string|null $propertyPath |
||
1029 | * @return bool |
||
1030 | * @throws \Assert\AssertionFailedException |
||
1031 | */ |
||
1032 | public static function keyExists($value, $key, $message = null, $propertyPath = null) |
||
1047 | |||
1048 | /** |
||
1049 | * Assert that key does not exist in an array |
||
1050 | * |
||
1051 | * @param mixed $value |
||
1052 | * @param string|integer $key |
||
1053 | * @param string|null $message |
||
1054 | * @param string|null $propertyPath |
||
1055 | * @return bool |
||
1056 | * @throws \Assert\AssertionFailedException |
||
1057 | */ |
||
1058 | public static function keyNotExists($value, $key, $message = null, $propertyPath = null) |
||
1073 | |||
1074 | /** |
||
1075 | * Assert that key exists in an array/array-accessible object using isset() |
||
1076 | * |
||
1077 | * @param mixed $value |
||
1078 | * @param string|integer $key |
||
1079 | * @param string|null $message |
||
1080 | * @param string|null $propertyPath |
||
1081 | * @return bool |
||
1082 | * @throws \Assert\AssertionFailedException |
||
1083 | */ |
||
1084 | public static function keyIsset($value, $key, $message = null, $propertyPath = null) |
||
1099 | |||
1100 | /** |
||
1101 | * Assert that key exists in an array/array-accessible object and its value is not empty. |
||
1102 | * |
||
1103 | * @param mixed $value |
||
1104 | * @param string|integer $key |
||
1105 | * @param string|null $message |
||
1106 | * @param string|null $propertyPath |
||
1107 | * @return bool |
||
1108 | * @throws \Assert\AssertionFailedException |
||
1109 | */ |
||
1110 | public static function notEmptyKey($value, $key, $message = null, $propertyPath = null) |
||
1117 | |||
1118 | /** |
||
1119 | * Assert that value is not blank |
||
1120 | * |
||
1121 | * @param mixed $value |
||
1122 | * @param string|null $message |
||
1123 | * @param string|null $propertyPath |
||
1124 | * @return bool |
||
1125 | * @throws \Assert\AssertionFailedException |
||
1126 | */ |
||
1127 | public static function notBlank($value, $message = null, $propertyPath = null) |
||
1140 | |||
1141 | /** |
||
1142 | * Assert that value is instance of given class-name. |
||
1143 | * |
||
1144 | * @param mixed $value |
||
1145 | * @param string $className |
||
1146 | * @param string|null $message |
||
1147 | * @param string|null $propertyPath |
||
1148 | * @return bool |
||
1149 | * @throws \Assert\AssertionFailedException |
||
1150 | */ |
||
1151 | public static function isInstanceOf($value, $className, $message = null, $propertyPath = null) |
||
1165 | |||
1166 | /** |
||
1167 | * Assert that value is not instance of given class-name. |
||
1168 | * |
||
1169 | * @param mixed $value |
||
1170 | * @param string $className |
||
1171 | * @param string|null $message |
||
1172 | * @param string|null $propertyPath |
||
1173 | * @return bool |
||
1174 | * @throws \Assert\AssertionFailedException |
||
1175 | */ |
||
1176 | public static function notIsInstanceOf($value, $className, $message = null, $propertyPath = null) |
||
1190 | |||
1191 | /** |
||
1192 | * Assert that value is subclass of given class-name. |
||
1193 | * |
||
1194 | * @param mixed $value |
||
1195 | * @param string $className |
||
1196 | * @param string|null $message |
||
1197 | * @param string|null $propertyPath |
||
1198 | * @return bool |
||
1199 | * @throws \Assert\AssertionFailedException |
||
1200 | */ |
||
1201 | public static function subclassOf($value, $className, $message = null, $propertyPath = null) |
||
1215 | |||
1216 | /** |
||
1217 | * Assert that value is in range of numbers. |
||
1218 | * |
||
1219 | * @param mixed $value |
||
1220 | * @param integer $minValue |
||
1221 | * @param integer $maxValue |
||
1222 | * @param string|null $message |
||
1223 | * @param string|null $propertyPath |
||
1224 | * @return bool |
||
1225 | * @throws \Assert\AssertionFailedException |
||
1226 | */ |
||
1227 | public static function range($value, $minValue, $maxValue, $message = null, $propertyPath = null) |
||
1244 | |||
1245 | /** |
||
1246 | * Assert that a value is at least as big as a given limit |
||
1247 | * |
||
1248 | * @param mixed $value |
||
1249 | * @param mixed $minValue |
||
1250 | * @param string|null $message |
||
1251 | * @param string|null $propertyPath |
||
1252 | * @return bool |
||
1253 | * @throws \Assert\AssertionFailedException |
||
1254 | */ |
||
1255 | public static function min($value, $minValue, $message = null, $propertyPath = null) |
||
1271 | |||
1272 | /** |
||
1273 | * Assert that a number is smaller as a given limit |
||
1274 | * |
||
1275 | * @param mixed $value |
||
1276 | * @param mixed $maxValue |
||
1277 | * @param string|null $message |
||
1278 | * @param string|null $propertyPath |
||
1279 | * @return bool |
||
1280 | * @throws \Assert\AssertionFailedException |
||
1281 | */ |
||
1282 | public static function max($value, $maxValue, $message = null, $propertyPath = null) |
||
1298 | |||
1299 | /** |
||
1300 | * Assert that a file exists |
||
1301 | * |
||
1302 | * @param string $value |
||
1303 | * @param string|null $message |
||
1304 | * @param string|null $propertyPath |
||
1305 | * @return bool |
||
1306 | * @throws \Assert\AssertionFailedException |
||
1307 | */ |
||
1308 | public static function file($value, $message = null, $propertyPath = null) |
||
1324 | |||
1325 | /** |
||
1326 | * Assert that a directory exists |
||
1327 | * |
||
1328 | * @param string $value |
||
1329 | * @param string|null $message |
||
1330 | * @param string|null $propertyPath |
||
1331 | * @return bool |
||
1332 | * @throws \Assert\AssertionFailedException |
||
1333 | */ |
||
1334 | public static function directory($value, $message = null, $propertyPath = null) |
||
1349 | |||
1350 | /** |
||
1351 | * Assert that the value is something readable |
||
1352 | * |
||
1353 | * @param string $value |
||
1354 | * @param string|null $message |
||
1355 | * @param string|null $propertyPath |
||
1356 | * @return bool |
||
1357 | * @throws \Assert\AssertionFailedException |
||
1358 | */ |
||
1359 | public static function readable($value, $message = null, $propertyPath = null) |
||
1374 | |||
1375 | /** |
||
1376 | * Assert that the value is something writeable |
||
1377 | * |
||
1378 | * @param string $value |
||
1379 | * @param string|null $message |
||
1380 | * @param string|null $propertyPath |
||
1381 | * @return bool |
||
1382 | * @throws \Assert\AssertionFailedException |
||
1383 | */ |
||
1384 | public static function writeable($value, $message = null, $propertyPath = null) |
||
1399 | |||
1400 | /** |
||
1401 | * Assert that value is an email adress (using input_filter/FILTER_VALIDATE_EMAIL). |
||
1402 | * |
||
1403 | * @param mixed $value |
||
1404 | * @param string|null $message |
||
1405 | * @param string|null $propertyPath |
||
1406 | * @return bool |
||
1407 | * @throws \Assert\AssertionFailedException |
||
1408 | */ |
||
1409 | public static function email($value, $message = null, $propertyPath = null) |
||
1436 | |||
1437 | /** |
||
1438 | * Assert that value is an URL. |
||
1439 | * |
||
1440 | * This code snipped was taken from the Symfony project and modified to the special demands of this method. |
||
1441 | * |
||
1442 | * @param mixed $value |
||
1443 | * @param string|null $message |
||
1444 | * @param string|null $propertyPath |
||
1445 | * @return bool |
||
1446 | * @throws \Assert\AssertionFailedException |
||
1447 | * |
||
1448 | * |
||
1449 | * @link https://github.com/symfony/Validator/blob/master/Constraints/UrlValidator.php |
||
1450 | * @link https://github.com/symfony/Validator/blob/master/Constraints/Url.php |
||
1451 | */ |
||
1452 | public static function url($value, $message = null, $propertyPath = null) |
||
1487 | |||
1488 | /** |
||
1489 | * Assert that value is alphanumeric. |
||
1490 | * |
||
1491 | * @param mixed $value |
||
1492 | * @param string|null $message |
||
1493 | * @param string|null $propertyPath |
||
1494 | * @return bool |
||
1495 | * @throws \Assert\AssertionFailedException |
||
1496 | */ |
||
1497 | public static function alnum($value, $message = null, $propertyPath = null) |
||
1512 | |||
1513 | /** |
||
1514 | * Assert that the value is boolean True. |
||
1515 | * |
||
1516 | * @param mixed $value |
||
1517 | * @param string|null $message |
||
1518 | * @param string|null $propertyPath |
||
1519 | * @return bool |
||
1520 | * @throws \Assert\AssertionFailedException |
||
1521 | */ |
||
1522 | public static function true($value, $message = null, $propertyPath = null) |
||
1535 | |||
1536 | /** |
||
1537 | * Assert that the value is boolean False. |
||
1538 | * |
||
1539 | * @param mixed $value |
||
1540 | * @param string|null $message |
||
1541 | * @param string|null $propertyPath |
||
1542 | * @return bool |
||
1543 | * @throws \Assert\AssertionFailedException |
||
1544 | */ |
||
1545 | public static function false($value, $message = null, $propertyPath = null) |
||
1558 | |||
1559 | /** |
||
1560 | * Assert that the class exists. |
||
1561 | * |
||
1562 | * @param mixed $value |
||
1563 | * @param string|null $message |
||
1564 | * @param string|null $propertyPath |
||
1565 | * @return bool |
||
1566 | * @throws \Assert\AssertionFailedException |
||
1567 | */ |
||
1568 | public static function classExists($value, $message = null, $propertyPath = null) |
||
1581 | |||
1582 | /** |
||
1583 | * Assert that the interface exists. |
||
1584 | * |
||
1585 | * @param mixed $value |
||
1586 | * @param string|null $message |
||
1587 | * @param string|null $propertyPath |
||
1588 | * @return bool |
||
1589 | * @throws \Assert\AssertionFailedException |
||
1590 | */ |
||
1591 | public static function interfaceExists($value, $message = null, $propertyPath = null) |
||
1604 | |||
1605 | /** |
||
1606 | * Assert that the class implements the interface |
||
1607 | * |
||
1608 | * @param mixed $class |
||
1609 | * @param string $interfaceName |
||
1610 | * @param string|null $message |
||
1611 | * @param string|null $propertyPath |
||
1612 | * @return bool |
||
1613 | * @throws \Assert\AssertionFailedException |
||
1614 | */ |
||
1615 | public static function implementsInterface($class, $interfaceName, $message = null, $propertyPath = null) |
||
1630 | |||
1631 | /** |
||
1632 | * Assert that the given string is a valid json string. |
||
1633 | * |
||
1634 | * NOTICE: |
||
1635 | * Since this does a json_decode to determine its validity |
||
1636 | * you probably should consider, when using the variable |
||
1637 | * content afterwards, just to decode and check for yourself instead |
||
1638 | * of using this assertion. |
||
1639 | * |
||
1640 | * @param mixed $value |
||
1641 | * @param string|null $message |
||
1642 | * @param string|null $propertyPath |
||
1643 | * @return bool |
||
1644 | * @throws \Assert\AssertionFailedException |
||
1645 | */ |
||
1646 | public static function isJsonString($value, $message = null, $propertyPath = null) |
||
1659 | |||
1660 | /** |
||
1661 | * Assert that the given string is a valid UUID |
||
1662 | * |
||
1663 | * Uses code from {@link https://github.com/ramsey/uuid} that is MIT licensed. |
||
1664 | * |
||
1665 | * @param string $value |
||
1666 | * @param string|null $message |
||
1667 | * @param string|null $propertyPath |
||
1668 | * @return bool |
||
1669 | * @throws \Assert\AssertionFailedException |
||
1670 | */ |
||
1671 | public static function uuid($value, $message = null, $propertyPath = null) |
||
1690 | |||
1691 | /** |
||
1692 | * Assert that the given string is a valid E164 Phone Number |
||
1693 | * |
||
1694 | * @link https://en.wikipedia.org/wiki/E.164 |
||
1695 | * |
||
1696 | * @param string $value |
||
1697 | * @param string|null $message |
||
1698 | * @param string|null $propertyPath |
||
1699 | * @return bool |
||
1700 | * @throws \Assert\AssertionFailedException |
||
1701 | */ |
||
1702 | public static function e164($value, $message = null, $propertyPath = null) |
||
1715 | |||
1716 | /** |
||
1717 | * Assert that the count of countable is equal to count. |
||
1718 | * |
||
1719 | * @param array|\Countable $countable |
||
1720 | * @param int $count |
||
1721 | * @param string $message |
||
1722 | * @param string $propertyPath |
||
1723 | * @return bool |
||
1724 | * @throws \Assert\AssertionFailedException |
||
1725 | */ |
||
1726 | public static function count($countable, $count, $message = null, $propertyPath = null) |
||
1739 | |||
1740 | /** |
||
1741 | * static call handler to implement: |
||
1742 | * - "null or assertion" delegation |
||
1743 | * - "all" delegation |
||
1744 | * |
||
1745 | * @param string $method |
||
1746 | * @param array $args |
||
1747 | * |
||
1748 | * @return bool|mixed |
||
1749 | */ |
||
1750 | public static function __callStatic($method, $args) |
||
1786 | |||
1787 | /** |
||
1788 | * Determines if the values array has every choice as key and that this choice has content. |
||
1789 | * |
||
1790 | * @param array $values |
||
1791 | * @param array $choices |
||
1792 | * @param null $message |
||
1793 | * @param null $propertyPath |
||
1794 | * @return bool |
||
1795 | */ |
||
1796 | public static function choicesNotEmpty(array $values, array $choices, $message = null, $propertyPath = null) |
||
1806 | |||
1807 | /** |
||
1808 | * Determines that the named method is defined in the provided object. |
||
1809 | * |
||
1810 | * @param string $value |
||
1811 | * @param mixed $object |
||
1812 | * @param null $message |
||
1813 | * @param null $propertyPath |
||
1814 | * @return bool |
||
1815 | * @throws |
||
1816 | */ |
||
1817 | public static function methodExists($value, $object, $message = null, $propertyPath = null) |
||
1832 | |||
1833 | /** |
||
1834 | * Determines that the provided value is an object. |
||
1835 | * |
||
1836 | * @param mixed $value |
||
1837 | * @param null $message |
||
1838 | * @param null $propertyPath |
||
1839 | * @return bool |
||
1840 | */ |
||
1841 | public static function isObject($value, $message = null, $propertyPath = null) |
||
1854 | |||
1855 | /** |
||
1856 | * Determines if the value is less than given limit. |
||
1857 | * |
||
1858 | * @param mixed $value |
||
1859 | * @param mixed $limit |
||
1860 | * @param null $message |
||
1861 | * @param null $propertyPath |
||
1862 | * @return bool |
||
1863 | */ |
||
1864 | public static function lessThan($value, $limit, $message = null, $propertyPath = null) |
||
1878 | |||
1879 | /** |
||
1880 | * Determines if the value is less or than given limit. |
||
1881 | * |
||
1882 | * @param mixed $value |
||
1883 | * @param mixed $limit |
||
1884 | * @param null $message |
||
1885 | * @param null $propertyPath |
||
1886 | * @return bool |
||
1887 | */ |
||
1888 | public static function lessOrEqualThan($value, $limit, $message = null, $propertyPath = null) |
||
1902 | |||
1903 | /** |
||
1904 | * Determines if the value is greater than given limit. |
||
1905 | * |
||
1906 | * @param mixed $value |
||
1907 | * @param mixed $limit |
||
1908 | * @param null $message |
||
1909 | * @param null $propertyPath |
||
1910 | * @return bool |
||
1911 | */ |
||
1912 | public static function greaterThan($value, $limit, $message = null, $propertyPath = null) |
||
1926 | |||
1927 | /** |
||
1928 | * Determines if the value is greater or equal than given limit. |
||
1929 | * |
||
1930 | * @param mixed $value |
||
1931 | * @param mixed $limit |
||
1932 | * @param null $message |
||
1933 | * @param null $propertyPath |
||
1934 | * @return bool |
||
1935 | */ |
||
1936 | public static function greaterOrEqualThan($value, $limit, $message = null, $propertyPath = null) |
||
1950 | |||
1951 | /** |
||
1952 | * Assert that a value is greater or equal than a lower limit, and less than or equal to an upper limit. |
||
1953 | * |
||
1954 | * @param mixed $value |
||
1955 | * @param mixed $lowerLimit |
||
1956 | * @param mixed $upperLimit |
||
1957 | * @param string $message |
||
1958 | * @param string $propertyPath |
||
1959 | * @return bool |
||
1960 | */ |
||
1961 | public static function between($value, $lowerLimit, $upperLimit, $message = null, $propertyPath = null) |
||
1976 | |||
1977 | /** |
||
1978 | * Assert that a value is greater than a lower limit, and less than an upper limit. |
||
1979 | * |
||
1980 | * @param mixed $value |
||
1981 | * @param mixed $lowerLimit |
||
1982 | * @param mixed $upperLimit |
||
1983 | * @param string $message |
||
1984 | * @param string $propertyPath |
||
1985 | * @return bool |
||
1986 | */ |
||
1987 | public static function betweenExclusive($value, $lowerLimit, $upperLimit, $message = null, $propertyPath = null) |
||
2002 | |||
2003 | /** |
||
2004 | * Assert that date is valid and corresponds to the given format. |
||
2005 | * |
||
2006 | * @param string $value |
||
2007 | * @param string $format supports all of the options date(), except for the following: |
||
2008 | * N, w, W, t, L, o, B, a, A, g, h, I, O, P, Z, c, r. |
||
2009 | * @param string|null $message |
||
2010 | * @param string|null $propertyPath |
||
2011 | * @return bool |
||
2012 | * |
||
2013 | * @link http://php.net/manual/function.date.php#refsect1-function.date-parameters |
||
2014 | */ |
||
2015 | public static function date($value, $format, $message = null, $propertyPath = null) |
||
2034 | |||
2035 | /** |
||
2036 | * Determines that the provided value is callable. |
||
2037 | * |
||
2038 | * @param mixed $value |
||
2039 | * @param null $message |
||
2040 | * @param null $propertyPath |
||
2041 | * @return bool |
||
2042 | */ |
||
2043 | public static function isCallable($value, $message = null, $propertyPath = null) |
||
2056 | |||
2057 | /** |
||
2058 | * Assert that the provided value is valid according to a callback. |
||
2059 | * |
||
2060 | * If the callback returns `false` the assertion will fail. |
||
2061 | * |
||
2062 | * @param mixed $value |
||
2063 | * @param callable $callback |
||
2064 | * @param string|null $message |
||
2065 | * @param string|null $propertyPath |
||
2066 | * @return bool |
||
2067 | */ |
||
2068 | public static function satisfy($value, $callback, $message = null, $propertyPath = null) |
||
2083 | |||
2084 | /** |
||
2085 | * Assert that value is an IPv4 or IPv6 address |
||
2086 | * (using input_filter/FILTER_VALIDATE_IP). |
||
2087 | * |
||
2088 | * @param string $value |
||
2089 | * @param null|int $flag |
||
2090 | * @param string|null $message |
||
2091 | * @param string|null $propertyPath |
||
2092 | * @return bool |
||
2093 | * |
||
2094 | * @link http://php.net/manual/filter.filters.flags.php |
||
2095 | */ |
||
2096 | public static function ip($value, $flag = null, $message = null, $propertyPath = null) |
||
2109 | |||
2110 | /** |
||
2111 | * Assert that value is an IPv4 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 ipv4($value, $flag = null, $message = null, $propertyPath = null) |
||
2128 | |||
2129 | /** |
||
2130 | * Assert that value is an IPv6 address |
||
2131 | * (using input_filter/FILTER_VALIDATE_IP). |
||
2132 | * |
||
2133 | * @param string $value |
||
2134 | * @param null|int $flag |
||
2135 | * @param string|null $message |
||
2136 | * @param string|null $propertyPath |
||
2137 | * @return bool |
||
2138 | * |
||
2139 | * @link http://php.net/manual/filter.filters.flags.php |
||
2140 | */ |
||
2141 | public static function ipv6($value, $flag = null, $message = null, $propertyPath = null) |
||
2147 | |||
2148 | /** |
||
2149 | * Make a string version of a value. |
||
2150 | * |
||
2151 | * @param mixed $value |
||
2152 | * @return string |
||
2153 | */ |
||
2154 | protected static function stringify($value) |
||
2188 | |||
2189 | /** |
||
2190 | * Assert that a constant is defined. |
||
2191 | * |
||
2192 | * @param mixed $constant |
||
2193 | * @param string|null $message |
||
2194 | * @param string|null $propertyPath |
||
2195 | * @return bool |
||
2196 | * @throws \Assert\AssertionFailedException |
||
2197 | */ |
||
2198 | public static function defined($constant, $message = null, $propertyPath = null) |
||
2208 | } |
||
2209 |
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.