Assert.php
1 year ago
InvalidArgumentException.php
1 year ago
Mixin.php
1 year ago
index.php
1 year ago
Assert.php
1046 lines
| 1 | <?php |
| 2 | namespace Webmozart\Assert; |
| 3 | if (!defined('ABSPATH')) exit; |
| 4 | use ArrayAccess; |
| 5 | use BadMethodCallException; |
| 6 | use Closure; |
| 7 | use Countable; |
| 8 | use DateTime; |
| 9 | use DateTimeImmutable; |
| 10 | use Exception; |
| 11 | use ResourceBundle; |
| 12 | use SimpleXMLElement; |
| 13 | use Throwable; |
| 14 | use Traversable; |
| 15 | class Assert |
| 16 | { |
| 17 | use Mixin; |
| 18 | public static function string($value, $message = '') |
| 19 | { |
| 20 | if (!\is_string($value)) { |
| 21 | static::reportInvalidArgument(\sprintf( |
| 22 | $message ?: 'Expected a string. Got: %s', |
| 23 | static::typeToString($value) |
| 24 | )); |
| 25 | } |
| 26 | } |
| 27 | public static function stringNotEmpty($value, $message = '') |
| 28 | { |
| 29 | static::string($value, $message); |
| 30 | static::notEq($value, '', $message); |
| 31 | } |
| 32 | public static function integer($value, $message = '') |
| 33 | { |
| 34 | if (!\is_int($value)) { |
| 35 | static::reportInvalidArgument(\sprintf( |
| 36 | $message ?: 'Expected an integer. Got: %s', |
| 37 | static::typeToString($value) |
| 38 | )); |
| 39 | } |
| 40 | } |
| 41 | public static function integerish($value, $message = '') |
| 42 | { |
| 43 | if (!\is_numeric($value) || $value != (int) $value) { |
| 44 | static::reportInvalidArgument(\sprintf( |
| 45 | $message ?: 'Expected an integerish value. Got: %s', |
| 46 | static::typeToString($value) |
| 47 | )); |
| 48 | } |
| 49 | } |
| 50 | public static function positiveInteger($value, $message = '') |
| 51 | { |
| 52 | if (!(\is_int($value) && $value > 0)) { |
| 53 | static::reportInvalidArgument(\sprintf( |
| 54 | $message ?: 'Expected a positive integer. Got: %s', |
| 55 | static::valueToString($value) |
| 56 | )); |
| 57 | } |
| 58 | } |
| 59 | public static function float($value, $message = '') |
| 60 | { |
| 61 | if (!\is_float($value)) { |
| 62 | static::reportInvalidArgument(\sprintf( |
| 63 | $message ?: 'Expected a float. Got: %s', |
| 64 | static::typeToString($value) |
| 65 | )); |
| 66 | } |
| 67 | } |
| 68 | public static function numeric($value, $message = '') |
| 69 | { |
| 70 | if (!\is_numeric($value)) { |
| 71 | static::reportInvalidArgument(\sprintf( |
| 72 | $message ?: 'Expected a numeric. Got: %s', |
| 73 | static::typeToString($value) |
| 74 | )); |
| 75 | } |
| 76 | } |
| 77 | public static function natural($value, $message = '') |
| 78 | { |
| 79 | if (!\is_int($value) || $value < 0) { |
| 80 | static::reportInvalidArgument(\sprintf( |
| 81 | $message ?: 'Expected a non-negative integer. Got: %s', |
| 82 | static::valueToString($value) |
| 83 | )); |
| 84 | } |
| 85 | } |
| 86 | public static function boolean($value, $message = '') |
| 87 | { |
| 88 | if (!\is_bool($value)) { |
| 89 | static::reportInvalidArgument(\sprintf( |
| 90 | $message ?: 'Expected a boolean. Got: %s', |
| 91 | static::typeToString($value) |
| 92 | )); |
| 93 | } |
| 94 | } |
| 95 | public static function scalar($value, $message = '') |
| 96 | { |
| 97 | if (!\is_scalar($value)) { |
| 98 | static::reportInvalidArgument(\sprintf( |
| 99 | $message ?: 'Expected a scalar. Got: %s', |
| 100 | static::typeToString($value) |
| 101 | )); |
| 102 | } |
| 103 | } |
| 104 | public static function object($value, $message = '') |
| 105 | { |
| 106 | if (!\is_object($value)) { |
| 107 | static::reportInvalidArgument(\sprintf( |
| 108 | $message ?: 'Expected an object. Got: %s', |
| 109 | static::typeToString($value) |
| 110 | )); |
| 111 | } |
| 112 | } |
| 113 | public static function resource($value, $type = null, $message = '') |
| 114 | { |
| 115 | if (!\is_resource($value)) { |
| 116 | static::reportInvalidArgument(\sprintf( |
| 117 | $message ?: 'Expected a resource. Got: %s', |
| 118 | static::typeToString($value) |
| 119 | )); |
| 120 | } |
| 121 | if ($type && $type !== \get_resource_type($value)) { |
| 122 | static::reportInvalidArgument(\sprintf( |
| 123 | $message ?: 'Expected a resource of type %2$s. Got: %s', |
| 124 | static::typeToString($value), |
| 125 | $type |
| 126 | )); |
| 127 | } |
| 128 | } |
| 129 | public static function isCallable($value, $message = '') |
| 130 | { |
| 131 | if (!\is_callable($value)) { |
| 132 | static::reportInvalidArgument(\sprintf( |
| 133 | $message ?: 'Expected a callable. Got: %s', |
| 134 | static::typeToString($value) |
| 135 | )); |
| 136 | } |
| 137 | } |
| 138 | public static function isArray($value, $message = '') |
| 139 | { |
| 140 | if (!\is_array($value)) { |
| 141 | static::reportInvalidArgument(\sprintf( |
| 142 | $message ?: 'Expected an array. Got: %s', |
| 143 | static::typeToString($value) |
| 144 | )); |
| 145 | } |
| 146 | } |
| 147 | public static function isTraversable($value, $message = '') |
| 148 | { |
| 149 | @\trigger_error( |
| 150 | \sprintf( |
| 151 | '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.', |
| 152 | __METHOD__ |
| 153 | ), |
| 154 | \E_USER_DEPRECATED |
| 155 | ); |
| 156 | if (!\is_array($value) && !($value instanceof Traversable)) { |
| 157 | static::reportInvalidArgument(\sprintf( |
| 158 | $message ?: 'Expected a traversable. Got: %s', |
| 159 | static::typeToString($value) |
| 160 | )); |
| 161 | } |
| 162 | } |
| 163 | public static function isArrayAccessible($value, $message = '') |
| 164 | { |
| 165 | if (!\is_array($value) && !($value instanceof ArrayAccess)) { |
| 166 | static::reportInvalidArgument(\sprintf( |
| 167 | $message ?: 'Expected an array accessible. Got: %s', |
| 168 | static::typeToString($value) |
| 169 | )); |
| 170 | } |
| 171 | } |
| 172 | public static function isCountable($value, $message = '') |
| 173 | { |
| 174 | if ( |
| 175 | !\is_array($value) |
| 176 | && !($value instanceof Countable) |
| 177 | && !($value instanceof ResourceBundle) |
| 178 | && !($value instanceof SimpleXMLElement) |
| 179 | ) { |
| 180 | static::reportInvalidArgument(\sprintf( |
| 181 | $message ?: 'Expected a countable. Got: %s', |
| 182 | static::typeToString($value) |
| 183 | )); |
| 184 | } |
| 185 | } |
| 186 | public static function isIterable($value, $message = '') |
| 187 | { |
| 188 | if (!\is_array($value) && !($value instanceof Traversable)) { |
| 189 | static::reportInvalidArgument(\sprintf( |
| 190 | $message ?: 'Expected an iterable. Got: %s', |
| 191 | static::typeToString($value) |
| 192 | )); |
| 193 | } |
| 194 | } |
| 195 | public static function isInstanceOf($value, $class, $message = '') |
| 196 | { |
| 197 | if (!($value instanceof $class)) { |
| 198 | static::reportInvalidArgument(\sprintf( |
| 199 | $message ?: 'Expected an instance of %2$s. Got: %s', |
| 200 | static::typeToString($value), |
| 201 | $class |
| 202 | )); |
| 203 | } |
| 204 | } |
| 205 | public static function notInstanceOf($value, $class, $message = '') |
| 206 | { |
| 207 | if ($value instanceof $class) { |
| 208 | static::reportInvalidArgument(\sprintf( |
| 209 | $message ?: 'Expected an instance other than %2$s. Got: %s', |
| 210 | static::typeToString($value), |
| 211 | $class |
| 212 | )); |
| 213 | } |
| 214 | } |
| 215 | public static function isInstanceOfAny($value, array $classes, $message = '') |
| 216 | { |
| 217 | foreach ($classes as $class) { |
| 218 | if ($value instanceof $class) { |
| 219 | return; |
| 220 | } |
| 221 | } |
| 222 | static::reportInvalidArgument(\sprintf( |
| 223 | $message ?: 'Expected an instance of any of %2$s. Got: %s', |
| 224 | static::typeToString($value), |
| 225 | \implode(', ', \array_map(array(static::class, 'valueToString'), $classes)) |
| 226 | )); |
| 227 | } |
| 228 | public static function isAOf($value, $class, $message = '') |
| 229 | { |
| 230 | static::string($class, 'Expected class as a string. Got: %s'); |
| 231 | if (!\is_a($value, $class, \is_string($value))) { |
| 232 | static::reportInvalidArgument(sprintf( |
| 233 | $message ?: 'Expected an instance of this class or to this class among its parents "%2$s". Got: %s', |
| 234 | static::valueToString($value), |
| 235 | $class |
| 236 | )); |
| 237 | } |
| 238 | } |
| 239 | public static function isNotA($value, $class, $message = '') |
| 240 | { |
| 241 | static::string($class, 'Expected class as a string. Got: %s'); |
| 242 | if (\is_a($value, $class, \is_string($value))) { |
| 243 | static::reportInvalidArgument(sprintf( |
| 244 | $message ?: 'Expected an instance of this class or to this class among its parents other than "%2$s". Got: %s', |
| 245 | static::valueToString($value), |
| 246 | $class |
| 247 | )); |
| 248 | } |
| 249 | } |
| 250 | public static function isAnyOf($value, array $classes, $message = '') |
| 251 | { |
| 252 | foreach ($classes as $class) { |
| 253 | static::string($class, 'Expected class as a string. Got: %s'); |
| 254 | if (\is_a($value, $class, \is_string($value))) { |
| 255 | return; |
| 256 | } |
| 257 | } |
| 258 | static::reportInvalidArgument(sprintf( |
| 259 | $message ?: 'Expected an instance of any of this classes or any of those classes among their parents "%2$s". Got: %s', |
| 260 | static::valueToString($value), |
| 261 | \implode(', ', $classes) |
| 262 | )); |
| 263 | } |
| 264 | public static function isEmpty($value, $message = '') |
| 265 | { |
| 266 | if (!empty($value)) { |
| 267 | static::reportInvalidArgument(\sprintf( |
| 268 | $message ?: 'Expected an empty value. Got: %s', |
| 269 | static::valueToString($value) |
| 270 | )); |
| 271 | } |
| 272 | } |
| 273 | public static function notEmpty($value, $message = '') |
| 274 | { |
| 275 | if (empty($value)) { |
| 276 | static::reportInvalidArgument(\sprintf( |
| 277 | $message ?: 'Expected a non-empty value. Got: %s', |
| 278 | static::valueToString($value) |
| 279 | )); |
| 280 | } |
| 281 | } |
| 282 | public static function null($value, $message = '') |
| 283 | { |
| 284 | if (null !== $value) { |
| 285 | static::reportInvalidArgument(\sprintf( |
| 286 | $message ?: 'Expected null. Got: %s', |
| 287 | static::valueToString($value) |
| 288 | )); |
| 289 | } |
| 290 | } |
| 291 | public static function notNull($value, $message = '') |
| 292 | { |
| 293 | if (null === $value) { |
| 294 | static::reportInvalidArgument( |
| 295 | $message ?: 'Expected a value other than null.' |
| 296 | ); |
| 297 | } |
| 298 | } |
| 299 | public static function true($value, $message = '') |
| 300 | { |
| 301 | if (true !== $value) { |
| 302 | static::reportInvalidArgument(\sprintf( |
| 303 | $message ?: 'Expected a value to be true. Got: %s', |
| 304 | static::valueToString($value) |
| 305 | )); |
| 306 | } |
| 307 | } |
| 308 | public static function false($value, $message = '') |
| 309 | { |
| 310 | if (false !== $value) { |
| 311 | static::reportInvalidArgument(\sprintf( |
| 312 | $message ?: 'Expected a value to be false. Got: %s', |
| 313 | static::valueToString($value) |
| 314 | )); |
| 315 | } |
| 316 | } |
| 317 | public static function notFalse($value, $message = '') |
| 318 | { |
| 319 | if (false === $value) { |
| 320 | static::reportInvalidArgument( |
| 321 | $message ?: 'Expected a value other than false.' |
| 322 | ); |
| 323 | } |
| 324 | } |
| 325 | public static function ip($value, $message = '') |
| 326 | { |
| 327 | if (false === \filter_var($value, \FILTER_VALIDATE_IP)) { |
| 328 | static::reportInvalidArgument(\sprintf( |
| 329 | $message ?: 'Expected a value to be an IP. Got: %s', |
| 330 | static::valueToString($value) |
| 331 | )); |
| 332 | } |
| 333 | } |
| 334 | public static function ipv4($value, $message = '') |
| 335 | { |
| 336 | if (false === \filter_var($value, \FILTER_VALIDATE_IP, \FILTER_FLAG_IPV4)) { |
| 337 | static::reportInvalidArgument(\sprintf( |
| 338 | $message ?: 'Expected a value to be an IPv4. Got: %s', |
| 339 | static::valueToString($value) |
| 340 | )); |
| 341 | } |
| 342 | } |
| 343 | public static function ipv6($value, $message = '') |
| 344 | { |
| 345 | if (false === \filter_var($value, \FILTER_VALIDATE_IP, \FILTER_FLAG_IPV6)) { |
| 346 | static::reportInvalidArgument(\sprintf( |
| 347 | $message ?: 'Expected a value to be an IPv6. Got: %s', |
| 348 | static::valueToString($value) |
| 349 | )); |
| 350 | } |
| 351 | } |
| 352 | public static function email($value, $message = '') |
| 353 | { |
| 354 | if (false === \filter_var($value, FILTER_VALIDATE_EMAIL)) { |
| 355 | static::reportInvalidArgument(\sprintf( |
| 356 | $message ?: 'Expected a value to be a valid e-mail address. Got: %s', |
| 357 | static::valueToString($value) |
| 358 | )); |
| 359 | } |
| 360 | } |
| 361 | public static function uniqueValues(array $values, $message = '') |
| 362 | { |
| 363 | $allValues = \count($values); |
| 364 | $uniqueValues = \count(\array_unique($values)); |
| 365 | if ($allValues !== $uniqueValues) { |
| 366 | $difference = $allValues - $uniqueValues; |
| 367 | static::reportInvalidArgument(\sprintf( |
| 368 | $message ?: 'Expected an array of unique values, but %s of them %s duplicated', |
| 369 | $difference, |
| 370 | (1 === $difference ? 'is' : 'are') |
| 371 | )); |
| 372 | } |
| 373 | } |
| 374 | public static function eq($value, $expect, $message = '') |
| 375 | { |
| 376 | if ($expect != $value) { |
| 377 | static::reportInvalidArgument(\sprintf( |
| 378 | $message ?: 'Expected a value equal to %2$s. Got: %s', |
| 379 | static::valueToString($value), |
| 380 | static::valueToString($expect) |
| 381 | )); |
| 382 | } |
| 383 | } |
| 384 | public static function notEq($value, $expect, $message = '') |
| 385 | { |
| 386 | if ($expect == $value) { |
| 387 | static::reportInvalidArgument(\sprintf( |
| 388 | $message ?: 'Expected a different value than %s.', |
| 389 | static::valueToString($expect) |
| 390 | )); |
| 391 | } |
| 392 | } |
| 393 | public static function same($value, $expect, $message = '') |
| 394 | { |
| 395 | if ($expect !== $value) { |
| 396 | static::reportInvalidArgument(\sprintf( |
| 397 | $message ?: 'Expected a value identical to %2$s. Got: %s', |
| 398 | static::valueToString($value), |
| 399 | static::valueToString($expect) |
| 400 | )); |
| 401 | } |
| 402 | } |
| 403 | public static function notSame($value, $expect, $message = '') |
| 404 | { |
| 405 | if ($expect === $value) { |
| 406 | static::reportInvalidArgument(\sprintf( |
| 407 | $message ?: 'Expected a value not identical to %s.', |
| 408 | static::valueToString($expect) |
| 409 | )); |
| 410 | } |
| 411 | } |
| 412 | public static function greaterThan($value, $limit, $message = '') |
| 413 | { |
| 414 | if ($value <= $limit) { |
| 415 | static::reportInvalidArgument(\sprintf( |
| 416 | $message ?: 'Expected a value greater than %2$s. Got: %s', |
| 417 | static::valueToString($value), |
| 418 | static::valueToString($limit) |
| 419 | )); |
| 420 | } |
| 421 | } |
| 422 | public static function greaterThanEq($value, $limit, $message = '') |
| 423 | { |
| 424 | if ($value < $limit) { |
| 425 | static::reportInvalidArgument(\sprintf( |
| 426 | $message ?: 'Expected a value greater than or equal to %2$s. Got: %s', |
| 427 | static::valueToString($value), |
| 428 | static::valueToString($limit) |
| 429 | )); |
| 430 | } |
| 431 | } |
| 432 | public static function lessThan($value, $limit, $message = '') |
| 433 | { |
| 434 | if ($value >= $limit) { |
| 435 | static::reportInvalidArgument(\sprintf( |
| 436 | $message ?: 'Expected a value less than %2$s. Got: %s', |
| 437 | static::valueToString($value), |
| 438 | static::valueToString($limit) |
| 439 | )); |
| 440 | } |
| 441 | } |
| 442 | public static function lessThanEq($value, $limit, $message = '') |
| 443 | { |
| 444 | if ($value > $limit) { |
| 445 | static::reportInvalidArgument(\sprintf( |
| 446 | $message ?: 'Expected a value less than or equal to %2$s. Got: %s', |
| 447 | static::valueToString($value), |
| 448 | static::valueToString($limit) |
| 449 | )); |
| 450 | } |
| 451 | } |
| 452 | public static function range($value, $min, $max, $message = '') |
| 453 | { |
| 454 | if ($value < $min || $value > $max) { |
| 455 | static::reportInvalidArgument(\sprintf( |
| 456 | $message ?: 'Expected a value between %2$s and %3$s. Got: %s', |
| 457 | static::valueToString($value), |
| 458 | static::valueToString($min), |
| 459 | static::valueToString($max) |
| 460 | )); |
| 461 | } |
| 462 | } |
| 463 | public static function oneOf($value, array $values, $message = '') |
| 464 | { |
| 465 | static::inArray($value, $values, $message); |
| 466 | } |
| 467 | public static function inArray($value, array $values, $message = '') |
| 468 | { |
| 469 | if (!\in_array($value, $values, true)) { |
| 470 | static::reportInvalidArgument(\sprintf( |
| 471 | $message ?: 'Expected one of: %2$s. Got: %s', |
| 472 | static::valueToString($value), |
| 473 | \implode(', ', \array_map(array(static::class, 'valueToString'), $values)) |
| 474 | )); |
| 475 | } |
| 476 | } |
| 477 | public static function contains($value, $subString, $message = '') |
| 478 | { |
| 479 | if (false === \strpos($value, $subString)) { |
| 480 | static::reportInvalidArgument(\sprintf( |
| 481 | $message ?: 'Expected a value to contain %2$s. Got: %s', |
| 482 | static::valueToString($value), |
| 483 | static::valueToString($subString) |
| 484 | )); |
| 485 | } |
| 486 | } |
| 487 | public static function notContains($value, $subString, $message = '') |
| 488 | { |
| 489 | if (false !== \strpos($value, $subString)) { |
| 490 | static::reportInvalidArgument(\sprintf( |
| 491 | $message ?: '%2$s was not expected to be contained in a value. Got: %s', |
| 492 | static::valueToString($value), |
| 493 | static::valueToString($subString) |
| 494 | )); |
| 495 | } |
| 496 | } |
| 497 | public static function notWhitespaceOnly($value, $message = '') |
| 498 | { |
| 499 | if (\preg_match('/^\s*$/', $value)) { |
| 500 | static::reportInvalidArgument(\sprintf( |
| 501 | $message ?: 'Expected a non-whitespace string. Got: %s', |
| 502 | static::valueToString($value) |
| 503 | )); |
| 504 | } |
| 505 | } |
| 506 | public static function startsWith($value, $prefix, $message = '') |
| 507 | { |
| 508 | if (0 !== \strpos($value, $prefix)) { |
| 509 | static::reportInvalidArgument(\sprintf( |
| 510 | $message ?: 'Expected a value to start with %2$s. Got: %s', |
| 511 | static::valueToString($value), |
| 512 | static::valueToString($prefix) |
| 513 | )); |
| 514 | } |
| 515 | } |
| 516 | public static function notStartsWith($value, $prefix, $message = '') |
| 517 | { |
| 518 | if (0 === \strpos($value, $prefix)) { |
| 519 | static::reportInvalidArgument(\sprintf( |
| 520 | $message ?: 'Expected a value not to start with %2$s. Got: %s', |
| 521 | static::valueToString($value), |
| 522 | static::valueToString($prefix) |
| 523 | )); |
| 524 | } |
| 525 | } |
| 526 | public static function startsWithLetter($value, $message = '') |
| 527 | { |
| 528 | static::string($value); |
| 529 | $valid = isset($value[0]); |
| 530 | if ($valid) { |
| 531 | $locale = \setlocale(LC_CTYPE, 0); |
| 532 | \setlocale(LC_CTYPE, 'C'); |
| 533 | $valid = \ctype_alpha($value[0]); |
| 534 | \setlocale(LC_CTYPE, $locale); |
| 535 | } |
| 536 | if (!$valid) { |
| 537 | static::reportInvalidArgument(\sprintf( |
| 538 | $message ?: 'Expected a value to start with a letter. Got: %s', |
| 539 | static::valueToString($value) |
| 540 | )); |
| 541 | } |
| 542 | } |
| 543 | public static function endsWith($value, $suffix, $message = '') |
| 544 | { |
| 545 | if ($suffix !== \substr($value, -\strlen($suffix))) { |
| 546 | static::reportInvalidArgument(\sprintf( |
| 547 | $message ?: 'Expected a value to end with %2$s. Got: %s', |
| 548 | static::valueToString($value), |
| 549 | static::valueToString($suffix) |
| 550 | )); |
| 551 | } |
| 552 | } |
| 553 | public static function notEndsWith($value, $suffix, $message = '') |
| 554 | { |
| 555 | if ($suffix === \substr($value, -\strlen($suffix))) { |
| 556 | static::reportInvalidArgument(\sprintf( |
| 557 | $message ?: 'Expected a value not to end with %2$s. Got: %s', |
| 558 | static::valueToString($value), |
| 559 | static::valueToString($suffix) |
| 560 | )); |
| 561 | } |
| 562 | } |
| 563 | public static function regex($value, $pattern, $message = '') |
| 564 | { |
| 565 | if (!\preg_match($pattern, $value)) { |
| 566 | static::reportInvalidArgument(\sprintf( |
| 567 | $message ?: 'The value %s does not match the expected pattern.', |
| 568 | static::valueToString($value) |
| 569 | )); |
| 570 | } |
| 571 | } |
| 572 | public static function notRegex($value, $pattern, $message = '') |
| 573 | { |
| 574 | if (\preg_match($pattern, $value, $matches, PREG_OFFSET_CAPTURE)) { |
| 575 | static::reportInvalidArgument(\sprintf( |
| 576 | $message ?: 'The value %s matches the pattern %s (at offset %d).', |
| 577 | static::valueToString($value), |
| 578 | static::valueToString($pattern), |
| 579 | $matches[0][1] |
| 580 | )); |
| 581 | } |
| 582 | } |
| 583 | public static function unicodeLetters($value, $message = '') |
| 584 | { |
| 585 | static::string($value); |
| 586 | if (!\preg_match('/^\p{L}+$/u', $value)) { |
| 587 | static::reportInvalidArgument(\sprintf( |
| 588 | $message ?: 'Expected a value to contain only Unicode letters. Got: %s', |
| 589 | static::valueToString($value) |
| 590 | )); |
| 591 | } |
| 592 | } |
| 593 | public static function alpha($value, $message = '') |
| 594 | { |
| 595 | static::string($value); |
| 596 | $locale = \setlocale(LC_CTYPE, 0); |
| 597 | \setlocale(LC_CTYPE, 'C'); |
| 598 | $valid = !\ctype_alpha($value); |
| 599 | \setlocale(LC_CTYPE, $locale); |
| 600 | if ($valid) { |
| 601 | static::reportInvalidArgument(\sprintf( |
| 602 | $message ?: 'Expected a value to contain only letters. Got: %s', |
| 603 | static::valueToString($value) |
| 604 | )); |
| 605 | } |
| 606 | } |
| 607 | public static function digits($value, $message = '') |
| 608 | { |
| 609 | $locale = \setlocale(LC_CTYPE, 0); |
| 610 | \setlocale(LC_CTYPE, 'C'); |
| 611 | $valid = !\ctype_digit($value); |
| 612 | \setlocale(LC_CTYPE, $locale); |
| 613 | if ($valid) { |
| 614 | static::reportInvalidArgument(\sprintf( |
| 615 | $message ?: 'Expected a value to contain digits only. Got: %s', |
| 616 | static::valueToString($value) |
| 617 | )); |
| 618 | } |
| 619 | } |
| 620 | public static function alnum($value, $message = '') |
| 621 | { |
| 622 | $locale = \setlocale(LC_CTYPE, 0); |
| 623 | \setlocale(LC_CTYPE, 'C'); |
| 624 | $valid = !\ctype_alnum($value); |
| 625 | \setlocale(LC_CTYPE, $locale); |
| 626 | if ($valid) { |
| 627 | static::reportInvalidArgument(\sprintf( |
| 628 | $message ?: 'Expected a value to contain letters and digits only. Got: %s', |
| 629 | static::valueToString($value) |
| 630 | )); |
| 631 | } |
| 632 | } |
| 633 | public static function lower($value, $message = '') |
| 634 | { |
| 635 | $locale = \setlocale(LC_CTYPE, 0); |
| 636 | \setlocale(LC_CTYPE, 'C'); |
| 637 | $valid = !\ctype_lower($value); |
| 638 | \setlocale(LC_CTYPE, $locale); |
| 639 | if ($valid) { |
| 640 | static::reportInvalidArgument(\sprintf( |
| 641 | $message ?: 'Expected a value to contain lowercase characters only. Got: %s', |
| 642 | static::valueToString($value) |
| 643 | )); |
| 644 | } |
| 645 | } |
| 646 | public static function upper($value, $message = '') |
| 647 | { |
| 648 | $locale = \setlocale(LC_CTYPE, 0); |
| 649 | \setlocale(LC_CTYPE, 'C'); |
| 650 | $valid = !\ctype_upper($value); |
| 651 | \setlocale(LC_CTYPE, $locale); |
| 652 | if ($valid) { |
| 653 | static::reportInvalidArgument(\sprintf( |
| 654 | $message ?: 'Expected a value to contain uppercase characters only. Got: %s', |
| 655 | static::valueToString($value) |
| 656 | )); |
| 657 | } |
| 658 | } |
| 659 | public static function length($value, $length, $message = '') |
| 660 | { |
| 661 | if ($length !== static::strlen($value)) { |
| 662 | static::reportInvalidArgument(\sprintf( |
| 663 | $message ?: 'Expected a value to contain %2$s characters. Got: %s', |
| 664 | static::valueToString($value), |
| 665 | $length |
| 666 | )); |
| 667 | } |
| 668 | } |
| 669 | public static function minLength($value, $min, $message = '') |
| 670 | { |
| 671 | if (static::strlen($value) < $min) { |
| 672 | static::reportInvalidArgument(\sprintf( |
| 673 | $message ?: 'Expected a value to contain at least %2$s characters. Got: %s', |
| 674 | static::valueToString($value), |
| 675 | $min |
| 676 | )); |
| 677 | } |
| 678 | } |
| 679 | public static function maxLength($value, $max, $message = '') |
| 680 | { |
| 681 | if (static::strlen($value) > $max) { |
| 682 | static::reportInvalidArgument(\sprintf( |
| 683 | $message ?: 'Expected a value to contain at most %2$s characters. Got: %s', |
| 684 | static::valueToString($value), |
| 685 | $max |
| 686 | )); |
| 687 | } |
| 688 | } |
| 689 | public static function lengthBetween($value, $min, $max, $message = '') |
| 690 | { |
| 691 | $length = static::strlen($value); |
| 692 | if ($length < $min || $length > $max) { |
| 693 | static::reportInvalidArgument(\sprintf( |
| 694 | $message ?: 'Expected a value to contain between %2$s and %3$s characters. Got: %s', |
| 695 | static::valueToString($value), |
| 696 | $min, |
| 697 | $max |
| 698 | )); |
| 699 | } |
| 700 | } |
| 701 | public static function fileExists($value, $message = '') |
| 702 | { |
| 703 | static::string($value); |
| 704 | if (!\file_exists($value)) { |
| 705 | static::reportInvalidArgument(\sprintf( |
| 706 | $message ?: 'The file %s does not exist.', |
| 707 | static::valueToString($value) |
| 708 | )); |
| 709 | } |
| 710 | } |
| 711 | public static function file($value, $message = '') |
| 712 | { |
| 713 | static::fileExists($value, $message); |
| 714 | if (!\is_file($value)) { |
| 715 | static::reportInvalidArgument(\sprintf( |
| 716 | $message ?: 'The path %s is not a file.', |
| 717 | static::valueToString($value) |
| 718 | )); |
| 719 | } |
| 720 | } |
| 721 | public static function directory($value, $message = '') |
| 722 | { |
| 723 | static::fileExists($value, $message); |
| 724 | if (!\is_dir($value)) { |
| 725 | static::reportInvalidArgument(\sprintf( |
| 726 | $message ?: 'The path %s is no directory.', |
| 727 | static::valueToString($value) |
| 728 | )); |
| 729 | } |
| 730 | } |
| 731 | public static function readable($value, $message = '') |
| 732 | { |
| 733 | if (!\is_readable($value)) { |
| 734 | static::reportInvalidArgument(\sprintf( |
| 735 | $message ?: 'The path %s is not readable.', |
| 736 | static::valueToString($value) |
| 737 | )); |
| 738 | } |
| 739 | } |
| 740 | public static function writable($value, $message = '') |
| 741 | { |
| 742 | if (!\is_writable($value)) { |
| 743 | static::reportInvalidArgument(\sprintf( |
| 744 | $message ?: 'The path %s is not writable.', |
| 745 | static::valueToString($value) |
| 746 | )); |
| 747 | } |
| 748 | } |
| 749 | public static function classExists($value, $message = '') |
| 750 | { |
| 751 | if (!\class_exists($value)) { |
| 752 | static::reportInvalidArgument(\sprintf( |
| 753 | $message ?: 'Expected an existing class name. Got: %s', |
| 754 | static::valueToString($value) |
| 755 | )); |
| 756 | } |
| 757 | } |
| 758 | public static function subclassOf($value, $class, $message = '') |
| 759 | { |
| 760 | if (!\is_subclass_of($value, $class)) { |
| 761 | static::reportInvalidArgument(\sprintf( |
| 762 | $message ?: 'Expected a sub-class of %2$s. Got: %s', |
| 763 | static::valueToString($value), |
| 764 | static::valueToString($class) |
| 765 | )); |
| 766 | } |
| 767 | } |
| 768 | public static function interfaceExists($value, $message = '') |
| 769 | { |
| 770 | if (!\interface_exists($value)) { |
| 771 | static::reportInvalidArgument(\sprintf( |
| 772 | $message ?: 'Expected an existing interface name. got %s', |
| 773 | static::valueToString($value) |
| 774 | )); |
| 775 | } |
| 776 | } |
| 777 | public static function implementsInterface($value, $interface, $message = '') |
| 778 | { |
| 779 | if (!\in_array($interface, \class_implements($value))) { |
| 780 | static::reportInvalidArgument(\sprintf( |
| 781 | $message ?: 'Expected an implementation of %2$s. Got: %s', |
| 782 | static::valueToString($value), |
| 783 | static::valueToString($interface) |
| 784 | )); |
| 785 | } |
| 786 | } |
| 787 | public static function propertyExists($classOrObject, $property, $message = '') |
| 788 | { |
| 789 | if (!\property_exists($classOrObject, $property)) { |
| 790 | static::reportInvalidArgument(\sprintf( |
| 791 | $message ?: 'Expected the property %s to exist.', |
| 792 | static::valueToString($property) |
| 793 | )); |
| 794 | } |
| 795 | } |
| 796 | public static function propertyNotExists($classOrObject, $property, $message = '') |
| 797 | { |
| 798 | if (\property_exists($classOrObject, $property)) { |
| 799 | static::reportInvalidArgument(\sprintf( |
| 800 | $message ?: 'Expected the property %s to not exist.', |
| 801 | static::valueToString($property) |
| 802 | )); |
| 803 | } |
| 804 | } |
| 805 | public static function methodExists($classOrObject, $method, $message = '') |
| 806 | { |
| 807 | if (!(\is_string($classOrObject) || \is_object($classOrObject)) || !\method_exists($classOrObject, $method)) { |
| 808 | static::reportInvalidArgument(\sprintf( |
| 809 | $message ?: 'Expected the method %s to exist.', |
| 810 | static::valueToString($method) |
| 811 | )); |
| 812 | } |
| 813 | } |
| 814 | public static function methodNotExists($classOrObject, $method, $message = '') |
| 815 | { |
| 816 | if ((\is_string($classOrObject) || \is_object($classOrObject)) && \method_exists($classOrObject, $method)) { |
| 817 | static::reportInvalidArgument(\sprintf( |
| 818 | $message ?: 'Expected the method %s to not exist.', |
| 819 | static::valueToString($method) |
| 820 | )); |
| 821 | } |
| 822 | } |
| 823 | public static function keyExists($array, $key, $message = '') |
| 824 | { |
| 825 | if (!(isset($array[$key]) || \array_key_exists($key, $array))) { |
| 826 | static::reportInvalidArgument(\sprintf( |
| 827 | $message ?: 'Expected the key %s to exist.', |
| 828 | static::valueToString($key) |
| 829 | )); |
| 830 | } |
| 831 | } |
| 832 | public static function keyNotExists($array, $key, $message = '') |
| 833 | { |
| 834 | if (isset($array[$key]) || \array_key_exists($key, $array)) { |
| 835 | static::reportInvalidArgument(\sprintf( |
| 836 | $message ?: 'Expected the key %s to not exist.', |
| 837 | static::valueToString($key) |
| 838 | )); |
| 839 | } |
| 840 | } |
| 841 | public static function validArrayKey($value, $message = '') |
| 842 | { |
| 843 | if (!(\is_int($value) || \is_string($value))) { |
| 844 | static::reportInvalidArgument(\sprintf( |
| 845 | $message ?: 'Expected string or integer. Got: %s', |
| 846 | static::typeToString($value) |
| 847 | )); |
| 848 | } |
| 849 | } |
| 850 | public static function count($array, $number, $message = '') |
| 851 | { |
| 852 | static::eq( |
| 853 | \count($array), |
| 854 | $number, |
| 855 | \sprintf( |
| 856 | $message ?: 'Expected an array to contain %d elements. Got: %d.', |
| 857 | $number, |
| 858 | \count($array) |
| 859 | ) |
| 860 | ); |
| 861 | } |
| 862 | public static function minCount($array, $min, $message = '') |
| 863 | { |
| 864 | if (\count($array) < $min) { |
| 865 | static::reportInvalidArgument(\sprintf( |
| 866 | $message ?: 'Expected an array to contain at least %2$d elements. Got: %d', |
| 867 | \count($array), |
| 868 | $min |
| 869 | )); |
| 870 | } |
| 871 | } |
| 872 | public static function maxCount($array, $max, $message = '') |
| 873 | { |
| 874 | if (\count($array) > $max) { |
| 875 | static::reportInvalidArgument(\sprintf( |
| 876 | $message ?: 'Expected an array to contain at most %2$d elements. Got: %d', |
| 877 | \count($array), |
| 878 | $max |
| 879 | )); |
| 880 | } |
| 881 | } |
| 882 | public static function countBetween($array, $min, $max, $message = '') |
| 883 | { |
| 884 | $count = \count($array); |
| 885 | if ($count < $min || $count > $max) { |
| 886 | static::reportInvalidArgument(\sprintf( |
| 887 | $message ?: 'Expected an array to contain between %2$d and %3$d elements. Got: %d', |
| 888 | $count, |
| 889 | $min, |
| 890 | $max |
| 891 | )); |
| 892 | } |
| 893 | } |
| 894 | public static function isList($array, $message = '') |
| 895 | { |
| 896 | if (!\is_array($array)) { |
| 897 | static::reportInvalidArgument( |
| 898 | $message ?: 'Expected list - non-associative array.' |
| 899 | ); |
| 900 | } |
| 901 | if ($array === \array_values($array)) { |
| 902 | return; |
| 903 | } |
| 904 | $nextKey = -1; |
| 905 | foreach ($array as $k => $v) { |
| 906 | if ($k !== ++$nextKey) { |
| 907 | static::reportInvalidArgument( |
| 908 | $message ?: 'Expected list - non-associative array.' |
| 909 | ); |
| 910 | } |
| 911 | } |
| 912 | } |
| 913 | public static function isNonEmptyList($array, $message = '') |
| 914 | { |
| 915 | static::isList($array, $message); |
| 916 | static::notEmpty($array, $message); |
| 917 | } |
| 918 | public static function isMap($array, $message = '') |
| 919 | { |
| 920 | if ( |
| 921 | !\is_array($array) || |
| 922 | \array_keys($array) !== \array_filter(\array_keys($array), '\is_string') |
| 923 | ) { |
| 924 | static::reportInvalidArgument( |
| 925 | $message ?: 'Expected map - associative array with string keys.' |
| 926 | ); |
| 927 | } |
| 928 | } |
| 929 | public static function isNonEmptyMap($array, $message = '') |
| 930 | { |
| 931 | static::isMap($array, $message); |
| 932 | static::notEmpty($array, $message); |
| 933 | } |
| 934 | public static function uuid($value, $message = '') |
| 935 | { |
| 936 | $value = \str_replace(array('urn:', 'uuid:', '{', '}'), '', $value); |
| 937 | // The nil UUID is special form of UUID that is specified to have all |
| 938 | // 128 bits set to zero. |
| 939 | if ('00000000-0000-0000-0000-000000000000' === $value) { |
| 940 | return; |
| 941 | } |
| 942 | 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)) { |
| 943 | static::reportInvalidArgument(\sprintf( |
| 944 | $message ?: 'Value %s is not a valid UUID.', |
| 945 | static::valueToString($value) |
| 946 | )); |
| 947 | } |
| 948 | } |
| 949 | public static function throws(Closure $expression, $class = 'Exception', $message = '') |
| 950 | { |
| 951 | static::string($class); |
| 952 | $actual = 'none'; |
| 953 | try { |
| 954 | $expression(); |
| 955 | } catch (Exception $e) { |
| 956 | $actual = \get_class($e); |
| 957 | if ($e instanceof $class) { |
| 958 | return; |
| 959 | } |
| 960 | } catch (Throwable $e) { |
| 961 | $actual = \get_class($e); |
| 962 | if ($e instanceof $class) { |
| 963 | return; |
| 964 | } |
| 965 | } |
| 966 | static::reportInvalidArgument($message ?: \sprintf( |
| 967 | 'Expected to throw "%s", got "%s"', |
| 968 | $class, |
| 969 | $actual |
| 970 | )); |
| 971 | } |
| 972 | public static function __callStatic($name, $arguments) |
| 973 | { |
| 974 | if ('nullOr' === \substr($name, 0, 6)) { |
| 975 | if (null !== $arguments[0]) { |
| 976 | $method = \lcfirst(\substr($name, 6)); |
| 977 | \call_user_func_array(array(static::class, $method), $arguments); |
| 978 | } |
| 979 | return; |
| 980 | } |
| 981 | if ('all' === \substr($name, 0, 3)) { |
| 982 | static::isIterable($arguments[0]); |
| 983 | $method = \lcfirst(\substr($name, 3)); |
| 984 | $args = $arguments; |
| 985 | foreach ($arguments[0] as $entry) { |
| 986 | $args[0] = $entry; |
| 987 | \call_user_func_array(array(static::class, $method), $args); |
| 988 | } |
| 989 | return; |
| 990 | } |
| 991 | throw new BadMethodCallException('No such method: '.$name); |
| 992 | } |
| 993 | protected static function valueToString($value) |
| 994 | { |
| 995 | if (null === $value) { |
| 996 | return 'null'; |
| 997 | } |
| 998 | if (true === $value) { |
| 999 | return 'true'; |
| 1000 | } |
| 1001 | if (false === $value) { |
| 1002 | return 'false'; |
| 1003 | } |
| 1004 | if (\is_array($value)) { |
| 1005 | return 'array'; |
| 1006 | } |
| 1007 | if (\is_object($value)) { |
| 1008 | if (\method_exists($value, '__toString')) { |
| 1009 | return \get_class($value).': '.self::valueToString($value->__toString()); |
| 1010 | } |
| 1011 | if ($value instanceof DateTime || $value instanceof DateTimeImmutable) { |
| 1012 | return \get_class($value).': '.self::valueToString($value->format('c')); |
| 1013 | } |
| 1014 | return \get_class($value); |
| 1015 | } |
| 1016 | if (\is_resource($value)) { |
| 1017 | return 'resource'; |
| 1018 | } |
| 1019 | if (\is_string($value)) { |
| 1020 | return '"'.$value.'"'; |
| 1021 | } |
| 1022 | return (string) $value; |
| 1023 | } |
| 1024 | protected static function typeToString($value) |
| 1025 | { |
| 1026 | return \is_object($value) ? \get_class($value) : \gettype($value); |
| 1027 | } |
| 1028 | protected static function strlen($value) |
| 1029 | { |
| 1030 | if (!\function_exists('mb_detect_encoding')) { |
| 1031 | return \strlen($value); |
| 1032 | } |
| 1033 | if (false === $encoding = \mb_detect_encoding($value)) { |
| 1034 | return \strlen($value); |
| 1035 | } |
| 1036 | return \mb_strlen($value, $encoding); |
| 1037 | } |
| 1038 | protected static function reportInvalidArgument($message) |
| 1039 | { |
| 1040 | throw new InvalidArgumentException($message); |
| 1041 | } |
| 1042 | private function __construct() |
| 1043 | { |
| 1044 | } |
| 1045 | } |
| 1046 |