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 |
||
| 146 | class Assertion |
||
| 147 | { |
||
| 148 | const INVALID_FLOAT = 9; |
||
| 149 | const INVALID_INTEGER = 10; |
||
| 150 | const INVALID_DIGIT = 11; |
||
| 151 | const INVALID_INTEGERISH = 12; |
||
| 152 | const INVALID_BOOLEAN = 13; |
||
| 153 | const VALUE_EMPTY = 14; |
||
| 154 | const VALUE_NULL = 15; |
||
| 155 | const INVALID_STRING = 16; |
||
| 156 | const INVALID_REGEX = 17; |
||
| 157 | const INVALID_MIN_LENGTH = 18; |
||
| 158 | const INVALID_MAX_LENGTH = 19; |
||
| 159 | const INVALID_STRING_START = 20; |
||
| 160 | const INVALID_STRING_CONTAINS = 21; |
||
| 161 | const INVALID_CHOICE = 22; |
||
| 162 | const INVALID_NUMERIC = 23; |
||
| 163 | const INVALID_ARRAY = 24; |
||
| 164 | const INVALID_KEY_EXISTS = 26; |
||
| 165 | const INVALID_NOT_BLANK = 27; |
||
| 166 | const INVALID_INSTANCE_OF = 28; |
||
| 167 | const INVALID_SUBCLASS_OF = 29; |
||
| 168 | const INVALID_RANGE = 30; |
||
| 169 | const INVALID_ALNUM = 31; |
||
| 170 | const INVALID_TRUE = 32; |
||
| 171 | const INVALID_EQ = 33; |
||
| 172 | const INVALID_SAME = 34; |
||
| 173 | const INVALID_MIN = 35; |
||
| 174 | const INVALID_MAX = 36; |
||
| 175 | const INVALID_LENGTH = 37; |
||
| 176 | const INVALID_FALSE = 38; |
||
| 177 | const INVALID_STRING_END = 39; |
||
| 178 | const INVALID_UUID = 40; |
||
| 179 | const INVALID_COUNT = 41; |
||
| 180 | const INVALID_NOT_EQ = 42; |
||
| 181 | const INVALID_NOT_SAME = 43; |
||
| 182 | const INVALID_TRAVERSABLE = 44; |
||
| 183 | const INVALID_ARRAY_ACCESSIBLE = 45; |
||
| 184 | const INVALID_KEY_ISSET = 46; |
||
| 185 | const INVALID_DIRECTORY = 101; |
||
| 186 | const INVALID_FILE = 102; |
||
| 187 | const INVALID_READABLE = 103; |
||
| 188 | const INVALID_WRITEABLE = 104; |
||
| 189 | const INVALID_CLASS = 105; |
||
| 190 | const INVALID_EMAIL = 201; |
||
| 191 | const INTERFACE_NOT_IMPLEMENTED = 202; |
||
| 192 | const INVALID_URL = 203; |
||
| 193 | const INVALID_NOT_INSTANCE_OF = 204; |
||
| 194 | const VALUE_NOT_EMPTY = 205; |
||
| 195 | const INVALID_JSON_STRING = 206; |
||
| 196 | const INVALID_OBJECT = 207; |
||
| 197 | const INVALID_METHOD = 208; |
||
| 198 | const INVALID_SCALAR = 209; |
||
| 199 | const INVALID_LESS = 210; |
||
| 200 | const INVALID_LESS_OR_EQUAL = 211; |
||
| 201 | const INVALID_GREATER = 212; |
||
| 202 | const INVALID_GREATER_OR_EQUAL = 212; |
||
| 203 | const INVALID_DATE = 213; |
||
| 204 | const INVALID_CALLABLE = 214; |
||
| 205 | |||
| 206 | /** |
||
| 207 | * Exception to throw when an assertion failed. |
||
| 208 | * |
||
| 209 | * @var string |
||
| 210 | */ |
||
| 211 | static protected $exceptionClass = 'Assert\InvalidArgumentException'; |
||
| 212 | |||
| 213 | /** |
||
| 214 | * Assertion class name. |
||
| 215 | * |
||
| 216 | * @var string |
||
| 217 | */ |
||
| 218 | static protected $assertionClass = 'Assert\Assertion'; |
||
| 219 | |||
| 220 | /** |
||
| 221 | * AssertionChain class name. |
||
| 222 | * |
||
| 223 | * @var string |
||
| 224 | */ |
||
| 225 | static protected $chainClass = 'Assert\AssertionChain'; |
||
| 226 | |||
| 227 | /** |
||
| 228 | * Lazy Assertion class name. |
||
| 229 | * |
||
| 230 | * @var string |
||
| 231 | */ |
||
| 232 | static protected $lazyClass = 'Assert\LazyAssertion'; |
||
| 233 | |||
| 234 | /** |
||
| 235 | * @return string |
||
| 236 | */ |
||
| 237 | public static function getAssertionClass() |
||
| 241 | |||
| 242 | /** |
||
| 243 | * @return string |
||
| 244 | */ |
||
| 245 | public static function getChainClass() |
||
| 249 | |||
| 250 | /** |
||
| 251 | * @return string |
||
| 252 | */ |
||
| 253 | public static function getLazyClass() |
||
| 257 | |||
| 258 | /** |
||
| 259 | * Helper method that handles building the assertion failure exceptions. |
||
| 260 | * They are returned from this method so that the stack trace still shows |
||
| 261 | * the assertions method. |
||
| 262 | */ |
||
| 263 | protected static function createException($value, $message, $code, $propertyPath, array $constraints = array()) |
||
| 268 | |||
| 269 | /** |
||
| 270 | * @param string $assertionClass |
||
| 271 | */ |
||
| 272 | public static function setAssertionClass($assertionClass) |
||
| 276 | |||
| 277 | /** |
||
| 278 | * @param string $chainClass |
||
| 279 | */ |
||
| 280 | public static function setChainClass($chainClass) |
||
| 284 | |||
| 285 | /** |
||
| 286 | * @param string $lazyClass |
||
| 287 | */ |
||
| 288 | public static function setLazyClass($lazyClass) |
||
| 292 | |||
| 293 | /** |
||
| 294 | * Assert that two values are equal (using == ). |
||
| 295 | * |
||
| 296 | * @param mixed $value |
||
| 297 | * @param mixed $value2 |
||
| 298 | * @param string|null $message |
||
| 299 | * @param string|null $propertyPath |
||
| 300 | * @return void |
||
| 301 | * @throws \Assert\AssertionFailedException |
||
| 302 | */ |
||
| 303 | public static function eq($value, $value2, $message = null, $propertyPath = null) |
||
| 315 | |||
| 316 | /** |
||
| 317 | * Assert that two values are the same (using ===). |
||
| 318 | * |
||
| 319 | * @param mixed $value |
||
| 320 | * @param mixed $value2 |
||
| 321 | * @param string|null $message |
||
| 322 | * @param string|null $propertyPath |
||
| 323 | * @return void |
||
| 324 | * @throws \Assert\AssertionFailedException |
||
| 325 | */ |
||
| 326 | public static function same($value, $value2, $message = null, $propertyPath = null) |
||
| 338 | |||
| 339 | /** |
||
| 340 | * Assert that two values are not equal (using == ). |
||
| 341 | * |
||
| 342 | * @param mixed $value1 |
||
| 343 | * @param mixed $value2 |
||
| 344 | * @param string|null $message |
||
| 345 | * @param string|null $propertyPath |
||
| 346 | * @return void |
||
| 347 | * @throws \Assert\AssertionFailedException |
||
| 348 | */ |
||
| 349 | public static function notEq($value1, $value2, $message = null, $propertyPath = null) |
||
| 360 | |||
| 361 | /** |
||
| 362 | * Assert that two values are not the same (using === ). |
||
| 363 | * |
||
| 364 | * @param mixed $value1 |
||
| 365 | * @param mixed $value2 |
||
| 366 | * @param string|null $message |
||
| 367 | * @param string|null $propertyPath |
||
| 368 | * @return void |
||
| 369 | * @throws \Assert\AssertionFailedException |
||
| 370 | */ |
||
| 371 | public static function notSame($value1, $value2, $message = null, $propertyPath = null) |
||
| 382 | |||
| 383 | /** |
||
| 384 | * Assert that value is a php integer. |
||
| 385 | * |
||
| 386 | * @param mixed $value |
||
| 387 | * @param string|null $message |
||
| 388 | * @param string|null $propertyPath |
||
| 389 | * @return void |
||
| 390 | * @throws \Assert\AssertionFailedException |
||
| 391 | */ |
||
| 392 | public static function integer($value, $message = null, $propertyPath = null) |
||
| 403 | |||
| 404 | /** |
||
| 405 | * Assert that value is a php float. |
||
| 406 | * |
||
| 407 | * @param mixed $value |
||
| 408 | * @param string|null $message |
||
| 409 | * @param string|null $propertyPath |
||
| 410 | * @return void |
||
| 411 | * @throws \Assert\AssertionFailedException |
||
| 412 | */ |
||
| 413 | public static function float($value, $message = null, $propertyPath = null) |
||
| 424 | |||
| 425 | /** |
||
| 426 | * Validates if an integer or integerish is a digit. |
||
| 427 | * |
||
| 428 | * @param mixed $value |
||
| 429 | * @param string|null $message |
||
| 430 | * @param string|null $propertyPath |
||
| 431 | * @return void |
||
| 432 | * @throws \Assert\AssertionFailedException |
||
| 433 | */ |
||
| 434 | public static function digit($value, $message = null, $propertyPath = null) |
||
| 445 | |||
| 446 | /** |
||
| 447 | * Assert that value is a php integer'ish. |
||
| 448 | * |
||
| 449 | * @param mixed $value |
||
| 450 | * @param string|null $message |
||
| 451 | * @param string|null $propertyPath |
||
| 452 | * @return void |
||
| 453 | * @throws \Assert\AssertionFailedException |
||
| 454 | */ |
||
| 455 | public static function integerish($value, $message = null, $propertyPath = null) |
||
| 466 | |||
| 467 | /** |
||
| 468 | * Assert that value is php boolean |
||
| 469 | * |
||
| 470 | * @param mixed $value |
||
| 471 | * @param string|null $message |
||
| 472 | * @param string|null $propertyPath |
||
| 473 | * @return void |
||
| 474 | * @throws \Assert\AssertionFailedException |
||
| 475 | */ |
||
| 476 | public static function boolean($value, $message = null, $propertyPath = null) |
||
| 487 | |||
| 488 | /** |
||
| 489 | * Assert that value is a PHP scalar |
||
| 490 | * |
||
| 491 | * @param mixed $value |
||
| 492 | * @param string|null $message |
||
| 493 | * @param string|null $propertyPath |
||
| 494 | * @return void |
||
| 495 | * @throws \Assert\AssertionFailedException |
||
| 496 | */ |
||
| 497 | public static function scalar($value, $message = null, $propertyPath = null) |
||
| 508 | |||
| 509 | /** |
||
| 510 | * Assert that value is not empty |
||
| 511 | * |
||
| 512 | * @param mixed $value |
||
| 513 | * @param string|null $message |
||
| 514 | * @param string|null $propertyPath |
||
| 515 | * @return void |
||
| 516 | * @throws \Assert\AssertionFailedException |
||
| 517 | */ |
||
| 518 | public static function notEmpty($value, $message = null, $propertyPath = null) |
||
| 529 | |||
| 530 | /** |
||
| 531 | * Assert that value is empty |
||
| 532 | * |
||
| 533 | * @param mixed $value |
||
| 534 | * @param string|null $message |
||
| 535 | * @param string|null $propertyPath |
||
| 536 | * @return void |
||
| 537 | * @throws \Assert\AssertionFailedException |
||
| 538 | */ |
||
| 539 | public static function noContent($value, $message = null, $propertyPath = null) |
||
| 550 | |||
| 551 | /** |
||
| 552 | * Assert that value is not null |
||
| 553 | * |
||
| 554 | * @param mixed $value |
||
| 555 | * @param string|null $message |
||
| 556 | * @param string|null $propertyPath |
||
| 557 | * @return void |
||
| 558 | * @throws \Assert\AssertionFailedException |
||
| 559 | */ |
||
| 560 | public static function notNull($value, $message = null, $propertyPath = null) |
||
| 571 | |||
| 572 | /** |
||
| 573 | * Assert that value is a string |
||
| 574 | * |
||
| 575 | * @param mixed $value |
||
| 576 | * @param string|null $message |
||
| 577 | * @param string|null $propertyPath |
||
| 578 | * @return void |
||
| 579 | * @throws \Assert\AssertionFailedException |
||
| 580 | */ |
||
| 581 | public static function string($value, $message = null, $propertyPath = null) |
||
| 593 | |||
| 594 | /** |
||
| 595 | * Assert that value matches a regex |
||
| 596 | * |
||
| 597 | * @param mixed $value |
||
| 598 | * @param string $pattern |
||
| 599 | * @param string|null $message |
||
| 600 | * @param string|null $propertyPath |
||
| 601 | * @return void |
||
| 602 | * @throws \Assert\AssertionFailedException |
||
| 603 | */ |
||
| 604 | public static function regex($value, $pattern, $message = null, $propertyPath = null) |
||
| 617 | |||
| 618 | /** |
||
| 619 | * Assert that string has a given length. |
||
| 620 | * |
||
| 621 | * @param mixed $value |
||
| 622 | * @param int $length |
||
| 623 | * @param string|null $message |
||
| 624 | * @param string|null $propertyPath |
||
| 625 | * @param string $encoding |
||
| 626 | * @return void |
||
| 627 | * @throws \Assert\AssertionFailedException |
||
| 628 | */ |
||
| 629 | public static function length($value, $length, $message = null, $propertyPath = null, $encoding = 'utf8') |
||
| 645 | |||
| 646 | /** |
||
| 647 | * Assert that a string is at least $minLength chars long. |
||
| 648 | * |
||
| 649 | * @param mixed $value |
||
| 650 | * @param int $minLength |
||
| 651 | * @param string|null $message |
||
| 652 | * @param string|null $propertyPath |
||
| 653 | * @param string $encoding |
||
| 654 | * @return void |
||
| 655 | * @throws \Assert\AssertionFailedException |
||
| 656 | */ |
||
| 657 | public static function minLength($value, $minLength, $message = null, $propertyPath = null, $encoding = 'utf8') |
||
| 673 | |||
| 674 | /** |
||
| 675 | * Assert that string value is not longer than $maxLength chars. |
||
| 676 | * |
||
| 677 | * @param mixed $value |
||
| 678 | * @param integer $maxLength |
||
| 679 | * @param string|null $message |
||
| 680 | * @param string|null $propertyPath |
||
| 681 | * @param string $encoding |
||
| 682 | * @return void |
||
| 683 | * @throws \Assert\AssertionFailedException |
||
| 684 | */ |
||
| 685 | public static function maxLength($value, $maxLength, $message = null, $propertyPath = null, $encoding = 'utf8') |
||
| 701 | |||
| 702 | /** |
||
| 703 | * Assert that string length is between min,max lengths. |
||
| 704 | * |
||
| 705 | * @param mixed $value |
||
| 706 | * @param integer $minLength |
||
| 707 | * @param integer $maxLength |
||
| 708 | * @param string|null $message |
||
| 709 | * @param string|null $propertyPath |
||
| 710 | * @param string $encoding |
||
| 711 | * @return void |
||
| 712 | * @throws \Assert\AssertionFailedException |
||
| 713 | */ |
||
| 714 | public static function betweenLength($value, $minLength, $maxLength, $message = null, $propertyPath = null, $encoding = 'utf8') |
||
| 742 | |||
| 743 | /** |
||
| 744 | * Assert that string starts with a sequence of chars. |
||
| 745 | * |
||
| 746 | * @param mixed $string |
||
| 747 | * @param string $needle |
||
| 748 | * @param string|null $message |
||
| 749 | * @param string|null $propertyPath |
||
| 750 | * @param string $encoding |
||
| 751 | * @return void |
||
| 752 | * @throws \Assert\AssertionFailedException |
||
| 753 | */ |
||
| 754 | public static function startsWith($string, $needle, $message = null, $propertyPath = null, $encoding = 'utf8') |
||
| 769 | |||
| 770 | /** |
||
| 771 | * Assert that string ends with a sequence of chars. |
||
| 772 | * |
||
| 773 | * @param mixed $string |
||
| 774 | * @param string $needle |
||
| 775 | * @param string|null $message |
||
| 776 | * @param string|null $propertyPath |
||
| 777 | * @param string $encoding |
||
| 778 | * @return void |
||
| 779 | * @throws \Assert\AssertionFailedException |
||
| 780 | */ |
||
| 781 | public static function endsWith($string, $needle, $message = null, $propertyPath = null, $encoding = 'utf8') |
||
| 798 | |||
| 799 | /** |
||
| 800 | * Assert that string contains a sequence of chars. |
||
| 801 | * |
||
| 802 | * @param mixed $string |
||
| 803 | * @param string $needle |
||
| 804 | * @param string|null $message |
||
| 805 | * @param string|null $propertyPath |
||
| 806 | * @param string $encoding |
||
| 807 | * @return void |
||
| 808 | * @throws \Assert\AssertionFailedException |
||
| 809 | */ |
||
| 810 | public static function contains($string, $needle, $message = null, $propertyPath = null, $encoding = 'utf8') |
||
| 825 | |||
| 826 | /** |
||
| 827 | * Assert that value is in array of choices. |
||
| 828 | * |
||
| 829 | * @param mixed $value |
||
| 830 | * @param array $choices |
||
| 831 | * @param string|null $message |
||
| 832 | * @param string|null $propertyPath |
||
| 833 | * @return void |
||
| 834 | * @throws \Assert\AssertionFailedException |
||
| 835 | */ |
||
| 836 | public static function choice($value, array $choices, $message = null, $propertyPath = null) |
||
| 848 | |||
| 849 | /** |
||
| 850 | * Alias of {@see choice()} |
||
| 851 | * |
||
| 852 | * @throws \Assert\AssertionFailedException |
||
| 853 | */ |
||
| 854 | public static function inArray($value, array $choices, $message = null, $propertyPath = null) |
||
| 858 | |||
| 859 | /** |
||
| 860 | * Assert that value is numeric. |
||
| 861 | * |
||
| 862 | * @param mixed $value |
||
| 863 | * @param string|null $message |
||
| 864 | * @param string|null $propertyPath |
||
| 865 | * @return void |
||
| 866 | * @throws \Assert\AssertionFailedException |
||
| 867 | */ |
||
| 868 | public static function numeric($value, $message = null, $propertyPath = null) |
||
| 879 | |||
| 880 | /** |
||
| 881 | * Assert that value is an array. |
||
| 882 | * |
||
| 883 | * @param mixed $value |
||
| 884 | * @param string|null $message |
||
| 885 | * @param string|null $propertyPath |
||
| 886 | * @return void |
||
| 887 | * @throws \Assert\AssertionFailedException |
||
| 888 | */ |
||
| 889 | public static function isArray($value, $message = null, $propertyPath = null) |
||
| 900 | |||
| 901 | /** |
||
| 902 | * Assert that value is an array or a traversable object. |
||
| 903 | * |
||
| 904 | * @param mixed $value |
||
| 905 | * @param string|null $message |
||
| 906 | * @param string|null $propertyPath |
||
| 907 | * @return void |
||
| 908 | * @throws \Assert\AssertionFailedException |
||
| 909 | */ |
||
| 910 | public static function isTraversable($value, $message = null, $propertyPath = null) |
||
| 921 | |||
| 922 | /** |
||
| 923 | * Assert that value is an array or an array-accessible object. |
||
| 924 | * |
||
| 925 | * @param mixed $value |
||
| 926 | * @param string|null $message |
||
| 927 | * @param string|null $propertyPath |
||
| 928 | * @return void |
||
| 929 | * @throws \Assert\AssertionFailedException |
||
| 930 | */ |
||
| 931 | public static function isArrayAccessible($value, $message = null, $propertyPath = null) |
||
| 942 | |||
| 943 | /** |
||
| 944 | * Assert that key exists in an array |
||
| 945 | * |
||
| 946 | * @param mixed $value |
||
| 947 | * @param string|integer $key |
||
| 948 | * @param string|null $message |
||
| 949 | * @param string|null $propertyPath |
||
| 950 | * @return void |
||
| 951 | * @throws \Assert\AssertionFailedException |
||
| 952 | */ |
||
| 953 | public static function keyExists($value, $key, $message = null, $propertyPath = null) |
||
| 966 | |||
| 967 | /** |
||
| 968 | * Assert that key exists in an array/array-accessible object using isset() |
||
| 969 | * |
||
| 970 | * @param mixed $value |
||
| 971 | * @param string|integer $key |
||
| 972 | * @param string|null $message |
||
| 973 | * @param string|null $propertyPath |
||
| 974 | * @return void |
||
| 975 | * @throws \Assert\AssertionFailedException |
||
| 976 | */ |
||
| 977 | public static function keyIsset($value, $key, $message = null, $propertyPath = null) |
||
| 990 | |||
| 991 | /** |
||
| 992 | * Assert that key exists in an array/array-accessible object and it's value is not empty. |
||
| 993 | * |
||
| 994 | * @param mixed $value |
||
| 995 | * @param string|integer $key |
||
| 996 | * @param string|null $message |
||
| 997 | * @param string|null $propertyPath |
||
| 998 | * @return void |
||
| 999 | * @throws \Assert\AssertionFailedException |
||
| 1000 | */ |
||
| 1001 | public static function notEmptyKey($value, $key, $message = null, $propertyPath = null) |
||
| 1006 | |||
| 1007 | /** |
||
| 1008 | * Assert that value is not blank |
||
| 1009 | * |
||
| 1010 | * @param mixed $value |
||
| 1011 | * @param string|null $message |
||
| 1012 | * @param string|null $propertyPath |
||
| 1013 | * @return void |
||
| 1014 | * @throws \Assert\AssertionFailedException |
||
| 1015 | */ |
||
| 1016 | public static function notBlank($value, $message = null, $propertyPath = null) |
||
| 1027 | |||
| 1028 | /** |
||
| 1029 | * Assert that value is instance of given class-name. |
||
| 1030 | * |
||
| 1031 | * @param mixed $value |
||
| 1032 | * @param string $className |
||
| 1033 | * @param string|null $message |
||
| 1034 | * @param string|null $propertyPath |
||
| 1035 | * @return void |
||
| 1036 | * @throws \Assert\AssertionFailedException |
||
| 1037 | */ |
||
| 1038 | public static function isInstanceOf($value, $className, $message = null, $propertyPath = null) |
||
| 1050 | |||
| 1051 | /** |
||
| 1052 | * Assert that value is not 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 notIsInstanceOf($value, $className, $message = null, $propertyPath = null) |
||
| 1073 | |||
| 1074 | /** |
||
| 1075 | * Assert that value is subclass 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 subclassOf($value, $className, $message = null, $propertyPath = null) |
||
| 1096 | |||
| 1097 | /** |
||
| 1098 | * Assert that value is in range of numbers. |
||
| 1099 | * |
||
| 1100 | * @param mixed $value |
||
| 1101 | * @param integer $minValue |
||
| 1102 | * @param integer $maxValue |
||
| 1103 | * @param string|null $message |
||
| 1104 | * @param string|null $propertyPath |
||
| 1105 | * @return void |
||
| 1106 | * @throws \Assert\AssertionFailedException |
||
| 1107 | */ |
||
| 1108 | public static function range($value, $minValue, $maxValue, $message = null, $propertyPath = null) |
||
| 1123 | |||
| 1124 | /** |
||
| 1125 | * Assert that a value is at least as big as a given limit |
||
| 1126 | * |
||
| 1127 | * @param mixed $value |
||
| 1128 | * @param mixed $minValue |
||
| 1129 | * @param string|null $message |
||
| 1130 | * @param string|null $propertyPath |
||
| 1131 | * @return void |
||
| 1132 | * @throws \Assert\AssertionFailedException |
||
| 1133 | */ |
||
| 1134 | public static function min($value, $minValue, $message = null, $propertyPath = null) |
||
| 1148 | |||
| 1149 | /** |
||
| 1150 | * Assert that a number is smaller as a given limit |
||
| 1151 | * |
||
| 1152 | * @param mixed $value |
||
| 1153 | * @param mixed $maxValue |
||
| 1154 | * @param string|null $message |
||
| 1155 | * @param string|null $propertyPath |
||
| 1156 | * @return void |
||
| 1157 | * @throws \Assert\AssertionFailedException |
||
| 1158 | */ |
||
| 1159 | public static function max($value, $maxValue, $message = null, $propertyPath = null) |
||
| 1173 | |||
| 1174 | /** |
||
| 1175 | * Assert that a file exists |
||
| 1176 | * |
||
| 1177 | * @param string $value |
||
| 1178 | * @param string|null $message |
||
| 1179 | * @param string|null $propertyPath |
||
| 1180 | * @return void |
||
| 1181 | * @throws \Assert\AssertionFailedException |
||
| 1182 | */ |
||
| 1183 | public static function file($value, $message = null, $propertyPath = null) |
||
| 1197 | |||
| 1198 | /** |
||
| 1199 | * Assert that a directory exists |
||
| 1200 | * |
||
| 1201 | * @param string $value |
||
| 1202 | * @param string|null $message |
||
| 1203 | * @param string|null $propertyPath |
||
| 1204 | * @return void |
||
| 1205 | * @throws \Assert\AssertionFailedException |
||
| 1206 | */ |
||
| 1207 | public static function directory($value, $message = null, $propertyPath = null) |
||
| 1220 | |||
| 1221 | /** |
||
| 1222 | * Assert that the value is something readable |
||
| 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 readable($value, $message = null, $propertyPath = null) |
||
| 1243 | |||
| 1244 | /** |
||
| 1245 | * Assert that the value is something writeable |
||
| 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 writeable($value, $message = null, $propertyPath = null) |
||
| 1266 | |||
| 1267 | /** |
||
| 1268 | * Assert that value is an email adress (using input_filter/FILTER_VALIDATE_EMAIL). |
||
| 1269 | * |
||
| 1270 | * @param mixed $value |
||
| 1271 | * @param string|null $message |
||
| 1272 | * @param string|null $propertyPath |
||
| 1273 | * @return void |
||
| 1274 | * @throws \Assert\AssertionFailedException |
||
| 1275 | */ |
||
| 1276 | public static function email($value, $message = null, $propertyPath = null) |
||
| 1301 | |||
| 1302 | /** |
||
| 1303 | * Assert that value is an URL. |
||
| 1304 | * |
||
| 1305 | * This code snipped was taken from the Symfony project and modified to the special demands of this method. |
||
| 1306 | * |
||
| 1307 | * @param mixed $value |
||
| 1308 | * @param string|null $message |
||
| 1309 | * @param string|null $propertyPath |
||
| 1310 | * @return void |
||
| 1311 | * @throws \Assert\AssertionFailedException |
||
| 1312 | * |
||
| 1313 | * |
||
| 1314 | * @link https://github.com/symfony/Validator/blob/master/Constraints/UrlValidator.php |
||
| 1315 | * @link https://github.com/symfony/Validator/blob/master/Constraints/Url.php |
||
| 1316 | */ |
||
| 1317 | public static function url($value, $message = null, $propertyPath = null) |
||
| 1350 | |||
| 1351 | /** |
||
| 1352 | * Assert that value is alphanumeric. |
||
| 1353 | * |
||
| 1354 | * @param mixed $value |
||
| 1355 | * @param string|null $message |
||
| 1356 | * @param string|null $propertyPath |
||
| 1357 | * @return void |
||
| 1358 | * @throws \Assert\AssertionFailedException |
||
| 1359 | */ |
||
| 1360 | public static function alnum($value, $message = null, $propertyPath = null) |
||
| 1373 | |||
| 1374 | /** |
||
| 1375 | * Assert that the value is boolean True. |
||
| 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 true($value, $message = null, $propertyPath = null) |
||
| 1394 | |||
| 1395 | /** |
||
| 1396 | * Assert that the value is boolean False. |
||
| 1397 | * |
||
| 1398 | * @param mixed $value |
||
| 1399 | * @param string|null $message |
||
| 1400 | * @param string|null $propertyPath |
||
| 1401 | * @return void |
||
| 1402 | * @throws \Assert\AssertionFailedException |
||
| 1403 | */ |
||
| 1404 | public static function false($value, $message = null, $propertyPath = null) |
||
| 1415 | |||
| 1416 | /** |
||
| 1417 | * Assert that the class exists. |
||
| 1418 | * |
||
| 1419 | * @param mixed $value |
||
| 1420 | * @param string|null $message |
||
| 1421 | * @param string|null $propertyPath |
||
| 1422 | * @return void |
||
| 1423 | * @throws \Assert\AssertionFailedException |
||
| 1424 | */ |
||
| 1425 | public static function classExists($value, $message = null, $propertyPath = null) |
||
| 1436 | |||
| 1437 | /** |
||
| 1438 | * Assert that the class implements the interface |
||
| 1439 | * |
||
| 1440 | * @param mixed $class |
||
| 1441 | * @param string $interfaceName |
||
| 1442 | * @param string|null $message |
||
| 1443 | * @param string|null $propertyPath |
||
| 1444 | * @return void |
||
| 1445 | * @throws \Assert\AssertionFailedException |
||
| 1446 | */ |
||
| 1447 | public static function implementsInterface($class, $interfaceName, $message = null, $propertyPath = null) |
||
| 1460 | |||
| 1461 | /** |
||
| 1462 | * Assert that the given string is a valid json string. |
||
| 1463 | * |
||
| 1464 | * NOTICE: |
||
| 1465 | * Since this does a json_decode to determine its validity |
||
| 1466 | * you probably should consider, when using the variable |
||
| 1467 | * content afterwards, just to decode and check for yourself instead |
||
| 1468 | * of using this assertion. |
||
| 1469 | * |
||
| 1470 | * @param mixed $value |
||
| 1471 | * @param string|null $message |
||
| 1472 | * @param string|null $propertyPath |
||
| 1473 | * @return void |
||
| 1474 | * @throws \Assert\AssertionFailedException |
||
| 1475 | */ |
||
| 1476 | public static function isJsonString($value, $message = null, $propertyPath = null) |
||
| 1487 | |||
| 1488 | /** |
||
| 1489 | * Assert that the given string is a valid UUID |
||
| 1490 | * |
||
| 1491 | * Uses code from {@link https://github.com/ramsey/uuid} that is MIT licensed. |
||
| 1492 | * |
||
| 1493 | * @param string $value |
||
| 1494 | * @param string|null $message |
||
| 1495 | * @param string|null $propertyPath |
||
| 1496 | * @return void |
||
| 1497 | * @throws \Assert\AssertionFailedException |
||
| 1498 | */ |
||
| 1499 | public static function uuid($value, $message = null, $propertyPath = null) |
||
| 1516 | |||
| 1517 | /** |
||
| 1518 | * Assert that the count of countable is equal to count. |
||
| 1519 | * |
||
| 1520 | * @param array|\Countable $countable |
||
| 1521 | * @param int $count |
||
| 1522 | * @param string $message |
||
| 1523 | * @param string $propertyPath |
||
| 1524 | * @return void |
||
| 1525 | * @throws \Assert\AssertionFailedException |
||
| 1526 | */ |
||
| 1527 | public static function count($countable, $count, $message = null, $propertyPath = null) |
||
| 1538 | |||
| 1539 | /** |
||
| 1540 | * static call handler to implement: |
||
| 1541 | * - "null or assertion" delegation |
||
| 1542 | * - "all" delegation |
||
| 1543 | */ |
||
| 1544 | public static function __callStatic($method, $args) |
||
| 1580 | |||
| 1581 | /** |
||
| 1582 | * Determines if the values array has every choice as key and that this choice has content. |
||
| 1583 | * |
||
| 1584 | * @param array $values |
||
| 1585 | * @param array $choices |
||
| 1586 | * @param null $message |
||
| 1587 | * @param null $propertyPath |
||
| 1588 | */ |
||
| 1589 | public static function choicesNotEmpty(array $values, array $choices, $message = null, $propertyPath = null) |
||
| 1598 | |||
| 1599 | /** |
||
| 1600 | * Determines that the named method is defined in the provided object. |
||
| 1601 | * |
||
| 1602 | * @param string $value |
||
| 1603 | * @param mixed $object |
||
| 1604 | * @param null $message |
||
| 1605 | * @param null $propertyPath |
||
| 1606 | * |
||
| 1607 | * @throws |
||
| 1608 | */ |
||
| 1609 | public static function methodExists($value, $object, $message = null, $propertyPath = null) |
||
| 1622 | |||
| 1623 | /** |
||
| 1624 | * Determines that the provided value is an object. |
||
| 1625 | * |
||
| 1626 | * @param mixed $value |
||
| 1627 | * @param null $message |
||
| 1628 | * @param null $propertyPath |
||
| 1629 | */ |
||
| 1630 | public static function isObject($value, $message = null, $propertyPath = null) |
||
| 1642 | |||
| 1643 | /** |
||
| 1644 | * Determines if the value is less than given limit. |
||
| 1645 | * |
||
| 1646 | * @param mixed $value |
||
| 1647 | * @param mixed $limit |
||
| 1648 | * @param null $message |
||
| 1649 | * @param null $propertyPath |
||
| 1650 | */ |
||
| 1651 | public static function lessThan($value, $limit, $message = null, $propertyPath = null) |
||
| 1663 | |||
| 1664 | /** |
||
| 1665 | * Determines if the value is less or than given limit. |
||
| 1666 | * |
||
| 1667 | * @param mixed $value |
||
| 1668 | * @param mixed $limit |
||
| 1669 | * @param null $message |
||
| 1670 | * @param null $propertyPath |
||
| 1671 | */ |
||
| 1672 | public static function lessOrEqualThan($value, $limit, $message = null, $propertyPath = null) |
||
| 1684 | |||
| 1685 | /** |
||
| 1686 | * Determines if the value is greater than given limit. |
||
| 1687 | * |
||
| 1688 | * @param mixed $value |
||
| 1689 | * @param mixed $limit |
||
| 1690 | * @param null $message |
||
| 1691 | * @param null $propertyPath |
||
| 1692 | */ |
||
| 1693 | public static function greaterThan($value, $limit, $message = null, $propertyPath = null) |
||
| 1705 | |||
| 1706 | /** |
||
| 1707 | * Determines if the value is greater or equal than given limit. |
||
| 1708 | * |
||
| 1709 | * @param mixed $value |
||
| 1710 | * @param mixed $limit |
||
| 1711 | * @param null $message |
||
| 1712 | * @param null $propertyPath |
||
| 1713 | */ |
||
| 1714 | public static function greaterOrEqualThan($value, $limit, $message = null, $propertyPath = null) |
||
| 1726 | |||
| 1727 | /** |
||
| 1728 | * Assert that date is valid and corresponds to the given format. |
||
| 1729 | * |
||
| 1730 | * @param string $value |
||
| 1731 | * @param string $format supports all of the options date(), except for the following: |
||
| 1732 | * N, w, W, t, L, o, B, a, A, g, h, I, O, P, Z, c, r. |
||
| 1733 | * @param string|null $message |
||
| 1734 | * @param string|null $propertyPath |
||
| 1735 | * |
||
| 1736 | * @link http://php.net/manual/function.date.php#refsect1-function.date-parameters |
||
| 1737 | */ |
||
| 1738 | public static function date($value, $format, $message = null, $propertyPath = null) |
||
| 1755 | |||
| 1756 | /** |
||
| 1757 | * Determines that the provided value is callable. |
||
| 1758 | * |
||
| 1759 | * @param mixed $value |
||
| 1760 | * @param null $message |
||
| 1761 | * @param null $propertyPath |
||
| 1762 | */ |
||
| 1763 | public static function isCallable($value, $message = null, $propertyPath = null) |
||
| 1774 | |||
| 1775 | /** |
||
| 1776 | * Make a string version of a value. |
||
| 1777 | * |
||
| 1778 | * @param mixed $value |
||
| 1779 | * @return string |
||
| 1780 | */ |
||
| 1781 | protected static function stringify($value) |
||
| 1815 | } |
||
| 1816 | |||
| 1817 |