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 |
||
| 177 | class Assertion |
||
| 178 | { |
||
| 179 | const INVALID_FLOAT = 9; |
||
| 180 | const INVALID_INTEGER = 10; |
||
| 181 | const INVALID_DIGIT = 11; |
||
| 182 | const INVALID_INTEGERISH = 12; |
||
| 183 | const INVALID_BOOLEAN = 13; |
||
| 184 | const VALUE_EMPTY = 14; |
||
| 185 | const VALUE_NULL = 15; |
||
| 186 | const VALUE_NOT_NULL = 25; |
||
| 187 | const INVALID_STRING = 16; |
||
| 188 | const INVALID_REGEX = 17; |
||
| 189 | const INVALID_MIN_LENGTH = 18; |
||
| 190 | const INVALID_MAX_LENGTH = 19; |
||
| 191 | const INVALID_STRING_START = 20; |
||
| 192 | const INVALID_STRING_CONTAINS = 21; |
||
| 193 | const INVALID_CHOICE = 22; |
||
| 194 | const INVALID_NUMERIC = 23; |
||
| 195 | const INVALID_ARRAY = 24; |
||
| 196 | const INVALID_KEY_EXISTS = 26; |
||
| 197 | const INVALID_NOT_BLANK = 27; |
||
| 198 | const INVALID_INSTANCE_OF = 28; |
||
| 199 | const INVALID_SUBCLASS_OF = 29; |
||
| 200 | const INVALID_RANGE = 30; |
||
| 201 | const INVALID_ALNUM = 31; |
||
| 202 | const INVALID_TRUE = 32; |
||
| 203 | const INVALID_EQ = 33; |
||
| 204 | const INVALID_SAME = 34; |
||
| 205 | const INVALID_MIN = 35; |
||
| 206 | const INVALID_MAX = 36; |
||
| 207 | const INVALID_LENGTH = 37; |
||
| 208 | const INVALID_FALSE = 38; |
||
| 209 | const INVALID_STRING_END = 39; |
||
| 210 | const INVALID_UUID = 40; |
||
| 211 | const INVALID_COUNT = 41; |
||
| 212 | const INVALID_NOT_EQ = 42; |
||
| 213 | const INVALID_NOT_SAME = 43; |
||
| 214 | const INVALID_TRAVERSABLE = 44; |
||
| 215 | const INVALID_ARRAY_ACCESSIBLE = 45; |
||
| 216 | const INVALID_KEY_ISSET = 46; |
||
| 217 | const INVALID_VALUE_IN_ARRAY = 47; |
||
| 218 | const INVALID_E164 = 48; |
||
| 219 | const INVALID_DIRECTORY = 101; |
||
| 220 | const INVALID_FILE = 102; |
||
| 221 | const INVALID_READABLE = 103; |
||
| 222 | const INVALID_WRITEABLE = 104; |
||
| 223 | const INVALID_CLASS = 105; |
||
| 224 | const INVALID_INTERFACE = 106; |
||
| 225 | const INVALID_EMAIL = 201; |
||
| 226 | const INTERFACE_NOT_IMPLEMENTED = 202; |
||
| 227 | const INVALID_URL = 203; |
||
| 228 | const INVALID_NOT_INSTANCE_OF = 204; |
||
| 229 | const VALUE_NOT_EMPTY = 205; |
||
| 230 | const INVALID_JSON_STRING = 206; |
||
| 231 | const INVALID_OBJECT = 207; |
||
| 232 | const INVALID_METHOD = 208; |
||
| 233 | const INVALID_SCALAR = 209; |
||
| 234 | const INVALID_LESS = 210; |
||
| 235 | const INVALID_LESS_OR_EQUAL = 211; |
||
| 236 | const INVALID_GREATER = 212; |
||
| 237 | const INVALID_GREATER_OR_EQUAL = 213; |
||
| 238 | const INVALID_DATE = 214; |
||
| 239 | const INVALID_CALLABLE = 215; |
||
| 240 | const INVALID_KEY_NOT_EXISTS = 216; |
||
| 241 | const INVALID_SATISFY = 217; |
||
| 242 | const INVALID_IP = 218; |
||
| 243 | const INVALID_BETWEEN = 219; |
||
| 244 | const INVALID_BETWEEN_EXCLUSIVE = 220; |
||
| 245 | const INVALID_EXTENSION = 222; |
||
| 246 | const INVALID_CONSTANT = 221; |
||
| 247 | const INVALID_VERSION = 223; |
||
| 248 | |||
| 249 | /** |
||
| 250 | * Exception to throw when an assertion failed. |
||
| 251 | * |
||
| 252 | * @var string |
||
| 253 | */ |
||
| 254 | protected static $exceptionClass = 'Assert\InvalidArgumentException'; |
||
| 255 | |||
| 256 | /** |
||
| 257 | * Helper method that handles building the assertion failure exceptions. |
||
| 258 | * They are returned from this method so that the stack trace still shows |
||
| 259 | * the assertions method. |
||
| 260 | * |
||
| 261 | * @param mixed $value |
||
| 262 | * @param string $message |
||
| 263 | * @param int $code |
||
| 264 | * @param string|null $propertyPath |
||
| 265 | * @param array $constraints |
||
| 266 | * |
||
| 267 | * @return mixed |
||
|
|
|||
| 268 | */ |
||
| 269 | protected static function createException($value, $message, $code, $propertyPath, array $constraints = array()) |
||
| 270 | { |
||
| 271 | $exceptionClass = static::$exceptionClass; |
||
| 272 | return new $exceptionClass($message, $code, $propertyPath, $value, $constraints); |
||
| 273 | } |
||
| 274 | |||
| 275 | /** |
||
| 276 | * Assert that two values are equal (using == ). |
||
| 277 | * |
||
| 278 | * @param mixed $value |
||
| 279 | * @param mixed $value2 |
||
| 280 | * @param string|null $message |
||
| 281 | * @param string|null $propertyPath |
||
| 282 | * @return bool |
||
| 283 | * @throws \Assert\AssertionFailedException |
||
| 284 | */ |
||
| 285 | public static function eq($value, $value2, $message = null, $propertyPath = null) |
||
| 286 | { |
||
| 287 | if ($value != $value2) { |
||
| 288 | $message = sprintf( |
||
| 289 | $message ?: 'Value "%s" does not equal expected value "%s".', |
||
| 290 | static::stringify($value), |
||
| 291 | static::stringify($value2) |
||
| 292 | ); |
||
| 293 | |||
| 294 | throw static::createException($value, $message, static::INVALID_EQ, $propertyPath, array('expected' => $value2)); |
||
| 295 | } |
||
| 296 | |||
| 297 | return true; |
||
| 298 | } |
||
| 299 | |||
| 300 | /** |
||
| 301 | * Assert that two values are the same (using ===). |
||
| 302 | * |
||
| 303 | * @param mixed $value |
||
| 304 | * @param mixed $value2 |
||
| 305 | * @param string|null $message |
||
| 306 | * @param string|null $propertyPath |
||
| 307 | * @return bool |
||
| 308 | * @throws \Assert\AssertionFailedException |
||
| 309 | */ |
||
| 310 | public static function same($value, $value2, $message = null, $propertyPath = null) |
||
| 311 | { |
||
| 312 | if ($value !== $value2) { |
||
| 313 | $message = sprintf( |
||
| 314 | $message ?: 'Value "%s" is not the same as expected value "%s".', |
||
| 315 | static::stringify($value), |
||
| 316 | static::stringify($value2) |
||
| 317 | ); |
||
| 318 | |||
| 319 | throw static::createException($value, $message, static::INVALID_SAME, $propertyPath, array('expected' => $value2)); |
||
| 320 | } |
||
| 321 | |||
| 322 | return true; |
||
| 323 | } |
||
| 324 | |||
| 325 | /** |
||
| 326 | * Assert that two values are not equal (using == ). |
||
| 327 | * |
||
| 328 | * @param mixed $value1 |
||
| 329 | * @param mixed $value2 |
||
| 330 | * @param string|null $message |
||
| 331 | * @param string|null $propertyPath |
||
| 332 | * @return bool |
||
| 333 | * @throws \Assert\AssertionFailedException |
||
| 334 | */ |
||
| 335 | public static function notEq($value1, $value2, $message = null, $propertyPath = null) |
||
| 336 | { |
||
| 337 | if ($value1 == $value2) { |
||
| 338 | $message = sprintf( |
||
| 339 | $message ?: 'Value "%s" is equal to expected value "%s".', |
||
| 340 | static::stringify($value1), |
||
| 341 | static::stringify($value2) |
||
| 342 | ); |
||
| 343 | throw static::createException($value1, $message, static::INVALID_NOT_EQ, $propertyPath, array('expected' => $value2)); |
||
| 344 | } |
||
| 345 | |||
| 346 | return true; |
||
| 347 | } |
||
| 348 | |||
| 349 | /** |
||
| 350 | * Assert that two values are not the same (using === ). |
||
| 351 | * |
||
| 352 | * @param mixed $value1 |
||
| 353 | * @param mixed $value2 |
||
| 354 | * @param string|null $message |
||
| 355 | * @param string|null $propertyPath |
||
| 356 | * @return bool |
||
| 357 | * @throws \Assert\AssertionFailedException |
||
| 358 | */ |
||
| 359 | public static function notSame($value1, $value2, $message = null, $propertyPath = null) |
||
| 360 | { |
||
| 361 | if ($value1 === $value2) { |
||
| 362 | $message = sprintf( |
||
| 363 | $message ?: 'Value "%s" is the same as expected value "%s".', |
||
| 364 | static::stringify($value1), |
||
| 365 | static::stringify($value2) |
||
| 366 | ); |
||
| 367 | throw static::createException($value1, $message, static::INVALID_NOT_SAME, $propertyPath, array('expected' => $value2)); |
||
| 368 | } |
||
| 369 | |||
| 370 | return true; |
||
| 371 | } |
||
| 372 | |||
| 373 | /** |
||
| 374 | * Assert that value is not in array of choices. |
||
| 375 | * |
||
| 376 | * @param mixed $value |
||
| 377 | * @param array $choices |
||
| 378 | * @param string|null $message |
||
| 379 | * @param string|null $propertyPath |
||
| 380 | * @return bool |
||
| 381 | * @throws \Assert\AssertionFailedException |
||
| 382 | */ |
||
| 383 | public static function notInArray($value, array $choices, $message = null, $propertyPath = null) |
||
| 396 | |||
| 397 | /** |
||
| 398 | * Assert that value is a php integer. |
||
| 399 | * |
||
| 400 | * @param mixed $value |
||
| 401 | * @param string|null $message |
||
| 402 | * @param string|null $propertyPath |
||
| 403 | * @return bool |
||
| 404 | * @throws \Assert\AssertionFailedException |
||
| 405 | */ |
||
| 406 | public static function integer($value, $message = null, $propertyPath = null) |
||
| 419 | |||
| 420 | /** |
||
| 421 | * Assert that value is a php float. |
||
| 422 | * |
||
| 423 | * @param mixed $value |
||
| 424 | * @param string|null $message |
||
| 425 | * @param string|null $propertyPath |
||
| 426 | * @return bool |
||
| 427 | * @throws \Assert\AssertionFailedException |
||
| 428 | */ |
||
| 429 | public static function float($value, $message = null, $propertyPath = null) |
||
| 442 | |||
| 443 | /** |
||
| 444 | * Validates if an integer or integerish is a digit. |
||
| 445 | * |
||
| 446 | * @param mixed $value |
||
| 447 | * @param string|null $message |
||
| 448 | * @param string|null $propertyPath |
||
| 449 | * @return bool |
||
| 450 | * @throws \Assert\AssertionFailedException |
||
| 451 | */ |
||
| 452 | public static function digit($value, $message = null, $propertyPath = null) |
||
| 465 | |||
| 466 | /** |
||
| 467 | * Assert that value is a php integer'ish. |
||
| 468 | * |
||
| 469 | * @param mixed $value |
||
| 470 | * @param string|null $message |
||
| 471 | * @param string|null $propertyPath |
||
| 472 | * @return bool |
||
| 473 | * @throws \Assert\AssertionFailedException |
||
| 474 | */ |
||
| 475 | public static function integerish($value, $message = null, $propertyPath = null) |
||
| 488 | |||
| 489 | /** |
||
| 490 | * Assert that value is php boolean |
||
| 491 | * |
||
| 492 | * @param mixed $value |
||
| 493 | * @param string|null $message |
||
| 494 | * @param string|null $propertyPath |
||
| 495 | * @return bool |
||
| 496 | * @throws \Assert\AssertionFailedException |
||
| 497 | */ |
||
| 498 | public static function boolean($value, $message = null, $propertyPath = null) |
||
| 511 | |||
| 512 | /** |
||
| 513 | * Assert that value is a PHP scalar |
||
| 514 | * |
||
| 515 | * @param mixed $value |
||
| 516 | * @param string|null $message |
||
| 517 | * @param string|null $propertyPath |
||
| 518 | * @return bool |
||
| 519 | * @throws \Assert\AssertionFailedException |
||
| 520 | */ |
||
| 521 | public static function scalar($value, $message = null, $propertyPath = null) |
||
| 534 | |||
| 535 | /** |
||
| 536 | * Assert that value is not empty |
||
| 537 | * |
||
| 538 | * @param mixed $value |
||
| 539 | * @param string|null $message |
||
| 540 | * @param string|null $propertyPath |
||
| 541 | * @return bool |
||
| 542 | * @throws \Assert\AssertionFailedException |
||
| 543 | */ |
||
| 544 | public static function notEmpty($value, $message = null, $propertyPath = null) |
||
| 557 | |||
| 558 | /** |
||
| 559 | * Assert that value is empty |
||
| 560 | * |
||
| 561 | * @param mixed $value |
||
| 562 | * @param string|null $message |
||
| 563 | * @param string|null $propertyPath |
||
| 564 | * @return bool |
||
| 565 | * @throws \Assert\AssertionFailedException |
||
| 566 | */ |
||
| 567 | public static function noContent($value, $message = null, $propertyPath = null) |
||
| 580 | |||
| 581 | /** |
||
| 582 | * Assert that value is null |
||
| 583 | * |
||
| 584 | * @param mixed $value |
||
| 585 | * @param string|null $message |
||
| 586 | * @param string|null $propertyPath |
||
| 587 | * @return bool |
||
| 588 | * @throws \Assert\AssertionFailedException |
||
| 589 | */ |
||
| 590 | public static function null($value, $message = null, $propertyPath = null) |
||
| 603 | |||
| 604 | /** |
||
| 605 | * Assert that value is not null |
||
| 606 | * |
||
| 607 | * @param mixed $value |
||
| 608 | * @param string|null $message |
||
| 609 | * @param string|null $propertyPath |
||
| 610 | * @return bool |
||
| 611 | * @throws \Assert\AssertionFailedException |
||
| 612 | */ |
||
| 613 | public static function notNull($value, $message = null, $propertyPath = null) |
||
| 626 | |||
| 627 | /** |
||
| 628 | * Assert that value is a string |
||
| 629 | * |
||
| 630 | * @param mixed $value |
||
| 631 | * @param string|null $message |
||
| 632 | * @param string|null $propertyPath |
||
| 633 | * @return bool |
||
| 634 | * @throws \Assert\AssertionFailedException |
||
| 635 | */ |
||
| 636 | public static function string($value, $message = null, $propertyPath = null) |
||
| 650 | |||
| 651 | /** |
||
| 652 | * Assert that value matches a regex |
||
| 653 | * |
||
| 654 | * @param mixed $value |
||
| 655 | * @param string $pattern |
||
| 656 | * @param string|null $message |
||
| 657 | * @param string|null $propertyPath |
||
| 658 | * @return bool |
||
| 659 | * @throws \Assert\AssertionFailedException |
||
| 660 | */ |
||
| 661 | public static function regex($value, $pattern, $message = null, $propertyPath = null) |
||
| 676 | |||
| 677 | /** |
||
| 678 | * Assert that string has a given length. |
||
| 679 | * |
||
| 680 | * @param mixed $value |
||
| 681 | * @param int $length |
||
| 682 | * @param string|null $message |
||
| 683 | * @param string|null $propertyPath |
||
| 684 | * @param string $encoding |
||
| 685 | * @return bool |
||
| 686 | * @throws \Assert\AssertionFailedException |
||
| 687 | */ |
||
| 688 | public static function length($value, $length, $message = null, $propertyPath = null, $encoding = 'utf8') |
||
| 706 | |||
| 707 | /** |
||
| 708 | * Assert that a string is at least $minLength chars long. |
||
| 709 | * |
||
| 710 | * @param mixed $value |
||
| 711 | * @param int $minLength |
||
| 712 | * @param string|null $message |
||
| 713 | * @param string|null $propertyPath |
||
| 714 | * @param string $encoding |
||
| 715 | * @return bool |
||
| 716 | * @throws \Assert\AssertionFailedException |
||
| 717 | */ |
||
| 718 | public static function minLength($value, $minLength, $message = null, $propertyPath = null, $encoding = 'utf8') |
||
| 736 | |||
| 737 | /** |
||
| 738 | * Assert that string value is not longer than $maxLength chars. |
||
| 739 | * |
||
| 740 | * @param mixed $value |
||
| 741 | * @param integer $maxLength |
||
| 742 | * @param string|null $message |
||
| 743 | * @param string|null $propertyPath |
||
| 744 | * @param string $encoding |
||
| 745 | * @return bool |
||
| 746 | * @throws \Assert\AssertionFailedException |
||
| 747 | */ |
||
| 748 | public static function maxLength($value, $maxLength, $message = null, $propertyPath = null, $encoding = 'utf8') |
||
| 766 | |||
| 767 | /** |
||
| 768 | * Assert that string length is between min,max lengths. |
||
| 769 | * |
||
| 770 | * @param mixed $value |
||
| 771 | * @param integer $minLength |
||
| 772 | * @param integer $maxLength |
||
| 773 | * @param string|null $message |
||
| 774 | * @param string|null $propertyPath |
||
| 775 | * @param string $encoding |
||
| 776 | * @return bool |
||
| 777 | * @throws \Assert\AssertionFailedException |
||
| 778 | */ |
||
| 779 | public static function betweenLength($value, $minLength, $maxLength, $message = null, $propertyPath = null, $encoding = 'utf8') |
||
| 809 | |||
| 810 | /** |
||
| 811 | * Assert that string starts with a sequence of chars. |
||
| 812 | * |
||
| 813 | * @param mixed $string |
||
| 814 | * @param string $needle |
||
| 815 | * @param string|null $message |
||
| 816 | * @param string|null $propertyPath |
||
| 817 | * @param string $encoding |
||
| 818 | * @return bool |
||
| 819 | * @throws \Assert\AssertionFailedException |
||
| 820 | */ |
||
| 821 | public static function startsWith($string, $needle, $message = null, $propertyPath = null, $encoding = 'utf8') |
||
| 838 | |||
| 839 | /** |
||
| 840 | * Assert that string ends with a sequence of chars. |
||
| 841 | * |
||
| 842 | * @param mixed $string |
||
| 843 | * @param string $needle |
||
| 844 | * @param string|null $message |
||
| 845 | * @param string|null $propertyPath |
||
| 846 | * @param string $encoding |
||
| 847 | * @return bool |
||
| 848 | * @throws \Assert\AssertionFailedException |
||
| 849 | */ |
||
| 850 | public static function endsWith($string, $needle, $message = null, $propertyPath = null, $encoding = 'utf8') |
||
| 869 | |||
| 870 | /** |
||
| 871 | * Assert that string contains a sequence of chars. |
||
| 872 | * |
||
| 873 | * @param mixed $string |
||
| 874 | * @param string $needle |
||
| 875 | * @param string|null $message |
||
| 876 | * @param string|null $propertyPath |
||
| 877 | * @param string $encoding |
||
| 878 | * @return bool |
||
| 879 | * @throws \Assert\AssertionFailedException |
||
| 880 | */ |
||
| 881 | public static function contains($string, $needle, $message = null, $propertyPath = null, $encoding = 'utf8') |
||
| 898 | |||
| 899 | /** |
||
| 900 | * Assert that value is in array of choices. |
||
| 901 | * |
||
| 902 | * @param mixed $value |
||
| 903 | * @param array $choices |
||
| 904 | * @param string|null $message |
||
| 905 | * @param string|null $propertyPath |
||
| 906 | * @return bool |
||
| 907 | * @throws \Assert\AssertionFailedException |
||
| 908 | */ |
||
| 909 | public static function choice($value, array $choices, $message = null, $propertyPath = null) |
||
| 923 | |||
| 924 | /** |
||
| 925 | * Alias of {@see choice()} |
||
| 926 | * |
||
| 927 | * @param mixed $value |
||
| 928 | * @param array $choices |
||
| 929 | * @param string|null $message |
||
| 930 | * @param string|null $propertyPath |
||
| 931 | * @return bool |
||
| 932 | */ |
||
| 933 | public static function inArray($value, array $choices, $message = null, $propertyPath = null) |
||
| 937 | |||
| 938 | /** |
||
| 939 | * Assert that value is numeric. |
||
| 940 | * |
||
| 941 | * @param mixed $value |
||
| 942 | * @param string|null $message |
||
| 943 | * @param string|null $propertyPath |
||
| 944 | * @return bool |
||
| 945 | * @throws \Assert\AssertionFailedException |
||
| 946 | */ |
||
| 947 | public static function numeric($value, $message = null, $propertyPath = null) |
||
| 960 | |||
| 961 | /** |
||
| 962 | * Assert that value is an array. |
||
| 963 | * |
||
| 964 | * @param mixed $value |
||
| 965 | * @param string|null $message |
||
| 966 | * @param string|null $propertyPath |
||
| 967 | * @return bool |
||
| 968 | * @throws \Assert\AssertionFailedException |
||
| 969 | */ |
||
| 970 | public static function isArray($value, $message = null, $propertyPath = null) |
||
| 983 | |||
| 984 | /** |
||
| 985 | * Assert that value is an array or a traversable object. |
||
| 986 | * |
||
| 987 | * @param mixed $value |
||
| 988 | * @param string|null $message |
||
| 989 | * @param string|null $propertyPath |
||
| 990 | * @return bool |
||
| 991 | * @throws \Assert\AssertionFailedException |
||
| 992 | */ |
||
| 993 | public static function isTraversable($value, $message = null, $propertyPath = null) |
||
| 1006 | |||
| 1007 | /** |
||
| 1008 | * Assert that value is an array or an array-accessible object. |
||
| 1009 | * |
||
| 1010 | * @param mixed $value |
||
| 1011 | * @param string|null $message |
||
| 1012 | * @param string|null $propertyPath |
||
| 1013 | * @return bool |
||
| 1014 | * @throws \Assert\AssertionFailedException |
||
| 1015 | */ |
||
| 1016 | public static function isArrayAccessible($value, $message = null, $propertyPath = null) |
||
| 1029 | |||
| 1030 | /** |
||
| 1031 | * Assert that key exists in an array |
||
| 1032 | * |
||
| 1033 | * @param mixed $value |
||
| 1034 | * @param string|integer $key |
||
| 1035 | * @param string|null $message |
||
| 1036 | * @param string|null $propertyPath |
||
| 1037 | * @return bool |
||
| 1038 | * @throws \Assert\AssertionFailedException |
||
| 1039 | */ |
||
| 1040 | public static function keyExists($value, $key, $message = null, $propertyPath = null) |
||
| 1055 | |||
| 1056 | /** |
||
| 1057 | * Assert that key does not exist in an array |
||
| 1058 | * |
||
| 1059 | * @param mixed $value |
||
| 1060 | * @param string|integer $key |
||
| 1061 | * @param string|null $message |
||
| 1062 | * @param string|null $propertyPath |
||
| 1063 | * @return bool |
||
| 1064 | * @throws \Assert\AssertionFailedException |
||
| 1065 | */ |
||
| 1066 | public static function keyNotExists($value, $key, $message = null, $propertyPath = null) |
||
| 1081 | |||
| 1082 | /** |
||
| 1083 | * Assert that key exists in an array/array-accessible object using isset() |
||
| 1084 | * |
||
| 1085 | * @param mixed $value |
||
| 1086 | * @param string|integer $key |
||
| 1087 | * @param string|null $message |
||
| 1088 | * @param string|null $propertyPath |
||
| 1089 | * @return bool |
||
| 1090 | * @throws \Assert\AssertionFailedException |
||
| 1091 | */ |
||
| 1092 | public static function keyIsset($value, $key, $message = null, $propertyPath = null) |
||
| 1107 | |||
| 1108 | /** |
||
| 1109 | * Assert that key exists in an array/array-accessible object and its value is not empty. |
||
| 1110 | * |
||
| 1111 | * @param mixed $value |
||
| 1112 | * @param string|integer $key |
||
| 1113 | * @param string|null $message |
||
| 1114 | * @param string|null $propertyPath |
||
| 1115 | * @return bool |
||
| 1116 | * @throws \Assert\AssertionFailedException |
||
| 1117 | */ |
||
| 1118 | public static function notEmptyKey($value, $key, $message = null, $propertyPath = null) |
||
| 1125 | |||
| 1126 | /** |
||
| 1127 | * Assert that value is not blank |
||
| 1128 | * |
||
| 1129 | * @param mixed $value |
||
| 1130 | * @param string|null $message |
||
| 1131 | * @param string|null $propertyPath |
||
| 1132 | * @return bool |
||
| 1133 | * @throws \Assert\AssertionFailedException |
||
| 1134 | */ |
||
| 1135 | public static function notBlank($value, $message = null, $propertyPath = null) |
||
| 1148 | |||
| 1149 | /** |
||
| 1150 | * Assert that value is instance of given class-name. |
||
| 1151 | * |
||
| 1152 | * @param mixed $value |
||
| 1153 | * @param string $className |
||
| 1154 | * @param string|null $message |
||
| 1155 | * @param string|null $propertyPath |
||
| 1156 | * @return bool |
||
| 1157 | * @throws \Assert\AssertionFailedException |
||
| 1158 | */ |
||
| 1159 | public static function isInstanceOf($value, $className, $message = null, $propertyPath = null) |
||
| 1173 | |||
| 1174 | /** |
||
| 1175 | * Assert that value is not instance of given class-name. |
||
| 1176 | * |
||
| 1177 | * @param mixed $value |
||
| 1178 | * @param string $className |
||
| 1179 | * @param string|null $message |
||
| 1180 | * @param string|null $propertyPath |
||
| 1181 | * @return bool |
||
| 1182 | * @throws \Assert\AssertionFailedException |
||
| 1183 | */ |
||
| 1184 | public static function notIsInstanceOf($value, $className, $message = null, $propertyPath = null) |
||
| 1198 | |||
| 1199 | /** |
||
| 1200 | * Assert that value is subclass of given class-name. |
||
| 1201 | * |
||
| 1202 | * @param mixed $value |
||
| 1203 | * @param string $className |
||
| 1204 | * @param string|null $message |
||
| 1205 | * @param string|null $propertyPath |
||
| 1206 | * @return bool |
||
| 1207 | * @throws \Assert\AssertionFailedException |
||
| 1208 | */ |
||
| 1209 | public static function subclassOf($value, $className, $message = null, $propertyPath = null) |
||
| 1223 | |||
| 1224 | /** |
||
| 1225 | * Assert that value is in range of numbers. |
||
| 1226 | * |
||
| 1227 | * @param mixed $value |
||
| 1228 | * @param mixed $minValue |
||
| 1229 | * @param mixed $maxValue |
||
| 1230 | * @param string|null $message |
||
| 1231 | * @param string|null $propertyPath |
||
| 1232 | * @return bool |
||
| 1233 | * @throws \Assert\AssertionFailedException |
||
| 1234 | */ |
||
| 1235 | public static function range($value, $minValue, $maxValue, $message = null, $propertyPath = null) |
||
| 1252 | |||
| 1253 | /** |
||
| 1254 | * Assert that a value is at least as big as a given limit |
||
| 1255 | * |
||
| 1256 | * @param mixed $value |
||
| 1257 | * @param mixed $minValue |
||
| 1258 | * @param string|null $message |
||
| 1259 | * @param string|null $propertyPath |
||
| 1260 | * @return bool |
||
| 1261 | * @throws \Assert\AssertionFailedException |
||
| 1262 | */ |
||
| 1263 | public static function min($value, $minValue, $message = null, $propertyPath = null) |
||
| 1279 | |||
| 1280 | /** |
||
| 1281 | * Assert that a number is smaller as a given limit |
||
| 1282 | * |
||
| 1283 | * @param mixed $value |
||
| 1284 | * @param mixed $maxValue |
||
| 1285 | * @param string|null $message |
||
| 1286 | * @param string|null $propertyPath |
||
| 1287 | * @return bool |
||
| 1288 | * @throws \Assert\AssertionFailedException |
||
| 1289 | */ |
||
| 1290 | public static function max($value, $maxValue, $message = null, $propertyPath = null) |
||
| 1306 | |||
| 1307 | /** |
||
| 1308 | * Assert that a file exists |
||
| 1309 | * |
||
| 1310 | * @param string $value |
||
| 1311 | * @param string|null $message |
||
| 1312 | * @param string|null $propertyPath |
||
| 1313 | * @return bool |
||
| 1314 | * @throws \Assert\AssertionFailedException |
||
| 1315 | */ |
||
| 1316 | public static function file($value, $message = null, $propertyPath = null) |
||
| 1332 | |||
| 1333 | /** |
||
| 1334 | * Assert that a directory exists |
||
| 1335 | * |
||
| 1336 | * @param string $value |
||
| 1337 | * @param string|null $message |
||
| 1338 | * @param string|null $propertyPath |
||
| 1339 | * @return bool |
||
| 1340 | * @throws \Assert\AssertionFailedException |
||
| 1341 | */ |
||
| 1342 | public static function directory($value, $message = null, $propertyPath = null) |
||
| 1357 | |||
| 1358 | /** |
||
| 1359 | * Assert that the value is something readable |
||
| 1360 | * |
||
| 1361 | * @param string $value |
||
| 1362 | * @param string|null $message |
||
| 1363 | * @param string|null $propertyPath |
||
| 1364 | * @return bool |
||
| 1365 | * @throws \Assert\AssertionFailedException |
||
| 1366 | */ |
||
| 1367 | public static function readable($value, $message = null, $propertyPath = null) |
||
| 1382 | |||
| 1383 | /** |
||
| 1384 | * Assert that the value is something writeable |
||
| 1385 | * |
||
| 1386 | * @param string $value |
||
| 1387 | * @param string|null $message |
||
| 1388 | * @param string|null $propertyPath |
||
| 1389 | * @return bool |
||
| 1390 | * @throws \Assert\AssertionFailedException |
||
| 1391 | */ |
||
| 1392 | public static function writeable($value, $message = null, $propertyPath = null) |
||
| 1407 | |||
| 1408 | /** |
||
| 1409 | * Assert that value is an email adress (using input_filter/FILTER_VALIDATE_EMAIL). |
||
| 1410 | * |
||
| 1411 | * @param mixed $value |
||
| 1412 | * @param string|null $message |
||
| 1413 | * @param string|null $propertyPath |
||
| 1414 | * @return bool |
||
| 1415 | * @throws \Assert\AssertionFailedException |
||
| 1416 | */ |
||
| 1417 | public static function email($value, $message = null, $propertyPath = null) |
||
| 1444 | |||
| 1445 | /** |
||
| 1446 | * Assert that value is an URL. |
||
| 1447 | * |
||
| 1448 | * This code snipped was taken from the Symfony project and modified to the special demands of this method. |
||
| 1449 | * |
||
| 1450 | * @param mixed $value |
||
| 1451 | * @param string|null $message |
||
| 1452 | * @param string|null $propertyPath |
||
| 1453 | * @return bool |
||
| 1454 | * @throws \Assert\AssertionFailedException |
||
| 1455 | * |
||
| 1456 | * |
||
| 1457 | * @link https://github.com/symfony/Validator/blob/master/Constraints/UrlValidator.php |
||
| 1458 | * @link https://github.com/symfony/Validator/blob/master/Constraints/Url.php |
||
| 1459 | */ |
||
| 1460 | public static function url($value, $message = null, $propertyPath = null) |
||
| 1495 | |||
| 1496 | /** |
||
| 1497 | * Assert that value is alphanumeric. |
||
| 1498 | * |
||
| 1499 | * @param mixed $value |
||
| 1500 | * @param string|null $message |
||
| 1501 | * @param string|null $propertyPath |
||
| 1502 | * @return bool |
||
| 1503 | * @throws \Assert\AssertionFailedException |
||
| 1504 | */ |
||
| 1505 | public static function alnum($value, $message = null, $propertyPath = null) |
||
| 1520 | |||
| 1521 | /** |
||
| 1522 | * Assert that the value is boolean True. |
||
| 1523 | * |
||
| 1524 | * @param mixed $value |
||
| 1525 | * @param string|null $message |
||
| 1526 | * @param string|null $propertyPath |
||
| 1527 | * @return bool |
||
| 1528 | * @throws \Assert\AssertionFailedException |
||
| 1529 | */ |
||
| 1530 | public static function true($value, $message = null, $propertyPath = null) |
||
| 1543 | |||
| 1544 | /** |
||
| 1545 | * Assert that the value is boolean False. |
||
| 1546 | * |
||
| 1547 | * @param mixed $value |
||
| 1548 | * @param string|null $message |
||
| 1549 | * @param string|null $propertyPath |
||
| 1550 | * @return bool |
||
| 1551 | * @throws \Assert\AssertionFailedException |
||
| 1552 | */ |
||
| 1553 | public static function false($value, $message = null, $propertyPath = null) |
||
| 1566 | |||
| 1567 | /** |
||
| 1568 | * Assert that the class exists. |
||
| 1569 | * |
||
| 1570 | * @param mixed $value |
||
| 1571 | * @param string|null $message |
||
| 1572 | * @param string|null $propertyPath |
||
| 1573 | * @return bool |
||
| 1574 | * @throws \Assert\AssertionFailedException |
||
| 1575 | */ |
||
| 1576 | public static function classExists($value, $message = null, $propertyPath = null) |
||
| 1589 | |||
| 1590 | /** |
||
| 1591 | * Assert that the interface exists. |
||
| 1592 | * |
||
| 1593 | * @param mixed $value |
||
| 1594 | * @param string|null $message |
||
| 1595 | * @param string|null $propertyPath |
||
| 1596 | * @return bool |
||
| 1597 | * @throws \Assert\AssertionFailedException |
||
| 1598 | */ |
||
| 1599 | public static function interfaceExists($value, $message = null, $propertyPath = null) |
||
| 1612 | |||
| 1613 | /** |
||
| 1614 | * Assert that the class implements the interface |
||
| 1615 | * |
||
| 1616 | * @param mixed $class |
||
| 1617 | * @param string $interfaceName |
||
| 1618 | * @param string|null $message |
||
| 1619 | * @param string|null $propertyPath |
||
| 1620 | * @return bool |
||
| 1621 | * @throws \Assert\AssertionFailedException |
||
| 1622 | */ |
||
| 1623 | public static function implementsInterface($class, $interfaceName, $message = null, $propertyPath = null) |
||
| 1638 | |||
| 1639 | /** |
||
| 1640 | * Assert that the given string is a valid json string. |
||
| 1641 | * |
||
| 1642 | * NOTICE: |
||
| 1643 | * Since this does a json_decode to determine its validity |
||
| 1644 | * you probably should consider, when using the variable |
||
| 1645 | * content afterwards, just to decode and check for yourself instead |
||
| 1646 | * of using this assertion. |
||
| 1647 | * |
||
| 1648 | * @param mixed $value |
||
| 1649 | * @param string|null $message |
||
| 1650 | * @param string|null $propertyPath |
||
| 1651 | * @return bool |
||
| 1652 | * @throws \Assert\AssertionFailedException |
||
| 1653 | */ |
||
| 1654 | public static function isJsonString($value, $message = null, $propertyPath = null) |
||
| 1667 | |||
| 1668 | /** |
||
| 1669 | * Assert that the given string is a valid UUID |
||
| 1670 | * |
||
| 1671 | * Uses code from {@link https://github.com/ramsey/uuid} that is MIT licensed. |
||
| 1672 | * |
||
| 1673 | * @param string $value |
||
| 1674 | * @param string|null $message |
||
| 1675 | * @param string|null $propertyPath |
||
| 1676 | * @return bool |
||
| 1677 | * @throws \Assert\AssertionFailedException |
||
| 1678 | */ |
||
| 1679 | public static function uuid($value, $message = null, $propertyPath = null) |
||
| 1698 | |||
| 1699 | /** |
||
| 1700 | * Assert that the given string is a valid E164 Phone Number |
||
| 1701 | * |
||
| 1702 | * @link https://en.wikipedia.org/wiki/E.164 |
||
| 1703 | * |
||
| 1704 | * @param string $value |
||
| 1705 | * @param string|null $message |
||
| 1706 | * @param string|null $propertyPath |
||
| 1707 | * @return bool |
||
| 1708 | * @throws \Assert\AssertionFailedException |
||
| 1709 | */ |
||
| 1710 | public static function e164($value, $message = null, $propertyPath = null) |
||
| 1723 | |||
| 1724 | /** |
||
| 1725 | * Assert that the count of countable is equal to count. |
||
| 1726 | * |
||
| 1727 | * @param array|\Countable $countable |
||
| 1728 | * @param int $count |
||
| 1729 | * @param string|null $message |
||
| 1730 | * @param string|null $propertyPath |
||
| 1731 | * @return bool |
||
| 1732 | * @throws \Assert\AssertionFailedException |
||
| 1733 | */ |
||
| 1734 | public static function count($countable, $count, $message = null, $propertyPath = null) |
||
| 1747 | |||
| 1748 | /** |
||
| 1749 | * static call handler to implement: |
||
| 1750 | * - "null or assertion" delegation |
||
| 1751 | * - "all" delegation |
||
| 1752 | * |
||
| 1753 | * @param string $method |
||
| 1754 | * @param array $args |
||
| 1755 | * |
||
| 1756 | * @return bool|mixed |
||
| 1757 | */ |
||
| 1758 | public static function __callStatic($method, $args) |
||
| 1794 | |||
| 1795 | /** |
||
| 1796 | * Determines if the values array has every choice as key and that this choice has content. |
||
| 1797 | * |
||
| 1798 | * @param array $values |
||
| 1799 | * @param array $choices |
||
| 1800 | * @param string|null $message |
||
| 1801 | * @param string|null $propertyPath |
||
| 1802 | * @return bool |
||
| 1803 | */ |
||
| 1804 | public static function choicesNotEmpty(array $values, array $choices, $message = null, $propertyPath = null) |
||
| 1814 | |||
| 1815 | /** |
||
| 1816 | * Determines that the named method is defined in the provided object. |
||
| 1817 | * |
||
| 1818 | * @param string $value |
||
| 1819 | * @param mixed $object |
||
| 1820 | * @param string|null $message |
||
| 1821 | * @param string|null $propertyPath |
||
| 1822 | * @return bool |
||
| 1823 | * @throws |
||
| 1824 | */ |
||
| 1825 | public static function methodExists($value, $object, $message = null, $propertyPath = null) |
||
| 1840 | |||
| 1841 | /** |
||
| 1842 | * Determines that the provided value is an object. |
||
| 1843 | * |
||
| 1844 | * @param mixed $value |
||
| 1845 | * @param string|null $message |
||
| 1846 | * @param string|null $propertyPath |
||
| 1847 | * @return bool |
||
| 1848 | */ |
||
| 1849 | public static function isObject($value, $message = null, $propertyPath = null) |
||
| 1862 | |||
| 1863 | /** |
||
| 1864 | * Determines if the value is less than given limit. |
||
| 1865 | * |
||
| 1866 | * @param mixed $value |
||
| 1867 | * @param mixed $limit |
||
| 1868 | * @param string|null $message |
||
| 1869 | * @param string|null $propertyPath |
||
| 1870 | * @return bool |
||
| 1871 | */ |
||
| 1872 | public static function lessThan($value, $limit, $message = null, $propertyPath = null) |
||
| 1886 | |||
| 1887 | /** |
||
| 1888 | * Determines if the value is less or than given limit. |
||
| 1889 | * |
||
| 1890 | * @param mixed $value |
||
| 1891 | * @param mixed $limit |
||
| 1892 | * @param string|null $message |
||
| 1893 | * @param string|null $propertyPath |
||
| 1894 | * @return bool |
||
| 1895 | */ |
||
| 1896 | public static function lessOrEqualThan($value, $limit, $message = null, $propertyPath = null) |
||
| 1910 | |||
| 1911 | /** |
||
| 1912 | * Determines if the value is greater than given limit. |
||
| 1913 | * |
||
| 1914 | * @param mixed $value |
||
| 1915 | * @param mixed $limit |
||
| 1916 | * @param string|null $message |
||
| 1917 | * @param string|null $propertyPath |
||
| 1918 | * @return bool |
||
| 1919 | */ |
||
| 1920 | public static function greaterThan($value, $limit, $message = null, $propertyPath = null) |
||
| 1934 | |||
| 1935 | /** |
||
| 1936 | * Determines if the value is greater or equal than given limit. |
||
| 1937 | * |
||
| 1938 | * @param mixed $value |
||
| 1939 | * @param mixed $limit |
||
| 1940 | * @param string|null $message |
||
| 1941 | * @param string|null $propertyPath |
||
| 1942 | * @return bool |
||
| 1943 | */ |
||
| 1944 | public static function greaterOrEqualThan($value, $limit, $message = null, $propertyPath = null) |
||
| 1958 | |||
| 1959 | /** |
||
| 1960 | * Assert that a value is greater or equal than a lower limit, and less than or equal to an upper limit. |
||
| 1961 | * |
||
| 1962 | * @param mixed $value |
||
| 1963 | * @param mixed $lowerLimit |
||
| 1964 | * @param mixed $upperLimit |
||
| 1965 | * @param string $message |
||
| 1966 | * @param string $propertyPath |
||
| 1967 | * @return bool |
||
| 1968 | */ |
||
| 1969 | public static function between($value, $lowerLimit, $upperLimit, $message = null, $propertyPath = null) |
||
| 1984 | |||
| 1985 | /** |
||
| 1986 | * Assert that a value is greater than a lower limit, and less than an upper limit. |
||
| 1987 | * |
||
| 1988 | * @param mixed $value |
||
| 1989 | * @param mixed $lowerLimit |
||
| 1990 | * @param mixed $upperLimit |
||
| 1991 | * @param string $message |
||
| 1992 | * @param string $propertyPath |
||
| 1993 | * @return bool |
||
| 1994 | */ |
||
| 1995 | public static function betweenExclusive($value, $lowerLimit, $upperLimit, $message = null, $propertyPath = null) |
||
| 2010 | |||
| 2011 | /** |
||
| 2012 | * Assert that extension is loaded. |
||
| 2013 | * |
||
| 2014 | * @param mixed $value |
||
| 2015 | * @param string|null $message |
||
| 2016 | * @param string|null $propertyPath |
||
| 2017 | * @return bool |
||
| 2018 | * @throws \Assert\AssertionFailedException |
||
| 2019 | */ |
||
| 2020 | public static function extensionLoaded($value, $message = null, $propertyPath = null) |
||
| 2033 | |||
| 2034 | /** |
||
| 2035 | * Assert comparison of two versions. |
||
| 2036 | * |
||
| 2037 | * @param string $version1 |
||
| 2038 | * @param string $operator |
||
| 2039 | * @param string $version2 |
||
| 2040 | * @param string|null $message |
||
| 2041 | * @param string|null $propertyPath |
||
| 2042 | * @return bool |
||
| 2043 | * @throws \Assert\AssertionFailedException |
||
| 2044 | */ |
||
| 2045 | public static function version($version1, $operator, $version2, $message = null, $propertyPath = null) |
||
| 2062 | |||
| 2063 | /** |
||
| 2064 | * Assert on PHP version. |
||
| 2065 | * |
||
| 2066 | * @param string $operator |
||
| 2067 | * @param mixed $version |
||
| 2068 | * @param string|null $message |
||
| 2069 | * @param string|null $propertyPath |
||
| 2070 | * @return bool |
||
| 2071 | * @throws \Assert\AssertionFailedException |
||
| 2072 | */ |
||
| 2073 | public static function phpVersion($operator, $version, $message = null, $propertyPath = null) |
||
| 2079 | |||
| 2080 | /** |
||
| 2081 | * Assert that date is valid and corresponds to the given format. |
||
| 2082 | * |
||
| 2083 | * @param string $value |
||
| 2084 | * @param string $format supports all of the options date(), except for the following: |
||
| 2085 | * N, w, W, t, L, o, B, a, A, g, h, I, O, P, Z, c, r. |
||
| 2086 | * @param string|null $message |
||
| 2087 | * @param string|null $propertyPath |
||
| 2088 | * @return bool |
||
| 2089 | * |
||
| 2090 | * @link http://php.net/manual/function.date.php#refsect1-function.date-parameters |
||
| 2091 | */ |
||
| 2092 | public static function date($value, $format, $message = null, $propertyPath = null) |
||
| 2111 | |||
| 2112 | /** |
||
| 2113 | * Determines that the provided value is callable. |
||
| 2114 | * |
||
| 2115 | * @param mixed $value |
||
| 2116 | * @param string|null $message |
||
| 2117 | * @param string|null $propertyPath |
||
| 2118 | * @return bool |
||
| 2119 | */ |
||
| 2120 | public static function isCallable($value, $message = null, $propertyPath = null) |
||
| 2133 | |||
| 2134 | /** |
||
| 2135 | * Assert that the provided value is valid according to a callback. |
||
| 2136 | * |
||
| 2137 | * If the callback returns `false` the assertion will fail. |
||
| 2138 | * |
||
| 2139 | * @param mixed $value |
||
| 2140 | * @param callable $callback |
||
| 2141 | * @param string|null $message |
||
| 2142 | * @param string|null $propertyPath |
||
| 2143 | * @return bool |
||
| 2144 | */ |
||
| 2145 | public static function satisfy($value, $callback, $message = null, $propertyPath = null) |
||
| 2160 | |||
| 2161 | /** |
||
| 2162 | * Assert that value is an IPv4 or IPv6 address |
||
| 2163 | * (using input_filter/FILTER_VALIDATE_IP). |
||
| 2164 | * |
||
| 2165 | * @param string $value |
||
| 2166 | * @param null|int $flag |
||
| 2167 | * @param string|null $message |
||
| 2168 | * @param string|null $propertyPath |
||
| 2169 | * @return bool |
||
| 2170 | * |
||
| 2171 | * @link http://php.net/manual/filter.filters.flags.php |
||
| 2172 | */ |
||
| 2173 | public static function ip($value, $flag = null, $message = null, $propertyPath = null) |
||
| 2186 | |||
| 2187 | /** |
||
| 2188 | * Assert that value is an IPv4 address |
||
| 2189 | * (using input_filter/FILTER_VALIDATE_IP). |
||
| 2190 | * |
||
| 2191 | * @param string $value |
||
| 2192 | * @param null|int $flag |
||
| 2193 | * @param string|null $message |
||
| 2194 | * @param string|null $propertyPath |
||
| 2195 | * @return bool |
||
| 2196 | * |
||
| 2197 | * @link http://php.net/manual/filter.filters.flags.php |
||
| 2198 | */ |
||
| 2199 | public static function ipv4($value, $flag = null, $message = null, $propertyPath = null) |
||
| 2205 | |||
| 2206 | /** |
||
| 2207 | * Assert that value is an IPv6 address |
||
| 2208 | * (using input_filter/FILTER_VALIDATE_IP). |
||
| 2209 | * |
||
| 2210 | * @param string $value |
||
| 2211 | * @param null|int $flag |
||
| 2212 | * @param string|null $message |
||
| 2213 | * @param string|null $propertyPath |
||
| 2214 | * @return bool |
||
| 2215 | * |
||
| 2216 | * @link http://php.net/manual/filter.filters.flags.php |
||
| 2217 | */ |
||
| 2218 | public static function ipv6($value, $flag = null, $message = null, $propertyPath = null) |
||
| 2224 | |||
| 2225 | /** |
||
| 2226 | * Make a string version of a value. |
||
| 2227 | * |
||
| 2228 | * @param mixed $value |
||
| 2229 | * @return string |
||
| 2230 | */ |
||
| 2231 | protected static function stringify($value) |
||
| 2265 | |||
| 2266 | /** |
||
| 2267 | * Assert that a constant is defined. |
||
| 2268 | * |
||
| 2269 | * @param mixed $constant |
||
| 2270 | * @param string|null $message |
||
| 2271 | * @param string|null $propertyPath |
||
| 2272 | * @return bool |
||
| 2273 | * @throws \Assert\AssertionFailedException |
||
| 2274 | */ |
||
| 2275 | public static function defined($constant, $message = null, $propertyPath = null) |
||
| 2285 | } |
||
| 2286 |
This check looks for the generic type
arrayas a return type and suggests a more specific type. This type is inferred from the actual code.