| 1: | <?php | 
| 2: |  | 
| 3: |  | 
| 4: |  | 
| 5: |  | 
| 6: |  | 
| 7: |  | 
| 8: |  | 
| 9: |  | 
| 10: |  | 
| 11: |  | 
| 12: | namespace Webmozart\Assert; | 
| 13: |  | 
| 14: | use ArrayAccess; | 
| 15: | use BadMethodCallException; | 
| 16: | use Closure; | 
| 17: | use Countable; | 
| 18: | use DateTime; | 
| 19: | use DateTimeImmutable; | 
| 20: | use Exception; | 
| 21: | use InvalidArgumentException; | 
| 22: | use ResourceBundle; | 
| 23: | use SimpleXMLElement; | 
| 24: | use Throwable; | 
| 25: | use Traversable; | 
| 26: |  | 
| 27: |  | 
| 28: |  | 
| 29: |  | 
| 30: |  | 
| 31: |  | 
| 32: |  | 
| 33: |  | 
| 34: |  | 
| 35: |  | 
| 36: | class Assert | 
| 37: | { | 
| 38: |  | 
| 39: |  | 
| 40: |  | 
| 41: |  | 
| 42: |  | 
| 43: |  | 
| 44: |  | 
| 45: |  | 
| 46: |  | 
| 47: | public static function string($value, $message = '') | 
| 48: | { | 
| 49: | if (!\is_string($value)) { | 
| 50: | static::reportInvalidArgument(\sprintf( | 
| 51: | $message ?: 'Expected a string. Got: %s', | 
| 52: | static::typeToString($value) | 
| 53: | )); | 
| 54: | } | 
| 55: | } | 
| 56: |  | 
| 57: |  | 
| 58: |  | 
| 59: |  | 
| 60: |  | 
| 61: |  | 
| 62: |  | 
| 63: |  | 
| 64: |  | 
| 65: |  | 
| 66: | public static function stringNotEmpty($value, $message = '') | 
| 67: | { | 
| 68: | static::string($value, $message); | 
| 69: | static::notEq($value, '', $message); | 
| 70: | } | 
| 71: |  | 
| 72: |  | 
| 73: |  | 
| 74: |  | 
| 75: |  | 
| 76: |  | 
| 77: |  | 
| 78: |  | 
| 79: |  | 
| 80: |  | 
| 81: | public static function integer($value, $message = '') | 
| 82: | { | 
| 83: | if (!\is_int($value)) { | 
| 84: | static::reportInvalidArgument(\sprintf( | 
| 85: | $message ?: 'Expected an integer. Got: %s', | 
| 86: | static::typeToString($value) | 
| 87: | )); | 
| 88: | } | 
| 89: | } | 
| 90: |  | 
| 91: |  | 
| 92: |  | 
| 93: |  | 
| 94: |  | 
| 95: |  | 
| 96: |  | 
| 97: |  | 
| 98: |  | 
| 99: |  | 
| 100: | public static function integerish($value, $message = '') | 
| 101: | { | 
| 102: | if (!\is_numeric($value) || $value != (int) $value) { | 
| 103: | static::reportInvalidArgument(\sprintf( | 
| 104: | $message ?: 'Expected an integerish value. Got: %s', | 
| 105: | static::typeToString($value) | 
| 106: | )); | 
| 107: | } | 
| 108: | } | 
| 109: |  | 
| 110: |  | 
| 111: |  | 
| 112: |  | 
| 113: |  | 
| 114: |  | 
| 115: |  | 
| 116: |  | 
| 117: |  | 
| 118: |  | 
| 119: | public static function float($value, $message = '') | 
| 120: | { | 
| 121: | if (!\is_float($value)) { | 
| 122: | static::reportInvalidArgument(\sprintf( | 
| 123: | $message ?: 'Expected a float. Got: %s', | 
| 124: | static::typeToString($value) | 
| 125: | )); | 
| 126: | } | 
| 127: | } | 
| 128: |  | 
| 129: |  | 
| 130: |  | 
| 131: |  | 
| 132: |  | 
| 133: |  | 
| 134: |  | 
| 135: |  | 
| 136: |  | 
| 137: |  | 
| 138: | public static function numeric($value, $message = '') | 
| 139: | { | 
| 140: | if (!\is_numeric($value)) { | 
| 141: | static::reportInvalidArgument(\sprintf( | 
| 142: | $message ?: 'Expected a numeric. Got: %s', | 
| 143: | static::typeToString($value) | 
| 144: | )); | 
| 145: | } | 
| 146: | } | 
| 147: |  | 
| 148: |  | 
| 149: |  | 
| 150: |  | 
| 151: |  | 
| 152: |  | 
| 153: |  | 
| 154: |  | 
| 155: |  | 
| 156: |  | 
| 157: | public static function natural($value, $message = '') | 
| 158: | { | 
| 159: | if (!\is_int($value) || $value < 0) { | 
| 160: | static::reportInvalidArgument(\sprintf( | 
| 161: | $message ?: 'Expected a non-negative integer. Got: %s', | 
| 162: | static::valueToString($value) | 
| 163: | )); | 
| 164: | } | 
| 165: | } | 
| 166: |  | 
| 167: |  | 
| 168: |  | 
| 169: |  | 
| 170: |  | 
| 171: |  | 
| 172: |  | 
| 173: |  | 
| 174: |  | 
| 175: |  | 
| 176: | public static function boolean($value, $message = '') | 
| 177: | { | 
| 178: | if (!\is_bool($value)) { | 
| 179: | static::reportInvalidArgument(\sprintf( | 
| 180: | $message ?: 'Expected a boolean. Got: %s', | 
| 181: | static::typeToString($value) | 
| 182: | )); | 
| 183: | } | 
| 184: | } | 
| 185: |  | 
| 186: |  | 
| 187: |  | 
| 188: |  | 
| 189: |  | 
| 190: |  | 
| 191: |  | 
| 192: |  | 
| 193: |  | 
| 194: |  | 
| 195: | public static function scalar($value, $message = '') | 
| 196: | { | 
| 197: | if (!\is_scalar($value)) { | 
| 198: | static::reportInvalidArgument(\sprintf( | 
| 199: | $message ?: 'Expected a scalar. Got: %s', | 
| 200: | static::typeToString($value) | 
| 201: | )); | 
| 202: | } | 
| 203: | } | 
| 204: |  | 
| 205: |  | 
| 206: |  | 
| 207: |  | 
| 208: |  | 
| 209: |  | 
| 210: |  | 
| 211: |  | 
| 212: |  | 
| 213: |  | 
| 214: | public static function object($value, $message = '') | 
| 215: | { | 
| 216: | if (!\is_object($value)) { | 
| 217: | static::reportInvalidArgument(\sprintf( | 
| 218: | $message ?: 'Expected an object. Got: %s', | 
| 219: | static::typeToString($value) | 
| 220: | )); | 
| 221: | } | 
| 222: | } | 
| 223: |  | 
| 224: |  | 
| 225: |  | 
| 226: |  | 
| 227: |  | 
| 228: |  | 
| 229: |  | 
| 230: |  | 
| 231: |  | 
| 232: |  | 
| 233: |  | 
| 234: | public static function resource($value, $type = null, $message = '') | 
| 235: | { | 
| 236: | if (!\is_resource($value)) { | 
| 237: | static::reportInvalidArgument(\sprintf( | 
| 238: | $message ?: 'Expected a resource. Got: %s', | 
| 239: | static::typeToString($value) | 
| 240: | )); | 
| 241: | } | 
| 242: |  | 
| 243: | if ($type && $type !== \get_resource_type($value)) { | 
| 244: | static::reportInvalidArgument(\sprintf( | 
| 245: | $message ?: 'Expected a resource of type %2$s. Got: %s', | 
| 246: | static::typeToString($value), | 
| 247: | $type | 
| 248: | )); | 
| 249: | } | 
| 250: | } | 
| 251: |  | 
| 252: |  | 
| 253: |  | 
| 254: |  | 
| 255: |  | 
| 256: |  | 
| 257: |  | 
| 258: |  | 
| 259: |  | 
| 260: |  | 
| 261: | public static function isCallable($value, $message = '') | 
| 262: | { | 
| 263: | if (!\is_callable($value)) { | 
| 264: | static::reportInvalidArgument(\sprintf( | 
| 265: | $message ?: 'Expected a callable. Got: %s', | 
| 266: | static::typeToString($value) | 
| 267: | )); | 
| 268: | } | 
| 269: | } | 
| 270: |  | 
| 271: |  | 
| 272: |  | 
| 273: |  | 
| 274: |  | 
| 275: |  | 
| 276: |  | 
| 277: |  | 
| 278: |  | 
| 279: |  | 
| 280: | public static function isArray($value, $message = '') | 
| 281: | { | 
| 282: | if (!\is_array($value)) { | 
| 283: | static::reportInvalidArgument(\sprintf( | 
| 284: | $message ?: 'Expected an array. Got: %s', | 
| 285: | static::typeToString($value) | 
| 286: | )); | 
| 287: | } | 
| 288: | } | 
| 289: |  | 
| 290: |  | 
| 291: |  | 
| 292: |  | 
| 293: |  | 
| 294: |  | 
| 295: |  | 
| 296: |  | 
| 297: |  | 
| 298: |  | 
| 299: |  | 
| 300: |  | 
| 301: | public static function isTraversable($value, $message = '') | 
| 302: | { | 
| 303: | @\trigger_error( | 
| 304: | \sprintf( | 
| 305: | 'The "%s" assertion is deprecated. You should stop using it, as it will soon be removed in 2.0 version. Use "isIterable" or "isInstanceOf" instead.', | 
| 306: | __METHOD__ | 
| 307: | ), | 
| 308: | \E_USER_DEPRECATED | 
| 309: | ); | 
| 310: |  | 
| 311: | if (!\is_array($value) && !($value instanceof Traversable)) { | 
| 312: | static::reportInvalidArgument(\sprintf( | 
| 313: | $message ?: 'Expected a traversable. Got: %s', | 
| 314: | static::typeToString($value) | 
| 315: | )); | 
| 316: | } | 
| 317: | } | 
| 318: |  | 
| 319: |  | 
| 320: |  | 
| 321: |  | 
| 322: |  | 
| 323: |  | 
| 324: |  | 
| 325: |  | 
| 326: |  | 
| 327: |  | 
| 328: | public static function isArrayAccessible($value, $message = '') | 
| 329: | { | 
| 330: | if (!\is_array($value) && !($value instanceof ArrayAccess)) { | 
| 331: | static::reportInvalidArgument(\sprintf( | 
| 332: | $message ?: 'Expected an array accessible. Got: %s', | 
| 333: | static::typeToString($value) | 
| 334: | )); | 
| 335: | } | 
| 336: | } | 
| 337: |  | 
| 338: |  | 
| 339: |  | 
| 340: |  | 
| 341: |  | 
| 342: |  | 
| 343: |  | 
| 344: |  | 
| 345: |  | 
| 346: |  | 
| 347: | public static function isCountable($value, $message = '') | 
| 348: | { | 
| 349: | if ( | 
| 350: | !\is_array($value) | 
| 351: | && !($value instanceof Countable) | 
| 352: | && !($value instanceof ResourceBundle) | 
| 353: | && !($value instanceof SimpleXMLElement) | 
| 354: | ) { | 
| 355: | static::reportInvalidArgument(\sprintf( | 
| 356: | $message ?: 'Expected a countable. Got: %s', | 
| 357: | static::typeToString($value) | 
| 358: | )); | 
| 359: | } | 
| 360: | } | 
| 361: |  | 
| 362: |  | 
| 363: |  | 
| 364: |  | 
| 365: |  | 
| 366: |  | 
| 367: |  | 
| 368: |  | 
| 369: |  | 
| 370: |  | 
| 371: | public static function isIterable($value, $message = '') | 
| 372: | { | 
| 373: | if (!\is_array($value) && !($value instanceof Traversable)) { | 
| 374: | static::reportInvalidArgument(\sprintf( | 
| 375: | $message ?: 'Expected an iterable. Got: %s', | 
| 376: | static::typeToString($value) | 
| 377: | )); | 
| 378: | } | 
| 379: | } | 
| 380: |  | 
| 381: |  | 
| 382: |  | 
| 383: |  | 
| 384: |  | 
| 385: |  | 
| 386: |  | 
| 387: |  | 
| 388: |  | 
| 389: |  | 
| 390: |  | 
| 391: |  | 
| 392: |  | 
| 393: | public static function isInstanceOf($value, $class, $message = '') | 
| 394: | { | 
| 395: | if (!($value instanceof $class)) { | 
| 396: | static::reportInvalidArgument(\sprintf( | 
| 397: | $message ?: 'Expected an instance of %2$s. Got: %s', | 
| 398: | static::typeToString($value), | 
| 399: | $class | 
| 400: | )); | 
| 401: | } | 
| 402: | } | 
| 403: |  | 
| 404: |  | 
| 405: |  | 
| 406: |  | 
| 407: |  | 
| 408: |  | 
| 409: |  | 
| 410: |  | 
| 411: |  | 
| 412: |  | 
| 413: |  | 
| 414: |  | 
| 415: |  | 
| 416: | public static function notInstanceOf($value, $class, $message = '') | 
| 417: | { | 
| 418: | if ($value instanceof $class) { | 
| 419: | static::reportInvalidArgument(\sprintf( | 
| 420: | $message ?: 'Expected an instance other than %2$s. Got: %s', | 
| 421: | static::typeToString($value), | 
| 422: | $class | 
| 423: | )); | 
| 424: | } | 
| 425: | } | 
| 426: |  | 
| 427: |  | 
| 428: |  | 
| 429: |  | 
| 430: |  | 
| 431: |  | 
| 432: |  | 
| 433: |  | 
| 434: |  | 
| 435: |  | 
| 436: |  | 
| 437: | public static function isInstanceOfAny($value, array $classes, $message = '') | 
| 438: | { | 
| 439: | foreach ($classes as $class) { | 
| 440: | if ($value instanceof $class) { | 
| 441: | return; | 
| 442: | } | 
| 443: | } | 
| 444: |  | 
| 445: | static::reportInvalidArgument(\sprintf( | 
| 446: | $message ?: 'Expected an instance of any of %2$s. Got: %s', | 
| 447: | static::typeToString($value), | 
| 448: | \implode(', ', \array_map(array('static', 'valueToString'), $classes)) | 
| 449: | )); | 
| 450: | } | 
| 451: |  | 
| 452: |  | 
| 453: |  | 
| 454: |  | 
| 455: |  | 
| 456: |  | 
| 457: |  | 
| 458: |  | 
| 459: |  | 
| 460: |  | 
| 461: |  | 
| 462: |  | 
| 463: |  | 
| 464: | public static function isAOf($value, $class, $message = '') | 
| 465: | { | 
| 466: | static::string($class, 'Expected class as a string. Got: %s'); | 
| 467: |  | 
| 468: | if (!\is_a($value, $class, \is_string($value))) { | 
| 469: | static::reportInvalidArgument(sprintf( | 
| 470: | $message ?: 'Expected an instance of this class or to this class among his parents %2$s. Got: %s', | 
| 471: | static::typeToString($value), | 
| 472: | $class | 
| 473: | )); | 
| 474: | } | 
| 475: | } | 
| 476: |  | 
| 477: |  | 
| 478: |  | 
| 479: |  | 
| 480: |  | 
| 481: |  | 
| 482: |  | 
| 483: |  | 
| 484: |  | 
| 485: |  | 
| 486: |  | 
| 487: |  | 
| 488: |  | 
| 489: |  | 
| 490: | public static function isNotA($value, $class, $message = '') | 
| 491: | { | 
| 492: | static::string($class, 'Expected class as a string. Got: %s'); | 
| 493: |  | 
| 494: | if (\is_a($value, $class, \is_string($value))) { | 
| 495: | static::reportInvalidArgument(sprintf( | 
| 496: | $message ?: 'Expected an instance of this class or to this class among his parents other than %2$s. Got: %s', | 
| 497: | static::typeToString($value), | 
| 498: | $class | 
| 499: | )); | 
| 500: | } | 
| 501: | } | 
| 502: |  | 
| 503: |  | 
| 504: |  | 
| 505: |  | 
| 506: |  | 
| 507: |  | 
| 508: |  | 
| 509: |  | 
| 510: |  | 
| 511: |  | 
| 512: |  | 
| 513: | public static function isAnyOf($value, array $classes, $message = '') | 
| 514: | { | 
| 515: | foreach ($classes as $class) { | 
| 516: | static::string($class, 'Expected class as a string. Got: %s'); | 
| 517: |  | 
| 518: | if (\is_a($value, $class, \is_string($value))) { | 
| 519: | return; | 
| 520: | } | 
| 521: | } | 
| 522: |  | 
| 523: | static::reportInvalidArgument(sprintf( | 
| 524: | $message ?: 'Expected an any of instance of this class or to this class among his parents other than %2$s. Got: %s', | 
| 525: | static::typeToString($value), | 
| 526: | \implode(', ', \array_map(array('static', 'valueToString'), $classes)) | 
| 527: | )); | 
| 528: | } | 
| 529: |  | 
| 530: |  | 
| 531: |  | 
| 532: |  | 
| 533: |  | 
| 534: |  | 
| 535: |  | 
| 536: |  | 
| 537: |  | 
| 538: |  | 
| 539: | public static function isEmpty($value, $message = '') | 
| 540: | { | 
| 541: | if (!empty($value)) { | 
| 542: | static::reportInvalidArgument(\sprintf( | 
| 543: | $message ?: 'Expected an empty value. Got: %s', | 
| 544: | static::valueToString($value) | 
| 545: | )); | 
| 546: | } | 
| 547: | } | 
| 548: |  | 
| 549: |  | 
| 550: |  | 
| 551: |  | 
| 552: |  | 
| 553: |  | 
| 554: |  | 
| 555: |  | 
| 556: |  | 
| 557: |  | 
| 558: | public static function notEmpty($value, $message = '') | 
| 559: | { | 
| 560: | if (empty($value)) { | 
| 561: | static::reportInvalidArgument(\sprintf( | 
| 562: | $message ?: 'Expected a non-empty value. Got: %s', | 
| 563: | static::valueToString($value) | 
| 564: | )); | 
| 565: | } | 
| 566: | } | 
| 567: |  | 
| 568: |  | 
| 569: |  | 
| 570: |  | 
| 571: |  | 
| 572: |  | 
| 573: |  | 
| 574: |  | 
| 575: |  | 
| 576: |  | 
| 577: | public static function null($value, $message = '') | 
| 578: | { | 
| 579: | if (null !== $value) { | 
| 580: | static::reportInvalidArgument(\sprintf( | 
| 581: | $message ?: 'Expected null. Got: %s', | 
| 582: | static::valueToString($value) | 
| 583: | )); | 
| 584: | } | 
| 585: | } | 
| 586: |  | 
| 587: |  | 
| 588: |  | 
| 589: |  | 
| 590: |  | 
| 591: |  | 
| 592: |  | 
| 593: |  | 
| 594: |  | 
| 595: |  | 
| 596: | public static function notNull($value, $message = '') | 
| 597: | { | 
| 598: | if (null === $value) { | 
| 599: | static::reportInvalidArgument( | 
| 600: | $message ?: 'Expected a value other than null.' | 
| 601: | ); | 
| 602: | } | 
| 603: | } | 
| 604: |  | 
| 605: |  | 
| 606: |  | 
| 607: |  | 
| 608: |  | 
| 609: |  | 
| 610: |  | 
| 611: |  | 
| 612: |  | 
| 613: |  | 
| 614: | public static function true($value, $message = '') | 
| 615: | { | 
| 616: | if (true !== $value) { | 
| 617: | static::reportInvalidArgument(\sprintf( | 
| 618: | $message ?: 'Expected a value to be true. Got: %s', | 
| 619: | static::valueToString($value) | 
| 620: | )); | 
| 621: | } | 
| 622: | } | 
| 623: |  | 
| 624: |  | 
| 625: |  | 
| 626: |  | 
| 627: |  | 
| 628: |  | 
| 629: |  | 
| 630: |  | 
| 631: |  | 
| 632: |  | 
| 633: | public static function false($value, $message = '') | 
| 634: | { | 
| 635: | if (false !== $value) { | 
| 636: | static::reportInvalidArgument(\sprintf( | 
| 637: | $message ?: 'Expected a value to be false. Got: %s', | 
| 638: | static::valueToString($value) | 
| 639: | )); | 
| 640: | } | 
| 641: | } | 
| 642: |  | 
| 643: |  | 
| 644: |  | 
| 645: |  | 
| 646: |  | 
| 647: |  | 
| 648: |  | 
| 649: |  | 
| 650: |  | 
| 651: |  | 
| 652: | public static function notFalse($value, $message = '') | 
| 653: | { | 
| 654: | if (false === $value) { | 
| 655: | static::reportInvalidArgument( | 
| 656: | $message ?: 'Expected a value other than false.' | 
| 657: | ); | 
| 658: | } | 
| 659: | } | 
| 660: |  | 
| 661: |  | 
| 662: |  | 
| 663: |  | 
| 664: |  | 
| 665: |  | 
| 666: |  | 
| 667: | public static function ip($value, $message = '') | 
| 668: | { | 
| 669: | if (false === \filter_var($value, \FILTER_VALIDATE_IP)) { | 
| 670: | static::reportInvalidArgument(\sprintf( | 
| 671: | $message ?: 'Expected a value to be an IP. Got: %s', | 
| 672: | static::valueToString($value) | 
| 673: | )); | 
| 674: | } | 
| 675: | } | 
| 676: |  | 
| 677: |  | 
| 678: |  | 
| 679: |  | 
| 680: |  | 
| 681: |  | 
| 682: |  | 
| 683: | public static function ipv4($value, $message = '') | 
| 684: | { | 
| 685: | if (false === \filter_var($value, \FILTER_VALIDATE_IP, \FILTER_FLAG_IPV4)) { | 
| 686: | static::reportInvalidArgument(\sprintf( | 
| 687: | $message ?: 'Expected a value to be an IPv4. Got: %s', | 
| 688: | static::valueToString($value) | 
| 689: | )); | 
| 690: | } | 
| 691: | } | 
| 692: |  | 
| 693: |  | 
| 694: |  | 
| 695: |  | 
| 696: |  | 
| 697: |  | 
| 698: |  | 
| 699: | public static function ipv6($value, $message = '') | 
| 700: | { | 
| 701: | if (false === \filter_var($value, \FILTER_VALIDATE_IP, \FILTER_FLAG_IPV6)) { | 
| 702: | static::reportInvalidArgument(\sprintf( | 
| 703: | $message ?: 'Expected a value to be an IPv6. Got: %s', | 
| 704: | static::valueToString($value) | 
| 705: | )); | 
| 706: | } | 
| 707: | } | 
| 708: |  | 
| 709: |  | 
| 710: |  | 
| 711: |  | 
| 712: |  | 
| 713: |  | 
| 714: |  | 
| 715: | public static function email($value, $message = '') | 
| 716: | { | 
| 717: | if (false === \filter_var($value, FILTER_VALIDATE_EMAIL)) { | 
| 718: | static::reportInvalidArgument(\sprintf( | 
| 719: | $message ?: 'Expected a value to be a valid e-mail address. Got: %s', | 
| 720: | static::valueToString($value) | 
| 721: | )); | 
| 722: | } | 
| 723: | } | 
| 724: |  | 
| 725: |  | 
| 726: |  | 
| 727: |  | 
| 728: |  | 
| 729: |  | 
| 730: |  | 
| 731: |  | 
| 732: |  | 
| 733: | public static function uniqueValues(array $values, $message = '') | 
| 734: | { | 
| 735: | $allValues = \count($values); | 
| 736: | $uniqueValues = \count(\array_unique($values)); | 
| 737: |  | 
| 738: | if ($allValues !== $uniqueValues) { | 
| 739: | $difference = $allValues - $uniqueValues; | 
| 740: |  | 
| 741: | static::reportInvalidArgument(\sprintf( | 
| 742: | $message ?: 'Expected an array of unique values, but %s of them %s duplicated', | 
| 743: | $difference, | 
| 744: | (1 === $difference ? 'is' : 'are') | 
| 745: | )); | 
| 746: | } | 
| 747: | } | 
| 748: |  | 
| 749: |  | 
| 750: |  | 
| 751: |  | 
| 752: |  | 
| 753: |  | 
| 754: |  | 
| 755: |  | 
| 756: | public static function eq($value, $expect, $message = '') | 
| 757: | { | 
| 758: | if ($expect != $value) { | 
| 759: | static::reportInvalidArgument(\sprintf( | 
| 760: | $message ?: 'Expected a value equal to %2$s. Got: %s', | 
| 761: | static::valueToString($value), | 
| 762: | static::valueToString($expect) | 
| 763: | )); | 
| 764: | } | 
| 765: | } | 
| 766: |  | 
| 767: |  | 
| 768: |  | 
| 769: |  | 
| 770: |  | 
| 771: |  | 
| 772: |  | 
| 773: |  | 
| 774: | public static function notEq($value, $expect, $message = '') | 
| 775: | { | 
| 776: | if ($expect == $value) { | 
| 777: | static::reportInvalidArgument(\sprintf( | 
| 778: | $message ?: 'Expected a different value than %s.', | 
| 779: | static::valueToString($expect) | 
| 780: | )); | 
| 781: | } | 
| 782: | } | 
| 783: |  | 
| 784: |  | 
| 785: |  | 
| 786: |  | 
| 787: |  | 
| 788: |  | 
| 789: |  | 
| 790: |  | 
| 791: |  | 
| 792: |  | 
| 793: | public static function same($value, $expect, $message = '') | 
| 794: | { | 
| 795: | if ($expect !== $value) { | 
| 796: | static::reportInvalidArgument(\sprintf( | 
| 797: | $message ?: 'Expected a value identical to %2$s. Got: %s', | 
| 798: | static::valueToString($value), | 
| 799: | static::valueToString($expect) | 
| 800: | )); | 
| 801: | } | 
| 802: | } | 
| 803: |  | 
| 804: |  | 
| 805: |  | 
| 806: |  | 
| 807: |  | 
| 808: |  | 
| 809: |  | 
| 810: |  | 
| 811: |  | 
| 812: |  | 
| 813: | public static function notSame($value, $expect, $message = '') | 
| 814: | { | 
| 815: | if ($expect === $value) { | 
| 816: | static::reportInvalidArgument(\sprintf( | 
| 817: | $message ?: 'Expected a value not identical to %s.', | 
| 818: | static::valueToString($expect) | 
| 819: | )); | 
| 820: | } | 
| 821: | } | 
| 822: |  | 
| 823: |  | 
| 824: |  | 
| 825: |  | 
| 826: |  | 
| 827: |  | 
| 828: |  | 
| 829: |  | 
| 830: |  | 
| 831: |  | 
| 832: | public static function greaterThan($value, $limit, $message = '') | 
| 833: | { | 
| 834: | if ($value <= $limit) { | 
| 835: | static::reportInvalidArgument(\sprintf( | 
| 836: | $message ?: 'Expected a value greater than %2$s. Got: %s', | 
| 837: | static::valueToString($value), | 
| 838: | static::valueToString($limit) | 
| 839: | )); | 
| 840: | } | 
| 841: | } | 
| 842: |  | 
| 843: |  | 
| 844: |  | 
| 845: |  | 
| 846: |  | 
| 847: |  | 
| 848: |  | 
| 849: |  | 
| 850: |  | 
| 851: |  | 
| 852: | public static function greaterThanEq($value, $limit, $message = '') | 
| 853: | { | 
| 854: | if ($value < $limit) { | 
| 855: | static::reportInvalidArgument(\sprintf( | 
| 856: | $message ?: 'Expected a value greater than or equal to %2$s. Got: %s', | 
| 857: | static::valueToString($value), | 
| 858: | static::valueToString($limit) | 
| 859: | )); | 
| 860: | } | 
| 861: | } | 
| 862: |  | 
| 863: |  | 
| 864: |  | 
| 865: |  | 
| 866: |  | 
| 867: |  | 
| 868: |  | 
| 869: |  | 
| 870: |  | 
| 871: |  | 
| 872: | public static function lessThan($value, $limit, $message = '') | 
| 873: | { | 
| 874: | if ($value >= $limit) { | 
| 875: | static::reportInvalidArgument(\sprintf( | 
| 876: | $message ?: 'Expected a value less than %2$s. Got: %s', | 
| 877: | static::valueToString($value), | 
| 878: | static::valueToString($limit) | 
| 879: | )); | 
| 880: | } | 
| 881: | } | 
| 882: |  | 
| 883: |  | 
| 884: |  | 
| 885: |  | 
| 886: |  | 
| 887: |  | 
| 888: |  | 
| 889: |  | 
| 890: |  | 
| 891: |  | 
| 892: | public static function lessThanEq($value, $limit, $message = '') | 
| 893: | { | 
| 894: | if ($value > $limit) { | 
| 895: | static::reportInvalidArgument(\sprintf( | 
| 896: | $message ?: 'Expected a value less than or equal to %2$s. Got: %s', | 
| 897: | static::valueToString($value), | 
| 898: | static::valueToString($limit) | 
| 899: | )); | 
| 900: | } | 
| 901: | } | 
| 902: |  | 
| 903: |  | 
| 904: |  | 
| 905: |  | 
| 906: |  | 
| 907: |  | 
| 908: |  | 
| 909: |  | 
| 910: |  | 
| 911: |  | 
| 912: |  | 
| 913: |  | 
| 914: |  | 
| 915: | public static function range($value, $min, $max, $message = '') | 
| 916: | { | 
| 917: | if ($value < $min || $value > $max) { | 
| 918: | static::reportInvalidArgument(\sprintf( | 
| 919: | $message ?: 'Expected a value between %2$s and %3$s. Got: %s', | 
| 920: | static::valueToString($value), | 
| 921: | static::valueToString($min), | 
| 922: | static::valueToString($max) | 
| 923: | )); | 
| 924: | } | 
| 925: | } | 
| 926: |  | 
| 927: |  | 
| 928: |  | 
| 929: |  | 
| 930: |  | 
| 931: |  | 
| 932: |  | 
| 933: |  | 
| 934: |  | 
| 935: |  | 
| 936: |  | 
| 937: |  | 
| 938: | public static function oneOf($value, array $values, $message = '') | 
| 939: | { | 
| 940: | static::inArray($value, $values, $message); | 
| 941: | } | 
| 942: |  | 
| 943: |  | 
| 944: |  | 
| 945: |  | 
| 946: |  | 
| 947: |  | 
| 948: |  | 
| 949: |  | 
| 950: |  | 
| 951: |  | 
| 952: |  | 
| 953: |  | 
| 954: | public static function inArray($value, array $values, $message = '') | 
| 955: | { | 
| 956: | if (!\in_array($value, $values, true)) { | 
| 957: | static::reportInvalidArgument(\sprintf( | 
| 958: | $message ?: 'Expected one of: %2$s. Got: %s', | 
| 959: | static::valueToString($value), | 
| 960: | \implode(', ', \array_map(array('static', 'valueToString'), $values)) | 
| 961: | )); | 
| 962: | } | 
| 963: | } | 
| 964: |  | 
| 965: |  | 
| 966: |  | 
| 967: |  | 
| 968: |  | 
| 969: |  | 
| 970: |  | 
| 971: |  | 
| 972: |  | 
| 973: |  | 
| 974: | public static function contains($value, $subString, $message = '') | 
| 975: | { | 
| 976: | if (false === \strpos($value, $subString)) { | 
| 977: | static::reportInvalidArgument(\sprintf( | 
| 978: | $message ?: 'Expected a value to contain %2$s. Got: %s', | 
| 979: | static::valueToString($value), | 
| 980: | static::valueToString($subString) | 
| 981: | )); | 
| 982: | } | 
| 983: | } | 
| 984: |  | 
| 985: |  | 
| 986: |  | 
| 987: |  | 
| 988: |  | 
| 989: |  | 
| 990: |  | 
| 991: |  | 
| 992: |  | 
| 993: |  | 
| 994: | public static function notContains($value, $subString, $message = '') | 
| 995: | { | 
| 996: | if (false !== \strpos($value, $subString)) { | 
| 997: | static::reportInvalidArgument(\sprintf( | 
| 998: | $message ?: '%2$s was not expected to be contained in a value. Got: %s', | 
| 999: | static::valueToString($value), | 
| 1000: | static::valueToString($subString) | 
| 1001: | )); | 
| 1002: | } | 
| 1003: | } | 
| 1004: |  | 
| 1005: |  | 
| 1006: |  | 
| 1007: |  | 
| 1008: |  | 
| 1009: |  | 
| 1010: |  | 
| 1011: |  | 
| 1012: |  | 
| 1013: | public static function notWhitespaceOnly($value, $message = '') | 
| 1014: | { | 
| 1015: | if (\preg_match('/^\s*$/', $value)) { | 
| 1016: | static::reportInvalidArgument(\sprintf( | 
| 1017: | $message ?: 'Expected a non-whitespace string. Got: %s', | 
| 1018: | static::valueToString($value) | 
| 1019: | )); | 
| 1020: | } | 
| 1021: | } | 
| 1022: |  | 
| 1023: |  | 
| 1024: |  | 
| 1025: |  | 
| 1026: |  | 
| 1027: |  | 
| 1028: |  | 
| 1029: |  | 
| 1030: |  | 
| 1031: |  | 
| 1032: | public static function startsWith($value, $prefix, $message = '') | 
| 1033: | { | 
| 1034: | if (0 !== \strpos($value, $prefix)) { | 
| 1035: | static::reportInvalidArgument(\sprintf( | 
| 1036: | $message ?: 'Expected a value to start with %2$s. Got: %s', | 
| 1037: | static::valueToString($value), | 
| 1038: | static::valueToString($prefix) | 
| 1039: | )); | 
| 1040: | } | 
| 1041: | } | 
| 1042: |  | 
| 1043: |  | 
| 1044: |  | 
| 1045: |  | 
| 1046: |  | 
| 1047: |  | 
| 1048: |  | 
| 1049: |  | 
| 1050: |  | 
| 1051: |  | 
| 1052: | public static function notStartsWith($value, $prefix, $message = '') | 
| 1053: | { | 
| 1054: | if (0 === \strpos($value, $prefix)) { | 
| 1055: | static::reportInvalidArgument(\sprintf( | 
| 1056: | $message ?: 'Expected a value not to start with %2$s. Got: %s', | 
| 1057: | static::valueToString($value), | 
| 1058: | static::valueToString($prefix) | 
| 1059: | )); | 
| 1060: | } | 
| 1061: | } | 
| 1062: |  | 
| 1063: |  | 
| 1064: |  | 
| 1065: |  | 
| 1066: |  | 
| 1067: |  | 
| 1068: |  | 
| 1069: |  | 
| 1070: |  | 
| 1071: | public static function startsWithLetter($value, $message = '') | 
| 1072: | { | 
| 1073: | static::string($value); | 
| 1074: |  | 
| 1075: | $valid = isset($value[0]); | 
| 1076: |  | 
| 1077: | if ($valid) { | 
| 1078: | $locale = \setlocale(LC_CTYPE, 0); | 
| 1079: | \setlocale(LC_CTYPE, 'C'); | 
| 1080: | $valid = \ctype_alpha($value[0]); | 
| 1081: | \setlocale(LC_CTYPE, $locale); | 
| 1082: | } | 
| 1083: |  | 
| 1084: | if (!$valid) { | 
| 1085: | static::reportInvalidArgument(\sprintf( | 
| 1086: | $message ?: 'Expected a value to start with a letter. Got: %s', | 
| 1087: | static::valueToString($value) | 
| 1088: | )); | 
| 1089: | } | 
| 1090: | } | 
| 1091: |  | 
| 1092: |  | 
| 1093: |  | 
| 1094: |  | 
| 1095: |  | 
| 1096: |  | 
| 1097: |  | 
| 1098: |  | 
| 1099: |  | 
| 1100: |  | 
| 1101: | public static function endsWith($value, $suffix, $message = '') | 
| 1102: | { | 
| 1103: | if ($suffix !== \substr($value, -\strlen($suffix))) { | 
| 1104: | static::reportInvalidArgument(\sprintf( | 
| 1105: | $message ?: 'Expected a value to end with %2$s. Got: %s', | 
| 1106: | static::valueToString($value), | 
| 1107: | static::valueToString($suffix) | 
| 1108: | )); | 
| 1109: | } | 
| 1110: | } | 
| 1111: |  | 
| 1112: |  | 
| 1113: |  | 
| 1114: |  | 
| 1115: |  | 
| 1116: |  | 
| 1117: |  | 
| 1118: |  | 
| 1119: |  | 
| 1120: |  | 
| 1121: | public static function notEndsWith($value, $suffix, $message = '') | 
| 1122: | { | 
| 1123: | if ($suffix === \substr($value, -\strlen($suffix))) { | 
| 1124: | static::reportInvalidArgument(\sprintf( | 
| 1125: | $message ?: 'Expected a value not to end with %2$s. Got: %s', | 
| 1126: | static::valueToString($value), | 
| 1127: | static::valueToString($suffix) | 
| 1128: | )); | 
| 1129: | } | 
| 1130: | } | 
| 1131: |  | 
| 1132: |  | 
| 1133: |  | 
| 1134: |  | 
| 1135: |  | 
| 1136: |  | 
| 1137: |  | 
| 1138: |  | 
| 1139: |  | 
| 1140: |  | 
| 1141: | public static function regex($value, $pattern, $message = '') | 
| 1142: | { | 
| 1143: | if (!\preg_match($pattern, $value)) { | 
| 1144: | static::reportInvalidArgument(\sprintf( | 
| 1145: | $message ?: 'The value %s does not match the expected pattern.', | 
| 1146: | static::valueToString($value) | 
| 1147: | )); | 
| 1148: | } | 
| 1149: | } | 
| 1150: |  | 
| 1151: |  | 
| 1152: |  | 
| 1153: |  | 
| 1154: |  | 
| 1155: |  | 
| 1156: |  | 
| 1157: |  | 
| 1158: |  | 
| 1159: |  | 
| 1160: | public static function notRegex($value, $pattern, $message = '') | 
| 1161: | { | 
| 1162: | if (\preg_match($pattern, $value, $matches, PREG_OFFSET_CAPTURE)) { | 
| 1163: | static::reportInvalidArgument(\sprintf( | 
| 1164: | $message ?: 'The value %s matches the pattern %s (at offset %d).', | 
| 1165: | static::valueToString($value), | 
| 1166: | static::valueToString($pattern), | 
| 1167: | $matches[0][1] | 
| 1168: | )); | 
| 1169: | } | 
| 1170: | } | 
| 1171: |  | 
| 1172: |  | 
| 1173: |  | 
| 1174: |  | 
| 1175: |  | 
| 1176: |  | 
| 1177: |  | 
| 1178: |  | 
| 1179: |  | 
| 1180: | public static function unicodeLetters($value, $message = '') | 
| 1181: | { | 
| 1182: | static::string($value); | 
| 1183: |  | 
| 1184: | if (!\preg_match('/^\p{L}+$/u', $value)) { | 
| 1185: | static::reportInvalidArgument(\sprintf( | 
| 1186: | $message ?: 'Expected a value to contain only Unicode letters. Got: %s', | 
| 1187: | static::valueToString($value) | 
| 1188: | )); | 
| 1189: | } | 
| 1190: | } | 
| 1191: |  | 
| 1192: |  | 
| 1193: |  | 
| 1194: |  | 
| 1195: |  | 
| 1196: |  | 
| 1197: |  | 
| 1198: |  | 
| 1199: |  | 
| 1200: | public static function alpha($value, $message = '') | 
| 1201: | { | 
| 1202: | static::string($value); | 
| 1203: |  | 
| 1204: | $locale = \setlocale(LC_CTYPE, 0); | 
| 1205: | \setlocale(LC_CTYPE, 'C'); | 
| 1206: | $valid = !\ctype_alpha($value); | 
| 1207: | \setlocale(LC_CTYPE, $locale); | 
| 1208: |  | 
| 1209: | if ($valid) { | 
| 1210: | static::reportInvalidArgument(\sprintf( | 
| 1211: | $message ?: 'Expected a value to contain only letters. Got: %s', | 
| 1212: | static::valueToString($value) | 
| 1213: | )); | 
| 1214: | } | 
| 1215: | } | 
| 1216: |  | 
| 1217: |  | 
| 1218: |  | 
| 1219: |  | 
| 1220: |  | 
| 1221: |  | 
| 1222: |  | 
| 1223: |  | 
| 1224: |  | 
| 1225: | public static function digits($value, $message = '') | 
| 1226: | { | 
| 1227: | $locale = \setlocale(LC_CTYPE, 0); | 
| 1228: | \setlocale(LC_CTYPE, 'C'); | 
| 1229: | $valid = !\ctype_digit($value); | 
| 1230: | \setlocale(LC_CTYPE, $locale); | 
| 1231: |  | 
| 1232: | if ($valid) { | 
| 1233: | static::reportInvalidArgument(\sprintf( | 
| 1234: | $message ?: 'Expected a value to contain digits only. Got: %s', | 
| 1235: | static::valueToString($value) | 
| 1236: | )); | 
| 1237: | } | 
| 1238: | } | 
| 1239: |  | 
| 1240: |  | 
| 1241: |  | 
| 1242: |  | 
| 1243: |  | 
| 1244: |  | 
| 1245: |  | 
| 1246: |  | 
| 1247: |  | 
| 1248: | public static function alnum($value, $message = '') | 
| 1249: | { | 
| 1250: | $locale = \setlocale(LC_CTYPE, 0); | 
| 1251: | \setlocale(LC_CTYPE, 'C'); | 
| 1252: | $valid = !\ctype_alnum($value); | 
| 1253: | \setlocale(LC_CTYPE, $locale); | 
| 1254: |  | 
| 1255: | if ($valid) { | 
| 1256: | static::reportInvalidArgument(\sprintf( | 
| 1257: | $message ?: 'Expected a value to contain letters and digits only. Got: %s', | 
| 1258: | static::valueToString($value) | 
| 1259: | )); | 
| 1260: | } | 
| 1261: | } | 
| 1262: |  | 
| 1263: |  | 
| 1264: |  | 
| 1265: |  | 
| 1266: |  | 
| 1267: |  | 
| 1268: |  | 
| 1269: |  | 
| 1270: |  | 
| 1271: |  | 
| 1272: | public static function lower($value, $message = '') | 
| 1273: | { | 
| 1274: | $locale = \setlocale(LC_CTYPE, 0); | 
| 1275: | \setlocale(LC_CTYPE, 'C'); | 
| 1276: | $valid = !\ctype_lower($value); | 
| 1277: | \setlocale(LC_CTYPE, $locale); | 
| 1278: |  | 
| 1279: | if ($valid) { | 
| 1280: | static::reportInvalidArgument(\sprintf( | 
| 1281: | $message ?: 'Expected a value to contain lowercase characters only. Got: %s', | 
| 1282: | static::valueToString($value) | 
| 1283: | )); | 
| 1284: | } | 
| 1285: | } | 
| 1286: |  | 
| 1287: |  | 
| 1288: |  | 
| 1289: |  | 
| 1290: |  | 
| 1291: |  | 
| 1292: |  | 
| 1293: |  | 
| 1294: |  | 
| 1295: |  | 
| 1296: | public static function upper($value, $message = '') | 
| 1297: | { | 
| 1298: | $locale = \setlocale(LC_CTYPE, 0); | 
| 1299: | \setlocale(LC_CTYPE, 'C'); | 
| 1300: | $valid = !\ctype_upper($value); | 
| 1301: | \setlocale(LC_CTYPE, $locale); | 
| 1302: |  | 
| 1303: | if ($valid) { | 
| 1304: | static::reportInvalidArgument(\sprintf( | 
| 1305: | $message ?: 'Expected a value to contain uppercase characters only. Got: %s', | 
| 1306: | static::valueToString($value) | 
| 1307: | )); | 
| 1308: | } | 
| 1309: | } | 
| 1310: |  | 
| 1311: |  | 
| 1312: |  | 
| 1313: |  | 
| 1314: |  | 
| 1315: |  | 
| 1316: |  | 
| 1317: |  | 
| 1318: |  | 
| 1319: |  | 
| 1320: | public static function length($value, $length, $message = '') | 
| 1321: | { | 
| 1322: | if ($length !== static::strlen($value)) { | 
| 1323: | static::reportInvalidArgument(\sprintf( | 
| 1324: | $message ?: 'Expected a value to contain %2$s characters. Got: %s', | 
| 1325: | static::valueToString($value), | 
| 1326: | $length | 
| 1327: | )); | 
| 1328: | } | 
| 1329: | } | 
| 1330: |  | 
| 1331: |  | 
| 1332: |  | 
| 1333: |  | 
| 1334: |  | 
| 1335: |  | 
| 1336: |  | 
| 1337: |  | 
| 1338: |  | 
| 1339: |  | 
| 1340: |  | 
| 1341: |  | 
| 1342: | public static function minLength($value, $min, $message = '') | 
| 1343: | { | 
| 1344: | if (static::strlen($value) < $min) { | 
| 1345: | static::reportInvalidArgument(\sprintf( | 
| 1346: | $message ?: 'Expected a value to contain at least %2$s characters. Got: %s', | 
| 1347: | static::valueToString($value), | 
| 1348: | $min | 
| 1349: | )); | 
| 1350: | } | 
| 1351: | } | 
| 1352: |  | 
| 1353: |  | 
| 1354: |  | 
| 1355: |  | 
| 1356: |  | 
| 1357: |  | 
| 1358: |  | 
| 1359: |  | 
| 1360: |  | 
| 1361: |  | 
| 1362: |  | 
| 1363: |  | 
| 1364: | public static function maxLength($value, $max, $message = '') | 
| 1365: | { | 
| 1366: | if (static::strlen($value) > $max) { | 
| 1367: | static::reportInvalidArgument(\sprintf( | 
| 1368: | $message ?: 'Expected a value to contain at most %2$s characters. Got: %s', | 
| 1369: | static::valueToString($value), | 
| 1370: | $max | 
| 1371: | )); | 
| 1372: | } | 
| 1373: | } | 
| 1374: |  | 
| 1375: |  | 
| 1376: |  | 
| 1377: |  | 
| 1378: |  | 
| 1379: |  | 
| 1380: |  | 
| 1381: |  | 
| 1382: |  | 
| 1383: |  | 
| 1384: |  | 
| 1385: |  | 
| 1386: |  | 
| 1387: | public static function lengthBetween($value, $min, $max, $message = '') | 
| 1388: | { | 
| 1389: | $length = static::strlen($value); | 
| 1390: |  | 
| 1391: | if ($length < $min || $length > $max) { | 
| 1392: | static::reportInvalidArgument(\sprintf( | 
| 1393: | $message ?: 'Expected a value to contain between %2$s and %3$s characters. Got: %s', | 
| 1394: | static::valueToString($value), | 
| 1395: | $min, | 
| 1396: | $max | 
| 1397: | )); | 
| 1398: | } | 
| 1399: | } | 
| 1400: |  | 
| 1401: |  | 
| 1402: |  | 
| 1403: |  | 
| 1404: |  | 
| 1405: |  | 
| 1406: |  | 
| 1407: |  | 
| 1408: |  | 
| 1409: | public static function fileExists($value, $message = '') | 
| 1410: | { | 
| 1411: | static::string($value); | 
| 1412: |  | 
| 1413: | if (!\file_exists($value)) { | 
| 1414: | static::reportInvalidArgument(\sprintf( | 
| 1415: | $message ?: 'The file %s does not exist.', | 
| 1416: | static::valueToString($value) | 
| 1417: | )); | 
| 1418: | } | 
| 1419: | } | 
| 1420: |  | 
| 1421: |  | 
| 1422: |  | 
| 1423: |  | 
| 1424: |  | 
| 1425: |  | 
| 1426: |  | 
| 1427: | public static function file($value, $message = '') | 
| 1428: | { | 
| 1429: | static::fileExists($value, $message); | 
| 1430: |  | 
| 1431: | if (!\is_file($value)) { | 
| 1432: | static::reportInvalidArgument(\sprintf( | 
| 1433: | $message ?: 'The path %s is not a file.', | 
| 1434: | static::valueToString($value) | 
| 1435: | )); | 
| 1436: | } | 
| 1437: | } | 
| 1438: |  | 
| 1439: |  | 
| 1440: |  | 
| 1441: |  | 
| 1442: |  | 
| 1443: |  | 
| 1444: |  | 
| 1445: | public static function directory($value, $message = '') | 
| 1446: | { | 
| 1447: | static::fileExists($value, $message); | 
| 1448: |  | 
| 1449: | if (!\is_dir($value)) { | 
| 1450: | static::reportInvalidArgument(\sprintf( | 
| 1451: | $message ?: 'The path %s is no directory.', | 
| 1452: | static::valueToString($value) | 
| 1453: | )); | 
| 1454: | } | 
| 1455: | } | 
| 1456: |  | 
| 1457: |  | 
| 1458: |  | 
| 1459: |  | 
| 1460: |  | 
| 1461: |  | 
| 1462: |  | 
| 1463: | public static function readable($value, $message = '') | 
| 1464: | { | 
| 1465: | if (!\is_readable($value)) { | 
| 1466: | static::reportInvalidArgument(\sprintf( | 
| 1467: | $message ?: 'The path %s is not readable.', | 
| 1468: | static::valueToString($value) | 
| 1469: | )); | 
| 1470: | } | 
| 1471: | } | 
| 1472: |  | 
| 1473: |  | 
| 1474: |  | 
| 1475: |  | 
| 1476: |  | 
| 1477: |  | 
| 1478: |  | 
| 1479: | public static function writable($value, $message = '') | 
| 1480: | { | 
| 1481: | if (!\is_writable($value)) { | 
| 1482: | static::reportInvalidArgument(\sprintf( | 
| 1483: | $message ?: 'The path %s is not writable.', | 
| 1484: | static::valueToString($value) | 
| 1485: | )); | 
| 1486: | } | 
| 1487: | } | 
| 1488: |  | 
| 1489: |  | 
| 1490: |  | 
| 1491: |  | 
| 1492: |  | 
| 1493: |  | 
| 1494: |  | 
| 1495: |  | 
| 1496: |  | 
| 1497: | public static function classExists($value, $message = '') | 
| 1498: | { | 
| 1499: | if (!\class_exists($value)) { | 
| 1500: | static::reportInvalidArgument(\sprintf( | 
| 1501: | $message ?: 'Expected an existing class name. Got: %s', | 
| 1502: | static::valueToString($value) | 
| 1503: | )); | 
| 1504: | } | 
| 1505: | } | 
| 1506: |  | 
| 1507: |  | 
| 1508: |  | 
| 1509: |  | 
| 1510: |  | 
| 1511: |  | 
| 1512: |  | 
| 1513: |  | 
| 1514: |  | 
| 1515: |  | 
| 1516: |  | 
| 1517: |  | 
| 1518: |  | 
| 1519: | public static function subclassOf($value, $class, $message = '') | 
| 1520: | { | 
| 1521: | if (!\is_subclass_of($value, $class)) { | 
| 1522: | static::reportInvalidArgument(\sprintf( | 
| 1523: | $message ?: 'Expected a sub-class of %2$s. Got: %s', | 
| 1524: | static::valueToString($value), | 
| 1525: | static::valueToString($class) | 
| 1526: | )); | 
| 1527: | } | 
| 1528: | } | 
| 1529: |  | 
| 1530: |  | 
| 1531: |  | 
| 1532: |  | 
| 1533: |  | 
| 1534: |  | 
| 1535: |  | 
| 1536: |  | 
| 1537: |  | 
| 1538: | public static function interfaceExists($value, $message = '') | 
| 1539: | { | 
| 1540: | if (!\interface_exists($value)) { | 
| 1541: | static::reportInvalidArgument(\sprintf( | 
| 1542: | $message ?: 'Expected an existing interface name. got %s', | 
| 1543: | static::valueToString($value) | 
| 1544: | )); | 
| 1545: | } | 
| 1546: | } | 
| 1547: |  | 
| 1548: |  | 
| 1549: |  | 
| 1550: |  | 
| 1551: |  | 
| 1552: |  | 
| 1553: |  | 
| 1554: |  | 
| 1555: |  | 
| 1556: |  | 
| 1557: |  | 
| 1558: |  | 
| 1559: |  | 
| 1560: | public static function implementsInterface($value, $interface, $message = '') | 
| 1561: | { | 
| 1562: | if (!\in_array($interface, \class_implements($value))) { | 
| 1563: | static::reportInvalidArgument(\sprintf( | 
| 1564: | $message ?: 'Expected an implementation of %2$s. Got: %s', | 
| 1565: | static::valueToString($value), | 
| 1566: | static::valueToString($interface) | 
| 1567: | )); | 
| 1568: | } | 
| 1569: | } | 
| 1570: |  | 
| 1571: |  | 
| 1572: |  | 
| 1573: |  | 
| 1574: |  | 
| 1575: |  | 
| 1576: |  | 
| 1577: |  | 
| 1578: |  | 
| 1579: |  | 
| 1580: |  | 
| 1581: | public static function propertyExists($classOrObject, $property, $message = '') | 
| 1582: | { | 
| 1583: | if (!\property_exists($classOrObject, $property)) { | 
| 1584: | static::reportInvalidArgument(\sprintf( | 
| 1585: | $message ?: 'Expected the property %s to exist.', | 
| 1586: | static::valueToString($property) | 
| 1587: | )); | 
| 1588: | } | 
| 1589: | } | 
| 1590: |  | 
| 1591: |  | 
| 1592: |  | 
| 1593: |  | 
| 1594: |  | 
| 1595: |  | 
| 1596: |  | 
| 1597: |  | 
| 1598: |  | 
| 1599: |  | 
| 1600: |  | 
| 1601: | public static function propertyNotExists($classOrObject, $property, $message = '') | 
| 1602: | { | 
| 1603: | if (\property_exists($classOrObject, $property)) { | 
| 1604: | static::reportInvalidArgument(\sprintf( | 
| 1605: | $message ?: 'Expected the property %s to not exist.', | 
| 1606: | static::valueToString($property) | 
| 1607: | )); | 
| 1608: | } | 
| 1609: | } | 
| 1610: |  | 
| 1611: |  | 
| 1612: |  | 
| 1613: |  | 
| 1614: |  | 
| 1615: |  | 
| 1616: |  | 
| 1617: |  | 
| 1618: |  | 
| 1619: |  | 
| 1620: |  | 
| 1621: | public static function methodExists($classOrObject, $method, $message = '') | 
| 1622: | { | 
| 1623: | if (!(\is_string($classOrObject) || \is_object($classOrObject)) || !\method_exists($classOrObject, $method)) { | 
| 1624: | static::reportInvalidArgument(\sprintf( | 
| 1625: | $message ?: 'Expected the method %s to exist.', | 
| 1626: | static::valueToString($method) | 
| 1627: | )); | 
| 1628: | } | 
| 1629: | } | 
| 1630: |  | 
| 1631: |  | 
| 1632: |  | 
| 1633: |  | 
| 1634: |  | 
| 1635: |  | 
| 1636: |  | 
| 1637: |  | 
| 1638: |  | 
| 1639: |  | 
| 1640: |  | 
| 1641: | public static function methodNotExists($classOrObject, $method, $message = '') | 
| 1642: | { | 
| 1643: | if ((\is_string($classOrObject) || \is_object($classOrObject)) && \method_exists($classOrObject, $method)) { | 
| 1644: | static::reportInvalidArgument(\sprintf( | 
| 1645: | $message ?: 'Expected the method %s to not exist.', | 
| 1646: | static::valueToString($method) | 
| 1647: | )); | 
| 1648: | } | 
| 1649: | } | 
| 1650: |  | 
| 1651: |  | 
| 1652: |  | 
| 1653: |  | 
| 1654: |  | 
| 1655: |  | 
| 1656: |  | 
| 1657: |  | 
| 1658: |  | 
| 1659: |  | 
| 1660: | public static function keyExists($array, $key, $message = '') | 
| 1661: | { | 
| 1662: | if (!(isset($array[$key]) || \array_key_exists($key, $array))) { | 
| 1663: | static::reportInvalidArgument(\sprintf( | 
| 1664: | $message ?: 'Expected the key %s to exist.', | 
| 1665: | static::valueToString($key) | 
| 1666: | )); | 
| 1667: | } | 
| 1668: | } | 
| 1669: |  | 
| 1670: |  | 
| 1671: |  | 
| 1672: |  | 
| 1673: |  | 
| 1674: |  | 
| 1675: |  | 
| 1676: |  | 
| 1677: |  | 
| 1678: |  | 
| 1679: | public static function keyNotExists($array, $key, $message = '') | 
| 1680: | { | 
| 1681: | if (isset($array[$key]) || \array_key_exists($key, $array)) { | 
| 1682: | static::reportInvalidArgument(\sprintf( | 
| 1683: | $message ?: 'Expected the key %s to not exist.', | 
| 1684: | static::valueToString($key) | 
| 1685: | )); | 
| 1686: | } | 
| 1687: | } | 
| 1688: |  | 
| 1689: |  | 
| 1690: |  | 
| 1691: |  | 
| 1692: |  | 
| 1693: |  | 
| 1694: |  | 
| 1695: |  | 
| 1696: |  | 
| 1697: |  | 
| 1698: |  | 
| 1699: |  | 
| 1700: | public static function validArrayKey($value, $message = '') | 
| 1701: | { | 
| 1702: | if (!(\is_int($value) || \is_string($value))) { | 
| 1703: | static::reportInvalidArgument(\sprintf( | 
| 1704: | $message ?: 'Expected string or integer. Got: %s', | 
| 1705: | static::typeToString($value) | 
| 1706: | )); | 
| 1707: | } | 
| 1708: | } | 
| 1709: |  | 
| 1710: |  | 
| 1711: |  | 
| 1712: |  | 
| 1713: |  | 
| 1714: |  | 
| 1715: |  | 
| 1716: |  | 
| 1717: |  | 
| 1718: |  | 
| 1719: | public static function count($array, $number, $message = '') | 
| 1720: | { | 
| 1721: | static::eq( | 
| 1722: | \count($array), | 
| 1723: | $number, | 
| 1724: | \sprintf( | 
| 1725: | $message ?: 'Expected an array to contain %d elements. Got: %d.', | 
| 1726: | $number, | 
| 1727: | \count($array) | 
| 1728: | ) | 
| 1729: | ); | 
| 1730: | } | 
| 1731: |  | 
| 1732: |  | 
| 1733: |  | 
| 1734: |  | 
| 1735: |  | 
| 1736: |  | 
| 1737: |  | 
| 1738: |  | 
| 1739: |  | 
| 1740: |  | 
| 1741: | public static function minCount($array, $min, $message = '') | 
| 1742: | { | 
| 1743: | if (\count($array) < $min) { | 
| 1744: | static::reportInvalidArgument(\sprintf( | 
| 1745: | $message ?: 'Expected an array to contain at least %2$d elements. Got: %d', | 
| 1746: | \count($array), | 
| 1747: | $min | 
| 1748: | )); | 
| 1749: | } | 
| 1750: | } | 
| 1751: |  | 
| 1752: |  | 
| 1753: |  | 
| 1754: |  | 
| 1755: |  | 
| 1756: |  | 
| 1757: |  | 
| 1758: |  | 
| 1759: |  | 
| 1760: |  | 
| 1761: | public static function maxCount($array, $max, $message = '') | 
| 1762: | { | 
| 1763: | if (\count($array) > $max) { | 
| 1764: | static::reportInvalidArgument(\sprintf( | 
| 1765: | $message ?: 'Expected an array to contain at most %2$d elements. Got: %d', | 
| 1766: | \count($array), | 
| 1767: | $max | 
| 1768: | )); | 
| 1769: | } | 
| 1770: | } | 
| 1771: |  | 
| 1772: |  | 
| 1773: |  | 
| 1774: |  | 
| 1775: |  | 
| 1776: |  | 
| 1777: |  | 
| 1778: |  | 
| 1779: |  | 
| 1780: |  | 
| 1781: |  | 
| 1782: | public static function countBetween($array, $min, $max, $message = '') | 
| 1783: | { | 
| 1784: | $count = \count($array); | 
| 1785: |  | 
| 1786: | if ($count < $min || $count > $max) { | 
| 1787: | static::reportInvalidArgument(\sprintf( | 
| 1788: | $message ?: 'Expected an array to contain between %2$d and %3$d elements. Got: %d', | 
| 1789: | $count, | 
| 1790: | $min, | 
| 1791: | $max | 
| 1792: | )); | 
| 1793: | } | 
| 1794: | } | 
| 1795: |  | 
| 1796: |  | 
| 1797: |  | 
| 1798: |  | 
| 1799: |  | 
| 1800: |  | 
| 1801: |  | 
| 1802: |  | 
| 1803: |  | 
| 1804: |  | 
| 1805: | public static function isList($array, $message = '') | 
| 1806: | { | 
| 1807: | if (!\is_array($array) || $array !== \array_values($array)) { | 
| 1808: | static::reportInvalidArgument( | 
| 1809: | $message ?: 'Expected list - non-associative array.' | 
| 1810: | ); | 
| 1811: | } | 
| 1812: | } | 
| 1813: |  | 
| 1814: |  | 
| 1815: |  | 
| 1816: |  | 
| 1817: |  | 
| 1818: |  | 
| 1819: |  | 
| 1820: |  | 
| 1821: |  | 
| 1822: |  | 
| 1823: | public static function isNonEmptyList($array, $message = '') | 
| 1824: | { | 
| 1825: | static::isList($array, $message); | 
| 1826: | static::notEmpty($array, $message); | 
| 1827: | } | 
| 1828: |  | 
| 1829: |  | 
| 1830: |  | 
| 1831: |  | 
| 1832: |  | 
| 1833: |  | 
| 1834: |  | 
| 1835: |  | 
| 1836: |  | 
| 1837: |  | 
| 1838: |  | 
| 1839: |  | 
| 1840: | public static function isMap($array, $message = '') | 
| 1841: | { | 
| 1842: | if ( | 
| 1843: | !\is_array($array) || | 
| 1844: | \array_keys($array) !== \array_filter(\array_keys($array), '\is_string') | 
| 1845: | ) { | 
| 1846: | static::reportInvalidArgument( | 
| 1847: | $message ?: 'Expected map - associative array with string keys.' | 
| 1848: | ); | 
| 1849: | } | 
| 1850: | } | 
| 1851: |  | 
| 1852: |  | 
| 1853: |  | 
| 1854: |  | 
| 1855: |  | 
| 1856: |  | 
| 1857: |  | 
| 1858: |  | 
| 1859: |  | 
| 1860: |  | 
| 1861: |  | 
| 1862: |  | 
| 1863: |  | 
| 1864: | public static function isNonEmptyMap($array, $message = '') | 
| 1865: | { | 
| 1866: | static::isMap($array, $message); | 
| 1867: | static::notEmpty($array, $message); | 
| 1868: | } | 
| 1869: |  | 
| 1870: |  | 
| 1871: |  | 
| 1872: |  | 
| 1873: |  | 
| 1874: |  | 
| 1875: |  | 
| 1876: |  | 
| 1877: |  | 
| 1878: | public static function uuid($value, $message = '') | 
| 1879: | { | 
| 1880: | $value = \str_replace(array('urn:', 'uuid:', '{', '}'), '', $value); | 
| 1881: |  | 
| 1882: |  | 
| 1883: |  | 
| 1884: | if ('00000000-0000-0000-0000-000000000000' === $value) { | 
| 1885: | return; | 
| 1886: | } | 
| 1887: |  | 
| 1888: | if (!\preg_match('/^[0-9A-Fa-f]{8}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{12}$/', $value)) { | 
| 1889: | static::reportInvalidArgument(\sprintf( | 
| 1890: | $message ?: 'Value %s is not a valid UUID.', | 
| 1891: | static::valueToString($value) | 
| 1892: | )); | 
| 1893: | } | 
| 1894: | } | 
| 1895: |  | 
| 1896: |  | 
| 1897: |  | 
| 1898: |  | 
| 1899: |  | 
| 1900: |  | 
| 1901: |  | 
| 1902: |  | 
| 1903: |  | 
| 1904: |  | 
| 1905: | public static function throws(Closure $expression, $class = 'Exception', $message = '') | 
| 1906: | { | 
| 1907: | static::string($class); | 
| 1908: |  | 
| 1909: | $actual = 'none'; | 
| 1910: |  | 
| 1911: | try { | 
| 1912: | $expression(); | 
| 1913: | } catch (Exception $e) { | 
| 1914: | $actual = \get_class($e); | 
| 1915: | if ($e instanceof $class) { | 
| 1916: | return; | 
| 1917: | } | 
| 1918: | } catch (Throwable $e) { | 
| 1919: | $actual = \get_class($e); | 
| 1920: | if ($e instanceof $class) { | 
| 1921: | return; | 
| 1922: | } | 
| 1923: | } | 
| 1924: |  | 
| 1925: | static::reportInvalidArgument($message ?: \sprintf( | 
| 1926: | 'Expected to throw "%s", got "%s"', | 
| 1927: | $class, | 
| 1928: | $actual | 
| 1929: | )); | 
| 1930: | } | 
| 1931: |  | 
| 1932: |  | 
| 1933: |  | 
| 1934: |  | 
| 1935: | public static function __callStatic($name, $arguments) | 
| 1936: | { | 
| 1937: | if ('nullOr' === \substr($name, 0, 6)) { | 
| 1938: | if (null !== $arguments[0]) { | 
| 1939: | $method = \lcfirst(\substr($name, 6)); | 
| 1940: | \call_user_func_array(array('static', $method), $arguments); | 
| 1941: | } | 
| 1942: |  | 
| 1943: | return; | 
| 1944: | } | 
| 1945: |  | 
| 1946: | if ('all' === \substr($name, 0, 3)) { | 
| 1947: | static::isIterable($arguments[0]); | 
| 1948: |  | 
| 1949: | $method = \lcfirst(\substr($name, 3)); | 
| 1950: | $args = $arguments; | 
| 1951: |  | 
| 1952: | foreach ($arguments[0] as $entry) { | 
| 1953: | $args[0] = $entry; | 
| 1954: |  | 
| 1955: | \call_user_func_array(array('static', $method), $args); | 
| 1956: | } | 
| 1957: |  | 
| 1958: | return; | 
| 1959: | } | 
| 1960: |  | 
| 1961: | throw new BadMethodCallException('No such method: '.$name); | 
| 1962: | } | 
| 1963: |  | 
| 1964: |  | 
| 1965: |  | 
| 1966: |  | 
| 1967: |  | 
| 1968: |  | 
| 1969: | protected static function valueToString($value) | 
| 1970: | { | 
| 1971: | if (null === $value) { | 
| 1972: | return 'null'; | 
| 1973: | } | 
| 1974: |  | 
| 1975: | if (true === $value) { | 
| 1976: | return 'true'; | 
| 1977: | } | 
| 1978: |  | 
| 1979: | if (false === $value) { | 
| 1980: | return 'false'; | 
| 1981: | } | 
| 1982: |  | 
| 1983: | if (\is_array($value)) { | 
| 1984: | return 'array'; | 
| 1985: | } | 
| 1986: |  | 
| 1987: | if (\is_object($value)) { | 
| 1988: | if (\method_exists($value, '__toString')) { | 
| 1989: | return \get_class($value).': '.self::valueToString($value->__toString()); | 
| 1990: | } | 
| 1991: |  | 
| 1992: | if ($value instanceof DateTime || $value instanceof DateTimeImmutable) { | 
| 1993: | return \get_class($value).': '.self::valueToString($value->format('c')); | 
| 1994: | } | 
| 1995: |  | 
| 1996: | return \get_class($value); | 
| 1997: | } | 
| 1998: |  | 
| 1999: | if (\is_resource($value)) { | 
| 2000: | return 'resource'; | 
| 2001: | } | 
| 2002: |  | 
| 2003: | if (\is_string($value)) { | 
| 2004: | return '"'.$value.'"'; | 
| 2005: | } | 
| 2006: |  | 
| 2007: | return (string) $value; | 
| 2008: | } | 
| 2009: |  | 
| 2010: |  | 
| 2011: |  | 
| 2012: |  | 
| 2013: |  | 
| 2014: |  | 
| 2015: | protected static function typeToString($value) | 
| 2016: | { | 
| 2017: | return \is_object($value) ? \get_class($value) : \gettype($value); | 
| 2018: | } | 
| 2019: |  | 
| 2020: | protected static function strlen($value) | 
| 2021: | { | 
| 2022: | if (!\function_exists('mb_detect_encoding')) { | 
| 2023: | return \strlen($value); | 
| 2024: | } | 
| 2025: |  | 
| 2026: | if (false === $encoding = \mb_detect_encoding($value)) { | 
| 2027: | return \strlen($value); | 
| 2028: | } | 
| 2029: |  | 
| 2030: | return \mb_strlen($value, $encoding); | 
| 2031: | } | 
| 2032: |  | 
| 2033: |  | 
| 2034: |  | 
| 2035: |  | 
| 2036: |  | 
| 2037: |  | 
| 2038: |  | 
| 2039: |  | 
| 2040: | protected static function reportInvalidArgument($message) | 
| 2041: | { | 
| 2042: | throw new InvalidArgumentException($message); | 
| 2043: | } | 
| 2044: |  | 
| 2045: | private function __construct() | 
| 2046: | { | 
| 2047: | } | 
| 2048: | } | 
| 2049: |  |