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 |
||
| 164 | class Assertion |
||
| 165 | { |
||
| 166 | const INVALID_FLOAT = 9; |
||
| 167 | const INVALID_INTEGER = 10; |
||
| 168 | const INVALID_DIGIT = 11; |
||
| 169 | const INVALID_INTEGERISH = 12; |
||
| 170 | const INVALID_BOOLEAN = 13; |
||
| 171 | const VALUE_EMPTY = 14; |
||
| 172 | const VALUE_NULL = 15; |
||
| 173 | const VALUE_NOT_NULL = 25; |
||
| 174 | const INVALID_STRING = 16; |
||
| 175 | const INVALID_REGEX = 17; |
||
| 176 | const INVALID_MIN_LENGTH = 18; |
||
| 177 | const INVALID_MAX_LENGTH = 19; |
||
| 178 | const INVALID_STRING_START = 20; |
||
| 179 | const INVALID_STRING_CONTAINS = 21; |
||
| 180 | const INVALID_CHOICE = 22; |
||
| 181 | const INVALID_NUMERIC = 23; |
||
| 182 | const INVALID_ARRAY = 24; |
||
| 183 | const INVALID_KEY_EXISTS = 26; |
||
| 184 | const INVALID_NOT_BLANK = 27; |
||
| 185 | const INVALID_INSTANCE_OF = 28; |
||
| 186 | const INVALID_SUBCLASS_OF = 29; |
||
| 187 | const INVALID_RANGE = 30; |
||
| 188 | const INVALID_ALNUM = 31; |
||
| 189 | const INVALID_TRUE = 32; |
||
| 190 | const INVALID_EQ = 33; |
||
| 191 | const INVALID_SAME = 34; |
||
| 192 | const INVALID_MIN = 35; |
||
| 193 | const INVALID_MAX = 36; |
||
| 194 | const INVALID_LENGTH = 37; |
||
| 195 | const INVALID_FALSE = 38; |
||
| 196 | const INVALID_STRING_END = 39; |
||
| 197 | const INVALID_UUID = 40; |
||
| 198 | const INVALID_COUNT = 41; |
||
| 199 | const INVALID_NOT_EQ = 42; |
||
| 200 | const INVALID_NOT_SAME = 43; |
||
| 201 | const INVALID_TRAVERSABLE = 44; |
||
| 202 | const INVALID_ARRAY_ACCESSIBLE = 45; |
||
| 203 | const INVALID_KEY_ISSET = 46; |
||
| 204 | const INVALID_VALUE_IN_ARRAY = 47; |
||
| 205 | const INVALID_E164 = 48; |
||
| 206 | const INVALID_DIRECTORY = 101; |
||
| 207 | const INVALID_FILE = 102; |
||
| 208 | const INVALID_READABLE = 103; |
||
| 209 | const INVALID_WRITEABLE = 104; |
||
| 210 | const INVALID_CLASS = 105; |
||
| 211 | const INVALID_INTERFACE = 106; |
||
| 212 | const INVALID_EMAIL = 201; |
||
| 213 | const INTERFACE_NOT_IMPLEMENTED = 202; |
||
| 214 | const INVALID_URL = 203; |
||
| 215 | const INVALID_NOT_INSTANCE_OF = 204; |
||
| 216 | const VALUE_NOT_EMPTY = 205; |
||
| 217 | const INVALID_JSON_STRING = 206; |
||
| 218 | const INVALID_OBJECT = 207; |
||
| 219 | const INVALID_METHOD = 208; |
||
| 220 | const INVALID_SCALAR = 209; |
||
| 221 | const INVALID_LESS = 210; |
||
| 222 | const INVALID_LESS_OR_EQUAL = 211; |
||
| 223 | const INVALID_GREATER = 212; |
||
| 224 | const INVALID_GREATER_OR_EQUAL = 213; |
||
| 225 | const INVALID_DATE = 214; |
||
| 226 | const INVALID_CALLABLE = 215; |
||
| 227 | const INVALID_KEY_NOT_EXISTS = 216; |
||
| 228 | const INVALID_SATISFY = 217; |
||
| 229 | const INVALID_IP = 218; |
||
| 230 | |||
| 231 | /** |
||
| 232 | * Exception to throw when an assertion failed. |
||
| 233 | * |
||
| 234 | * @var string |
||
| 235 | */ |
||
| 236 | static protected $exceptionClass = 'Assert\InvalidArgumentException'; |
||
| 237 | |||
| 238 | /** |
||
| 239 | * Helper method that handles building the assertion failure exceptions. |
||
| 240 | * They are returned from this method so that the stack trace still shows |
||
| 241 | * the assertions method. |
||
| 242 | */ |
||
| 243 | protected static function createException($value, $message, $code, $propertyPath, array $constraints = array()) |
||
| 248 | |||
| 249 | /** |
||
| 250 | * Assert that two values are equal (using == ). |
||
| 251 | * |
||
| 252 | * @param mixed $value |
||
| 253 | * @param mixed $value2 |
||
| 254 | * @param string|null $message |
||
| 255 | * @param string|null $propertyPath |
||
| 256 | * @return void |
||
| 257 | * @throws \Assert\AssertionFailedException |
||
| 258 | */ |
||
| 259 | public static function eq($value, $value2, $message = null, $propertyPath = null) |
||
| 271 | |||
| 272 | /** |
||
| 273 | * Assert that two values are the same (using ===). |
||
| 274 | * |
||
| 275 | * @param mixed $value |
||
| 276 | * @param mixed $value2 |
||
| 277 | * @param string|null $message |
||
| 278 | * @param string|null $propertyPath |
||
| 279 | * @return void |
||
| 280 | * @throws \Assert\AssertionFailedException |
||
| 281 | */ |
||
| 282 | public static function same($value, $value2, $message = null, $propertyPath = null) |
||
| 294 | |||
| 295 | /** |
||
| 296 | * Assert that two values are not equal (using == ). |
||
| 297 | * |
||
| 298 | * @param mixed $value1 |
||
| 299 | * @param mixed $value2 |
||
| 300 | * @param string|null $message |
||
| 301 | * @param string|null $propertyPath |
||
| 302 | * @return void |
||
| 303 | * @throws \Assert\AssertionFailedException |
||
| 304 | */ |
||
| 305 | public static function notEq($value1, $value2, $message = null, $propertyPath = null) |
||
| 316 | |||
| 317 | /** |
||
| 318 | * Assert that two values are not the same (using === ). |
||
| 319 | * |
||
| 320 | * @param mixed $value1 |
||
| 321 | * @param mixed $value2 |
||
| 322 | * @param string|null $message |
||
| 323 | * @param string|null $propertyPath |
||
| 324 | * @return void |
||
| 325 | * @throws \Assert\AssertionFailedException |
||
| 326 | */ |
||
| 327 | public static function notSame($value1, $value2, $message = null, $propertyPath = null) |
||
| 338 | |||
| 339 | /** |
||
| 340 | * Assert that value is not in array of choices. |
||
| 341 | * |
||
| 342 | * @param mixed $value |
||
| 343 | * @param array $choices |
||
| 344 | * @param string|null $message |
||
| 345 | * @param string|null $propertyPath |
||
| 346 | * @return void |
||
| 347 | * @throws \Assert\AssertionFailedException |
||
| 348 | */ |
||
| 349 | public static function notInArray($value, array $choices, $message = null, $propertyPath = null) |
||
| 360 | |||
| 361 | /** |
||
| 362 | * Assert that value is a php integer. |
||
| 363 | * |
||
| 364 | * @param mixed $value |
||
| 365 | * @param string|null $message |
||
| 366 | * @param string|null $propertyPath |
||
| 367 | * @return void |
||
| 368 | * @throws \Assert\AssertionFailedException |
||
| 369 | */ |
||
| 370 | public static function integer($value, $message = null, $propertyPath = null) |
||
| 381 | |||
| 382 | /** |
||
| 383 | * Assert that value is a php float. |
||
| 384 | * |
||
| 385 | * @param mixed $value |
||
| 386 | * @param string|null $message |
||
| 387 | * @param string|null $propertyPath |
||
| 388 | * @return void |
||
| 389 | * @throws \Assert\AssertionFailedException |
||
| 390 | */ |
||
| 391 | public static function float($value, $message = null, $propertyPath = null) |
||
| 402 | |||
| 403 | /** |
||
| 404 | * Validates if an integer or integerish is a digit. |
||
| 405 | * |
||
| 406 | * @param mixed $value |
||
| 407 | * @param string|null $message |
||
| 408 | * @param string|null $propertyPath |
||
| 409 | * @return void |
||
| 410 | * @throws \Assert\AssertionFailedException |
||
| 411 | */ |
||
| 412 | public static function digit($value, $message = null, $propertyPath = null) |
||
| 423 | |||
| 424 | /** |
||
| 425 | * Assert that value is a php integer'ish. |
||
| 426 | * |
||
| 427 | * @param mixed $value |
||
| 428 | * @param string|null $message |
||
| 429 | * @param string|null $propertyPath |
||
| 430 | * @return void |
||
| 431 | * @throws \Assert\AssertionFailedException |
||
| 432 | */ |
||
| 433 | public static function integerish($value, $message = null, $propertyPath = null) |
||
| 444 | |||
| 445 | /** |
||
| 446 | * Assert that value is php boolean |
||
| 447 | * |
||
| 448 | * @param mixed $value |
||
| 449 | * @param string|null $message |
||
| 450 | * @param string|null $propertyPath |
||
| 451 | * @return void |
||
| 452 | * @throws \Assert\AssertionFailedException |
||
| 453 | */ |
||
| 454 | public static function boolean($value, $message = null, $propertyPath = null) |
||
| 465 | |||
| 466 | /** |
||
| 467 | * Assert that value is a PHP scalar |
||
| 468 | * |
||
| 469 | * @param mixed $value |
||
| 470 | * @param string|null $message |
||
| 471 | * @param string|null $propertyPath |
||
| 472 | * @return void |
||
| 473 | * @throws \Assert\AssertionFailedException |
||
| 474 | */ |
||
| 475 | public static function scalar($value, $message = null, $propertyPath = null) |
||
| 486 | |||
| 487 | /** |
||
| 488 | * Assert that value is not empty |
||
| 489 | * |
||
| 490 | * @param mixed $value |
||
| 491 | * @param string|null $message |
||
| 492 | * @param string|null $propertyPath |
||
| 493 | * @return void |
||
| 494 | * @throws \Assert\AssertionFailedException |
||
| 495 | */ |
||
| 496 | public static function notEmpty($value, $message = null, $propertyPath = null) |
||
| 507 | |||
| 508 | /** |
||
| 509 | * Assert that value is empty |
||
| 510 | * |
||
| 511 | * @param mixed $value |
||
| 512 | * @param string|null $message |
||
| 513 | * @param string|null $propertyPath |
||
| 514 | * @return void |
||
| 515 | * @throws \Assert\AssertionFailedException |
||
| 516 | */ |
||
| 517 | public static function noContent($value, $message = null, $propertyPath = null) |
||
| 528 | |||
| 529 | /** |
||
| 530 | * Assert that value is null |
||
| 531 | * |
||
| 532 | * @param mixed $value |
||
| 533 | * @param string|null $message |
||
| 534 | * @param string|null $propertyPath |
||
| 535 | * @return void |
||
| 536 | * @throws \Assert\AssertionFailedException |
||
| 537 | */ |
||
| 538 | public static function null($value, $message = null, $propertyPath = null) |
||
| 549 | |||
| 550 | /** |
||
| 551 | * Assert that value is not null |
||
| 552 | * |
||
| 553 | * @param mixed $value |
||
| 554 | * @param string|null $message |
||
| 555 | * @param string|null $propertyPath |
||
| 556 | * @return void |
||
| 557 | * @throws \Assert\AssertionFailedException |
||
| 558 | */ |
||
| 559 | public static function notNull($value, $message = null, $propertyPath = null) |
||
| 570 | |||
| 571 | /** |
||
| 572 | * Assert that value is a string |
||
| 573 | * |
||
| 574 | * @param mixed $value |
||
| 575 | * @param string|null $message |
||
| 576 | * @param string|null $propertyPath |
||
| 577 | * @return void |
||
| 578 | * @throws \Assert\AssertionFailedException |
||
| 579 | */ |
||
| 580 | public static function string($value, $message = null, $propertyPath = null) |
||
| 592 | |||
| 593 | /** |
||
| 594 | * Assert that value matches a regex |
||
| 595 | * |
||
| 596 | * @param mixed $value |
||
| 597 | * @param string $pattern |
||
| 598 | * @param string|null $message |
||
| 599 | * @param string|null $propertyPath |
||
| 600 | * @return void |
||
| 601 | * @throws \Assert\AssertionFailedException |
||
| 602 | */ |
||
| 603 | public static function regex($value, $pattern, $message = null, $propertyPath = null) |
||
| 616 | |||
| 617 | /** |
||
| 618 | * Assert that string has a given length. |
||
| 619 | * |
||
| 620 | * @param mixed $value |
||
| 621 | * @param int $length |
||
| 622 | * @param string|null $message |
||
| 623 | * @param string|null $propertyPath |
||
| 624 | * @param string $encoding |
||
| 625 | * @return void |
||
| 626 | * @throws \Assert\AssertionFailedException |
||
| 627 | */ |
||
| 628 | public static function length($value, $length, $message = null, $propertyPath = null, $encoding = 'utf8') |
||
| 644 | |||
| 645 | /** |
||
| 646 | * Assert that a string is at least $minLength chars long. |
||
| 647 | * |
||
| 648 | * @param mixed $value |
||
| 649 | * @param int $minLength |
||
| 650 | * @param string|null $message |
||
| 651 | * @param string|null $propertyPath |
||
| 652 | * @param string $encoding |
||
| 653 | * @return void |
||
| 654 | * @throws \Assert\AssertionFailedException |
||
| 655 | */ |
||
| 656 | public static function minLength($value, $minLength, $message = null, $propertyPath = null, $encoding = 'utf8') |
||
| 672 | |||
| 673 | /** |
||
| 674 | * Assert that string value is not longer than $maxLength chars. |
||
| 675 | * |
||
| 676 | * @param mixed $value |
||
| 677 | * @param integer $maxLength |
||
| 678 | * @param string|null $message |
||
| 679 | * @param string|null $propertyPath |
||
| 680 | * @param string $encoding |
||
| 681 | * @return void |
||
| 682 | * @throws \Assert\AssertionFailedException |
||
| 683 | */ |
||
| 684 | public static function maxLength($value, $maxLength, $message = null, $propertyPath = null, $encoding = 'utf8') |
||
| 700 | |||
| 701 | /** |
||
| 702 | * Assert that string length is between min,max lengths. |
||
| 703 | * |
||
| 704 | * @param mixed $value |
||
| 705 | * @param integer $minLength |
||
| 706 | * @param integer $maxLength |
||
| 707 | * @param string|null $message |
||
| 708 | * @param string|null $propertyPath |
||
| 709 | * @param string $encoding |
||
| 710 | * @return void |
||
| 711 | * @throws \Assert\AssertionFailedException |
||
| 712 | */ |
||
| 713 | public static function betweenLength($value, $minLength, $maxLength, $message = null, $propertyPath = null, $encoding = 'utf8') |
||
| 741 | |||
| 742 | /** |
||
| 743 | * Assert that string starts with a sequence of chars. |
||
| 744 | * |
||
| 745 | * @param mixed $string |
||
| 746 | * @param string $needle |
||
| 747 | * @param string|null $message |
||
| 748 | * @param string|null $propertyPath |
||
| 749 | * @param string $encoding |
||
| 750 | * @return void |
||
| 751 | * @throws \Assert\AssertionFailedException |
||
| 752 | */ |
||
| 753 | public static function startsWith($string, $needle, $message = null, $propertyPath = null, $encoding = 'utf8') |
||
| 768 | |||
| 769 | /** |
||
| 770 | * Assert that string ends with a sequence of chars. |
||
| 771 | * |
||
| 772 | * @param mixed $string |
||
| 773 | * @param string $needle |
||
| 774 | * @param string|null $message |
||
| 775 | * @param string|null $propertyPath |
||
| 776 | * @param string $encoding |
||
| 777 | * @return void |
||
| 778 | * @throws \Assert\AssertionFailedException |
||
| 779 | */ |
||
| 780 | public static function endsWith($string, $needle, $message = null, $propertyPath = null, $encoding = 'utf8') |
||
| 797 | |||
| 798 | /** |
||
| 799 | * Assert that string contains a sequence of chars. |
||
| 800 | * |
||
| 801 | * @param mixed $string |
||
| 802 | * @param string $needle |
||
| 803 | * @param string|null $message |
||
| 804 | * @param string|null $propertyPath |
||
| 805 | * @param string $encoding |
||
| 806 | * @return void |
||
| 807 | * @throws \Assert\AssertionFailedException |
||
| 808 | */ |
||
| 809 | public static function contains($string, $needle, $message = null, $propertyPath = null, $encoding = 'utf8') |
||
| 824 | |||
| 825 | /** |
||
| 826 | * Assert that value is in array of choices. |
||
| 827 | * |
||
| 828 | * @param mixed $value |
||
| 829 | * @param array $choices |
||
| 830 | * @param string|null $message |
||
| 831 | * @param string|null $propertyPath |
||
| 832 | * @return void |
||
| 833 | * @throws \Assert\AssertionFailedException |
||
| 834 | */ |
||
| 835 | public static function choice($value, array $choices, $message = null, $propertyPath = null) |
||
| 847 | |||
| 848 | /** |
||
| 849 | * Alias of {@see choice()} |
||
| 850 | * |
||
| 851 | * @throws \Assert\AssertionFailedException |
||
| 852 | */ |
||
| 853 | public static function inArray($value, array $choices, $message = null, $propertyPath = null) |
||
| 857 | |||
| 858 | /** |
||
| 859 | * Assert that value is numeric. |
||
| 860 | * |
||
| 861 | * @param mixed $value |
||
| 862 | * @param string|null $message |
||
| 863 | * @param string|null $propertyPath |
||
| 864 | * @return void |
||
| 865 | * @throws \Assert\AssertionFailedException |
||
| 866 | */ |
||
| 867 | public static function numeric($value, $message = null, $propertyPath = null) |
||
| 878 | |||
| 879 | /** |
||
| 880 | * Assert that value is an array. |
||
| 881 | * |
||
| 882 | * @param mixed $value |
||
| 883 | * @param string|null $message |
||
| 884 | * @param string|null $propertyPath |
||
| 885 | * @return void |
||
| 886 | * @throws \Assert\AssertionFailedException |
||
| 887 | */ |
||
| 888 | public static function isArray($value, $message = null, $propertyPath = null) |
||
| 899 | |||
| 900 | /** |
||
| 901 | * Assert that value is an array or a traversable object. |
||
| 902 | * |
||
| 903 | * @param mixed $value |
||
| 904 | * @param string|null $message |
||
| 905 | * @param string|null $propertyPath |
||
| 906 | * @return void |
||
| 907 | * @throws \Assert\AssertionFailedException |
||
| 908 | */ |
||
| 909 | public static function isTraversable($value, $message = null, $propertyPath = null) |
||
| 920 | |||
| 921 | /** |
||
| 922 | * Assert that value is an array or an array-accessible object. |
||
| 923 | * |
||
| 924 | * @param mixed $value |
||
| 925 | * @param string|null $message |
||
| 926 | * @param string|null $propertyPath |
||
| 927 | * @return void |
||
| 928 | * @throws \Assert\AssertionFailedException |
||
| 929 | */ |
||
| 930 | public static function isArrayAccessible($value, $message = null, $propertyPath = null) |
||
| 941 | |||
| 942 | /** |
||
| 943 | * Assert that key exists in an array |
||
| 944 | * |
||
| 945 | * @param mixed $value |
||
| 946 | * @param string|integer $key |
||
| 947 | * @param string|null $message |
||
| 948 | * @param string|null $propertyPath |
||
| 949 | * @return void |
||
| 950 | * @throws \Assert\AssertionFailedException |
||
| 951 | */ |
||
| 952 | public static function keyExists($value, $key, $message = null, $propertyPath = null) |
||
| 965 | |||
| 966 | /** |
||
| 967 | * Assert that key does not exist in an array |
||
| 968 | * |
||
| 969 | * @param mixed $value |
||
| 970 | * @param string|integer $key |
||
| 971 | * @param string|null $message |
||
| 972 | * @param string|null $propertyPath |
||
| 973 | * @return void |
||
| 974 | * @throws \Assert\AssertionFailedException |
||
| 975 | */ |
||
| 976 | public static function keyNotExists($value, $key, $message = null, $propertyPath = null) |
||
| 989 | |||
| 990 | /** |
||
| 991 | * Assert that key exists in an array/array-accessible object using isset() |
||
| 992 | * |
||
| 993 | * @param mixed $value |
||
| 994 | * @param string|integer $key |
||
| 995 | * @param string|null $message |
||
| 996 | * @param string|null $propertyPath |
||
| 997 | * @return void |
||
| 998 | * @throws \Assert\AssertionFailedException |
||
| 999 | */ |
||
| 1000 | public static function keyIsset($value, $key, $message = null, $propertyPath = null) |
||
| 1013 | |||
| 1014 | /** |
||
| 1015 | * Assert that key exists in an array/array-accessible object and it's value is not empty. |
||
| 1016 | * |
||
| 1017 | * @param mixed $value |
||
| 1018 | * @param string|integer $key |
||
| 1019 | * @param string|null $message |
||
| 1020 | * @param string|null $propertyPath |
||
| 1021 | * @return void |
||
| 1022 | * @throws \Assert\AssertionFailedException |
||
| 1023 | */ |
||
| 1024 | public static function notEmptyKey($value, $key, $message = null, $propertyPath = null) |
||
| 1029 | |||
| 1030 | /** |
||
| 1031 | * Assert that value is not blank |
||
| 1032 | * |
||
| 1033 | * @param mixed $value |
||
| 1034 | * @param string|null $message |
||
| 1035 | * @param string|null $propertyPath |
||
| 1036 | * @return void |
||
| 1037 | * @throws \Assert\AssertionFailedException |
||
| 1038 | */ |
||
| 1039 | public static function notBlank($value, $message = null, $propertyPath = null) |
||
| 1050 | |||
| 1051 | /** |
||
| 1052 | * Assert that value is instance of given class-name. |
||
| 1053 | * |
||
| 1054 | * @param mixed $value |
||
| 1055 | * @param string $className |
||
| 1056 | * @param string|null $message |
||
| 1057 | * @param string|null $propertyPath |
||
| 1058 | * @return void |
||
| 1059 | * @throws \Assert\AssertionFailedException |
||
| 1060 | */ |
||
| 1061 | public static function isInstanceOf($value, $className, $message = null, $propertyPath = null) |
||
| 1073 | |||
| 1074 | /** |
||
| 1075 | * Assert that value is not instance of given class-name. |
||
| 1076 | * |
||
| 1077 | * @param mixed $value |
||
| 1078 | * @param string $className |
||
| 1079 | * @param string|null $message |
||
| 1080 | * @param string|null $propertyPath |
||
| 1081 | * @return void |
||
| 1082 | * @throws \Assert\AssertionFailedException |
||
| 1083 | */ |
||
| 1084 | public static function notIsInstanceOf($value, $className, $message = null, $propertyPath = null) |
||
| 1096 | |||
| 1097 | /** |
||
| 1098 | * Assert that value is subclass of given class-name. |
||
| 1099 | * |
||
| 1100 | * @param mixed $value |
||
| 1101 | * @param string $className |
||
| 1102 | * @param string|null $message |
||
| 1103 | * @param string|null $propertyPath |
||
| 1104 | * @return void |
||
| 1105 | * @throws \Assert\AssertionFailedException |
||
| 1106 | */ |
||
| 1107 | public static function subclassOf($value, $className, $message = null, $propertyPath = null) |
||
| 1119 | |||
| 1120 | /** |
||
| 1121 | * Assert that value is in range of numbers. |
||
| 1122 | * |
||
| 1123 | * @param mixed $value |
||
| 1124 | * @param integer $minValue |
||
| 1125 | * @param integer $maxValue |
||
| 1126 | * @param string|null $message |
||
| 1127 | * @param string|null $propertyPath |
||
| 1128 | * @return void |
||
| 1129 | * @throws \Assert\AssertionFailedException |
||
| 1130 | */ |
||
| 1131 | public static function range($value, $minValue, $maxValue, $message = null, $propertyPath = null) |
||
| 1146 | |||
| 1147 | /** |
||
| 1148 | * Assert that a value is at least as big as a given limit |
||
| 1149 | * |
||
| 1150 | * @param mixed $value |
||
| 1151 | * @param mixed $minValue |
||
| 1152 | * @param string|null $message |
||
| 1153 | * @param string|null $propertyPath |
||
| 1154 | * @return void |
||
| 1155 | * @throws \Assert\AssertionFailedException |
||
| 1156 | */ |
||
| 1157 | public static function min($value, $minValue, $message = null, $propertyPath = null) |
||
| 1171 | |||
| 1172 | /** |
||
| 1173 | * Assert that a number is smaller as a given limit |
||
| 1174 | * |
||
| 1175 | * @param mixed $value |
||
| 1176 | * @param mixed $maxValue |
||
| 1177 | * @param string|null $message |
||
| 1178 | * @param string|null $propertyPath |
||
| 1179 | * @return void |
||
| 1180 | * @throws \Assert\AssertionFailedException |
||
| 1181 | */ |
||
| 1182 | public static function max($value, $maxValue, $message = null, $propertyPath = null) |
||
| 1196 | |||
| 1197 | /** |
||
| 1198 | * Assert that a file exists |
||
| 1199 | * |
||
| 1200 | * @param string $value |
||
| 1201 | * @param string|null $message |
||
| 1202 | * @param string|null $propertyPath |
||
| 1203 | * @return void |
||
| 1204 | * @throws \Assert\AssertionFailedException |
||
| 1205 | */ |
||
| 1206 | public static function file($value, $message = null, $propertyPath = null) |
||
| 1220 | |||
| 1221 | /** |
||
| 1222 | * Assert that a directory exists |
||
| 1223 | * |
||
| 1224 | * @param string $value |
||
| 1225 | * @param string|null $message |
||
| 1226 | * @param string|null $propertyPath |
||
| 1227 | * @return void |
||
| 1228 | * @throws \Assert\AssertionFailedException |
||
| 1229 | */ |
||
| 1230 | public static function directory($value, $message = null, $propertyPath = null) |
||
| 1243 | |||
| 1244 | /** |
||
| 1245 | * Assert that the value is something readable |
||
| 1246 | * |
||
| 1247 | * @param string $value |
||
| 1248 | * @param string|null $message |
||
| 1249 | * @param string|null $propertyPath |
||
| 1250 | * @return void |
||
| 1251 | * @throws \Assert\AssertionFailedException |
||
| 1252 | */ |
||
| 1253 | public static function readable($value, $message = null, $propertyPath = null) |
||
| 1266 | |||
| 1267 | /** |
||
| 1268 | * Assert that the value is something writeable |
||
| 1269 | * |
||
| 1270 | * @param string $value |
||
| 1271 | * @param string|null $message |
||
| 1272 | * @param string|null $propertyPath |
||
| 1273 | * @return void |
||
| 1274 | * @throws \Assert\AssertionFailedException |
||
| 1275 | */ |
||
| 1276 | public static function writeable($value, $message = null, $propertyPath = null) |
||
| 1289 | |||
| 1290 | /** |
||
| 1291 | * Assert that value is an email adress (using input_filter/FILTER_VALIDATE_EMAIL). |
||
| 1292 | * |
||
| 1293 | * @param mixed $value |
||
| 1294 | * @param string|null $message |
||
| 1295 | * @param string|null $propertyPath |
||
| 1296 | * @return void |
||
| 1297 | * @throws \Assert\AssertionFailedException |
||
| 1298 | */ |
||
| 1299 | public static function email($value, $message = null, $propertyPath = null) |
||
| 1324 | |||
| 1325 | /** |
||
| 1326 | * Assert that value is an URL. |
||
| 1327 | * |
||
| 1328 | * This code snipped was taken from the Symfony project and modified to the special demands of this method. |
||
| 1329 | * |
||
| 1330 | * @param mixed $value |
||
| 1331 | * @param string|null $message |
||
| 1332 | * @param string|null $propertyPath |
||
| 1333 | * @return void |
||
| 1334 | * @throws \Assert\AssertionFailedException |
||
| 1335 | * |
||
| 1336 | * |
||
| 1337 | * @link https://github.com/symfony/Validator/blob/master/Constraints/UrlValidator.php |
||
| 1338 | * @link https://github.com/symfony/Validator/blob/master/Constraints/Url.php |
||
| 1339 | */ |
||
| 1340 | public static function url($value, $message = null, $propertyPath = null) |
||
| 1373 | |||
| 1374 | /** |
||
| 1375 | * Assert that value is alphanumeric. |
||
| 1376 | * |
||
| 1377 | * @param mixed $value |
||
| 1378 | * @param string|null $message |
||
| 1379 | * @param string|null $propertyPath |
||
| 1380 | * @return void |
||
| 1381 | * @throws \Assert\AssertionFailedException |
||
| 1382 | */ |
||
| 1383 | public static function alnum($value, $message = null, $propertyPath = null) |
||
| 1396 | |||
| 1397 | /** |
||
| 1398 | * Assert that the value is boolean True. |
||
| 1399 | * |
||
| 1400 | * @param mixed $value |
||
| 1401 | * @param string|null $message |
||
| 1402 | * @param string|null $propertyPath |
||
| 1403 | * @return void |
||
| 1404 | * @throws \Assert\AssertionFailedException |
||
| 1405 | */ |
||
| 1406 | public static function true($value, $message = null, $propertyPath = null) |
||
| 1417 | |||
| 1418 | /** |
||
| 1419 | * Assert that the value is boolean False. |
||
| 1420 | * |
||
| 1421 | * @param mixed $value |
||
| 1422 | * @param string|null $message |
||
| 1423 | * @param string|null $propertyPath |
||
| 1424 | * @return void |
||
| 1425 | * @throws \Assert\AssertionFailedException |
||
| 1426 | */ |
||
| 1427 | public static function false($value, $message = null, $propertyPath = null) |
||
| 1438 | |||
| 1439 | /** |
||
| 1440 | * Assert that the class exists. |
||
| 1441 | * |
||
| 1442 | * @param mixed $value |
||
| 1443 | * @param string|null $message |
||
| 1444 | * @param string|null $propertyPath |
||
| 1445 | * @return void |
||
| 1446 | * @throws \Assert\AssertionFailedException |
||
| 1447 | */ |
||
| 1448 | public static function classExists($value, $message = null, $propertyPath = null) |
||
| 1459 | |||
| 1460 | /** |
||
| 1461 | * Assert that the interface exists. |
||
| 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 interfaceExists($value, $message = null, $propertyPath = null) |
||
| 1480 | |||
| 1481 | /** |
||
| 1482 | * Assert that the class implements the interface |
||
| 1483 | * |
||
| 1484 | * @param mixed $class |
||
| 1485 | * @param string $interfaceName |
||
| 1486 | * @param string|null $message |
||
| 1487 | * @param string|null $propertyPath |
||
| 1488 | * @return void |
||
| 1489 | * @throws \Assert\AssertionFailedException |
||
| 1490 | */ |
||
| 1491 | public static function implementsInterface($class, $interfaceName, $message = null, $propertyPath = null) |
||
| 1504 | |||
| 1505 | /** |
||
| 1506 | * Assert that the given string is a valid json string. |
||
| 1507 | * |
||
| 1508 | * NOTICE: |
||
| 1509 | * Since this does a json_decode to determine its validity |
||
| 1510 | * you probably should consider, when using the variable |
||
| 1511 | * content afterwards, just to decode and check for yourself instead |
||
| 1512 | * of using this assertion. |
||
| 1513 | * |
||
| 1514 | * @param mixed $value |
||
| 1515 | * @param string|null $message |
||
| 1516 | * @param string|null $propertyPath |
||
| 1517 | * @return void |
||
| 1518 | * @throws \Assert\AssertionFailedException |
||
| 1519 | */ |
||
| 1520 | public static function isJsonString($value, $message = null, $propertyPath = null) |
||
| 1531 | |||
| 1532 | /** |
||
| 1533 | * Assert that the given string is a valid UUID |
||
| 1534 | * |
||
| 1535 | * Uses code from {@link https://github.com/ramsey/uuid} that is MIT licensed. |
||
| 1536 | * |
||
| 1537 | * @param string $value |
||
| 1538 | * @param string|null $message |
||
| 1539 | * @param string|null $propertyPath |
||
| 1540 | * @return void |
||
| 1541 | * @throws \Assert\AssertionFailedException |
||
| 1542 | */ |
||
| 1543 | public static function uuid($value, $message = null, $propertyPath = null) |
||
| 1560 | |||
| 1561 | /** |
||
| 1562 | * Assert that the given string is a valid E164 Phone Number |
||
| 1563 | * |
||
| 1564 | * @link https://en.wikipedia.org/wiki/E.164 |
||
| 1565 | * |
||
| 1566 | * @param string $value |
||
| 1567 | * @param string|null $message |
||
| 1568 | * @param string|null $propertyPath |
||
| 1569 | * @return void |
||
| 1570 | * @throws \Assert\AssertionFailedException |
||
| 1571 | */ |
||
| 1572 | public static function e164($value, $message = null, $propertyPath = null) |
||
| 1583 | |||
| 1584 | /** |
||
| 1585 | * Assert that the count of countable is equal to count. |
||
| 1586 | * |
||
| 1587 | * @param array|\Countable $countable |
||
| 1588 | * @param int $count |
||
| 1589 | * @param string $message |
||
| 1590 | * @param string $propertyPath |
||
| 1591 | * @return void |
||
| 1592 | * @throws \Assert\AssertionFailedException |
||
| 1593 | */ |
||
| 1594 | public static function count($countable, $count, $message = null, $propertyPath = null) |
||
| 1605 | |||
| 1606 | /** |
||
| 1607 | * static call handler to implement: |
||
| 1608 | * - "null or assertion" delegation |
||
| 1609 | * - "all" delegation |
||
| 1610 | */ |
||
| 1611 | public static function __callStatic($method, $args) |
||
| 1647 | |||
| 1648 | /** |
||
| 1649 | * Determines if the values array has every choice as key and that this choice has content. |
||
| 1650 | * |
||
| 1651 | * @param array $values |
||
| 1652 | * @param array $choices |
||
| 1653 | * @param null $message |
||
| 1654 | * @param null $propertyPath |
||
| 1655 | */ |
||
| 1656 | public static function choicesNotEmpty(array $values, array $choices, $message = null, $propertyPath = null) |
||
| 1665 | |||
| 1666 | /** |
||
| 1667 | * Determines that the named method is defined in the provided object. |
||
| 1668 | * |
||
| 1669 | * @param string $value |
||
| 1670 | * @param mixed $object |
||
| 1671 | * @param null $message |
||
| 1672 | * @param null $propertyPath |
||
| 1673 | * |
||
| 1674 | * @throws |
||
| 1675 | */ |
||
| 1676 | public static function methodExists($value, $object, $message = null, $propertyPath = null) |
||
| 1689 | |||
| 1690 | /** |
||
| 1691 | * Determines that the provided value is an object. |
||
| 1692 | * |
||
| 1693 | * @param mixed $value |
||
| 1694 | * @param null $message |
||
| 1695 | * @param null $propertyPath |
||
| 1696 | */ |
||
| 1697 | public static function isObject($value, $message = null, $propertyPath = null) |
||
| 1709 | |||
| 1710 | /** |
||
| 1711 | * Determines if the value is less than given limit. |
||
| 1712 | * |
||
| 1713 | * @param mixed $value |
||
| 1714 | * @param mixed $limit |
||
| 1715 | * @param null $message |
||
| 1716 | * @param null $propertyPath |
||
| 1717 | */ |
||
| 1718 | public static function lessThan($value, $limit, $message = null, $propertyPath = null) |
||
| 1730 | |||
| 1731 | /** |
||
| 1732 | * Determines if the value is less or than given limit. |
||
| 1733 | * |
||
| 1734 | * @param mixed $value |
||
| 1735 | * @param mixed $limit |
||
| 1736 | * @param null $message |
||
| 1737 | * @param null $propertyPath |
||
| 1738 | */ |
||
| 1739 | public static function lessOrEqualThan($value, $limit, $message = null, $propertyPath = null) |
||
| 1751 | |||
| 1752 | /** |
||
| 1753 | * Determines if the value is greater than given limit. |
||
| 1754 | * |
||
| 1755 | * @param mixed $value |
||
| 1756 | * @param mixed $limit |
||
| 1757 | * @param null $message |
||
| 1758 | * @param null $propertyPath |
||
| 1759 | */ |
||
| 1760 | public static function greaterThan($value, $limit, $message = null, $propertyPath = null) |
||
| 1772 | |||
| 1773 | /** |
||
| 1774 | * Determines if the value is greater or equal than given limit. |
||
| 1775 | * |
||
| 1776 | * @param mixed $value |
||
| 1777 | * @param mixed $limit |
||
| 1778 | * @param null $message |
||
| 1779 | * @param null $propertyPath |
||
| 1780 | */ |
||
| 1781 | public static function greaterOrEqualThan($value, $limit, $message = null, $propertyPath = null) |
||
| 1793 | |||
| 1794 | /** |
||
| 1795 | * Assert that date is valid and corresponds to the given format. |
||
| 1796 | * |
||
| 1797 | * @param string $value |
||
| 1798 | * @param string $format supports all of the options date(), except for the following: |
||
| 1799 | * N, w, W, t, L, o, B, a, A, g, h, I, O, P, Z, c, r. |
||
| 1800 | * @param string|null $message |
||
| 1801 | * @param string|null $propertyPath |
||
| 1802 | * |
||
| 1803 | * @link http://php.net/manual/function.date.php#refsect1-function.date-parameters |
||
| 1804 | */ |
||
| 1805 | public static function date($value, $format, $message = null, $propertyPath = null) |
||
| 1822 | |||
| 1823 | /** |
||
| 1824 | * Determines that the provided value is callable. |
||
| 1825 | * |
||
| 1826 | * @param mixed $value |
||
| 1827 | * @param null $message |
||
| 1828 | * @param null $propertyPath |
||
| 1829 | */ |
||
| 1830 | public static function isCallable($value, $message = null, $propertyPath = null) |
||
| 1841 | |||
| 1842 | /** |
||
| 1843 | * Assert that the provided value is valid according to a callback. |
||
| 1844 | * |
||
| 1845 | * If the callback returns `false` the assertion will fail. |
||
| 1846 | * |
||
| 1847 | * @param mixed $value |
||
| 1848 | * @param callable $callback |
||
| 1849 | * @param string|null $message |
||
| 1850 | * @param string|null $propertyPath |
||
| 1851 | */ |
||
| 1852 | public static function satisfy($value, $callback, $message = null, $propertyPath = null) |
||
| 1865 | |||
| 1866 | /** |
||
| 1867 | * Assert that value is an IPv4 or IPv6 address |
||
| 1868 | * (using input_filter/FILTER_VALIDATE_IP). |
||
| 1869 | * |
||
| 1870 | * @param string $value |
||
| 1871 | * @param null|int $flag |
||
| 1872 | * @param string|null $message |
||
| 1873 | * @param string|null $propertyPath |
||
| 1874 | * |
||
| 1875 | * @link http://php.net/manual/filter.filters.flags.php |
||
| 1876 | */ |
||
| 1877 | public static function ip($value, $flag = null, $message = null, $propertyPath = null) |
||
| 1888 | |||
| 1889 | /** |
||
| 1890 | * Assert that value is an IPv4 address |
||
| 1891 | * (using input_filter/FILTER_VALIDATE_IP). |
||
| 1892 | * |
||
| 1893 | * @param string $value |
||
| 1894 | * @param null|int $flag |
||
| 1895 | * @param string|null $message |
||
| 1896 | * @param string|null $propertyPath |
||
| 1897 | * |
||
| 1898 | * @link http://php.net/manual/filter.filters.flags.php |
||
| 1899 | */ |
||
| 1900 | public static function ipv4($value, $flag = null, $message = null, $propertyPath = null) |
||
| 1904 | |||
| 1905 | /** |
||
| 1906 | * Assert that value is an IPv6 address |
||
| 1907 | * (using input_filter/FILTER_VALIDATE_IP). |
||
| 1908 | * |
||
| 1909 | * @param string $value |
||
| 1910 | * @param null|int $flag |
||
| 1911 | * @param string|null $message |
||
| 1912 | * @param string|null $propertyPath |
||
| 1913 | * |
||
| 1914 | * @link http://php.net/manual/filter.filters.flags.php |
||
| 1915 | */ |
||
| 1916 | public static function ipv6($value, $flag = null, $message = null, $propertyPath = null) |
||
| 1920 | |||
| 1921 | /** |
||
| 1922 | * Make a string version of a value. |
||
| 1923 | * |
||
| 1924 | * @param mixed $value |
||
| 1925 | * @return string |
||
| 1926 | */ |
||
| 1927 | protected static function stringify($value) |
||
| 1961 | } |
||
| 1962 |