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 |
||
| 158 | class Assertion |
||
| 159 | { |
||
| 160 | const INVALID_FLOAT = 9; |
||
| 161 | const INVALID_INTEGER = 10; |
||
| 162 | const INVALID_DIGIT = 11; |
||
| 163 | const INVALID_INTEGERISH = 12; |
||
| 164 | const INVALID_BOOLEAN = 13; |
||
| 165 | const VALUE_EMPTY = 14; |
||
| 166 | const VALUE_NULL = 15; |
||
| 167 | const INVALID_STRING = 16; |
||
| 168 | const INVALID_REGEX = 17; |
||
| 169 | const INVALID_MIN_LENGTH = 18; |
||
| 170 | const INVALID_MAX_LENGTH = 19; |
||
| 171 | const INVALID_STRING_START = 20; |
||
| 172 | const INVALID_STRING_CONTAINS = 21; |
||
| 173 | const INVALID_CHOICE = 22; |
||
| 174 | const INVALID_NUMERIC = 23; |
||
| 175 | const INVALID_ARRAY = 24; |
||
| 176 | const INVALID_KEY_EXISTS = 26; |
||
| 177 | const INVALID_NOT_BLANK = 27; |
||
| 178 | const INVALID_INSTANCE_OF = 28; |
||
| 179 | const INVALID_SUBCLASS_OF = 29; |
||
| 180 | const INVALID_RANGE = 30; |
||
| 181 | const INVALID_ALNUM = 31; |
||
| 182 | const INVALID_TRUE = 32; |
||
| 183 | const INVALID_EQ = 33; |
||
| 184 | const INVALID_SAME = 34; |
||
| 185 | const INVALID_MIN = 35; |
||
| 186 | const INVALID_MAX = 36; |
||
| 187 | const INVALID_LENGTH = 37; |
||
| 188 | const INVALID_FALSE = 38; |
||
| 189 | const INVALID_STRING_END = 39; |
||
| 190 | const INVALID_UUID = 40; |
||
| 191 | const INVALID_COUNT = 41; |
||
| 192 | const INVALID_NOT_EQ = 42; |
||
| 193 | const INVALID_NOT_SAME = 43; |
||
| 194 | const INVALID_TRAVERSABLE = 44; |
||
| 195 | const INVALID_ARRAY_ACCESSIBLE = 45; |
||
| 196 | const INVALID_KEY_ISSET = 46; |
||
| 197 | const INVALID_VALUE_IN_ARRAY = 47; |
||
| 198 | const INVALID_DIRECTORY = 101; |
||
| 199 | const INVALID_FILE = 102; |
||
| 200 | const INVALID_READABLE = 103; |
||
| 201 | const INVALID_WRITEABLE = 104; |
||
| 202 | const INVALID_CLASS = 105; |
||
| 203 | const INVALID_EMAIL = 201; |
||
| 204 | const INTERFACE_NOT_IMPLEMENTED = 202; |
||
| 205 | const INVALID_URL = 203; |
||
| 206 | const INVALID_NOT_INSTANCE_OF = 204; |
||
| 207 | const VALUE_NOT_EMPTY = 205; |
||
| 208 | const INVALID_JSON_STRING = 206; |
||
| 209 | const INVALID_OBJECT = 207; |
||
| 210 | const INVALID_METHOD = 208; |
||
| 211 | const INVALID_SCALAR = 209; |
||
| 212 | const INVALID_LESS = 210; |
||
| 213 | const INVALID_LESS_OR_EQUAL = 211; |
||
| 214 | const INVALID_GREATER = 212; |
||
| 215 | const INVALID_GREATER_OR_EQUAL = 213; |
||
| 216 | const INVALID_DATE = 214; |
||
| 217 | const INVALID_CALLABLE = 215; |
||
| 218 | const INVALID_KEY_NOT_EXISTS = 216; |
||
| 219 | const INVALID_SATISFY = 217; |
||
| 220 | const INVALID_IP = 218; |
||
| 221 | |||
| 222 | /** |
||
| 223 | * Exception to throw when an assertion failed. |
||
| 224 | * |
||
| 225 | * @var string |
||
| 226 | */ |
||
| 227 | static protected $exceptionClass = 'Assert\InvalidArgumentException'; |
||
| 228 | |||
| 229 | /** |
||
| 230 | * Helper method that handles building the assertion failure exceptions. |
||
| 231 | * They are returned from this method so that the stack trace still shows |
||
| 232 | * the assertions method. |
||
| 233 | */ |
||
| 234 | protected static function createException($value, $message, $code, $propertyPath, array $constraints = array()) |
||
| 239 | |||
| 240 | /** |
||
| 241 | * Assert that two values are equal (using == ). |
||
| 242 | * |
||
| 243 | * @param mixed $value |
||
| 244 | * @param mixed $value2 |
||
| 245 | * @param string|null $message |
||
| 246 | * @param string|null $propertyPath |
||
| 247 | * @return void |
||
| 248 | * @throws \Assert\AssertionFailedException |
||
| 249 | */ |
||
| 250 | public static function eq($value, $value2, $message = null, $propertyPath = null) |
||
| 262 | |||
| 263 | /** |
||
| 264 | * Assert that two values are the same (using ===). |
||
| 265 | * |
||
| 266 | * @param mixed $value |
||
| 267 | * @param mixed $value2 |
||
| 268 | * @param string|null $message |
||
| 269 | * @param string|null $propertyPath |
||
| 270 | * @return void |
||
| 271 | * @throws \Assert\AssertionFailedException |
||
| 272 | */ |
||
| 273 | public static function same($value, $value2, $message = null, $propertyPath = null) |
||
| 285 | |||
| 286 | /** |
||
| 287 | * Assert that two values are not equal (using == ). |
||
| 288 | * |
||
| 289 | * @param mixed $value1 |
||
| 290 | * @param mixed $value2 |
||
| 291 | * @param string|null $message |
||
| 292 | * @param string|null $propertyPath |
||
| 293 | * @return void |
||
| 294 | * @throws \Assert\AssertionFailedException |
||
| 295 | */ |
||
| 296 | public static function notEq($value1, $value2, $message = null, $propertyPath = null) |
||
| 307 | |||
| 308 | /** |
||
| 309 | * Assert that two values are not the same (using === ). |
||
| 310 | * |
||
| 311 | * @param mixed $value1 |
||
| 312 | * @param mixed $value2 |
||
| 313 | * @param string|null $message |
||
| 314 | * @param string|null $propertyPath |
||
| 315 | * @return void |
||
| 316 | * @throws \Assert\AssertionFailedException |
||
| 317 | */ |
||
| 318 | public static function notSame($value1, $value2, $message = null, $propertyPath = null) |
||
| 329 | |||
| 330 | /** |
||
| 331 | * Assert that value is not in array of choices. |
||
| 332 | * |
||
| 333 | * @param mixed $value |
||
| 334 | * @param array $choices |
||
| 335 | * @param string|null $message |
||
| 336 | * @param string|null $propertyPath |
||
| 337 | * @return void |
||
| 338 | * @throws \Assert\AssertionFailedException |
||
| 339 | */ |
||
| 340 | public static function notInArray($value, array $choices, $message = null, $propertyPath = null) |
||
| 351 | |||
| 352 | /** |
||
| 353 | * Assert that value is a php integer. |
||
| 354 | * |
||
| 355 | * @param mixed $value |
||
| 356 | * @param string|null $message |
||
| 357 | * @param string|null $propertyPath |
||
| 358 | * @return void |
||
| 359 | * @throws \Assert\AssertionFailedException |
||
| 360 | */ |
||
| 361 | public static function integer($value, $message = null, $propertyPath = null) |
||
| 372 | |||
| 373 | /** |
||
| 374 | * Assert that value is a php float. |
||
| 375 | * |
||
| 376 | * @param mixed $value |
||
| 377 | * @param string|null $message |
||
| 378 | * @param string|null $propertyPath |
||
| 379 | * @return void |
||
| 380 | * @throws \Assert\AssertionFailedException |
||
| 381 | */ |
||
| 382 | public static function float($value, $message = null, $propertyPath = null) |
||
| 393 | |||
| 394 | /** |
||
| 395 | * Validates if an integer or integerish is a digit. |
||
| 396 | * |
||
| 397 | * @param mixed $value |
||
| 398 | * @param string|null $message |
||
| 399 | * @param string|null $propertyPath |
||
| 400 | * @return void |
||
| 401 | * @throws \Assert\AssertionFailedException |
||
| 402 | */ |
||
| 403 | public static function digit($value, $message = null, $propertyPath = null) |
||
| 414 | |||
| 415 | /** |
||
| 416 | * Assert that value is a php integer'ish. |
||
| 417 | * |
||
| 418 | * @param mixed $value |
||
| 419 | * @param string|null $message |
||
| 420 | * @param string|null $propertyPath |
||
| 421 | * @return void |
||
| 422 | * @throws \Assert\AssertionFailedException |
||
| 423 | */ |
||
| 424 | public static function integerish($value, $message = null, $propertyPath = null) |
||
| 435 | |||
| 436 | /** |
||
| 437 | * Assert that value is php boolean |
||
| 438 | * |
||
| 439 | * @param mixed $value |
||
| 440 | * @param string|null $message |
||
| 441 | * @param string|null $propertyPath |
||
| 442 | * @return void |
||
| 443 | * @throws \Assert\AssertionFailedException |
||
| 444 | */ |
||
| 445 | public static function boolean($value, $message = null, $propertyPath = null) |
||
| 456 | |||
| 457 | /** |
||
| 458 | * Assert that value is a PHP scalar |
||
| 459 | * |
||
| 460 | * @param mixed $value |
||
| 461 | * @param string|null $message |
||
| 462 | * @param string|null $propertyPath |
||
| 463 | * @return void |
||
| 464 | * @throws \Assert\AssertionFailedException |
||
| 465 | */ |
||
| 466 | public static function scalar($value, $message = null, $propertyPath = null) |
||
| 477 | |||
| 478 | /** |
||
| 479 | * Assert that value is not empty |
||
| 480 | * |
||
| 481 | * @param mixed $value |
||
| 482 | * @param string|null $message |
||
| 483 | * @param string|null $propertyPath |
||
| 484 | * @return void |
||
| 485 | * @throws \Assert\AssertionFailedException |
||
| 486 | */ |
||
| 487 | public static function notEmpty($value, $message = null, $propertyPath = null) |
||
| 498 | |||
| 499 | /** |
||
| 500 | * Assert that value is empty |
||
| 501 | * |
||
| 502 | * @param mixed $value |
||
| 503 | * @param string|null $message |
||
| 504 | * @param string|null $propertyPath |
||
| 505 | * @return void |
||
| 506 | * @throws \Assert\AssertionFailedException |
||
| 507 | */ |
||
| 508 | public static function noContent($value, $message = null, $propertyPath = null) |
||
| 519 | |||
| 520 | /** |
||
| 521 | * Assert that value is not null |
||
| 522 | * |
||
| 523 | * @param mixed $value |
||
| 524 | * @param string|null $message |
||
| 525 | * @param string|null $propertyPath |
||
| 526 | * @return void |
||
| 527 | * @throws \Assert\AssertionFailedException |
||
| 528 | */ |
||
| 529 | public static function notNull($value, $message = null, $propertyPath = null) |
||
| 540 | |||
| 541 | /** |
||
| 542 | * Assert that value is a string |
||
| 543 | * |
||
| 544 | * @param mixed $value |
||
| 545 | * @param string|null $message |
||
| 546 | * @param string|null $propertyPath |
||
| 547 | * @return void |
||
| 548 | * @throws \Assert\AssertionFailedException |
||
| 549 | */ |
||
| 550 | public static function string($value, $message = null, $propertyPath = null) |
||
| 562 | |||
| 563 | /** |
||
| 564 | * Assert that value matches a regex |
||
| 565 | * |
||
| 566 | * @param mixed $value |
||
| 567 | * @param string $pattern |
||
| 568 | * @param string|null $message |
||
| 569 | * @param string|null $propertyPath |
||
| 570 | * @return void |
||
| 571 | * @throws \Assert\AssertionFailedException |
||
| 572 | */ |
||
| 573 | public static function regex($value, $pattern, $message = null, $propertyPath = null) |
||
| 586 | |||
| 587 | /** |
||
| 588 | * Assert that string has a given length. |
||
| 589 | * |
||
| 590 | * @param mixed $value |
||
| 591 | * @param int $length |
||
| 592 | * @param string|null $message |
||
| 593 | * @param string|null $propertyPath |
||
| 594 | * @param string $encoding |
||
| 595 | * @return void |
||
| 596 | * @throws \Assert\AssertionFailedException |
||
| 597 | */ |
||
| 598 | public static function length($value, $length, $message = null, $propertyPath = null, $encoding = 'utf8') |
||
| 614 | |||
| 615 | /** |
||
| 616 | * Assert that a string is at least $minLength chars long. |
||
| 617 | * |
||
| 618 | * @param mixed $value |
||
| 619 | * @param int $minLength |
||
| 620 | * @param string|null $message |
||
| 621 | * @param string|null $propertyPath |
||
| 622 | * @param string $encoding |
||
| 623 | * @return void |
||
| 624 | * @throws \Assert\AssertionFailedException |
||
| 625 | */ |
||
| 626 | public static function minLength($value, $minLength, $message = null, $propertyPath = null, $encoding = 'utf8') |
||
| 642 | |||
| 643 | /** |
||
| 644 | * Assert that string value is not longer than $maxLength chars. |
||
| 645 | * |
||
| 646 | * @param mixed $value |
||
| 647 | * @param integer $maxLength |
||
| 648 | * @param string|null $message |
||
| 649 | * @param string|null $propertyPath |
||
| 650 | * @param string $encoding |
||
| 651 | * @return void |
||
| 652 | * @throws \Assert\AssertionFailedException |
||
| 653 | */ |
||
| 654 | public static function maxLength($value, $maxLength, $message = null, $propertyPath = null, $encoding = 'utf8') |
||
| 670 | |||
| 671 | /** |
||
| 672 | * Assert that string length is between min,max lengths. |
||
| 673 | * |
||
| 674 | * @param mixed $value |
||
| 675 | * @param integer $minLength |
||
| 676 | * @param integer $maxLength |
||
| 677 | * @param string|null $message |
||
| 678 | * @param string|null $propertyPath |
||
| 679 | * @param string $encoding |
||
| 680 | * @return void |
||
| 681 | * @throws \Assert\AssertionFailedException |
||
| 682 | */ |
||
| 683 | public static function betweenLength($value, $minLength, $maxLength, $message = null, $propertyPath = null, $encoding = 'utf8') |
||
| 711 | |||
| 712 | /** |
||
| 713 | * Assert that string starts with a sequence of chars. |
||
| 714 | * |
||
| 715 | * @param mixed $string |
||
| 716 | * @param string $needle |
||
| 717 | * @param string|null $message |
||
| 718 | * @param string|null $propertyPath |
||
| 719 | * @param string $encoding |
||
| 720 | * @return void |
||
| 721 | * @throws \Assert\AssertionFailedException |
||
| 722 | */ |
||
| 723 | public static function startsWith($string, $needle, $message = null, $propertyPath = null, $encoding = 'utf8') |
||
| 738 | |||
| 739 | /** |
||
| 740 | * Assert that string ends with a sequence of chars. |
||
| 741 | * |
||
| 742 | * @param mixed $string |
||
| 743 | * @param string $needle |
||
| 744 | * @param string|null $message |
||
| 745 | * @param string|null $propertyPath |
||
| 746 | * @param string $encoding |
||
| 747 | * @return void |
||
| 748 | * @throws \Assert\AssertionFailedException |
||
| 749 | */ |
||
| 750 | public static function endsWith($string, $needle, $message = null, $propertyPath = null, $encoding = 'utf8') |
||
| 767 | |||
| 768 | /** |
||
| 769 | * Assert that string contains a sequence of chars. |
||
| 770 | * |
||
| 771 | * @param mixed $string |
||
| 772 | * @param string $needle |
||
| 773 | * @param string|null $message |
||
| 774 | * @param string|null $propertyPath |
||
| 775 | * @param string $encoding |
||
| 776 | * @return void |
||
| 777 | * @throws \Assert\AssertionFailedException |
||
| 778 | */ |
||
| 779 | public static function contains($string, $needle, $message = null, $propertyPath = null, $encoding = 'utf8') |
||
| 794 | |||
| 795 | /** |
||
| 796 | * Assert that value is in array of choices. |
||
| 797 | * |
||
| 798 | * @param mixed $value |
||
| 799 | * @param array $choices |
||
| 800 | * @param string|null $message |
||
| 801 | * @param string|null $propertyPath |
||
| 802 | * @return void |
||
| 803 | * @throws \Assert\AssertionFailedException |
||
| 804 | */ |
||
| 805 | public static function choice($value, array $choices, $message = null, $propertyPath = null) |
||
| 817 | |||
| 818 | /** |
||
| 819 | * Alias of {@see choice()} |
||
| 820 | * |
||
| 821 | * @throws \Assert\AssertionFailedException |
||
| 822 | */ |
||
| 823 | public static function inArray($value, array $choices, $message = null, $propertyPath = null) |
||
| 827 | |||
| 828 | /** |
||
| 829 | * Assert that value is numeric. |
||
| 830 | * |
||
| 831 | * @param mixed $value |
||
| 832 | * @param string|null $message |
||
| 833 | * @param string|null $propertyPath |
||
| 834 | * @return void |
||
| 835 | * @throws \Assert\AssertionFailedException |
||
| 836 | */ |
||
| 837 | public static function numeric($value, $message = null, $propertyPath = null) |
||
| 848 | |||
| 849 | /** |
||
| 850 | * Assert that value is an array. |
||
| 851 | * |
||
| 852 | * @param mixed $value |
||
| 853 | * @param string|null $message |
||
| 854 | * @param string|null $propertyPath |
||
| 855 | * @return void |
||
| 856 | * @throws \Assert\AssertionFailedException |
||
| 857 | */ |
||
| 858 | public static function isArray($value, $message = null, $propertyPath = null) |
||
| 869 | |||
| 870 | /** |
||
| 871 | * Assert that value is an array or a traversable object. |
||
| 872 | * |
||
| 873 | * @param mixed $value |
||
| 874 | * @param string|null $message |
||
| 875 | * @param string|null $propertyPath |
||
| 876 | * @return void |
||
| 877 | * @throws \Assert\AssertionFailedException |
||
| 878 | */ |
||
| 879 | public static function isTraversable($value, $message = null, $propertyPath = null) |
||
| 890 | |||
| 891 | /** |
||
| 892 | * Assert that value is an array or an array-accessible object. |
||
| 893 | * |
||
| 894 | * @param mixed $value |
||
| 895 | * @param string|null $message |
||
| 896 | * @param string|null $propertyPath |
||
| 897 | * @return void |
||
| 898 | * @throws \Assert\AssertionFailedException |
||
| 899 | */ |
||
| 900 | public static function isArrayAccessible($value, $message = null, $propertyPath = null) |
||
| 911 | |||
| 912 | /** |
||
| 913 | * Assert that key exists in an array |
||
| 914 | * |
||
| 915 | * @param mixed $value |
||
| 916 | * @param string|integer $key |
||
| 917 | * @param string|null $message |
||
| 918 | * @param string|null $propertyPath |
||
| 919 | * @return void |
||
| 920 | * @throws \Assert\AssertionFailedException |
||
| 921 | */ |
||
| 922 | public static function keyExists($value, $key, $message = null, $propertyPath = null) |
||
| 935 | |||
| 936 | /** |
||
| 937 | * Assert that key does not exist in an array |
||
| 938 | * |
||
| 939 | * @param mixed $value |
||
| 940 | * @param string|integer $key |
||
| 941 | * @param string|null $message |
||
| 942 | * @param string|null $propertyPath |
||
| 943 | * @return void |
||
| 944 | * @throws \Assert\AssertionFailedException |
||
| 945 | */ |
||
| 946 | public static function keyNotExists($value, $key, $message = null, $propertyPath = null) |
||
| 959 | |||
| 960 | /** |
||
| 961 | * Assert that key exists in an array/array-accessible object using isset() |
||
| 962 | * |
||
| 963 | * @param mixed $value |
||
| 964 | * @param string|integer $key |
||
| 965 | * @param string|null $message |
||
| 966 | * @param string|null $propertyPath |
||
| 967 | * @return void |
||
| 968 | * @throws \Assert\AssertionFailedException |
||
| 969 | */ |
||
| 970 | public static function keyIsset($value, $key, $message = null, $propertyPath = null) |
||
| 983 | |||
| 984 | /** |
||
| 985 | * Assert that key exists in an array/array-accessible object and it's value is not empty. |
||
| 986 | * |
||
| 987 | * @param mixed $value |
||
| 988 | * @param string|integer $key |
||
| 989 | * @param string|null $message |
||
| 990 | * @param string|null $propertyPath |
||
| 991 | * @return void |
||
| 992 | * @throws \Assert\AssertionFailedException |
||
| 993 | */ |
||
| 994 | public static function notEmptyKey($value, $key, $message = null, $propertyPath = null) |
||
| 999 | |||
| 1000 | /** |
||
| 1001 | * Assert that value is not blank |
||
| 1002 | * |
||
| 1003 | * @param mixed $value |
||
| 1004 | * @param string|null $message |
||
| 1005 | * @param string|null $propertyPath |
||
| 1006 | * @return void |
||
| 1007 | * @throws \Assert\AssertionFailedException |
||
| 1008 | */ |
||
| 1009 | public static function notBlank($value, $message = null, $propertyPath = null) |
||
| 1020 | |||
| 1021 | /** |
||
| 1022 | * Assert that value is instance of given class-name. |
||
| 1023 | * |
||
| 1024 | * @param mixed $value |
||
| 1025 | * @param string $className |
||
| 1026 | * @param string|null $message |
||
| 1027 | * @param string|null $propertyPath |
||
| 1028 | * @return void |
||
| 1029 | * @throws \Assert\AssertionFailedException |
||
| 1030 | */ |
||
| 1031 | public static function isInstanceOf($value, $className, $message = null, $propertyPath = null) |
||
| 1043 | |||
| 1044 | /** |
||
| 1045 | * Assert that value is not instance of given class-name. |
||
| 1046 | * |
||
| 1047 | * @param mixed $value |
||
| 1048 | * @param string $className |
||
| 1049 | * @param string|null $message |
||
| 1050 | * @param string|null $propertyPath |
||
| 1051 | * @return void |
||
| 1052 | * @throws \Assert\AssertionFailedException |
||
| 1053 | */ |
||
| 1054 | public static function notIsInstanceOf($value, $className, $message = null, $propertyPath = null) |
||
| 1066 | |||
| 1067 | /** |
||
| 1068 | * Assert that value is subclass of given class-name. |
||
| 1069 | * |
||
| 1070 | * @param mixed $value |
||
| 1071 | * @param string $className |
||
| 1072 | * @param string|null $message |
||
| 1073 | * @param string|null $propertyPath |
||
| 1074 | * @return void |
||
| 1075 | * @throws \Assert\AssertionFailedException |
||
| 1076 | */ |
||
| 1077 | public static function subclassOf($value, $className, $message = null, $propertyPath = null) |
||
| 1089 | |||
| 1090 | /** |
||
| 1091 | * Assert that value is in range of numbers. |
||
| 1092 | * |
||
| 1093 | * @param mixed $value |
||
| 1094 | * @param integer $minValue |
||
| 1095 | * @param integer $maxValue |
||
| 1096 | * @param string|null $message |
||
| 1097 | * @param string|null $propertyPath |
||
| 1098 | * @return void |
||
| 1099 | * @throws \Assert\AssertionFailedException |
||
| 1100 | */ |
||
| 1101 | public static function range($value, $minValue, $maxValue, $message = null, $propertyPath = null) |
||
| 1116 | |||
| 1117 | /** |
||
| 1118 | * Assert that a value is at least as big as a given limit |
||
| 1119 | * |
||
| 1120 | * @param mixed $value |
||
| 1121 | * @param mixed $minValue |
||
| 1122 | * @param string|null $message |
||
| 1123 | * @param string|null $propertyPath |
||
| 1124 | * @return void |
||
| 1125 | * @throws \Assert\AssertionFailedException |
||
| 1126 | */ |
||
| 1127 | public static function min($value, $minValue, $message = null, $propertyPath = null) |
||
| 1141 | |||
| 1142 | /** |
||
| 1143 | * Assert that a number is smaller as a given limit |
||
| 1144 | * |
||
| 1145 | * @param mixed $value |
||
| 1146 | * @param mixed $maxValue |
||
| 1147 | * @param string|null $message |
||
| 1148 | * @param string|null $propertyPath |
||
| 1149 | * @return void |
||
| 1150 | * @throws \Assert\AssertionFailedException |
||
| 1151 | */ |
||
| 1152 | public static function max($value, $maxValue, $message = null, $propertyPath = null) |
||
| 1166 | |||
| 1167 | /** |
||
| 1168 | * Assert that a file exists |
||
| 1169 | * |
||
| 1170 | * @param string $value |
||
| 1171 | * @param string|null $message |
||
| 1172 | * @param string|null $propertyPath |
||
| 1173 | * @return void |
||
| 1174 | * @throws \Assert\AssertionFailedException |
||
| 1175 | */ |
||
| 1176 | public static function file($value, $message = null, $propertyPath = null) |
||
| 1190 | |||
| 1191 | /** |
||
| 1192 | * Assert that a directory exists |
||
| 1193 | * |
||
| 1194 | * @param string $value |
||
| 1195 | * @param string|null $message |
||
| 1196 | * @param string|null $propertyPath |
||
| 1197 | * @return void |
||
| 1198 | * @throws \Assert\AssertionFailedException |
||
| 1199 | */ |
||
| 1200 | public static function directory($value, $message = null, $propertyPath = null) |
||
| 1213 | |||
| 1214 | /** |
||
| 1215 | * Assert that the value is something readable |
||
| 1216 | * |
||
| 1217 | * @param string $value |
||
| 1218 | * @param string|null $message |
||
| 1219 | * @param string|null $propertyPath |
||
| 1220 | * @return void |
||
| 1221 | * @throws \Assert\AssertionFailedException |
||
| 1222 | */ |
||
| 1223 | public static function readable($value, $message = null, $propertyPath = null) |
||
| 1236 | |||
| 1237 | /** |
||
| 1238 | * Assert that the value is something writeable |
||
| 1239 | * |
||
| 1240 | * @param string $value |
||
| 1241 | * @param string|null $message |
||
| 1242 | * @param string|null $propertyPath |
||
| 1243 | * @return void |
||
| 1244 | * @throws \Assert\AssertionFailedException |
||
| 1245 | */ |
||
| 1246 | public static function writeable($value, $message = null, $propertyPath = null) |
||
| 1259 | |||
| 1260 | /** |
||
| 1261 | * Assert that value is an email adress (using input_filter/FILTER_VALIDATE_EMAIL). |
||
| 1262 | * |
||
| 1263 | * @param mixed $value |
||
| 1264 | * @param string|null $message |
||
| 1265 | * @param string|null $propertyPath |
||
| 1266 | * @return void |
||
| 1267 | * @throws \Assert\AssertionFailedException |
||
| 1268 | */ |
||
| 1269 | public static function email($value, $message = null, $propertyPath = null) |
||
| 1294 | |||
| 1295 | /** |
||
| 1296 | * Assert that value is an URL. |
||
| 1297 | * |
||
| 1298 | * This code snipped was taken from the Symfony project and modified to the special demands of this method. |
||
| 1299 | * |
||
| 1300 | * @param mixed $value |
||
| 1301 | * @param string|null $message |
||
| 1302 | * @param string|null $propertyPath |
||
| 1303 | * @return void |
||
| 1304 | * @throws \Assert\AssertionFailedException |
||
| 1305 | * |
||
| 1306 | * |
||
| 1307 | * @link https://github.com/symfony/Validator/blob/master/Constraints/UrlValidator.php |
||
| 1308 | * @link https://github.com/symfony/Validator/blob/master/Constraints/Url.php |
||
| 1309 | */ |
||
| 1310 | public static function url($value, $message = null, $propertyPath = null) |
||
| 1343 | |||
| 1344 | /** |
||
| 1345 | * Assert that value is alphanumeric. |
||
| 1346 | * |
||
| 1347 | * @param mixed $value |
||
| 1348 | * @param string|null $message |
||
| 1349 | * @param string|null $propertyPath |
||
| 1350 | * @return void |
||
| 1351 | * @throws \Assert\AssertionFailedException |
||
| 1352 | */ |
||
| 1353 | public static function alnum($value, $message = null, $propertyPath = null) |
||
| 1366 | |||
| 1367 | /** |
||
| 1368 | * Assert that the value is boolean True. |
||
| 1369 | * |
||
| 1370 | * @param mixed $value |
||
| 1371 | * @param string|null $message |
||
| 1372 | * @param string|null $propertyPath |
||
| 1373 | * @return void |
||
| 1374 | * @throws \Assert\AssertionFailedException |
||
| 1375 | */ |
||
| 1376 | public static function true($value, $message = null, $propertyPath = null) |
||
| 1387 | |||
| 1388 | /** |
||
| 1389 | * Assert that the value is boolean False. |
||
| 1390 | * |
||
| 1391 | * @param mixed $value |
||
| 1392 | * @param string|null $message |
||
| 1393 | * @param string|null $propertyPath |
||
| 1394 | * @return void |
||
| 1395 | * @throws \Assert\AssertionFailedException |
||
| 1396 | */ |
||
| 1397 | public static function false($value, $message = null, $propertyPath = null) |
||
| 1408 | |||
| 1409 | /** |
||
| 1410 | * Assert that the class exists. |
||
| 1411 | * |
||
| 1412 | * @param mixed $value |
||
| 1413 | * @param string|null $message |
||
| 1414 | * @param string|null $propertyPath |
||
| 1415 | * @return void |
||
| 1416 | * @throws \Assert\AssertionFailedException |
||
| 1417 | */ |
||
| 1418 | public static function classExists($value, $message = null, $propertyPath = null) |
||
| 1429 | |||
| 1430 | /** |
||
| 1431 | * Assert that the class implements the interface |
||
| 1432 | * |
||
| 1433 | * @param mixed $class |
||
| 1434 | * @param string $interfaceName |
||
| 1435 | * @param string|null $message |
||
| 1436 | * @param string|null $propertyPath |
||
| 1437 | * @return void |
||
| 1438 | * @throws \Assert\AssertionFailedException |
||
| 1439 | */ |
||
| 1440 | public static function implementsInterface($class, $interfaceName, $message = null, $propertyPath = null) |
||
| 1453 | |||
| 1454 | /** |
||
| 1455 | * Assert that the given string is a valid json string. |
||
| 1456 | * |
||
| 1457 | * NOTICE: |
||
| 1458 | * Since this does a json_decode to determine its validity |
||
| 1459 | * you probably should consider, when using the variable |
||
| 1460 | * content afterwards, just to decode and check for yourself instead |
||
| 1461 | * of using this assertion. |
||
| 1462 | * |
||
| 1463 | * @param mixed $value |
||
| 1464 | * @param string|null $message |
||
| 1465 | * @param string|null $propertyPath |
||
| 1466 | * @return void |
||
| 1467 | * @throws \Assert\AssertionFailedException |
||
| 1468 | */ |
||
| 1469 | public static function isJsonString($value, $message = null, $propertyPath = null) |
||
| 1480 | |||
| 1481 | /** |
||
| 1482 | * Assert that the given string is a valid UUID |
||
| 1483 | * |
||
| 1484 | * Uses code from {@link https://github.com/ramsey/uuid} that is MIT licensed. |
||
| 1485 | * |
||
| 1486 | * @param string $value |
||
| 1487 | * @param string|null $message |
||
| 1488 | * @param string|null $propertyPath |
||
| 1489 | * @return void |
||
| 1490 | * @throws \Assert\AssertionFailedException |
||
| 1491 | */ |
||
| 1492 | public static function uuid($value, $message = null, $propertyPath = null) |
||
| 1509 | |||
| 1510 | /** |
||
| 1511 | * Assert that the count of countable is equal to count. |
||
| 1512 | * |
||
| 1513 | * @param array|\Countable $countable |
||
| 1514 | * @param int $count |
||
| 1515 | * @param string $message |
||
| 1516 | * @param string $propertyPath |
||
| 1517 | * @return void |
||
| 1518 | * @throws \Assert\AssertionFailedException |
||
| 1519 | */ |
||
| 1520 | public static function count($countable, $count, $message = null, $propertyPath = null) |
||
| 1531 | |||
| 1532 | /** |
||
| 1533 | * static call handler to implement: |
||
| 1534 | * - "null or assertion" delegation |
||
| 1535 | * - "all" delegation |
||
| 1536 | */ |
||
| 1537 | public static function __callStatic($method, $args) |
||
| 1573 | |||
| 1574 | /** |
||
| 1575 | * Determines if the values array has every choice as key and that this choice has content. |
||
| 1576 | * |
||
| 1577 | * @param array $values |
||
| 1578 | * @param array $choices |
||
| 1579 | * @param null $message |
||
| 1580 | * @param null $propertyPath |
||
| 1581 | */ |
||
| 1582 | public static function choicesNotEmpty(array $values, array $choices, $message = null, $propertyPath = null) |
||
| 1591 | |||
| 1592 | /** |
||
| 1593 | * Determines that the named method is defined in the provided object. |
||
| 1594 | * |
||
| 1595 | * @param string $value |
||
| 1596 | * @param mixed $object |
||
| 1597 | * @param null $message |
||
| 1598 | * @param null $propertyPath |
||
| 1599 | * |
||
| 1600 | * @throws |
||
| 1601 | */ |
||
| 1602 | public static function methodExists($value, $object, $message = null, $propertyPath = null) |
||
| 1615 | |||
| 1616 | /** |
||
| 1617 | * Determines that the provided value is an object. |
||
| 1618 | * |
||
| 1619 | * @param mixed $value |
||
| 1620 | * @param null $message |
||
| 1621 | * @param null $propertyPath |
||
| 1622 | */ |
||
| 1623 | public static function isObject($value, $message = null, $propertyPath = null) |
||
| 1635 | |||
| 1636 | /** |
||
| 1637 | * Determines if the value is less than given limit. |
||
| 1638 | * |
||
| 1639 | * @param mixed $value |
||
| 1640 | * @param mixed $limit |
||
| 1641 | * @param null $message |
||
| 1642 | * @param null $propertyPath |
||
| 1643 | */ |
||
| 1644 | public static function lessThan($value, $limit, $message = null, $propertyPath = null) |
||
| 1656 | |||
| 1657 | /** |
||
| 1658 | * Determines if the value is less or than given limit. |
||
| 1659 | * |
||
| 1660 | * @param mixed $value |
||
| 1661 | * @param mixed $limit |
||
| 1662 | * @param null $message |
||
| 1663 | * @param null $propertyPath |
||
| 1664 | */ |
||
| 1665 | public static function lessOrEqualThan($value, $limit, $message = null, $propertyPath = null) |
||
| 1677 | |||
| 1678 | /** |
||
| 1679 | * Determines if the value is greater than given limit. |
||
| 1680 | * |
||
| 1681 | * @param mixed $value |
||
| 1682 | * @param mixed $limit |
||
| 1683 | * @param null $message |
||
| 1684 | * @param null $propertyPath |
||
| 1685 | */ |
||
| 1686 | public static function greaterThan($value, $limit, $message = null, $propertyPath = null) |
||
| 1698 | |||
| 1699 | /** |
||
| 1700 | * Determines if the value is greater or equal than given limit. |
||
| 1701 | * |
||
| 1702 | * @param mixed $value |
||
| 1703 | * @param mixed $limit |
||
| 1704 | * @param null $message |
||
| 1705 | * @param null $propertyPath |
||
| 1706 | */ |
||
| 1707 | public static function greaterOrEqualThan($value, $limit, $message = null, $propertyPath = null) |
||
| 1719 | |||
| 1720 | /** |
||
| 1721 | * Assert that date is valid and corresponds to the given format. |
||
| 1722 | * |
||
| 1723 | * @param string $value |
||
| 1724 | * @param string $format supports all of the options date(), except for the following: |
||
| 1725 | * N, w, W, t, L, o, B, a, A, g, h, I, O, P, Z, c, r. |
||
| 1726 | * @param string|null $message |
||
| 1727 | * @param string|null $propertyPath |
||
| 1728 | * |
||
| 1729 | * @link http://php.net/manual/function.date.php#refsect1-function.date-parameters |
||
| 1730 | */ |
||
| 1731 | public static function date($value, $format, $message = null, $propertyPath = null) |
||
| 1748 | |||
| 1749 | /** |
||
| 1750 | * Determines that the provided value is callable. |
||
| 1751 | * |
||
| 1752 | * @param mixed $value |
||
| 1753 | * @param null $message |
||
| 1754 | * @param null $propertyPath |
||
| 1755 | */ |
||
| 1756 | public static function isCallable($value, $message = null, $propertyPath = null) |
||
| 1767 | |||
| 1768 | /** |
||
| 1769 | * Assert that the provided value is valid according to a callback. |
||
| 1770 | * |
||
| 1771 | * If the callback returns `false` the assertion will fail. |
||
| 1772 | * |
||
| 1773 | * @param mixed $value |
||
| 1774 | * @param callable $callback |
||
| 1775 | * @param string|null $message |
||
| 1776 | * @param string|null $propertyPath |
||
| 1777 | */ |
||
| 1778 | public static function satisfy($value, $callback, $message = null, $propertyPath = null) |
||
| 1791 | |||
| 1792 | /** |
||
| 1793 | * Assert that value is an IPv4 or IPv6 address |
||
| 1794 | * (using input_filter/FILTER_VALIDATE_IP). |
||
| 1795 | * |
||
| 1796 | * @param string $value |
||
| 1797 | * @param string|null $message |
||
| 1798 | * @param string|null $propertyPath |
||
| 1799 | * @param null|int $flag |
||
| 1800 | * |
||
| 1801 | * @link http://php.net/manual/filter.filters.flags.php |
||
| 1802 | */ |
||
| 1803 | public static function ip($value, $message = null, $propertyPath = null, $flag = null) |
||
| 1814 | |||
| 1815 | /** |
||
| 1816 | * Assert that value is an IPv4 address |
||
| 1817 | * (using input_filter/FILTER_VALIDATE_IP). |
||
| 1818 | * |
||
| 1819 | * @param string $value |
||
| 1820 | * @param string|null $message |
||
| 1821 | * @param string|null $propertyPath |
||
| 1822 | * @param null|int $flag |
||
| 1823 | * |
||
| 1824 | * @link http://php.net/manual/filter.filters.flags.php |
||
| 1825 | */ |
||
| 1826 | public static function ipv4($value, $message = null, $propertyPath = null, $flag = null) |
||
| 1830 | |||
| 1831 | /** |
||
| 1832 | * Assert that value is an IPv6 address |
||
| 1833 | * (using input_filter/FILTER_VALIDATE_IP). |
||
| 1834 | * |
||
| 1835 | * @param string $value |
||
| 1836 | * @param string|null $message |
||
| 1837 | * @param string|null $propertyPath |
||
| 1838 | * @param null|int $flag |
||
| 1839 | * |
||
| 1840 | * @link http://php.net/manual/filter.filters.flags.php |
||
| 1841 | */ |
||
| 1842 | public static function ipv6($value, $message = null, $propertyPath = null, $flag = null) |
||
| 1846 | |||
| 1847 | /** |
||
| 1848 | * Make a string version of a value. |
||
| 1849 | * |
||
| 1850 | * @param mixed $value |
||
| 1851 | * @return string |
||
| 1852 | */ |
||
| 1853 | protected static function stringify($value) |
||
| 1887 | } |
||
| 1888 | |||
| 1889 |