| Total Complexity | 279 |
| Total Lines | 999 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like Assert 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 Assert, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 187 | class Assert |
||
| 188 | { |
||
| 189 | public static function string($value, $message = '') |
||
| 190 | { |
||
| 191 | if (!is_string($value)) { |
||
| 192 | static::reportInvalidArgument(sprintf( |
||
| 193 | $message ?: 'Expected a string. Got: %s', |
||
| 194 | static::typeToString($value) |
||
| 195 | )); |
||
| 196 | } |
||
| 197 | } |
||
| 198 | |||
| 199 | public static function stringNotEmpty($value, $message = '') |
||
| 200 | { |
||
| 201 | static::string($value, $message); |
||
| 202 | static::notEq($value, '', $message); |
||
| 203 | } |
||
| 204 | |||
| 205 | public static function integer($value, $message = '') |
||
| 206 | { |
||
| 207 | if (!is_int($value)) { |
||
| 208 | static::reportInvalidArgument(sprintf( |
||
| 209 | $message ?: 'Expected an integer. Got: %s', |
||
| 210 | static::typeToString($value) |
||
| 211 | )); |
||
| 212 | } |
||
| 213 | } |
||
| 214 | |||
| 215 | public static function integerish($value, $message = '') |
||
| 216 | { |
||
| 217 | if (!is_numeric($value) || $value != (int) $value) { |
||
| 218 | static::reportInvalidArgument(sprintf( |
||
| 219 | $message ?: 'Expected an integerish value. Got: %s', |
||
| 220 | static::typeToString($value) |
||
| 221 | )); |
||
| 222 | } |
||
| 223 | } |
||
| 224 | |||
| 225 | public static function float($value, $message = '') |
||
| 226 | { |
||
| 227 | if (!is_float($value)) { |
||
| 228 | static::reportInvalidArgument(sprintf( |
||
| 229 | $message ?: 'Expected a float. Got: %s', |
||
| 230 | static::typeToString($value) |
||
| 231 | )); |
||
| 232 | } |
||
| 233 | } |
||
| 234 | |||
| 235 | public static function numeric($value, $message = '') |
||
| 236 | { |
||
| 237 | if (!is_numeric($value)) { |
||
| 238 | static::reportInvalidArgument(sprintf( |
||
| 239 | $message ?: 'Expected a numeric. Got: %s', |
||
| 240 | static::typeToString($value) |
||
| 241 | )); |
||
| 242 | } |
||
| 243 | } |
||
| 244 | |||
| 245 | public static function natural($value, $message = '') |
||
| 246 | { |
||
| 247 | if (!is_int($value) || $value < 0) { |
||
| 248 | static::reportInvalidArgument(sprintf( |
||
| 249 | $message ?: 'Expected a non-negative integer. Got %s', |
||
| 250 | static::valueToString($value) |
||
| 251 | )); |
||
| 252 | } |
||
| 253 | } |
||
| 254 | |||
| 255 | public static function boolean($value, $message = '') |
||
| 256 | { |
||
| 257 | if (!is_bool($value)) { |
||
| 258 | static::reportInvalidArgument(sprintf( |
||
| 259 | $message ?: 'Expected a boolean. Got: %s', |
||
| 260 | static::typeToString($value) |
||
| 261 | )); |
||
| 262 | } |
||
| 263 | } |
||
| 264 | |||
| 265 | public static function scalar($value, $message = '') |
||
| 266 | { |
||
| 267 | if (!is_scalar($value)) { |
||
| 268 | static::reportInvalidArgument(sprintf( |
||
| 269 | $message ?: 'Expected a scalar. Got: %s', |
||
| 270 | static::typeToString($value) |
||
| 271 | )); |
||
| 272 | } |
||
| 273 | } |
||
| 274 | |||
| 275 | public static function object($value, $message = '') |
||
| 276 | { |
||
| 277 | if (!is_object($value)) { |
||
| 278 | static::reportInvalidArgument(sprintf( |
||
| 279 | $message ?: 'Expected an object. Got: %s', |
||
| 280 | static::typeToString($value) |
||
| 281 | )); |
||
| 282 | } |
||
| 283 | } |
||
| 284 | |||
| 285 | public static function resource($value, $type = null, $message = '') |
||
| 286 | { |
||
| 287 | if (!is_resource($value)) { |
||
| 288 | static::reportInvalidArgument(sprintf( |
||
| 289 | $message ?: 'Expected a resource. Got: %s', |
||
| 290 | static::typeToString($value) |
||
| 291 | )); |
||
| 292 | } |
||
| 293 | |||
| 294 | if ($type && $type !== get_resource_type($value)) { |
||
| 295 | static::reportInvalidArgument(sprintf( |
||
| 296 | $message ?: 'Expected a resource of type %2$s. Got: %s', |
||
| 297 | static::typeToString($value), |
||
| 298 | $type |
||
| 299 | )); |
||
| 300 | } |
||
| 301 | } |
||
| 302 | |||
| 303 | public static function isCallable($value, $message = '') |
||
| 304 | { |
||
| 305 | if (!is_callable($value)) { |
||
| 306 | static::reportInvalidArgument(sprintf( |
||
| 307 | $message ?: 'Expected a callable. Got: %s', |
||
| 308 | static::typeToString($value) |
||
| 309 | )); |
||
| 310 | } |
||
| 311 | } |
||
| 312 | |||
| 313 | public static function isArray($value, $message = '') |
||
| 314 | { |
||
| 315 | if (!is_array($value)) { |
||
| 316 | static::reportInvalidArgument(sprintf( |
||
| 317 | $message ?: 'Expected an array. Got: %s', |
||
| 318 | static::typeToString($value) |
||
| 319 | )); |
||
| 320 | } |
||
| 321 | } |
||
| 322 | |||
| 323 | public static function isTraversable($value, $message = '') |
||
| 324 | { |
||
| 325 | @trigger_error( |
||
| 326 | sprintf( |
||
| 327 | '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.', |
||
| 328 | __METHOD__ |
||
| 329 | ), |
||
| 330 | E_USER_DEPRECATED |
||
| 331 | ); |
||
| 332 | |||
| 333 | if (!is_array($value) && !($value instanceof Traversable)) { |
||
| 334 | static::reportInvalidArgument(sprintf( |
||
| 335 | $message ?: 'Expected a traversable. Got: %s', |
||
| 336 | static::typeToString($value) |
||
| 337 | )); |
||
| 338 | } |
||
| 339 | } |
||
| 340 | |||
| 341 | public static function isArrayAccessible($value, $message = '') |
||
| 342 | { |
||
| 343 | if (!is_array($value) && !($value instanceof ArrayAccess)) { |
||
| 344 | static::reportInvalidArgument(sprintf( |
||
| 345 | $message ?: 'Expected an array accessible. Got: %s', |
||
| 346 | static::typeToString($value) |
||
| 347 | )); |
||
| 348 | } |
||
| 349 | } |
||
| 350 | |||
| 351 | public static function isCountable($value, $message = '') |
||
| 352 | { |
||
| 353 | if (!is_array($value) && !($value instanceof Countable)) { |
||
| 354 | static::reportInvalidArgument(sprintf( |
||
| 355 | $message ?: 'Expected a countable. Got: %s', |
||
| 356 | static::typeToString($value) |
||
| 357 | )); |
||
| 358 | } |
||
| 359 | } |
||
| 360 | |||
| 361 | public static function isIterable($value, $message = '') |
||
| 362 | { |
||
| 363 | if (!is_array($value) && !($value instanceof Traversable)) { |
||
| 364 | static::reportInvalidArgument(sprintf( |
||
| 365 | $message ?: 'Expected an iterable. Got: %s', |
||
| 366 | static::typeToString($value) |
||
| 367 | )); |
||
| 368 | } |
||
| 369 | } |
||
| 370 | |||
| 371 | public static function isInstanceOf($value, $class, $message = '') |
||
| 378 | )); |
||
| 379 | } |
||
| 380 | } |
||
| 381 | |||
| 382 | public static function notInstanceOf($value, $class, $message = '') |
||
| 383 | { |
||
| 384 | if ($value instanceof $class) { |
||
| 385 | static::reportInvalidArgument(sprintf( |
||
| 386 | $message ?: 'Expected an instance other than %2$s. Got: %s', |
||
| 387 | static::typeToString($value), |
||
| 388 | $class |
||
| 389 | )); |
||
| 390 | } |
||
| 391 | } |
||
| 392 | |||
| 393 | public static function isInstanceOfAny($value, array $classes, $message = '') |
||
| 394 | { |
||
| 395 | foreach ($classes as $class) { |
||
| 396 | if ($value instanceof $class) { |
||
| 397 | return; |
||
| 398 | } |
||
| 399 | } |
||
| 400 | |||
| 401 | static::reportInvalidArgument(sprintf( |
||
| 402 | $message ?: 'Expected an instance of any of %2$s. Got: %s', |
||
| 403 | static::typeToString($value), |
||
| 404 | implode(', ', array_map(array('static', 'valueToString'), $classes)) |
||
| 405 | )); |
||
| 406 | } |
||
| 407 | |||
| 408 | public static function isEmpty($value, $message = '') |
||
| 409 | { |
||
| 410 | if (!empty($value)) { |
||
| 411 | static::reportInvalidArgument(sprintf( |
||
| 412 | $message ?: 'Expected an empty value. Got: %s', |
||
| 413 | static::valueToString($value) |
||
| 414 | )); |
||
| 415 | } |
||
| 416 | } |
||
| 417 | |||
| 418 | public static function notEmpty($value, $message = '') |
||
| 419 | { |
||
| 420 | if (empty($value)) { |
||
| 421 | static::reportInvalidArgument(sprintf( |
||
| 422 | $message ?: 'Expected a non-empty value. Got: %s', |
||
| 423 | static::valueToString($value) |
||
| 424 | )); |
||
| 425 | } |
||
| 426 | } |
||
| 427 | |||
| 428 | public static function null($value, $message = '') |
||
| 429 | { |
||
| 430 | if (null !== $value) { |
||
| 431 | static::reportInvalidArgument(sprintf( |
||
| 432 | $message ?: 'Expected null. Got: %s', |
||
| 433 | static::valueToString($value) |
||
| 434 | )); |
||
| 435 | } |
||
| 436 | } |
||
| 437 | |||
| 438 | public static function notNull($value, $message = '') |
||
| 439 | { |
||
| 440 | if (null === $value) { |
||
| 441 | static::reportInvalidArgument( |
||
| 442 | $message ?: 'Expected a value other than null.' |
||
| 443 | ); |
||
| 444 | } |
||
| 445 | } |
||
| 446 | |||
| 447 | public static function true($value, $message = '') |
||
| 448 | { |
||
| 449 | if (true !== $value) { |
||
| 450 | static::reportInvalidArgument(sprintf( |
||
| 451 | $message ?: 'Expected a value to be true. Got: %s', |
||
| 452 | static::valueToString($value) |
||
| 453 | )); |
||
| 454 | } |
||
| 455 | } |
||
| 456 | |||
| 457 | public static function false($value, $message = '') |
||
| 458 | { |
||
| 459 | if (false !== $value) { |
||
| 460 | static::reportInvalidArgument(sprintf( |
||
| 461 | $message ?: 'Expected a value to be false. Got: %s', |
||
| 462 | static::valueToString($value) |
||
| 463 | )); |
||
| 464 | } |
||
| 465 | } |
||
| 466 | |||
| 467 | public static function ip($value, $message = '') |
||
| 468 | { |
||
| 469 | if (false === filter_var($value, FILTER_VALIDATE_IP)) { |
||
| 470 | static::reportInvalidArgument(sprintf( |
||
| 471 | $message ?: 'Expected a value to be an IP. Got: %s', |
||
| 472 | static::valueToString($value) |
||
| 473 | )); |
||
| 474 | } |
||
| 475 | } |
||
| 476 | |||
| 477 | public static function ipv4($value, $message = '') |
||
| 478 | { |
||
| 479 | if (false === filter_var($value, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) { |
||
| 480 | static::reportInvalidArgument(sprintf( |
||
| 481 | $message ?: 'Expected a value to be an IPv4. Got: %s', |
||
| 482 | static::valueToString($value) |
||
| 483 | )); |
||
| 484 | } |
||
| 485 | } |
||
| 486 | |||
| 487 | public static function ipv6($value, $message = '') |
||
| 488 | { |
||
| 489 | if (false === filter_var($value, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) { |
||
| 490 | static::reportInvalidArgument(sprintf( |
||
| 491 | $message ?: 'Expected a value to be an IPv6. Got %s', |
||
| 492 | static::valueToString($value) |
||
| 493 | )); |
||
| 494 | } |
||
| 495 | } |
||
| 496 | |||
| 497 | public static function eq($value, $value2, $message = '') |
||
| 498 | { |
||
| 499 | if ($value2 != $value) { |
||
| 500 | static::reportInvalidArgument(sprintf( |
||
| 501 | $message ?: 'Expected a value equal to %2$s. Got: %s', |
||
| 502 | static::valueToString($value), |
||
| 503 | static::valueToString($value2) |
||
| 504 | )); |
||
| 505 | } |
||
| 506 | } |
||
| 507 | |||
| 508 | public static function notEq($value, $value2, $message = '') |
||
| 509 | { |
||
| 510 | if ($value2 == $value) { |
||
| 511 | static::reportInvalidArgument(sprintf( |
||
| 512 | $message ?: 'Expected a different value than %s.', |
||
| 513 | static::valueToString($value2) |
||
| 514 | )); |
||
| 515 | } |
||
| 516 | } |
||
| 517 | |||
| 518 | public static function same($value, $value2, $message = '') |
||
| 519 | { |
||
| 520 | if ($value2 !== $value) { |
||
| 521 | static::reportInvalidArgument(sprintf( |
||
| 522 | $message ?: 'Expected a value identical to %2$s. Got: %s', |
||
| 523 | static::valueToString($value), |
||
| 524 | static::valueToString($value2) |
||
| 525 | )); |
||
| 526 | } |
||
| 527 | } |
||
| 528 | |||
| 529 | public static function notSame($value, $value2, $message = '') |
||
| 530 | { |
||
| 531 | if ($value2 === $value) { |
||
| 532 | static::reportInvalidArgument(sprintf( |
||
| 533 | $message ?: 'Expected a value not identical to %s.', |
||
| 534 | static::valueToString($value2) |
||
| 535 | )); |
||
| 536 | } |
||
| 537 | } |
||
| 538 | |||
| 539 | public static function greaterThan($value, $limit, $message = '') |
||
| 540 | { |
||
| 541 | if ($value <= $limit) { |
||
| 542 | static::reportInvalidArgument(sprintf( |
||
| 543 | $message ?: 'Expected a value greater than %2$s. Got: %s', |
||
| 544 | static::valueToString($value), |
||
| 545 | static::valueToString($limit) |
||
| 546 | )); |
||
| 547 | } |
||
| 548 | } |
||
| 549 | |||
| 550 | public static function greaterThanEq($value, $limit, $message = '') |
||
| 551 | { |
||
| 552 | if ($value < $limit) { |
||
| 553 | static::reportInvalidArgument(sprintf( |
||
| 554 | $message ?: 'Expected a value greater than or equal to %2$s. Got: %s', |
||
| 555 | static::valueToString($value), |
||
| 556 | static::valueToString($limit) |
||
| 557 | )); |
||
| 558 | } |
||
| 559 | } |
||
| 560 | |||
| 561 | public static function lessThan($value, $limit, $message = '') |
||
| 562 | { |
||
| 563 | if ($value >= $limit) { |
||
| 564 | static::reportInvalidArgument(sprintf( |
||
| 565 | $message ?: 'Expected a value less than %2$s. Got: %s', |
||
| 566 | static::valueToString($value), |
||
| 567 | static::valueToString($limit) |
||
| 568 | )); |
||
| 569 | } |
||
| 570 | } |
||
| 571 | |||
| 572 | public static function lessThanEq($value, $limit, $message = '') |
||
| 573 | { |
||
| 574 | if ($value > $limit) { |
||
| 575 | static::reportInvalidArgument(sprintf( |
||
| 576 | $message ?: 'Expected a value less than or equal to %2$s. Got: %s', |
||
| 577 | static::valueToString($value), |
||
| 578 | static::valueToString($limit) |
||
| 579 | )); |
||
| 580 | } |
||
| 581 | } |
||
| 582 | |||
| 583 | public static function range($value, $min, $max, $message = '') |
||
| 584 | { |
||
| 585 | if ($value < $min || $value > $max) { |
||
| 586 | static::reportInvalidArgument(sprintf( |
||
| 587 | $message ?: 'Expected a value between %2$s and %3$s. Got: %s', |
||
| 588 | static::valueToString($value), |
||
| 589 | static::valueToString($min), |
||
| 590 | static::valueToString($max) |
||
| 591 | )); |
||
| 592 | } |
||
| 593 | } |
||
| 594 | |||
| 595 | public static function oneOf($value, array $values, $message = '') |
||
| 596 | { |
||
| 597 | if (!in_array($value, $values, true)) { |
||
| 598 | static::reportInvalidArgument(sprintf( |
||
| 599 | $message ?: 'Expected one of: %2$s. Got: %s', |
||
| 600 | static::valueToString($value), |
||
| 601 | implode(', ', array_map(array('static', 'valueToString'), $values)) |
||
| 602 | )); |
||
| 603 | } |
||
| 604 | } |
||
| 605 | |||
| 606 | public static function contains($value, $subString, $message = '') |
||
| 607 | { |
||
| 608 | if (false === strpos($value, $subString)) { |
||
| 609 | static::reportInvalidArgument(sprintf( |
||
| 610 | $message ?: 'Expected a value to contain %2$s. Got: %s', |
||
| 611 | static::valueToString($value), |
||
| 612 | static::valueToString($subString) |
||
| 613 | )); |
||
| 614 | } |
||
| 615 | } |
||
| 616 | |||
| 617 | public static function notContains($value, $subString, $message = '') |
||
| 618 | { |
||
| 619 | if (false !== strpos($value, $subString)) { |
||
| 620 | static::reportInvalidArgument(sprintf( |
||
| 621 | $message ?: '%2$s was not expected to be contained in a value. Got: %s', |
||
| 622 | static::valueToString($value), |
||
| 623 | static::valueToString($subString) |
||
| 624 | )); |
||
| 625 | } |
||
| 626 | } |
||
| 627 | |||
| 628 | public static function notWhitespaceOnly($value, $message = '') |
||
| 629 | { |
||
| 630 | if (preg_match('/^\s*$/', $value)) { |
||
| 631 | static::reportInvalidArgument(sprintf( |
||
| 632 | $message ?: 'Expected a non-whitespace string. Got: %s', |
||
| 633 | static::valueToString($value) |
||
| 634 | )); |
||
| 635 | } |
||
| 636 | } |
||
| 637 | |||
| 638 | public static function startsWith($value, $prefix, $message = '') |
||
| 639 | { |
||
| 640 | if (0 !== strpos($value, $prefix)) { |
||
| 641 | static::reportInvalidArgument(sprintf( |
||
| 642 | $message ?: 'Expected a value to start with %2$s. Got: %s', |
||
| 643 | static::valueToString($value), |
||
| 644 | static::valueToString($prefix) |
||
| 645 | )); |
||
| 646 | } |
||
| 647 | } |
||
| 648 | |||
| 649 | public static function startsWithLetter($value, $message = '') |
||
| 650 | { |
||
| 651 | $valid = isset($value[0]); |
||
| 652 | |||
| 653 | if ($valid) { |
||
| 654 | $locale = setlocale(LC_CTYPE, 0); |
||
| 655 | setlocale(LC_CTYPE, 'C'); |
||
| 656 | $valid = ctype_alpha($value[0]); |
||
| 657 | setlocale(LC_CTYPE, $locale); |
||
| 658 | } |
||
| 659 | |||
| 660 | if (!$valid) { |
||
| 661 | static::reportInvalidArgument(sprintf( |
||
| 662 | $message ?: 'Expected a value to start with a letter. Got: %s', |
||
| 663 | static::valueToString($value) |
||
| 664 | )); |
||
| 665 | } |
||
| 666 | } |
||
| 667 | |||
| 668 | public static function endsWith($value, $suffix, $message = '') |
||
| 669 | { |
||
| 670 | if ($suffix !== substr($value, -static::strlen($suffix))) { |
||
| 671 | static::reportInvalidArgument(sprintf( |
||
| 672 | $message ?: 'Expected a value to end with %2$s. Got: %s', |
||
| 673 | static::valueToString($value), |
||
| 674 | static::valueToString($suffix) |
||
| 675 | )); |
||
| 676 | } |
||
| 677 | } |
||
| 678 | |||
| 679 | public static function regex($value, $pattern, $message = '') |
||
| 680 | { |
||
| 681 | if (!preg_match($pattern, $value)) { |
||
| 682 | static::reportInvalidArgument(sprintf( |
||
| 683 | $message ?: 'The value %s does not match the expected pattern.', |
||
| 684 | static::valueToString($value) |
||
| 685 | )); |
||
| 686 | } |
||
| 687 | } |
||
| 688 | |||
| 689 | public static function notRegex($value, $pattern, $message = '') |
||
| 690 | { |
||
| 691 | if (preg_match($pattern, $value, $matches, PREG_OFFSET_CAPTURE)) { |
||
| 692 | static::reportInvalidArgument(sprintf( |
||
| 693 | $message ?: 'The value %s matches the pattern %s (at offset %d).', |
||
| 694 | static::valueToString($value), |
||
| 695 | static::valueToString($pattern), |
||
| 696 | $matches[0][1] |
||
| 697 | )); |
||
| 698 | } |
||
| 699 | } |
||
| 700 | |||
| 701 | public static function alpha($value, $message = '') |
||
| 702 | { |
||
| 703 | $locale = setlocale(LC_CTYPE, 0); |
||
| 704 | setlocale(LC_CTYPE, 'C'); |
||
| 705 | $valid = !ctype_alpha($value); |
||
| 706 | setlocale(LC_CTYPE, $locale); |
||
| 707 | |||
| 708 | if ($valid) { |
||
| 709 | static::reportInvalidArgument(sprintf( |
||
| 710 | $message ?: 'Expected a value to contain only letters. Got: %s', |
||
| 711 | static::valueToString($value) |
||
| 712 | )); |
||
| 713 | } |
||
| 714 | } |
||
| 715 | |||
| 716 | public static function digits($value, $message = '') |
||
| 717 | { |
||
| 718 | $locale = setlocale(LC_CTYPE, 0); |
||
| 719 | setlocale(LC_CTYPE, 'C'); |
||
| 720 | $valid = !ctype_digit($value); |
||
| 721 | setlocale(LC_CTYPE, $locale); |
||
| 722 | |||
| 723 | if ($valid) { |
||
| 724 | static::reportInvalidArgument(sprintf( |
||
| 725 | $message ?: 'Expected a value to contain digits only. Got: %s', |
||
| 726 | static::valueToString($value) |
||
| 727 | )); |
||
| 728 | } |
||
| 729 | } |
||
| 730 | |||
| 731 | public static function alnum($value, $message = '') |
||
| 732 | { |
||
| 733 | $locale = setlocale(LC_CTYPE, 0); |
||
| 734 | setlocale(LC_CTYPE, 'C'); |
||
| 735 | $valid = !ctype_alnum($value); |
||
| 736 | setlocale(LC_CTYPE, $locale); |
||
| 737 | |||
| 738 | if ($valid) { |
||
| 739 | static::reportInvalidArgument(sprintf( |
||
| 740 | $message ?: 'Expected a value to contain letters and digits only. Got: %s', |
||
| 741 | static::valueToString($value) |
||
| 742 | )); |
||
| 743 | } |
||
| 744 | } |
||
| 745 | |||
| 746 | public static function lower($value, $message = '') |
||
| 747 | { |
||
| 748 | $locale = setlocale(LC_CTYPE, 0); |
||
| 749 | setlocale(LC_CTYPE, 'C'); |
||
| 750 | $valid = !ctype_lower($value); |
||
| 751 | setlocale(LC_CTYPE, $locale); |
||
| 752 | |||
| 753 | if ($valid) { |
||
| 754 | static::reportInvalidArgument(sprintf( |
||
| 755 | $message ?: 'Expected a value to contain lowercase characters only. Got: %s', |
||
| 756 | static::valueToString($value) |
||
| 757 | )); |
||
| 758 | } |
||
| 759 | } |
||
| 760 | |||
| 761 | public static function upper($value, $message = '') |
||
| 762 | { |
||
| 763 | $locale = setlocale(LC_CTYPE, 0); |
||
| 764 | setlocale(LC_CTYPE, 'C'); |
||
| 765 | $valid = !ctype_upper($value); |
||
| 766 | setlocale(LC_CTYPE, $locale); |
||
| 767 | |||
| 768 | if ($valid) { |
||
| 769 | static::reportInvalidArgument(sprintf( |
||
| 770 | $message ?: 'Expected a value to contain uppercase characters only. Got: %s', |
||
| 771 | static::valueToString($value) |
||
| 772 | )); |
||
| 773 | } |
||
| 774 | } |
||
| 775 | |||
| 776 | public static function length($value, $length, $message = '') |
||
| 777 | { |
||
| 778 | if ($length !== static::strlen($value)) { |
||
| 779 | static::reportInvalidArgument(sprintf( |
||
| 780 | $message ?: 'Expected a value to contain %2$s characters. Got: %s', |
||
| 781 | static::valueToString($value), |
||
| 782 | $length |
||
| 783 | )); |
||
| 784 | } |
||
| 785 | } |
||
| 786 | |||
| 787 | public static function minLength($value, $min, $message = '') |
||
| 788 | { |
||
| 789 | if (static::strlen($value) < $min) { |
||
| 790 | static::reportInvalidArgument(sprintf( |
||
| 791 | $message ?: 'Expected a value to contain at least %2$s characters. Got: %s', |
||
| 792 | static::valueToString($value), |
||
| 793 | $min |
||
| 794 | )); |
||
| 795 | } |
||
| 796 | } |
||
| 797 | |||
| 798 | public static function maxLength($value, $max, $message = '') |
||
| 799 | { |
||
| 800 | if (static::strlen($value) > $max) { |
||
| 801 | static::reportInvalidArgument(sprintf( |
||
| 802 | $message ?: 'Expected a value to contain at most %2$s characters. Got: %s', |
||
| 803 | static::valueToString($value), |
||
| 804 | $max |
||
| 805 | )); |
||
| 806 | } |
||
| 807 | } |
||
| 808 | |||
| 809 | public static function lengthBetween($value, $min, $max, $message = '') |
||
| 810 | { |
||
| 811 | $length = static::strlen($value); |
||
| 812 | |||
| 813 | if ($length < $min || $length > $max) { |
||
| 814 | static::reportInvalidArgument(sprintf( |
||
| 815 | $message ?: 'Expected a value to contain between %2$s and %3$s characters. Got: %s', |
||
| 816 | static::valueToString($value), |
||
| 817 | $min, |
||
| 818 | $max |
||
| 819 | )); |
||
| 820 | } |
||
| 821 | } |
||
| 822 | |||
| 823 | public static function fileExists($value, $message = '') |
||
| 824 | { |
||
| 825 | static::string($value); |
||
| 826 | |||
| 827 | if (!file_exists($value)) { |
||
| 828 | static::reportInvalidArgument(sprintf( |
||
| 829 | $message ?: 'The file %s does not exist.', |
||
| 830 | static::valueToString($value) |
||
| 831 | )); |
||
| 832 | } |
||
| 833 | } |
||
| 834 | |||
| 835 | public static function file($value, $message = '') |
||
| 836 | { |
||
| 837 | static::fileExists($value, $message); |
||
| 838 | |||
| 839 | if (!is_file($value)) { |
||
| 840 | static::reportInvalidArgument(sprintf( |
||
| 841 | $message ?: 'The path %s is not a file.', |
||
| 842 | static::valueToString($value) |
||
| 843 | )); |
||
| 844 | } |
||
| 845 | } |
||
| 846 | |||
| 847 | public static function directory($value, $message = '') |
||
| 848 | { |
||
| 849 | static::fileExists($value, $message); |
||
| 850 | |||
| 851 | if (!is_dir($value)) { |
||
| 852 | static::reportInvalidArgument(sprintf( |
||
| 853 | $message ?: 'The path %s is no directory.', |
||
| 854 | static::valueToString($value) |
||
| 855 | )); |
||
| 856 | } |
||
| 857 | } |
||
| 858 | |||
| 859 | public static function readable($value, $message = '') |
||
| 860 | { |
||
| 861 | if (!is_readable($value)) { |
||
| 862 | static::reportInvalidArgument(sprintf( |
||
| 863 | $message ?: 'The path %s is not readable.', |
||
| 864 | static::valueToString($value) |
||
| 865 | )); |
||
| 866 | } |
||
| 867 | } |
||
| 868 | |||
| 869 | public static function writable($value, $message = '') |
||
| 870 | { |
||
| 871 | if (!is_writable($value)) { |
||
| 872 | static::reportInvalidArgument(sprintf( |
||
| 873 | $message ?: 'The path %s is not writable.', |
||
| 874 | static::valueToString($value) |
||
| 875 | )); |
||
| 876 | } |
||
| 877 | } |
||
| 878 | |||
| 879 | public static function classExists($value, $message = '') |
||
| 880 | { |
||
| 881 | if (!class_exists($value)) { |
||
| 882 | static::reportInvalidArgument(sprintf( |
||
| 883 | $message ?: 'Expected an existing class name. Got: %s', |
||
| 884 | static::valueToString($value) |
||
| 885 | )); |
||
| 886 | } |
||
| 887 | } |
||
| 888 | |||
| 889 | public static function subclassOf($value, $class, $message = '') |
||
| 890 | { |
||
| 891 | if (!is_subclass_of($value, $class)) { |
||
| 892 | static::reportInvalidArgument(sprintf( |
||
| 893 | $message ?: 'Expected a sub-class of %2$s. Got: %s', |
||
| 894 | static::valueToString($value), |
||
| 895 | static::valueToString($class) |
||
| 896 | )); |
||
| 897 | } |
||
| 898 | } |
||
| 899 | |||
| 900 | public static function interfaceExists($value, $message = '') |
||
| 901 | { |
||
| 902 | if (!interface_exists($value)) { |
||
| 903 | static::reportInvalidArgument(sprintf( |
||
| 904 | $message ?: 'Expected an existing interface name. got %s', |
||
| 905 | static::valueToString($value) |
||
| 906 | )); |
||
| 907 | } |
||
| 908 | } |
||
| 909 | |||
| 910 | public static function implementsInterface($value, $interface, $message = '') |
||
| 917 | )); |
||
| 918 | } |
||
| 919 | } |
||
| 920 | |||
| 921 | public static function propertyExists($classOrObject, $property, $message = '') |
||
| 922 | { |
||
| 923 | if (!property_exists($classOrObject, $property)) { |
||
| 924 | static::reportInvalidArgument(sprintf( |
||
| 925 | $message ?: 'Expected the property %s to exist.', |
||
| 926 | static::valueToString($property) |
||
| 927 | )); |
||
| 928 | } |
||
| 929 | } |
||
| 930 | |||
| 931 | public static function propertyNotExists($classOrObject, $property, $message = '') |
||
| 932 | { |
||
| 933 | if (property_exists($classOrObject, $property)) { |
||
| 934 | static::reportInvalidArgument(sprintf( |
||
| 935 | $message ?: 'Expected the property %s to not exist.', |
||
| 936 | static::valueToString($property) |
||
| 937 | )); |
||
| 938 | } |
||
| 939 | } |
||
| 940 | |||
| 941 | public static function methodExists($classOrObject, $method, $message = '') |
||
| 942 | { |
||
| 943 | if (!method_exists($classOrObject, $method)) { |
||
| 944 | static::reportInvalidArgument(sprintf( |
||
| 945 | $message ?: 'Expected the method %s to exist.', |
||
| 946 | static::valueToString($method) |
||
| 947 | )); |
||
| 948 | } |
||
| 949 | } |
||
| 950 | |||
| 951 | public static function methodNotExists($classOrObject, $method, $message = '') |
||
| 952 | { |
||
| 953 | if (method_exists($classOrObject, $method)) { |
||
| 954 | static::reportInvalidArgument(sprintf( |
||
| 955 | $message ?: 'Expected the method %s to not exist.', |
||
| 956 | static::valueToString($method) |
||
| 957 | )); |
||
| 958 | } |
||
| 959 | } |
||
| 960 | |||
| 961 | public static function keyExists($array, $key, $message = '') |
||
| 962 | { |
||
| 963 | if (!(isset($array[$key]) || array_key_exists($key, $array))) { |
||
| 964 | static::reportInvalidArgument(sprintf( |
||
| 965 | $message ?: 'Expected the key %s to exist.', |
||
| 966 | static::valueToString($key) |
||
| 967 | )); |
||
| 968 | } |
||
| 969 | } |
||
| 970 | |||
| 971 | public static function keyNotExists($array, $key, $message = '') |
||
| 972 | { |
||
| 973 | if (isset($array[$key]) || array_key_exists($key, $array)) { |
||
| 974 | static::reportInvalidArgument(sprintf( |
||
| 975 | $message ?: 'Expected the key %s to not exist.', |
||
| 976 | static::valueToString($key) |
||
| 977 | )); |
||
| 978 | } |
||
| 979 | } |
||
| 980 | |||
| 981 | public static function count($array, $number, $message = '') |
||
| 987 | ); |
||
| 988 | } |
||
| 989 | |||
| 990 | public static function minCount($array, $min, $message = '') |
||
| 991 | { |
||
| 992 | if (count($array) < $min) { |
||
| 993 | static::reportInvalidArgument(sprintf( |
||
| 994 | $message ?: 'Expected an array to contain at least %2$d elements. Got: %d', |
||
| 995 | count($array), |
||
| 996 | $min |
||
| 997 | )); |
||
| 998 | } |
||
| 999 | } |
||
| 1000 | |||
| 1001 | public static function maxCount($array, $max, $message = '') |
||
| 1002 | { |
||
| 1003 | if (count($array) > $max) { |
||
| 1004 | static::reportInvalidArgument(sprintf( |
||
| 1005 | $message ?: 'Expected an array to contain at most %2$d elements. Got: %d', |
||
| 1006 | count($array), |
||
| 1007 | $max |
||
| 1008 | )); |
||
| 1009 | } |
||
| 1010 | } |
||
| 1011 | |||
| 1012 | public static function countBetween($array, $min, $max, $message = '') |
||
| 1022 | )); |
||
| 1023 | } |
||
| 1024 | } |
||
| 1025 | |||
| 1026 | public static function isList($array, $message = '') |
||
| 1027 | { |
||
| 1028 | if (!is_array($array) || !$array || array_keys($array) !== range(0, count($array) - 1)) { |
||
|
|
|||
| 1029 | static::reportInvalidArgument( |
||
| 1030 | $message ?: 'Expected list - non-associative array.' |
||
| 1031 | ); |
||
| 1032 | } |
||
| 1033 | } |
||
| 1034 | |||
| 1035 | public static function isMap($array, $message = '') |
||
| 1036 | { |
||
| 1037 | if ( |
||
| 1038 | !is_array($array) || |
||
| 1039 | !$array || |
||
| 1040 | array_keys($array) !== array_filter(array_keys($array), function ($key) { |
||
| 1041 | return is_string($key); |
||
| 1042 | }) |
||
| 1043 | ) { |
||
| 1044 | static::reportInvalidArgument( |
||
| 1045 | $message ?: 'Expected map - associative array with string keys.' |
||
| 1046 | ); |
||
| 1047 | } |
||
| 1048 | } |
||
| 1049 | |||
| 1050 | public static function uuid($value, $message = '') |
||
| 1051 | { |
||
| 1052 | $value = str_replace(array('urn:', 'uuid:', '{', '}'), '', $value); |
||
| 1053 | |||
| 1054 | // The nil UUID is special form of UUID that is specified to have all |
||
| 1055 | // 128 bits set to zero. |
||
| 1056 | if ('00000000-0000-0000-0000-000000000000' === $value) { |
||
| 1057 | return; |
||
| 1058 | } |
||
| 1059 | |||
| 1060 | 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)) { |
||
| 1061 | static::reportInvalidArgument(sprintf( |
||
| 1062 | $message ?: 'Value %s is not a valid UUID.', |
||
| 1063 | static::valueToString($value) |
||
| 1064 | )); |
||
| 1065 | } |
||
| 1066 | } |
||
| 1067 | |||
| 1068 | public static function throws(Closure $expression, $class = 'Exception', $message = '') |
||
| 1092 | )); |
||
| 1093 | } |
||
| 1094 | |||
| 1095 | public static function __callStatic($name, $arguments) |
||
| 1096 | { |
||
| 1097 | if ('nullOr' === substr($name, 0, 6)) { |
||
| 1098 | if (null !== $arguments[0]) { |
||
| 1099 | $method = lcfirst(substr($name, 6)); |
||
| 1100 | call_user_func_array(array('static', $method), $arguments); |
||
| 1101 | } |
||
| 1102 | |||
| 1103 | return; |
||
| 1104 | } |
||
| 1105 | |||
| 1106 | if ('all' === substr($name, 0, 3)) { |
||
| 1107 | static::isIterable($arguments[0]); |
||
| 1108 | |||
| 1109 | $method = lcfirst(substr($name, 3)); |
||
| 1110 | $args = $arguments; |
||
| 1111 | |||
| 1112 | foreach ($arguments[0] as $entry) { |
||
| 1113 | $args[0] = $entry; |
||
| 1114 | |||
| 1115 | call_user_func_array(array('static', $method), $args); |
||
| 1116 | } |
||
| 1117 | |||
| 1118 | return; |
||
| 1119 | } |
||
| 1120 | |||
| 1121 | throw new BadMethodCallException('No such method: '.$name); |
||
| 1122 | } |
||
| 1123 | |||
| 1124 | protected static function valueToString($value) |
||
| 1125 | { |
||
| 1126 | if (null === $value) { |
||
| 1127 | return 'null'; |
||
| 1128 | } |
||
| 1129 | |||
| 1130 | if (true === $value) { |
||
| 1131 | return 'true'; |
||
| 1132 | } |
||
| 1133 | |||
| 1134 | if (false === $value) { |
||
| 1135 | return 'false'; |
||
| 1136 | } |
||
| 1137 | |||
| 1138 | if (is_array($value)) { |
||
| 1139 | return 'array'; |
||
| 1140 | } |
||
| 1141 | |||
| 1142 | if (is_object($value)) { |
||
| 1143 | if (method_exists($value, '__toString')) { |
||
| 1144 | return get_class($value).': '.self::valueToString($value->__toString()); |
||
| 1145 | } |
||
| 1146 | |||
| 1147 | return get_class($value); |
||
| 1148 | } |
||
| 1149 | |||
| 1150 | if (is_resource($value)) { |
||
| 1151 | return 'resource'; |
||
| 1152 | } |
||
| 1153 | |||
| 1154 | if (is_string($value)) { |
||
| 1155 | return '"'.$value.'"'; |
||
| 1156 | } |
||
| 1157 | |||
| 1158 | return (string) $value; |
||
| 1159 | } |
||
| 1160 | |||
| 1161 | protected static function typeToString($value) |
||
| 1162 | { |
||
| 1163 | return is_object($value) ? get_class($value) : gettype($value); |
||
| 1164 | } |
||
| 1165 | |||
| 1166 | protected static function strlen($value) |
||
| 1167 | { |
||
| 1168 | if (!function_exists('mb_detect_encoding')) { |
||
| 1169 | return strlen($value); |
||
| 1170 | } |
||
| 1171 | |||
| 1172 | if (false === $encoding = mb_detect_encoding($value)) { |
||
| 1173 | return strlen($value); |
||
| 1174 | } |
||
| 1175 | |||
| 1176 | return mb_strwidth($value, $encoding); |
||
| 1177 | } |
||
| 1178 | |||
| 1179 | protected static function reportInvalidArgument($message) |
||
| 1182 | } |
||
| 1183 | |||
| 1184 | private function __construct() |
||
| 1185 | { |
||
| 1186 | } |
||
| 1187 | } |
||
| 1188 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.