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 |
||
| 152 | class Assertion |
||
| 153 | { |
||
| 154 | const INVALID_FLOAT = 9; |
||
| 155 | const INVALID_INTEGER = 10; |
||
| 156 | const INVALID_DIGIT = 11; |
||
| 157 | const INVALID_INTEGERISH = 12; |
||
| 158 | const INVALID_BOOLEAN = 13; |
||
| 159 | const VALUE_EMPTY = 14; |
||
| 160 | const VALUE_NULL = 15; |
||
| 161 | const INVALID_STRING = 16; |
||
| 162 | const INVALID_REGEX = 17; |
||
| 163 | const INVALID_MIN_LENGTH = 18; |
||
| 164 | const INVALID_MAX_LENGTH = 19; |
||
| 165 | const INVALID_STRING_START = 20; |
||
| 166 | const INVALID_STRING_CONTAINS = 21; |
||
| 167 | const INVALID_CHOICE = 22; |
||
| 168 | const INVALID_NUMERIC = 23; |
||
| 169 | const INVALID_ARRAY = 24; |
||
| 170 | const INVALID_KEY_EXISTS = 26; |
||
| 171 | const INVALID_NOT_BLANK = 27; |
||
| 172 | const INVALID_INSTANCE_OF = 28; |
||
| 173 | const INVALID_SUBCLASS_OF = 29; |
||
| 174 | const INVALID_RANGE = 30; |
||
| 175 | const INVALID_ALNUM = 31; |
||
| 176 | const INVALID_TRUE = 32; |
||
| 177 | const INVALID_EQ = 33; |
||
| 178 | const INVALID_SAME = 34; |
||
| 179 | const INVALID_MIN = 35; |
||
| 180 | const INVALID_MAX = 36; |
||
| 181 | const INVALID_LENGTH = 37; |
||
| 182 | const INVALID_FALSE = 38; |
||
| 183 | const INVALID_STRING_END = 39; |
||
| 184 | const INVALID_UUID = 40; |
||
| 185 | const INVALID_COUNT = 41; |
||
| 186 | const INVALID_NOT_EQ = 42; |
||
| 187 | const INVALID_NOT_SAME = 43; |
||
| 188 | const INVALID_TRAVERSABLE = 44; |
||
| 189 | const INVALID_ARRAY_ACCESSIBLE = 45; |
||
| 190 | const INVALID_KEY_ISSET = 46; |
||
| 191 | const INVALID_VALUE_IN_ARRAY = 47; |
||
| 192 | const INVALID_DIRECTORY = 101; |
||
| 193 | const INVALID_FILE = 102; |
||
| 194 | const INVALID_READABLE = 103; |
||
| 195 | const INVALID_WRITEABLE = 104; |
||
| 196 | const INVALID_CLASS = 105; |
||
| 197 | const INVALID_EMAIL = 201; |
||
| 198 | const INTERFACE_NOT_IMPLEMENTED = 202; |
||
| 199 | const INVALID_URL = 203; |
||
| 200 | const INVALID_NOT_INSTANCE_OF = 204; |
||
| 201 | const VALUE_NOT_EMPTY = 205; |
||
| 202 | const INVALID_JSON_STRING = 206; |
||
| 203 | const INVALID_OBJECT = 207; |
||
| 204 | const INVALID_METHOD = 208; |
||
| 205 | const INVALID_SCALAR = 209; |
||
| 206 | const INVALID_LESS = 210; |
||
| 207 | const INVALID_LESS_OR_EQUAL = 211; |
||
| 208 | const INVALID_GREATER = 212; |
||
| 209 | const INVALID_GREATER_OR_EQUAL = 213; |
||
| 210 | const INVALID_DATE = 214; |
||
| 211 | const INVALID_CALLABLE = 215; |
||
| 212 | const INVALID_KEY_NOT_EXISTS = 216; |
||
| 213 | const INVALID_SATISFY = 217; |
||
| 214 | |||
| 215 | /** |
||
| 216 | * Exception to throw when an assertion failed. |
||
| 217 | * |
||
| 218 | * @var string |
||
| 219 | */ |
||
| 220 | static protected $exceptionClass = 'Assert\InvalidArgumentException'; |
||
| 221 | |||
| 222 | /** |
||
| 223 | * Helper method that handles building the assertion failure exceptions. |
||
| 224 | * They are returned from this method so that the stack trace still shows |
||
| 225 | * the assertions method. |
||
| 226 | */ |
||
| 227 | protected static function createException($value, $message, $code, $propertyPath, array $constraints = array()) |
||
| 232 | |||
| 233 | /** |
||
| 234 | * Assert that two values are equal (using == ). |
||
| 235 | * |
||
| 236 | * @param mixed $value |
||
| 237 | * @param mixed $value2 |
||
| 238 | * @param string|null $message |
||
| 239 | * @param string|null $propertyPath |
||
| 240 | * @return void |
||
| 241 | * @throws \Assert\AssertionFailedException |
||
| 242 | */ |
||
| 243 | public static function eq($value, $value2, $message = null, $propertyPath = null) |
||
| 255 | |||
| 256 | /** |
||
| 257 | * Assert that two values are the same (using ===). |
||
| 258 | * |
||
| 259 | * @param mixed $value |
||
| 260 | * @param mixed $value2 |
||
| 261 | * @param string|null $message |
||
| 262 | * @param string|null $propertyPath |
||
| 263 | * @return void |
||
| 264 | * @throws \Assert\AssertionFailedException |
||
| 265 | */ |
||
| 266 | public static function same($value, $value2, $message = null, $propertyPath = null) |
||
| 278 | |||
| 279 | /** |
||
| 280 | * Assert that two values are not equal (using == ). |
||
| 281 | * |
||
| 282 | * @param mixed $value1 |
||
| 283 | * @param mixed $value2 |
||
| 284 | * @param string|null $message |
||
| 285 | * @param string|null $propertyPath |
||
| 286 | * @return void |
||
| 287 | * @throws \Assert\AssertionFailedException |
||
| 288 | */ |
||
| 289 | public static function notEq($value1, $value2, $message = null, $propertyPath = null) |
||
| 300 | |||
| 301 | /** |
||
| 302 | * Assert that two values are not the same (using === ). |
||
| 303 | * |
||
| 304 | * @param mixed $value1 |
||
| 305 | * @param mixed $value2 |
||
| 306 | * @param string|null $message |
||
| 307 | * @param string|null $propertyPath |
||
| 308 | * @return void |
||
| 309 | * @throws \Assert\AssertionFailedException |
||
| 310 | */ |
||
| 311 | public static function notSame($value1, $value2, $message = null, $propertyPath = null) |
||
| 322 | |||
| 323 | /** |
||
| 324 | * Assert that value is not in array of choices. |
||
| 325 | * |
||
| 326 | * @param mixed $value |
||
| 327 | * @param array $choices |
||
| 328 | * @param string|null $message |
||
| 329 | * @param string|null $propertyPath |
||
| 330 | * @return void |
||
| 331 | * @throws \Assert\AssertionFailedException |
||
| 332 | */ |
||
| 333 | public static function notInArray($value, array $choices, $message = null, $propertyPath = null) |
||
| 344 | |||
| 345 | /** |
||
| 346 | * Assert that value is a php integer. |
||
| 347 | * |
||
| 348 | * @param mixed $value |
||
| 349 | * @param string|null $message |
||
| 350 | * @param string|null $propertyPath |
||
| 351 | * @return void |
||
| 352 | * @throws \Assert\AssertionFailedException |
||
| 353 | */ |
||
| 354 | public static function integer($value, $message = null, $propertyPath = null) |
||
| 365 | |||
| 366 | /** |
||
| 367 | * Assert that value is a php float. |
||
| 368 | * |
||
| 369 | * @param mixed $value |
||
| 370 | * @param string|null $message |
||
| 371 | * @param string|null $propertyPath |
||
| 372 | * @return void |
||
| 373 | * @throws \Assert\AssertionFailedException |
||
| 374 | */ |
||
| 375 | public static function float($value, $message = null, $propertyPath = null) |
||
| 386 | |||
| 387 | /** |
||
| 388 | * Validates if an integer or integerish is a digit. |
||
| 389 | * |
||
| 390 | * @param mixed $value |
||
| 391 | * @param string|null $message |
||
| 392 | * @param string|null $propertyPath |
||
| 393 | * @return void |
||
| 394 | * @throws \Assert\AssertionFailedException |
||
| 395 | */ |
||
| 396 | public static function digit($value, $message = null, $propertyPath = null) |
||
| 407 | |||
| 408 | /** |
||
| 409 | * Assert that value is a php integer'ish. |
||
| 410 | * |
||
| 411 | * @param mixed $value |
||
| 412 | * @param string|null $message |
||
| 413 | * @param string|null $propertyPath |
||
| 414 | * @return void |
||
| 415 | * @throws \Assert\AssertionFailedException |
||
| 416 | */ |
||
| 417 | public static function integerish($value, $message = null, $propertyPath = null) |
||
| 428 | |||
| 429 | /** |
||
| 430 | * Assert that value is php boolean |
||
| 431 | * |
||
| 432 | * @param mixed $value |
||
| 433 | * @param string|null $message |
||
| 434 | * @param string|null $propertyPath |
||
| 435 | * @return void |
||
| 436 | * @throws \Assert\AssertionFailedException |
||
| 437 | */ |
||
| 438 | public static function boolean($value, $message = null, $propertyPath = null) |
||
| 449 | |||
| 450 | /** |
||
| 451 | * Assert that value is a PHP scalar |
||
| 452 | * |
||
| 453 | * @param mixed $value |
||
| 454 | * @param string|null $message |
||
| 455 | * @param string|null $propertyPath |
||
| 456 | * @return void |
||
| 457 | * @throws \Assert\AssertionFailedException |
||
| 458 | */ |
||
| 459 | public static function scalar($value, $message = null, $propertyPath = null) |
||
| 470 | |||
| 471 | /** |
||
| 472 | * Assert that value is not empty |
||
| 473 | * |
||
| 474 | * @param mixed $value |
||
| 475 | * @param string|null $message |
||
| 476 | * @param string|null $propertyPath |
||
| 477 | * @return void |
||
| 478 | * @throws \Assert\AssertionFailedException |
||
| 479 | */ |
||
| 480 | public static function notEmpty($value, $message = null, $propertyPath = null) |
||
| 491 | |||
| 492 | /** |
||
| 493 | * Assert that value is empty |
||
| 494 | * |
||
| 495 | * @param mixed $value |
||
| 496 | * @param string|null $message |
||
| 497 | * @param string|null $propertyPath |
||
| 498 | * @return void |
||
| 499 | * @throws \Assert\AssertionFailedException |
||
| 500 | */ |
||
| 501 | public static function noContent($value, $message = null, $propertyPath = null) |
||
| 512 | |||
| 513 | /** |
||
| 514 | * Assert that value is not null |
||
| 515 | * |
||
| 516 | * @param mixed $value |
||
| 517 | * @param string|null $message |
||
| 518 | * @param string|null $propertyPath |
||
| 519 | * @return void |
||
| 520 | * @throws \Assert\AssertionFailedException |
||
| 521 | */ |
||
| 522 | public static function notNull($value, $message = null, $propertyPath = null) |
||
| 533 | |||
| 534 | /** |
||
| 535 | * Assert that value is a string |
||
| 536 | * |
||
| 537 | * @param mixed $value |
||
| 538 | * @param string|null $message |
||
| 539 | * @param string|null $propertyPath |
||
| 540 | * @return void |
||
| 541 | * @throws \Assert\AssertionFailedException |
||
| 542 | */ |
||
| 543 | public static function string($value, $message = null, $propertyPath = null) |
||
| 555 | |||
| 556 | /** |
||
| 557 | * Assert that value matches a regex |
||
| 558 | * |
||
| 559 | * @param mixed $value |
||
| 560 | * @param string $pattern |
||
| 561 | * @param string|null $message |
||
| 562 | * @param string|null $propertyPath |
||
| 563 | * @return void |
||
| 564 | * @throws \Assert\AssertionFailedException |
||
| 565 | */ |
||
| 566 | public static function regex($value, $pattern, $message = null, $propertyPath = null) |
||
| 579 | |||
| 580 | /** |
||
| 581 | * Assert that string has a given length. |
||
| 582 | * |
||
| 583 | * @param mixed $value |
||
| 584 | * @param int $length |
||
| 585 | * @param string|null $message |
||
| 586 | * @param string|null $propertyPath |
||
| 587 | * @param string $encoding |
||
| 588 | * @return void |
||
| 589 | * @throws \Assert\AssertionFailedException |
||
| 590 | */ |
||
| 591 | public static function length($value, $length, $message = null, $propertyPath = null, $encoding = 'utf8') |
||
| 607 | |||
| 608 | /** |
||
| 609 | * Assert that a string is at least $minLength chars long. |
||
| 610 | * |
||
| 611 | * @param mixed $value |
||
| 612 | * @param int $minLength |
||
| 613 | * @param string|null $message |
||
| 614 | * @param string|null $propertyPath |
||
| 615 | * @param string $encoding |
||
| 616 | * @return void |
||
| 617 | * @throws \Assert\AssertionFailedException |
||
| 618 | */ |
||
| 619 | public static function minLength($value, $minLength, $message = null, $propertyPath = null, $encoding = 'utf8') |
||
| 635 | |||
| 636 | /** |
||
| 637 | * Assert that string value is not longer than $maxLength chars. |
||
| 638 | * |
||
| 639 | * @param mixed $value |
||
| 640 | * @param integer $maxLength |
||
| 641 | * @param string|null $message |
||
| 642 | * @param string|null $propertyPath |
||
| 643 | * @param string $encoding |
||
| 644 | * @return void |
||
| 645 | * @throws \Assert\AssertionFailedException |
||
| 646 | */ |
||
| 647 | public static function maxLength($value, $maxLength, $message = null, $propertyPath = null, $encoding = 'utf8') |
||
| 663 | |||
| 664 | /** |
||
| 665 | * Assert that string length is between min,max lengths. |
||
| 666 | * |
||
| 667 | * @param mixed $value |
||
| 668 | * @param integer $minLength |
||
| 669 | * @param integer $maxLength |
||
| 670 | * @param string|null $message |
||
| 671 | * @param string|null $propertyPath |
||
| 672 | * @param string $encoding |
||
| 673 | * @return void |
||
| 674 | * @throws \Assert\AssertionFailedException |
||
| 675 | */ |
||
| 676 | public static function betweenLength($value, $minLength, $maxLength, $message = null, $propertyPath = null, $encoding = 'utf8') |
||
| 704 | |||
| 705 | /** |
||
| 706 | * Assert that string starts with a sequence of chars. |
||
| 707 | * |
||
| 708 | * @param mixed $string |
||
| 709 | * @param string $needle |
||
| 710 | * @param string|null $message |
||
| 711 | * @param string|null $propertyPath |
||
| 712 | * @param string $encoding |
||
| 713 | * @return void |
||
| 714 | * @throws \Assert\AssertionFailedException |
||
| 715 | */ |
||
| 716 | public static function startsWith($string, $needle, $message = null, $propertyPath = null, $encoding = 'utf8') |
||
| 731 | |||
| 732 | /** |
||
| 733 | * Assert that string ends with a sequence of chars. |
||
| 734 | * |
||
| 735 | * @param mixed $string |
||
| 736 | * @param string $needle |
||
| 737 | * @param string|null $message |
||
| 738 | * @param string|null $propertyPath |
||
| 739 | * @param string $encoding |
||
| 740 | * @return void |
||
| 741 | * @throws \Assert\AssertionFailedException |
||
| 742 | */ |
||
| 743 | public static function endsWith($string, $needle, $message = null, $propertyPath = null, $encoding = 'utf8') |
||
| 760 | |||
| 761 | /** |
||
| 762 | * Assert that string contains a sequence of chars. |
||
| 763 | * |
||
| 764 | * @param mixed $string |
||
| 765 | * @param string $needle |
||
| 766 | * @param string|null $message |
||
| 767 | * @param string|null $propertyPath |
||
| 768 | * @param string $encoding |
||
| 769 | * @return void |
||
| 770 | * @throws \Assert\AssertionFailedException |
||
| 771 | */ |
||
| 772 | public static function contains($string, $needle, $message = null, $propertyPath = null, $encoding = 'utf8') |
||
| 787 | |||
| 788 | /** |
||
| 789 | * Assert that value is in array of choices. |
||
| 790 | * |
||
| 791 | * @param mixed $value |
||
| 792 | * @param array $choices |
||
| 793 | * @param string|null $message |
||
| 794 | * @param string|null $propertyPath |
||
| 795 | * @return void |
||
| 796 | * @throws \Assert\AssertionFailedException |
||
| 797 | */ |
||
| 798 | public static function choice($value, array $choices, $message = null, $propertyPath = null) |
||
| 810 | |||
| 811 | /** |
||
| 812 | * Alias of {@see choice()} |
||
| 813 | * |
||
| 814 | * @throws \Assert\AssertionFailedException |
||
| 815 | */ |
||
| 816 | public static function inArray($value, array $choices, $message = null, $propertyPath = null) |
||
| 820 | |||
| 821 | /** |
||
| 822 | * Assert that value is numeric. |
||
| 823 | * |
||
| 824 | * @param mixed $value |
||
| 825 | * @param string|null $message |
||
| 826 | * @param string|null $propertyPath |
||
| 827 | * @return void |
||
| 828 | * @throws \Assert\AssertionFailedException |
||
| 829 | */ |
||
| 830 | public static function numeric($value, $message = null, $propertyPath = null) |
||
| 841 | |||
| 842 | /** |
||
| 843 | * Assert that value is an array. |
||
| 844 | * |
||
| 845 | * @param mixed $value |
||
| 846 | * @param string|null $message |
||
| 847 | * @param string|null $propertyPath |
||
| 848 | * @return void |
||
| 849 | * @throws \Assert\AssertionFailedException |
||
| 850 | */ |
||
| 851 | public static function isArray($value, $message = null, $propertyPath = null) |
||
| 862 | |||
| 863 | /** |
||
| 864 | * Assert that value is an array or a traversable object. |
||
| 865 | * |
||
| 866 | * @param mixed $value |
||
| 867 | * @param string|null $message |
||
| 868 | * @param string|null $propertyPath |
||
| 869 | * @return void |
||
| 870 | * @throws \Assert\AssertionFailedException |
||
| 871 | */ |
||
| 872 | public static function isTraversable($value, $message = null, $propertyPath = null) |
||
| 883 | |||
| 884 | /** |
||
| 885 | * Assert that value is an array or an array-accessible object. |
||
| 886 | * |
||
| 887 | * @param mixed $value |
||
| 888 | * @param string|null $message |
||
| 889 | * @param string|null $propertyPath |
||
| 890 | * @return void |
||
| 891 | * @throws \Assert\AssertionFailedException |
||
| 892 | */ |
||
| 893 | public static function isArrayAccessible($value, $message = null, $propertyPath = null) |
||
| 904 | |||
| 905 | /** |
||
| 906 | * Assert that key exists in an array |
||
| 907 | * |
||
| 908 | * @param mixed $value |
||
| 909 | * @param string|integer $key |
||
| 910 | * @param string|null $message |
||
| 911 | * @param string|null $propertyPath |
||
| 912 | * @return void |
||
| 913 | * @throws \Assert\AssertionFailedException |
||
| 914 | */ |
||
| 915 | public static function keyExists($value, $key, $message = null, $propertyPath = null) |
||
| 928 | |||
| 929 | /** |
||
| 930 | * Assert that key does not exist in an array |
||
| 931 | * |
||
| 932 | * @param mixed $value |
||
| 933 | * @param string|integer $key |
||
| 934 | * @param string|null $message |
||
| 935 | * @param string|null $propertyPath |
||
| 936 | * @return void |
||
| 937 | * @throws \Assert\AssertionFailedException |
||
| 938 | */ |
||
| 939 | public static function keyNotExists($value, $key, $message = null, $propertyPath = null) |
||
| 952 | |||
| 953 | /** |
||
| 954 | * Assert that key exists in an array/array-accessible object using isset() |
||
| 955 | * |
||
| 956 | * @param mixed $value |
||
| 957 | * @param string|integer $key |
||
| 958 | * @param string|null $message |
||
| 959 | * @param string|null $propertyPath |
||
| 960 | * @return void |
||
| 961 | * @throws \Assert\AssertionFailedException |
||
| 962 | */ |
||
| 963 | public static function keyIsset($value, $key, $message = null, $propertyPath = null) |
||
| 976 | |||
| 977 | /** |
||
| 978 | * Assert that key exists in an array/array-accessible object and it's value is not empty. |
||
| 979 | * |
||
| 980 | * @param mixed $value |
||
| 981 | * @param string|integer $key |
||
| 982 | * @param string|null $message |
||
| 983 | * @param string|null $propertyPath |
||
| 984 | * @return void |
||
| 985 | * @throws \Assert\AssertionFailedException |
||
| 986 | */ |
||
| 987 | public static function notEmptyKey($value, $key, $message = null, $propertyPath = null) |
||
| 992 | |||
| 993 | /** |
||
| 994 | * Assert that value is not blank |
||
| 995 | * |
||
| 996 | * @param mixed $value |
||
| 997 | * @param string|null $message |
||
| 998 | * @param string|null $propertyPath |
||
| 999 | * @return void |
||
| 1000 | * @throws \Assert\AssertionFailedException |
||
| 1001 | */ |
||
| 1002 | public static function notBlank($value, $message = null, $propertyPath = null) |
||
| 1013 | |||
| 1014 | /** |
||
| 1015 | * Assert that value is instance of given class-name. |
||
| 1016 | * |
||
| 1017 | * @param mixed $value |
||
| 1018 | * @param string $className |
||
| 1019 | * @param string|null $message |
||
| 1020 | * @param string|null $propertyPath |
||
| 1021 | * @return void |
||
| 1022 | * @throws \Assert\AssertionFailedException |
||
| 1023 | */ |
||
| 1024 | public static function isInstanceOf($value, $className, $message = null, $propertyPath = null) |
||
| 1036 | |||
| 1037 | /** |
||
| 1038 | * Assert that value is not instance of given class-name. |
||
| 1039 | * |
||
| 1040 | * @param mixed $value |
||
| 1041 | * @param string $className |
||
| 1042 | * @param string|null $message |
||
| 1043 | * @param string|null $propertyPath |
||
| 1044 | * @return void |
||
| 1045 | * @throws \Assert\AssertionFailedException |
||
| 1046 | */ |
||
| 1047 | public static function notIsInstanceOf($value, $className, $message = null, $propertyPath = null) |
||
| 1059 | |||
| 1060 | /** |
||
| 1061 | * Assert that value is subclass of given class-name. |
||
| 1062 | * |
||
| 1063 | * @param mixed $value |
||
| 1064 | * @param string $className |
||
| 1065 | * @param string|null $message |
||
| 1066 | * @param string|null $propertyPath |
||
| 1067 | * @return void |
||
| 1068 | * @throws \Assert\AssertionFailedException |
||
| 1069 | */ |
||
| 1070 | public static function subclassOf($value, $className, $message = null, $propertyPath = null) |
||
| 1082 | |||
| 1083 | /** |
||
| 1084 | * Assert that value is in range of numbers. |
||
| 1085 | * |
||
| 1086 | * @param mixed $value |
||
| 1087 | * @param integer $minValue |
||
| 1088 | * @param integer $maxValue |
||
| 1089 | * @param string|null $message |
||
| 1090 | * @param string|null $propertyPath |
||
| 1091 | * @return void |
||
| 1092 | * @throws \Assert\AssertionFailedException |
||
| 1093 | */ |
||
| 1094 | public static function range($value, $minValue, $maxValue, $message = null, $propertyPath = null) |
||
| 1109 | |||
| 1110 | /** |
||
| 1111 | * Assert that a value is at least as big as a given limit |
||
| 1112 | * |
||
| 1113 | * @param mixed $value |
||
| 1114 | * @param mixed $minValue |
||
| 1115 | * @param string|null $message |
||
| 1116 | * @param string|null $propertyPath |
||
| 1117 | * @return void |
||
| 1118 | * @throws \Assert\AssertionFailedException |
||
| 1119 | */ |
||
| 1120 | public static function min($value, $minValue, $message = null, $propertyPath = null) |
||
| 1134 | |||
| 1135 | /** |
||
| 1136 | * Assert that a number is smaller as a given limit |
||
| 1137 | * |
||
| 1138 | * @param mixed $value |
||
| 1139 | * @param mixed $maxValue |
||
| 1140 | * @param string|null $message |
||
| 1141 | * @param string|null $propertyPath |
||
| 1142 | * @return void |
||
| 1143 | * @throws \Assert\AssertionFailedException |
||
| 1144 | */ |
||
| 1145 | public static function max($value, $maxValue, $message = null, $propertyPath = null) |
||
| 1159 | |||
| 1160 | /** |
||
| 1161 | * Assert that a file exists |
||
| 1162 | * |
||
| 1163 | * @param string $value |
||
| 1164 | * @param string|null $message |
||
| 1165 | * @param string|null $propertyPath |
||
| 1166 | * @return void |
||
| 1167 | * @throws \Assert\AssertionFailedException |
||
| 1168 | */ |
||
| 1169 | public static function file($value, $message = null, $propertyPath = null) |
||
| 1183 | |||
| 1184 | /** |
||
| 1185 | * Assert that a directory exists |
||
| 1186 | * |
||
| 1187 | * @param string $value |
||
| 1188 | * @param string|null $message |
||
| 1189 | * @param string|null $propertyPath |
||
| 1190 | * @return void |
||
| 1191 | * @throws \Assert\AssertionFailedException |
||
| 1192 | */ |
||
| 1193 | public static function directory($value, $message = null, $propertyPath = null) |
||
| 1206 | |||
| 1207 | /** |
||
| 1208 | * Assert that the value is something readable |
||
| 1209 | * |
||
| 1210 | * @param string $value |
||
| 1211 | * @param string|null $message |
||
| 1212 | * @param string|null $propertyPath |
||
| 1213 | * @return void |
||
| 1214 | * @throws \Assert\AssertionFailedException |
||
| 1215 | */ |
||
| 1216 | public static function readable($value, $message = null, $propertyPath = null) |
||
| 1229 | |||
| 1230 | /** |
||
| 1231 | * Assert that the value is something writeable |
||
| 1232 | * |
||
| 1233 | * @param string $value |
||
| 1234 | * @param string|null $message |
||
| 1235 | * @param string|null $propertyPath |
||
| 1236 | * @return void |
||
| 1237 | * @throws \Assert\AssertionFailedException |
||
| 1238 | */ |
||
| 1239 | public static function writeable($value, $message = null, $propertyPath = null) |
||
| 1252 | |||
| 1253 | /** |
||
| 1254 | * Assert that value is an email adress (using input_filter/FILTER_VALIDATE_EMAIL). |
||
| 1255 | * |
||
| 1256 | * @param mixed $value |
||
| 1257 | * @param string|null $message |
||
| 1258 | * @param string|null $propertyPath |
||
| 1259 | * @return void |
||
| 1260 | * @throws \Assert\AssertionFailedException |
||
| 1261 | */ |
||
| 1262 | public static function email($value, $message = null, $propertyPath = null) |
||
| 1287 | |||
| 1288 | /** |
||
| 1289 | * Assert that value is an URL. |
||
| 1290 | * |
||
| 1291 | * This code snipped was taken from the Symfony project and modified to the special demands of this method. |
||
| 1292 | * |
||
| 1293 | * @param mixed $value |
||
| 1294 | * @param string|null $message |
||
| 1295 | * @param string|null $propertyPath |
||
| 1296 | * @return void |
||
| 1297 | * @throws \Assert\AssertionFailedException |
||
| 1298 | * |
||
| 1299 | * |
||
| 1300 | * @link https://github.com/symfony/Validator/blob/master/Constraints/UrlValidator.php |
||
| 1301 | * @link https://github.com/symfony/Validator/blob/master/Constraints/Url.php |
||
| 1302 | */ |
||
| 1303 | public static function url($value, $message = null, $propertyPath = null) |
||
| 1336 | |||
| 1337 | /** |
||
| 1338 | * Assert that value is alphanumeric. |
||
| 1339 | * |
||
| 1340 | * @param mixed $value |
||
| 1341 | * @param string|null $message |
||
| 1342 | * @param string|null $propertyPath |
||
| 1343 | * @return void |
||
| 1344 | * @throws \Assert\AssertionFailedException |
||
| 1345 | */ |
||
| 1346 | public static function alnum($value, $message = null, $propertyPath = null) |
||
| 1359 | |||
| 1360 | /** |
||
| 1361 | * Assert that the value is boolean True. |
||
| 1362 | * |
||
| 1363 | * @param mixed $value |
||
| 1364 | * @param string|null $message |
||
| 1365 | * @param string|null $propertyPath |
||
| 1366 | * @return void |
||
| 1367 | * @throws \Assert\AssertionFailedException |
||
| 1368 | */ |
||
| 1369 | public static function true($value, $message = null, $propertyPath = null) |
||
| 1380 | |||
| 1381 | /** |
||
| 1382 | * Assert that the value is boolean False. |
||
| 1383 | * |
||
| 1384 | * @param mixed $value |
||
| 1385 | * @param string|null $message |
||
| 1386 | * @param string|null $propertyPath |
||
| 1387 | * @return void |
||
| 1388 | * @throws \Assert\AssertionFailedException |
||
| 1389 | */ |
||
| 1390 | public static function false($value, $message = null, $propertyPath = null) |
||
| 1401 | |||
| 1402 | /** |
||
| 1403 | * Assert that the class exists. |
||
| 1404 | * |
||
| 1405 | * @param mixed $value |
||
| 1406 | * @param string|null $message |
||
| 1407 | * @param string|null $propertyPath |
||
| 1408 | * @return void |
||
| 1409 | * @throws \Assert\AssertionFailedException |
||
| 1410 | */ |
||
| 1411 | public static function classExists($value, $message = null, $propertyPath = null) |
||
| 1422 | |||
| 1423 | /** |
||
| 1424 | * Assert that the class implements the interface |
||
| 1425 | * |
||
| 1426 | * @param mixed $class |
||
| 1427 | * @param string $interfaceName |
||
| 1428 | * @param string|null $message |
||
| 1429 | * @param string|null $propertyPath |
||
| 1430 | * @return void |
||
| 1431 | * @throws \Assert\AssertionFailedException |
||
| 1432 | */ |
||
| 1433 | public static function implementsInterface($class, $interfaceName, $message = null, $propertyPath = null) |
||
| 1446 | |||
| 1447 | /** |
||
| 1448 | * Assert that the given string is a valid json string. |
||
| 1449 | * |
||
| 1450 | * NOTICE: |
||
| 1451 | * Since this does a json_decode to determine its validity |
||
| 1452 | * you probably should consider, when using the variable |
||
| 1453 | * content afterwards, just to decode and check for yourself instead |
||
| 1454 | * of using this assertion. |
||
| 1455 | * |
||
| 1456 | * @param mixed $value |
||
| 1457 | * @param string|null $message |
||
| 1458 | * @param string|null $propertyPath |
||
| 1459 | * @return void |
||
| 1460 | * @throws \Assert\AssertionFailedException |
||
| 1461 | */ |
||
| 1462 | public static function isJsonString($value, $message = null, $propertyPath = null) |
||
| 1473 | |||
| 1474 | /** |
||
| 1475 | * Assert that the given string is a valid UUID |
||
| 1476 | * |
||
| 1477 | * Uses code from {@link https://github.com/ramsey/uuid} that is MIT licensed. |
||
| 1478 | * |
||
| 1479 | * @param string $value |
||
| 1480 | * @param string|null $message |
||
| 1481 | * @param string|null $propertyPath |
||
| 1482 | * @return void |
||
| 1483 | * @throws \Assert\AssertionFailedException |
||
| 1484 | */ |
||
| 1485 | public static function uuid($value, $message = null, $propertyPath = null) |
||
| 1502 | |||
| 1503 | /** |
||
| 1504 | * Assert that the count of countable is equal to count. |
||
| 1505 | * |
||
| 1506 | * @param array|\Countable $countable |
||
| 1507 | * @param int $count |
||
| 1508 | * @param string $message |
||
| 1509 | * @param string $propertyPath |
||
| 1510 | * @return void |
||
| 1511 | * @throws \Assert\AssertionFailedException |
||
| 1512 | */ |
||
| 1513 | public static function count($countable, $count, $message = null, $propertyPath = null) |
||
| 1524 | |||
| 1525 | /** |
||
| 1526 | * static call handler to implement: |
||
| 1527 | * - "null or assertion" delegation |
||
| 1528 | * - "all" delegation |
||
| 1529 | */ |
||
| 1530 | public static function __callStatic($method, $args) |
||
| 1566 | |||
| 1567 | /** |
||
| 1568 | * Determines if the values array has every choice as key and that this choice has content. |
||
| 1569 | * |
||
| 1570 | * @param array $values |
||
| 1571 | * @param array $choices |
||
| 1572 | * @param null $message |
||
| 1573 | * @param null $propertyPath |
||
| 1574 | */ |
||
| 1575 | public static function choicesNotEmpty(array $values, array $choices, $message = null, $propertyPath = null) |
||
| 1584 | |||
| 1585 | /** |
||
| 1586 | * Determines that the named method is defined in the provided object. |
||
| 1587 | * |
||
| 1588 | * @param string $value |
||
| 1589 | * @param mixed $object |
||
| 1590 | * @param null $message |
||
| 1591 | * @param null $propertyPath |
||
| 1592 | * |
||
| 1593 | * @throws |
||
| 1594 | */ |
||
| 1595 | public static function methodExists($value, $object, $message = null, $propertyPath = null) |
||
| 1608 | |||
| 1609 | /** |
||
| 1610 | * Determines that the provided value is an object. |
||
| 1611 | * |
||
| 1612 | * @param mixed $value |
||
| 1613 | * @param null $message |
||
| 1614 | * @param null $propertyPath |
||
| 1615 | */ |
||
| 1616 | public static function isObject($value, $message = null, $propertyPath = null) |
||
| 1628 | |||
| 1629 | /** |
||
| 1630 | * Determines if the value is less than given limit. |
||
| 1631 | * |
||
| 1632 | * @param mixed $value |
||
| 1633 | * @param mixed $limit |
||
| 1634 | * @param null $message |
||
| 1635 | * @param null $propertyPath |
||
| 1636 | */ |
||
| 1637 | public static function lessThan($value, $limit, $message = null, $propertyPath = null) |
||
| 1649 | |||
| 1650 | /** |
||
| 1651 | * Determines if the value is less or than given limit. |
||
| 1652 | * |
||
| 1653 | * @param mixed $value |
||
| 1654 | * @param mixed $limit |
||
| 1655 | * @param null $message |
||
| 1656 | * @param null $propertyPath |
||
| 1657 | */ |
||
| 1658 | public static function lessOrEqualThan($value, $limit, $message = null, $propertyPath = null) |
||
| 1670 | |||
| 1671 | /** |
||
| 1672 | * Determines if the value is greater than given limit. |
||
| 1673 | * |
||
| 1674 | * @param mixed $value |
||
| 1675 | * @param mixed $limit |
||
| 1676 | * @param null $message |
||
| 1677 | * @param null $propertyPath |
||
| 1678 | */ |
||
| 1679 | public static function greaterThan($value, $limit, $message = null, $propertyPath = null) |
||
| 1691 | |||
| 1692 | /** |
||
| 1693 | * Determines if the value is greater or equal than given limit. |
||
| 1694 | * |
||
| 1695 | * @param mixed $value |
||
| 1696 | * @param mixed $limit |
||
| 1697 | * @param null $message |
||
| 1698 | * @param null $propertyPath |
||
| 1699 | */ |
||
| 1700 | public static function greaterOrEqualThan($value, $limit, $message = null, $propertyPath = null) |
||
| 1712 | |||
| 1713 | /** |
||
| 1714 | * Assert that date is valid and corresponds to the given format. |
||
| 1715 | * |
||
| 1716 | * @param string $value |
||
| 1717 | * @param string $format supports all of the options date(), except for the following: |
||
| 1718 | * N, w, W, t, L, o, B, a, A, g, h, I, O, P, Z, c, r. |
||
| 1719 | * @param string|null $message |
||
| 1720 | * @param string|null $propertyPath |
||
| 1721 | * |
||
| 1722 | * @link http://php.net/manual/function.date.php#refsect1-function.date-parameters |
||
| 1723 | */ |
||
| 1724 | public static function date($value, $format, $message = null, $propertyPath = null) |
||
| 1741 | |||
| 1742 | /** |
||
| 1743 | * Determines that the provided value is callable. |
||
| 1744 | * |
||
| 1745 | * @param mixed $value |
||
| 1746 | * @param null $message |
||
| 1747 | * @param null $propertyPath |
||
| 1748 | */ |
||
| 1749 | public static function isCallable($value, $message = null, $propertyPath = null) |
||
| 1760 | |||
| 1761 | /** |
||
| 1762 | * Assert that the provided value is valid according to a callback. |
||
| 1763 | * |
||
| 1764 | * If the callback returns `false` the assertion will fail. |
||
| 1765 | * |
||
| 1766 | * @param mixed $value |
||
| 1767 | * @param callable $callback |
||
| 1768 | * @param string|null $message |
||
| 1769 | * @param string|null $propertyPath |
||
| 1770 | */ |
||
| 1771 | public static function satisfy($value, $callback, $message = null, $propertyPath = null) |
||
| 1782 | |||
| 1783 | /** |
||
| 1784 | * Make a string version of a value. |
||
| 1785 | * |
||
| 1786 | * @param mixed $value |
||
| 1787 | * @return string |
||
| 1788 | */ |
||
| 1789 | protected static function stringify($value) |
||
| 1823 | } |
||
| 1824 | |||
| 1825 |