| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
namespace Webmozart\Assert; |
| 15 |
|
| 16 |
use ArrayAccess; |
| 17 |
use Closure; |
| 18 |
use Countable; |
| 19 |
use DateTime; |
| 20 |
use DateTimeImmutable; |
| 21 |
use ReflectionFunction; |
| 22 |
use ReflectionProperty; |
| 23 |
use Throwable; |
| 24 |
use Traversable; |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
class Assert |
| 34 |
{ |
| 35 |
use Mixin; |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
public static function string(mixed $value, string|callable $message = ''): string |
| 46 |
{ |
| 47 |
if (!\is_string($value)) { |
| 48 |
$message = static::resolveMessage($message); |
| 49 |
|
| 50 |
static::reportInvalidArgument(\sprintf( |
| 51 |
$message ?: 'Expected a string. Got: %s', |
| 52 |
static::typeToString($value) |
| 53 |
)); |
| 54 |
} |
| 55 |
|
| 56 |
return $value; |
| 57 |
} |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
public static function stringNotEmpty(mixed $value, string|callable $message = ''): string |
| 70 |
{ |
| 71 |
static::string($value, $message); |
| 72 |
static::notSame($value, '', $message); |
| 73 |
|
| 74 |
return $value; |
| 75 |
} |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
public static function integer(mixed $value, string|callable $message = ''): int |
| 87 |
{ |
| 88 |
if (!\is_int($value)) { |
| 89 |
$message = self::resolveMessage($message); |
| 90 |
static::reportInvalidArgument(\sprintf( |
| 91 |
$message ?: 'Expected an integer. Got: %s', |
| 92 |
static::typeToString($value) |
| 93 |
)); |
| 94 |
} |
| 95 |
|
| 96 |
return $value; |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
public static function integerish(mixed $value, string|callable $message = ''): int|float|string |
| 108 |
{ |
| 109 |
if (!\is_numeric($value) || $value != (int) $value) { |
| 110 |
$message = self::resolveMessage($message); |
| 111 |
static::reportInvalidArgument(\sprintf( |
| 112 |
$message ?: 'Expected an integerish value. Got: %s', |
| 113 |
static::typeToString($value) |
| 114 |
)); |
| 115 |
} |
| 116 |
|
| 117 |
return $value; |
| 118 |
} |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
public static function positiveInteger(mixed $value, string|callable $message = ''): int |
| 132 |
{ |
| 133 |
static::integer($value, $message); |
| 134 |
|
| 135 |
if ($value < 1) { |
| 136 |
$message = self::resolveMessage($message); |
| 137 |
static::reportInvalidArgument(\sprintf( |
| 138 |
$message ?: 'Expected a positive integer. Got: %s', |
| 139 |
static::valueToString($value) |
| 140 |
)); |
| 141 |
} |
| 142 |
|
| 143 |
return $value; |
| 144 |
} |
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
public static function notNegativeInteger(mixed $value, string|callable $message = ''): int |
| 156 |
{ |
| 157 |
static::integer($value, $message); |
| 158 |
|
| 159 |
if ($value < 0) { |
| 160 |
$message = self::resolveMessage($message); |
| 161 |
static::reportInvalidArgument(\sprintf( |
| 162 |
$message ?: 'Expected a non negative integer. Got: %s', |
| 163 |
static::valueToString($value) |
| 164 |
)); |
| 165 |
} |
| 166 |
|
| 167 |
return $value; |
| 168 |
} |
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
public static function negativeInteger(mixed $value, string|callable $message = ''): int |
| 180 |
{ |
| 181 |
static::integer($value, $message); |
| 182 |
|
| 183 |
if ($value >= 0) { |
| 184 |
$message = self::resolveMessage($message); |
| 185 |
static::reportInvalidArgument(\sprintf( |
| 186 |
$message ?: 'Expected a negative integer. Got: %s', |
| 187 |
static::valueToString($value) |
| 188 |
)); |
| 189 |
} |
| 190 |
|
| 191 |
return $value; |
| 192 |
} |
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
public static function float(mixed $value, string|callable $message = ''): float |
| 204 |
{ |
| 205 |
if (!\is_float($value)) { |
| 206 |
$message = self::resolveMessage($message); |
| 207 |
static::reportInvalidArgument(\sprintf( |
| 208 |
$message ?: 'Expected a float. Got: %s', |
| 209 |
static::typeToString($value) |
| 210 |
)); |
| 211 |
} |
| 212 |
|
| 213 |
return $value; |
| 214 |
} |
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
public static function numeric(mixed $value, string|callable $message = ''): int|float|string |
| 226 |
{ |
| 227 |
if (!\is_numeric($value)) { |
| 228 |
$message = self::resolveMessage($message); |
| 229 |
static::reportInvalidArgument(\sprintf( |
| 230 |
$message ?: 'Expected a numeric. Got: %s', |
| 231 |
static::typeToString($value) |
| 232 |
)); |
| 233 |
} |
| 234 |
|
| 235 |
return $value; |
| 236 |
} |
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
public static function natural(mixed $value, string|callable $message = ''): int |
| 250 |
{ |
| 251 |
if (!\is_int($value) || $value < 0) { |
| 252 |
$message = self::resolveMessage($message); |
| 253 |
static::reportInvalidArgument(\sprintf( |
| 254 |
$message ?: 'Expected a non-negative integer. Got: %s', |
| 255 |
static::valueToString($value) |
| 256 |
)); |
| 257 |
} |
| 258 |
|
| 259 |
return $value; |
| 260 |
} |
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
public static function boolean(mixed $value, string|callable $message = ''): bool |
| 272 |
{ |
| 273 |
if (!\is_bool($value)) { |
| 274 |
$message = self::resolveMessage($message); |
| 275 |
static::reportInvalidArgument(\sprintf( |
| 276 |
$message ?: 'Expected a boolean. Got: %s', |
| 277 |
static::typeToString($value) |
| 278 |
)); |
| 279 |
} |
| 280 |
|
| 281 |
return $value; |
| 282 |
} |
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
public static function scalar(mixed $value, string|callable $message = ''): int|bool|float|string |
| 294 |
{ |
| 295 |
if (!\is_scalar($value)) { |
| 296 |
$message = self::resolveMessage($message); |
| 297 |
static::reportInvalidArgument(\sprintf( |
| 298 |
$message ?: 'Expected a scalar. Got: %s', |
| 299 |
static::typeToString($value) |
| 300 |
)); |
| 301 |
} |
| 302 |
|
| 303 |
return $value; |
| 304 |
} |
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
public static function object(mixed $value, string|callable $message = ''): object |
| 316 |
{ |
| 317 |
if (!\is_object($value)) { |
| 318 |
$message = self::resolveMessage($message); |
| 319 |
static::reportInvalidArgument(\sprintf( |
| 320 |
$message ?: 'Expected an object. Got: %s', |
| 321 |
static::typeToString($value) |
| 322 |
)); |
| 323 |
} |
| 324 |
|
| 325 |
return $value; |
| 326 |
} |
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
public static function objectish(mixed $value, string|callable $message = ''): object|string |
| 340 |
{ |
| 341 |
if (!\is_object($value) && !\is_string($value)) { |
| 342 |
$message = self::resolveMessage($message); |
| 343 |
static::reportInvalidArgument(\sprintf( |
| 344 |
$message ?: 'Expected an objectish value. Got: %s', |
| 345 |
static::typeToString($value) |
| 346 |
)); |
| 347 |
} |
| 348 |
|
| 349 |
if (\is_string($value) && !\class_exists($value)) { |
| 350 |
$message = self::resolveMessage($message); |
| 351 |
static::reportInvalidArgument(\sprintf( |
| 352 |
$message ?: 'Expected class to be defined. Got: %s', |
| 353 |
$value |
| 354 |
)); |
| 355 |
} |
| 356 |
|
| 357 |
return $value; |
| 358 |
} |
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
public static function resource(mixed $value, ?string $type = null, string|callable $message = ''): mixed |
| 374 |
{ |
| 375 |
if (!\is_resource($value)) { |
| 376 |
$message = self::resolveMessage($message); |
| 377 |
static::reportInvalidArgument(\sprintf( |
| 378 |
$message ?: 'Expected a resource. Got: %s', |
| 379 |
static::typeToString($value), |
| 380 |
$type |
| 381 |
)); |
| 382 |
} |
| 383 |
|
| 384 |
if ($type && $type !== \get_resource_type($value)) { |
| 385 |
$message = self::resolveMessage($message); |
| 386 |
static::reportInvalidArgument(\sprintf( |
| 387 |
$message ?: 'Expected a resource of type %2$s. Got: %s', |
| 388 |
static::typeToString($value), |
| 389 |
$type |
| 390 |
)); |
| 391 |
} |
| 392 |
|
| 393 |
return $value; |
| 394 |
} |
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
public static function isInitialized(mixed $value, string $property, string|callable $message = ''): object |
| 406 |
{ |
| 407 |
Assert::object($value); |
| 408 |
|
| 409 |
$reflectionProperty = new ReflectionProperty($value, $property); |
| 410 |
|
| 411 |
if (!$reflectionProperty->isInitialized($value)) { |
| 412 |
$message = self::resolveMessage($message); |
| 413 |
static::reportInvalidArgument(\sprintf( |
| 414 |
$message ?: 'Expected property %s to be initialized.', |
| 415 |
$property, |
| 416 |
)); |
| 417 |
} |
| 418 |
|
| 419 |
return $value; |
| 420 |
} |
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
public static function isCallable(mixed $value, string|callable $message = ''): callable |
| 432 |
{ |
| 433 |
if (!\is_callable($value)) { |
| 434 |
$message = self::resolveMessage($message); |
| 435 |
static::reportInvalidArgument(\sprintf( |
| 436 |
$message ?: 'Expected a callable. Got: %s', |
| 437 |
static::typeToString($value) |
| 438 |
)); |
| 439 |
} |
| 440 |
|
| 441 |
return $value; |
| 442 |
} |
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
public static function isArray(mixed $value, string|callable $message = ''): array |
| 454 |
{ |
| 455 |
if (!\is_array($value)) { |
| 456 |
$message = self::resolveMessage($message); |
| 457 |
static::reportInvalidArgument(\sprintf( |
| 458 |
$message ?: 'Expected an array. Got: %s', |
| 459 |
static::typeToString($value) |
| 460 |
)); |
| 461 |
} |
| 462 |
|
| 463 |
return $value; |
| 464 |
} |
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
public static function isArrayAccessible(mixed $value, string|callable $message = ''): array|ArrayAccess |
| 476 |
{ |
| 477 |
if (!\is_array($value) && !($value instanceof ArrayAccess)) { |
| 478 |
$message = self::resolveMessage($message); |
| 479 |
static::reportInvalidArgument(\sprintf( |
| 480 |
$message ?: 'Expected an array accessible. Got: %s', |
| 481 |
static::typeToString($value) |
| 482 |
)); |
| 483 |
} |
| 484 |
|
| 485 |
return $value; |
| 486 |
} |
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
public static function isCountable(mixed $value, string|callable $message = ''): array|Countable |
| 498 |
{ |
| 499 |
if (!\is_array($value) && !($value instanceof Countable)) { |
| 500 |
$message = self::resolveMessage($message); |
| 501 |
static::reportInvalidArgument(\sprintf( |
| 502 |
$message ?: 'Expected a countable. Got: %s', |
| 503 |
static::typeToString($value) |
| 504 |
)); |
| 505 |
} |
| 506 |
|
| 507 |
return $value; |
| 508 |
} |
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
public static function isIterable(mixed $value, string|callable $message = ''): iterable |
| 520 |
{ |
| 521 |
if (!\is_array($value) && !($value instanceof Traversable)) { |
| 522 |
$message = self::resolveMessage($message); |
| 523 |
static::reportInvalidArgument(\sprintf( |
| 524 |
$message ?: 'Expected an iterable. Got: %s', |
| 525 |
static::typeToString($value) |
| 526 |
)); |
| 527 |
} |
| 528 |
|
| 529 |
return $value; |
| 530 |
} |
| 531 |
|
| 532 |
|
| 533 |
|
| 534 |
|
| 535 |
|
| 536 |
|
| 537 |
|
| 538 |
|
| 539 |
|
| 540 |
|
| 541 |
|
| 542 |
|
| 543 |
|
| 544 |
|
| 545 |
|
| 546 |
public static function isInstanceOf(mixed $value, mixed $class, string|callable $message = ''): object |
| 547 |
{ |
| 548 |
static::string($class, 'Expected class as a string. Got: %s'); |
| 549 |
|
| 550 |
if (!($value instanceof $class)) { |
| 551 |
$message = self::resolveMessage($message); |
| 552 |
static::reportInvalidArgument(\sprintf( |
| 553 |
$message ?: 'Expected an instance of %2$s. Got: %s', |
| 554 |
static::typeToString($value), |
| 555 |
$class |
| 556 |
)); |
| 557 |
} |
| 558 |
|
| 559 |
return $value; |
| 560 |
} |
| 561 |
|
| 562 |
|
| 563 |
|
| 564 |
|
| 565 |
|
| 566 |
|
| 567 |
|
| 568 |
|
| 569 |
|
| 570 |
|
| 571 |
|
| 572 |
public static function notInstanceOf(mixed $value, mixed $class, string|callable $message = ''): object |
| 573 |
{ |
| 574 |
static::string($class, 'Expected class as a string. Got: %s'); |
| 575 |
|
| 576 |
if (!\is_object($value) || $value instanceof $class) { |
| 577 |
$message = self::resolveMessage($message); |
| 578 |
static::reportInvalidArgument(\sprintf( |
| 579 |
$message ?: 'Expected an instance other than %2$s. Got: %s', |
| 580 |
static::typeToString($value), |
| 581 |
$class |
| 582 |
)); |
| 583 |
} |
| 584 |
|
| 585 |
return $value; |
| 586 |
} |
| 587 |
|
| 588 |
|
| 589 |
|
| 590 |
|
| 591 |
|
| 592 |
|
| 593 |
|
| 594 |
|
| 595 |
|
| 596 |
|
| 597 |
|
| 598 |
|
| 599 |
|
| 600 |
public static function isInstanceOfAny(mixed $value, mixed $classes, string|callable $message = ''): object |
| 601 |
{ |
| 602 |
static::isIterable($classes); |
| 603 |
|
| 604 |
foreach ($classes as $class) { |
| 605 |
static::string($class, 'Expected class as a string. Got: %s'); |
| 606 |
|
| 607 |
if ($value instanceof $class) { |
| 608 |
return $value; |
| 609 |
} |
| 610 |
} |
| 611 |
|
| 612 |
$message = self::resolveMessage($message); |
| 613 |
static::reportInvalidArgument(\sprintf( |
| 614 |
$message ?: 'Expected an instance of any of %2$s. Got: %s', |
| 615 |
static::typeToString($value), |
| 616 |
\implode(', ', \array_map(static::valueToString(...), \iterator_to_array($classes))) |
| 617 |
)); |
| 618 |
} |
| 619 |
|
| 620 |
|
| 621 |
|
| 622 |
|
| 623 |
|
| 624 |
|
| 625 |
|
| 626 |
|
| 627 |
|
| 628 |
|
| 629 |
|
| 630 |
|
| 631 |
|
| 632 |
public static function isNotInstanceOfAny(mixed $value, mixed $classes, string|callable $message = ''): mixed |
| 633 |
{ |
| 634 |
static::isIterable($classes); |
| 635 |
|
| 636 |
foreach ($classes as $class) { |
| 637 |
static::string($class, 'Expected class as a string. Got: %s'); |
| 638 |
|
| 639 |
if ($value instanceof $class) { |
| 640 |
$message = self::resolveMessage($message); |
| 641 |
static::reportInvalidArgument(\sprintf( |
| 642 |
$message ?: 'Expected not an instance of %2$s. Got: %s', |
| 643 |
static::typeToString($value), |
| 644 |
\implode(', ', \array_map(static::valueToString(...), \iterator_to_array($classes))) |
| 645 |
)); |
| 646 |
} |
| 647 |
} |
| 648 |
|
| 649 |
return $value; |
| 650 |
} |
| 651 |
|
| 652 |
|
| 653 |
|
| 654 |
|
| 655 |
|
| 656 |
|
| 657 |
|
| 658 |
|
| 659 |
|
| 660 |
|
| 661 |
|
| 662 |
|
| 663 |
|
| 664 |
|
| 665 |
|
| 666 |
public static function isAOf(mixed $value, mixed $class, string|callable $message = ''): object|string |
| 667 |
{ |
| 668 |
static::string($class, 'Expected class as a string. Got: %s'); |
| 669 |
|
| 670 |
if (!\is_a($value, $class, \is_string($value))) { |
| 671 |
$message = self::resolveMessage($message); |
| 672 |
static::reportInvalidArgument(\sprintf( |
| 673 |
$message ?: 'Expected an instance of this class or to this class among its parents "%2$s". Got: %s', |
| 674 |
static::valueToString($value), |
| 675 |
$class |
| 676 |
)); |
| 677 |
} |
| 678 |
|
| 679 |
return $value; |
| 680 |
} |
| 681 |
|
| 682 |
|
| 683 |
|
| 684 |
|
| 685 |
|
| 686 |
|
| 687 |
|
| 688 |
|
| 689 |
|
| 690 |
|
| 691 |
|
| 692 |
|
| 693 |
|
| 694 |
|
| 695 |
|
| 696 |
public static function isNotA(mixed $value, mixed $class, string|callable $message = ''): object|string |
| 697 |
{ |
| 698 |
static::objectish($value, $message); |
| 699 |
static::string($class, 'Expected class as a string. Got: %s'); |
| 700 |
|
| 701 |
if (\is_a($value, $class, \is_string($value))) { |
| 702 |
$message = self::resolveMessage($message); |
| 703 |
static::reportInvalidArgument(\sprintf( |
| 704 |
$message ?: 'Expected an instance of this class or to this class among its parents other than "%2$s". Got: %s', |
| 705 |
static::valueToString($value), |
| 706 |
$class |
| 707 |
)); |
| 708 |
} |
| 709 |
|
| 710 |
return $value; |
| 711 |
} |
| 712 |
|
| 713 |
|
| 714 |
|
| 715 |
|
| 716 |
|
| 717 |
|
| 718 |
|
| 719 |
|
| 720 |
|
| 721 |
|
| 722 |
|
| 723 |
|
| 724 |
|
| 725 |
|
| 726 |
public static function isAnyOf(mixed $value, mixed $classes, string|callable $message = ''): object|string |
| 727 |
{ |
| 728 |
static::objectish($value, $message); |
| 729 |
static::isIterable($classes); |
| 730 |
|
| 731 |
foreach ($classes as $class) { |
| 732 |
static::string($class, 'Expected class as a string. Got: %s'); |
| 733 |
|
| 734 |
if (\is_a($value, $class, \is_string($value))) { |
| 735 |
return $value; |
| 736 |
} |
| 737 |
} |
| 738 |
|
| 739 |
$message = self::resolveMessage($message); |
| 740 |
static::reportInvalidArgument(\sprintf( |
| 741 |
$message ?: 'Expected an instance of any of this classes or any of those classes among their parents "%2$s". Got: %s', |
| 742 |
static::valueToString($value), |
| 743 |
\implode(', ', \iterator_to_array($classes)) |
| 744 |
)); |
| 745 |
} |
| 746 |
|
| 747 |
|
| 748 |
|
| 749 |
|
| 750 |
|
| 751 |
|
| 752 |
|
| 753 |
|
| 754 |
|
| 755 |
|
| 756 |
|
| 757 |
|
| 758 |
public static function isEmpty(mixed $value, string|callable $message = ''): mixed |
| 759 |
{ |
| 760 |
if (!empty($value)) { |
| 761 |
$message = self::resolveMessage($message); |
| 762 |
static::reportInvalidArgument(\sprintf( |
| 763 |
$message ?: 'Expected an empty value. Got: %s', |
| 764 |
static::valueToString($value) |
| 765 |
)); |
| 766 |
} |
| 767 |
|
| 768 |
return $value; |
| 769 |
} |
| 770 |
|
| 771 |
|
| 772 |
|
| 773 |
|
| 774 |
|
| 775 |
|
| 776 |
|
| 777 |
|
| 778 |
|
| 779 |
|
| 780 |
public static function notEmpty(mixed $value, string|callable $message = ''): mixed |
| 781 |
{ |
| 782 |
if (empty($value)) { |
| 783 |
$message = self::resolveMessage($message); |
| 784 |
static::reportInvalidArgument(\sprintf( |
| 785 |
$message ?: 'Expected a non-empty value. Got: %s', |
| 786 |
static::valueToString($value) |
| 787 |
)); |
| 788 |
} |
| 789 |
|
| 790 |
return $value; |
| 791 |
} |
| 792 |
|
| 793 |
|
| 794 |
|
| 795 |
|
| 796 |
|
| 797 |
|
| 798 |
|
| 799 |
|
| 800 |
|
| 801 |
|
| 802 |
public static function null(mixed $value, string|callable $message = ''): null |
| 803 |
{ |
| 804 |
if (null !== $value) { |
| 805 |
$message = self::resolveMessage($message); |
| 806 |
static::reportInvalidArgument(\sprintf( |
| 807 |
$message ?: 'Expected null. Got: %s', |
| 808 |
static::valueToString($value) |
| 809 |
)); |
| 810 |
} |
| 811 |
|
| 812 |
return $value; |
| 813 |
} |
| 814 |
|
| 815 |
|
| 816 |
|
| 817 |
|
| 818 |
|
| 819 |
|
| 820 |
|
| 821 |
|
| 822 |
|
| 823 |
|
| 824 |
|
| 825 |
|
| 826 |
|
| 827 |
public static function notNull(mixed $value, string|callable $message = ''): mixed |
| 828 |
{ |
| 829 |
if (null === $value) { |
| 830 |
$message = self::resolveMessage($message); |
| 831 |
static::reportInvalidArgument( |
| 832 |
$message ?: 'Expected a value other than null.' |
| 833 |
); |
| 834 |
} |
| 835 |
|
| 836 |
return $value; |
| 837 |
} |
| 838 |
|
| 839 |
|
| 840 |
|
| 841 |
|
| 842 |
|
| 843 |
|
| 844 |
|
| 845 |
|
| 846 |
|
| 847 |
|
| 848 |
public static function true(mixed $value, string|callable $message = ''): true |
| 849 |
{ |
| 850 |
if (true !== $value) { |
| 851 |
$message = self::resolveMessage($message); |
| 852 |
static::reportInvalidArgument(\sprintf( |
| 853 |
$message ?: 'Expected a value to be true. Got: %s', |
| 854 |
static::valueToString($value) |
| 855 |
)); |
| 856 |
} |
| 857 |
|
| 858 |
return $value; |
| 859 |
} |
| 860 |
|
| 861 |
|
| 862 |
|
| 863 |
|
| 864 |
|
| 865 |
|
| 866 |
|
| 867 |
|
| 868 |
|
| 869 |
|
| 870 |
public static function false(mixed $value, string|callable $message = ''): false |
| 871 |
{ |
| 872 |
if (false !== $value) { |
| 873 |
$message = self::resolveMessage($message); |
| 874 |
static::reportInvalidArgument(\sprintf( |
| 875 |
$message ?: 'Expected a value to be false. Got: %s', |
| 876 |
static::valueToString($value) |
| 877 |
)); |
| 878 |
} |
| 879 |
|
| 880 |
return $value; |
| 881 |
} |
| 882 |
|
| 883 |
|
| 884 |
|
| 885 |
|
| 886 |
|
| 887 |
|
| 888 |
|
| 889 |
|
| 890 |
|
| 891 |
|
| 892 |
|
| 893 |
|
| 894 |
|
| 895 |
public static function notFalse(mixed $value, string|callable $message = ''): mixed |
| 896 |
{ |
| 897 |
if (false === $value) { |
| 898 |
$message = self::resolveMessage($message); |
| 899 |
static::reportInvalidArgument( |
| 900 |
$message ?: 'Expected a value other than false.' |
| 901 |
); |
| 902 |
} |
| 903 |
|
| 904 |
return $value; |
| 905 |
} |
| 906 |
|
| 907 |
|
| 908 |
|
| 909 |
|
| 910 |
|
| 911 |
|
| 912 |
|
| 913 |
|
| 914 |
|
| 915 |
public static function ip(mixed $value, string|callable $message = ''): string |
| 916 |
{ |
| 917 |
static::string($value, $message); |
| 918 |
|
| 919 |
if (false === \filter_var($value, \FILTER_VALIDATE_IP)) { |
| 920 |
$message = self::resolveMessage($message); |
| 921 |
static::reportInvalidArgument(\sprintf( |
| 922 |
$message ?: 'Expected a value to be an IP. Got: %s', |
| 923 |
static::valueToString($value) |
| 924 |
)); |
| 925 |
} |
| 926 |
|
| 927 |
return $value; |
| 928 |
} |
| 929 |
|
| 930 |
|
| 931 |
|
| 932 |
|
| 933 |
|
| 934 |
|
| 935 |
|
| 936 |
|
| 937 |
|
| 938 |
public static function ipv4(mixed $value, string|callable $message = ''): string |
| 939 |
{ |
| 940 |
static::string($value, $message); |
| 941 |
|
| 942 |
if (false === \filter_var($value, \FILTER_VALIDATE_IP, \FILTER_FLAG_IPV4)) { |
| 943 |
$message = self::resolveMessage($message); |
| 944 |
static::reportInvalidArgument(\sprintf( |
| 945 |
$message ?: 'Expected a value to be an IPv4. Got: %s', |
| 946 |
static::valueToString($value) |
| 947 |
)); |
| 948 |
} |
| 949 |
|
| 950 |
return $value; |
| 951 |
} |
| 952 |
|
| 953 |
|
| 954 |
|
| 955 |
|
| 956 |
|
| 957 |
|
| 958 |
|
| 959 |
|
| 960 |
|
| 961 |
public static function ipv6(mixed $value, string|callable $message = ''): string |
| 962 |
{ |
| 963 |
static::string($value, $message); |
| 964 |
|
| 965 |
if (false === \filter_var($value, \FILTER_VALIDATE_IP, \FILTER_FLAG_IPV6)) { |
| 966 |
$message = self::resolveMessage($message); |
| 967 |
static::reportInvalidArgument(\sprintf( |
| 968 |
$message ?: 'Expected a value to be an IPv6. Got: %s', |
| 969 |
static::valueToString($value) |
| 970 |
)); |
| 971 |
} |
| 972 |
|
| 973 |
return $value; |
| 974 |
} |
| 975 |
|
| 976 |
|
| 977 |
|
| 978 |
|
| 979 |
|
| 980 |
|
| 981 |
|
| 982 |
|
| 983 |
|
| 984 |
public static function email(mixed $value, string|callable $message = ''): string |
| 985 |
{ |
| 986 |
static::string($value, $message); |
| 987 |
|
| 988 |
if (false === \filter_var($value, FILTER_VALIDATE_EMAIL, FILTER_FLAG_EMAIL_UNICODE)) { |
| 989 |
$message = self::resolveMessage($message); |
| 990 |
static::reportInvalidArgument(\sprintf( |
| 991 |
$message ?: 'Expected a value to be a valid e-mail address. Got: %s', |
| 992 |
static::valueToString($value) |
| 993 |
)); |
| 994 |
} |
| 995 |
|
| 996 |
return $value; |
| 997 |
} |
| 998 |
|
| 999 |
|
| 1000 |
|
| 1001 |
|
| 1002 |
|
| 1003 |
|
| 1004 |
|
| 1005 |
|
| 1006 |
|
| 1007 |
|
| 1008 |
|
| 1009 |
public static function uniqueValues(mixed $values, string|callable $message = ''): array |
| 1010 |
{ |
| 1011 |
static::isArray($values); |
| 1012 |
|
| 1013 |
$allValues = \count($values); |
| 1014 |
$uniqueValues = \count(\array_unique($values)); |
| 1015 |
|
| 1016 |
if ($allValues !== $uniqueValues) { |
| 1017 |
$difference = $allValues - $uniqueValues; |
| 1018 |
|
| 1019 |
$message = self::resolveMessage($message); |
| 1020 |
static::reportInvalidArgument(\sprintf( |
| 1021 |
$message ?: 'Expected an array of unique values, but %s of them %s duplicated', |
| 1022 |
$difference, |
| 1023 |
1 === $difference ? 'is' : 'are' |
| 1024 |
)); |
| 1025 |
} |
| 1026 |
|
| 1027 |
return $values; |
| 1028 |
} |
| 1029 |
|
| 1030 |
|
| 1031 |
|
| 1032 |
|
| 1033 |
|
| 1034 |
|
| 1035 |
public static function eq(mixed $value, mixed $expect, string|callable $message = ''): mixed |
| 1036 |
{ |
| 1037 |
if ($expect != $value) { |
| 1038 |
$message = self::resolveMessage($message); |
| 1039 |
static::reportInvalidArgument(\sprintf( |
| 1040 |
$message ?: 'Expected a value equal to %2$s. Got: %s', |
| 1041 |
static::valueToString($value), |
| 1042 |
static::valueToString($expect) |
| 1043 |
)); |
| 1044 |
} |
| 1045 |
|
| 1046 |
return $value; |
| 1047 |
} |
| 1048 |
|
| 1049 |
|
| 1050 |
|
| 1051 |
|
| 1052 |
|
| 1053 |
|
| 1054 |
public static function notEq(mixed $value, mixed $expect, string|callable $message = ''): mixed |
| 1055 |
{ |
| 1056 |
if ($expect == $value) { |
| 1057 |
$message = self::resolveMessage($message); |
| 1058 |
static::reportInvalidArgument(\sprintf( |
| 1059 |
$message ?: 'Expected a different value than %s.', |
| 1060 |
static::valueToString($expect) |
| 1061 |
)); |
| 1062 |
} |
| 1063 |
|
| 1064 |
return $value; |
| 1065 |
} |
| 1066 |
|
| 1067 |
|
| 1068 |
|
| 1069 |
|
| 1070 |
|
| 1071 |
|
| 1072 |
|
| 1073 |
|
| 1074 |
public static function same(mixed $value, mixed $expect, string|callable $message = ''): mixed |
| 1075 |
{ |
| 1076 |
if ($expect !== $value) { |
| 1077 |
$message = self::resolveMessage($message); |
| 1078 |
static::reportInvalidArgument(\sprintf( |
| 1079 |
$message ?: 'Expected a value identical to %2$s. Got: %s', |
| 1080 |
static::valueToString($value), |
| 1081 |
static::valueToString($expect) |
| 1082 |
)); |
| 1083 |
} |
| 1084 |
|
| 1085 |
return $value; |
| 1086 |
} |
| 1087 |
|
| 1088 |
|
| 1089 |
|
| 1090 |
|
| 1091 |
|
| 1092 |
|
| 1093 |
|
| 1094 |
|
| 1095 |
public static function notSame(mixed $value, mixed $expect, string|callable $message = ''): mixed |
| 1096 |
{ |
| 1097 |
if ($expect === $value) { |
| 1098 |
$message = self::resolveMessage($message); |
| 1099 |
static::reportInvalidArgument(\sprintf( |
| 1100 |
$message ?: 'Expected a value not identical to %s.', |
| 1101 |
static::valueToString($expect) |
| 1102 |
)); |
| 1103 |
} |
| 1104 |
|
| 1105 |
return $value; |
| 1106 |
} |
| 1107 |
|
| 1108 |
|
| 1109 |
|
| 1110 |
|
| 1111 |
|
| 1112 |
|
| 1113 |
|
| 1114 |
|
| 1115 |
public static function greaterThan(mixed $value, mixed $limit, string|callable $message = ''): mixed |
| 1116 |
{ |
| 1117 |
if ($value <= $limit) { |
| 1118 |
$message = self::resolveMessage($message); |
| 1119 |
static::reportInvalidArgument(\sprintf( |
| 1120 |
$message ?: 'Expected a value greater than %2$s. Got: %s', |
| 1121 |
static::valueToString($value), |
| 1122 |
static::valueToString($limit) |
| 1123 |
)); |
| 1124 |
} |
| 1125 |
|
| 1126 |
return $value; |
| 1127 |
} |
| 1128 |
|
| 1129 |
|
| 1130 |
|
| 1131 |
|
| 1132 |
|
| 1133 |
|
| 1134 |
|
| 1135 |
|
| 1136 |
public static function greaterThanEq(mixed $value, mixed $limit, string|callable $message = ''): mixed |
| 1137 |
{ |
| 1138 |
if ($value < $limit) { |
| 1139 |
$message = self::resolveMessage($message); |
| 1140 |
static::reportInvalidArgument(\sprintf( |
| 1141 |
$message ?: 'Expected a value greater than or equal to %2$s. Got: %s', |
| 1142 |
static::valueToString($value), |
| 1143 |
static::valueToString($limit) |
| 1144 |
)); |
| 1145 |
} |
| 1146 |
|
| 1147 |
return $value; |
| 1148 |
} |
| 1149 |
|
| 1150 |
|
| 1151 |
|
| 1152 |
|
| 1153 |
|
| 1154 |
|
| 1155 |
|
| 1156 |
|
| 1157 |
public static function lessThan(mixed $value, mixed $limit, string|callable $message = ''): mixed |
| 1158 |
{ |
| 1159 |
if ($value >= $limit) { |
| 1160 |
$message = self::resolveMessage($message); |
| 1161 |
static::reportInvalidArgument(\sprintf( |
| 1162 |
$message ?: 'Expected a value less than %2$s. Got: %s', |
| 1163 |
static::valueToString($value), |
| 1164 |
static::valueToString($limit) |
| 1165 |
)); |
| 1166 |
} |
| 1167 |
|
| 1168 |
return $value; |
| 1169 |
} |
| 1170 |
|
| 1171 |
|
| 1172 |
|
| 1173 |
|
| 1174 |
|
| 1175 |
|
| 1176 |
|
| 1177 |
|
| 1178 |
public static function lessThanEq(mixed $value, mixed $limit, string|callable $message = ''): mixed |
| 1179 |
{ |
| 1180 |
if ($value > $limit) { |
| 1181 |
$message = self::resolveMessage($message); |
| 1182 |
static::reportInvalidArgument(\sprintf( |
| 1183 |
$message ?: 'Expected a value less than or equal to %2$s. Got: %s', |
| 1184 |
static::valueToString($value), |
| 1185 |
static::valueToString($limit) |
| 1186 |
)); |
| 1187 |
} |
| 1188 |
|
| 1189 |
return $value; |
| 1190 |
} |
| 1191 |
|
| 1192 |
|
| 1193 |
|
| 1194 |
|
| 1195 |
|
| 1196 |
|
| 1197 |
|
| 1198 |
|
| 1199 |
|
| 1200 |
|
| 1201 |
public static function range(mixed $value, mixed $min, mixed $max, string|callable $message = ''): mixed |
| 1202 |
{ |
| 1203 |
if ($value < $min || $value > $max) { |
| 1204 |
$message = self::resolveMessage($message); |
| 1205 |
static::reportInvalidArgument(\sprintf( |
| 1206 |
$message ?: 'Expected a value between %2$s and %3$s. Got: %s', |
| 1207 |
static::valueToString($value), |
| 1208 |
static::valueToString($min), |
| 1209 |
static::valueToString($max) |
| 1210 |
)); |
| 1211 |
} |
| 1212 |
|
| 1213 |
return $value; |
| 1214 |
} |
| 1215 |
|
| 1216 |
|
| 1217 |
|
| 1218 |
|
| 1219 |
|
| 1220 |
|
| 1221 |
|
| 1222 |
|
| 1223 |
|
| 1224 |
|
| 1225 |
public static function oneOf(mixed $value, mixed $values, string|callable $message = ''): mixed |
| 1226 |
{ |
| 1227 |
static::inArray($value, $values, $message); |
| 1228 |
|
| 1229 |
return $value; |
| 1230 |
} |
| 1231 |
|
| 1232 |
|
| 1233 |
|
| 1234 |
|
| 1235 |
|
| 1236 |
|
| 1237 |
|
| 1238 |
|
| 1239 |
|
| 1240 |
|
| 1241 |
public static function inArray(mixed $value, mixed $values, string|callable $message = ''): mixed |
| 1242 |
{ |
| 1243 |
static::isArray($values); |
| 1244 |
|
| 1245 |
if (!\in_array($value, $values, true)) { |
| 1246 |
$message = self::resolveMessage($message); |
| 1247 |
static::reportInvalidArgument(\sprintf( |
| 1248 |
$message ?: 'Expected one of: %2$s. Got: %s', |
| 1249 |
static::valueToString($value), |
| 1250 |
\implode(', ', \array_map(static::valueToString(...), $values)) |
| 1251 |
)); |
| 1252 |
} |
| 1253 |
|
| 1254 |
return $value; |
| 1255 |
} |
| 1256 |
|
| 1257 |
|
| 1258 |
|
| 1259 |
|
| 1260 |
|
| 1261 |
|
| 1262 |
|
| 1263 |
|
| 1264 |
|
| 1265 |
|
| 1266 |
public static function notOneOf(mixed $value, mixed $values, string|callable $message = ''): mixed |
| 1267 |
{ |
| 1268 |
static::notInArray($value, $values, $message); |
| 1269 |
|
| 1270 |
return $value; |
| 1271 |
} |
| 1272 |
|
| 1273 |
|
| 1274 |
|
| 1275 |
|
| 1276 |
|
| 1277 |
|
| 1278 |
|
| 1279 |
|
| 1280 |
|
| 1281 |
|
| 1282 |
|
| 1283 |
|
| 1284 |
|
| 1285 |
public static function notInArray(mixed $value, mixed $values, string|callable $message = ''): mixed |
| 1286 |
{ |
| 1287 |
static::isArray($values); |
| 1288 |
|
| 1289 |
if (\in_array($value, $values, true)) { |
| 1290 |
$message = self::resolveMessage($message); |
| 1291 |
static::reportInvalidArgument(\sprintf( |
| 1292 |
$message ?: '%2$s was not expected to contain a value. Got: %s', |
| 1293 |
static::valueToString($value), |
| 1294 |
\implode(', ', \array_map(static::valueToString(...), $values)) |
| 1295 |
)); |
| 1296 |
} |
| 1297 |
|
| 1298 |
return $value; |
| 1299 |
} |
| 1300 |
|
| 1301 |
|
| 1302 |
|
| 1303 |
|
| 1304 |
|
| 1305 |
|
| 1306 |
|
| 1307 |
|
| 1308 |
public static function contains(mixed $value, mixed $subString, string|callable $message = ''): string |
| 1309 |
{ |
| 1310 |
static::string($value); |
| 1311 |
static::string($subString); |
| 1312 |
|
| 1313 |
if (!\str_contains($value, $subString)) { |
| 1314 |
$message = self::resolveMessage($message); |
| 1315 |
static::reportInvalidArgument(\sprintf( |
| 1316 |
$message ?: 'Expected a value to contain %2$s. Got: %s', |
| 1317 |
static::valueToString($value), |
| 1318 |
static::valueToString($subString) |
| 1319 |
)); |
| 1320 |
} |
| 1321 |
|
| 1322 |
return $value; |
| 1323 |
} |
| 1324 |
|
| 1325 |
|
| 1326 |
|
| 1327 |
|
| 1328 |
|
| 1329 |
|
| 1330 |
|
| 1331 |
|
| 1332 |
public static function notContains(mixed $value, mixed $subString, string|callable $message = ''): string |
| 1333 |
{ |
| 1334 |
static::string($value); |
| 1335 |
static::string($subString); |
| 1336 |
|
| 1337 |
if (\str_contains($value, $subString)) { |
| 1338 |
$message = self::resolveMessage($message); |
| 1339 |
static::reportInvalidArgument(\sprintf( |
| 1340 |
$message ?: '%2$s was not expected to be contained in a value. Got: %s', |
| 1341 |
static::valueToString($value), |
| 1342 |
static::valueToString($subString) |
| 1343 |
)); |
| 1344 |
} |
| 1345 |
|
| 1346 |
return $value; |
| 1347 |
} |
| 1348 |
|
| 1349 |
|
| 1350 |
|
| 1351 |
|
| 1352 |
|
| 1353 |
|
| 1354 |
|
| 1355 |
|
| 1356 |
public static function notWhitespaceOnly(mixed $value, string|callable $message = ''): string |
| 1357 |
{ |
| 1358 |
static::string($value); |
| 1359 |
|
| 1360 |
if (\preg_match('/^\s*$/', $value)) { |
| 1361 |
$message = self::resolveMessage($message); |
| 1362 |
static::reportInvalidArgument(\sprintf( |
| 1363 |
$message ?: 'Expected a non-whitespace string. Got: %s', |
| 1364 |
static::valueToString($value) |
| 1365 |
)); |
| 1366 |
} |
| 1367 |
|
| 1368 |
return $value; |
| 1369 |
} |
| 1370 |
|
| 1371 |
|
| 1372 |
|
| 1373 |
|
| 1374 |
|
| 1375 |
|
| 1376 |
|
| 1377 |
|
| 1378 |
public static function startsWith(mixed $value, mixed $prefix, string|callable $message = ''): string |
| 1379 |
{ |
| 1380 |
static::string($value); |
| 1381 |
static::string($prefix); |
| 1382 |
|
| 1383 |
if (!\str_starts_with($value, $prefix)) { |
| 1384 |
$message = self::resolveMessage($message); |
| 1385 |
static::reportInvalidArgument(\sprintf( |
| 1386 |
$message ?: 'Expected a value to start with %2$s. Got: %s', |
| 1387 |
static::valueToString($value), |
| 1388 |
static::valueToString($prefix) |
| 1389 |
)); |
| 1390 |
} |
| 1391 |
|
| 1392 |
return $value; |
| 1393 |
} |
| 1394 |
|
| 1395 |
|
| 1396 |
|
| 1397 |
|
| 1398 |
|
| 1399 |
|
| 1400 |
|
| 1401 |
|
| 1402 |
public static function notStartsWith(mixed $value, mixed $prefix, string|callable $message = ''): string |
| 1403 |
{ |
| 1404 |
static::string($value); |
| 1405 |
static::string($prefix); |
| 1406 |
|
| 1407 |
if (\str_starts_with($value, $prefix)) { |
| 1408 |
$message = self::resolveMessage($message); |
| 1409 |
static::reportInvalidArgument(\sprintf( |
| 1410 |
$message ?: 'Expected a value not to start with %2$s. Got: %s', |
| 1411 |
static::valueToString($value), |
| 1412 |
static::valueToString($prefix) |
| 1413 |
)); |
| 1414 |
} |
| 1415 |
|
| 1416 |
return $value; |
| 1417 |
} |
| 1418 |
|
| 1419 |
|
| 1420 |
|
| 1421 |
|
| 1422 |
|
| 1423 |
|
| 1424 |
|
| 1425 |
|
| 1426 |
public static function startsWithLetter(mixed $value, string|callable $message = ''): string |
| 1427 |
{ |
| 1428 |
static::string($value); |
| 1429 |
|
| 1430 |
$valid = isset($value[0]); |
| 1431 |
|
| 1432 |
if ($valid) { |
| 1433 |
$locale = \setlocale(LC_CTYPE, '0'); |
| 1434 |
\setlocale(LC_CTYPE, 'C'); |
| 1435 |
$valid = \ctype_alpha($value[0]); |
| 1436 |
\setlocale(LC_CTYPE, $locale); |
| 1437 |
} |
| 1438 |
|
| 1439 |
if (!$valid) { |
| 1440 |
$message = self::resolveMessage($message); |
| 1441 |
static::reportInvalidArgument(\sprintf( |
| 1442 |
$message ?: 'Expected a value to start with a letter. Got: %s', |
| 1443 |
static::valueToString($value) |
| 1444 |
)); |
| 1445 |
} |
| 1446 |
|
| 1447 |
return $value; |
| 1448 |
} |
| 1449 |
|
| 1450 |
|
| 1451 |
|
| 1452 |
|
| 1453 |
|
| 1454 |
|
| 1455 |
|
| 1456 |
|
| 1457 |
public static function endsWith(mixed $value, mixed $suffix, string|callable $message = ''): string |
| 1458 |
{ |
| 1459 |
static::string($value); |
| 1460 |
static::string($suffix); |
| 1461 |
|
| 1462 |
if (!\str_ends_with($value, $suffix)) { |
| 1463 |
$message = self::resolveMessage($message); |
| 1464 |
static::reportInvalidArgument(\sprintf( |
| 1465 |
$message ?: 'Expected a value to end with %2$s. Got: %s', |
| 1466 |
static::valueToString($value), |
| 1467 |
static::valueToString($suffix) |
| 1468 |
)); |
| 1469 |
} |
| 1470 |
|
| 1471 |
return $value; |
| 1472 |
} |
| 1473 |
|
| 1474 |
|
| 1475 |
|
| 1476 |
|
| 1477 |
|
| 1478 |
|
| 1479 |
|
| 1480 |
|
| 1481 |
public static function notEndsWith(mixed $value, mixed $suffix, string|callable $message = ''): string |
| 1482 |
{ |
| 1483 |
static::string($value); |
| 1484 |
static::string($suffix); |
| 1485 |
|
| 1486 |
if (\str_ends_with($value, $suffix)) { |
| 1487 |
$message = self::resolveMessage($message); |
| 1488 |
static::reportInvalidArgument(\sprintf( |
| 1489 |
$message ?: 'Expected a value not to end with %2$s. Got: %s', |
| 1490 |
static::valueToString($value), |
| 1491 |
static::valueToString($suffix) |
| 1492 |
)); |
| 1493 |
} |
| 1494 |
|
| 1495 |
return $value; |
| 1496 |
} |
| 1497 |
|
| 1498 |
|
| 1499 |
|
| 1500 |
|
| 1501 |
|
| 1502 |
|
| 1503 |
|
| 1504 |
|
| 1505 |
public static function regex(mixed $value, mixed $pattern, string|callable $message = ''): string |
| 1506 |
{ |
| 1507 |
static::string($value); |
| 1508 |
static::string($pattern); |
| 1509 |
|
| 1510 |
if (!\preg_match($pattern, $value)) { |
| 1511 |
$message = self::resolveMessage($message); |
| 1512 |
static::reportInvalidArgument(\sprintf( |
| 1513 |
$message ?: 'The value %s does not match the expected pattern.', |
| 1514 |
static::valueToString($value) |
| 1515 |
)); |
| 1516 |
} |
| 1517 |
|
| 1518 |
return $value; |
| 1519 |
} |
| 1520 |
|
| 1521 |
|
| 1522 |
|
| 1523 |
|
| 1524 |
|
| 1525 |
|
| 1526 |
|
| 1527 |
|
| 1528 |
public static function notRegex(mixed $value, mixed $pattern, string|callable $message = ''): string |
| 1529 |
{ |
| 1530 |
static::string($value); |
| 1531 |
static::string($pattern); |
| 1532 |
|
| 1533 |
if (\preg_match($pattern, $value, $matches, PREG_OFFSET_CAPTURE)) { |
| 1534 |
$message = self::resolveMessage($message); |
| 1535 |
static::reportInvalidArgument(\sprintf( |
| 1536 |
$message ?: 'The value %s matches the pattern %s (at offset %d).', |
| 1537 |
static::valueToString($value), |
| 1538 |
static::valueToString($pattern), |
| 1539 |
$matches[0][1] |
| 1540 |
)); |
| 1541 |
} |
| 1542 |
|
| 1543 |
return $value; |
| 1544 |
} |
| 1545 |
|
| 1546 |
|
| 1547 |
|
| 1548 |
|
| 1549 |
|
| 1550 |
|
| 1551 |
|
| 1552 |
|
| 1553 |
public static function unicodeLetters(mixed $value, string|callable $message = ''): string |
| 1554 |
{ |
| 1555 |
static::string($value, $message); |
| 1556 |
|
| 1557 |
if (!\preg_match('/^\p{L}+$/u', $value)) { |
| 1558 |
$message = self::resolveMessage($message); |
| 1559 |
static::reportInvalidArgument(\sprintf( |
| 1560 |
$message ?: 'Expected a value to contain only Unicode letters. Got: %s', |
| 1561 |
static::valueToString($value) |
| 1562 |
)); |
| 1563 |
} |
| 1564 |
|
| 1565 |
return $value; |
| 1566 |
} |
| 1567 |
|
| 1568 |
|
| 1569 |
|
| 1570 |
|
| 1571 |
|
| 1572 |
|
| 1573 |
|
| 1574 |
|
| 1575 |
public static function alpha(mixed $value, string|callable $message = ''): string |
| 1576 |
{ |
| 1577 |
static::string($value, $message); |
| 1578 |
|
| 1579 |
$locale = \setlocale(LC_CTYPE, '0'); |
| 1580 |
\setlocale(LC_CTYPE, 'C'); |
| 1581 |
$valid = !\ctype_alpha($value); |
| 1582 |
\setlocale(LC_CTYPE, $locale); |
| 1583 |
|
| 1584 |
if ($valid) { |
| 1585 |
$message = self::resolveMessage($message); |
| 1586 |
static::reportInvalidArgument(\sprintf( |
| 1587 |
$message ?: 'Expected a value to contain only letters. Got: %s', |
| 1588 |
static::valueToString($value) |
| 1589 |
)); |
| 1590 |
} |
| 1591 |
|
| 1592 |
return $value; |
| 1593 |
} |
| 1594 |
|
| 1595 |
|
| 1596 |
|
| 1597 |
|
| 1598 |
|
| 1599 |
|
| 1600 |
|
| 1601 |
|
| 1602 |
public static function digits(mixed $value, string|callable $message = ''): string |
| 1603 |
{ |
| 1604 |
static::string($value, $message); |
| 1605 |
|
| 1606 |
$locale = \setlocale(LC_CTYPE, '0'); |
| 1607 |
\setlocale(LC_CTYPE, 'C'); |
| 1608 |
$valid = !\ctype_digit($value); |
| 1609 |
\setlocale(LC_CTYPE, $locale); |
| 1610 |
|
| 1611 |
if ($valid) { |
| 1612 |
$message = self::resolveMessage($message); |
| 1613 |
static::reportInvalidArgument(\sprintf( |
| 1614 |
$message ?: 'Expected a value to contain digits only. Got: %s', |
| 1615 |
static::valueToString($value) |
| 1616 |
)); |
| 1617 |
} |
| 1618 |
|
| 1619 |
return $value; |
| 1620 |
} |
| 1621 |
|
| 1622 |
|
| 1623 |
|
| 1624 |
|
| 1625 |
|
| 1626 |
|
| 1627 |
|
| 1628 |
|
| 1629 |
public static function alnum(mixed $value, string|callable $message = ''): string |
| 1630 |
{ |
| 1631 |
static::string($value, $message); |
| 1632 |
|
| 1633 |
$locale = \setlocale(LC_CTYPE, '0'); |
| 1634 |
\setlocale(LC_CTYPE, 'C'); |
| 1635 |
$valid = !\ctype_alnum($value); |
| 1636 |
\setlocale(LC_CTYPE, $locale); |
| 1637 |
|
| 1638 |
if ($valid) { |
| 1639 |
$message = self::resolveMessage($message); |
| 1640 |
static::reportInvalidArgument(\sprintf( |
| 1641 |
$message ?: 'Expected a value to contain letters and digits only. Got: %s', |
| 1642 |
static::valueToString($value) |
| 1643 |
)); |
| 1644 |
} |
| 1645 |
|
| 1646 |
return $value; |
| 1647 |
} |
| 1648 |
|
| 1649 |
|
| 1650 |
|
| 1651 |
|
| 1652 |
|
| 1653 |
|
| 1654 |
|
| 1655 |
|
| 1656 |
|
| 1657 |
|
| 1658 |
|
| 1659 |
|
| 1660 |
public static function lower(mixed $value, string|callable $message = ''): string |
| 1661 |
{ |
| 1662 |
static::string($value, $message); |
| 1663 |
|
| 1664 |
$locale = \setlocale(LC_CTYPE, '0'); |
| 1665 |
\setlocale(LC_CTYPE, 'C'); |
| 1666 |
$valid = !\ctype_lower($value); |
| 1667 |
\setlocale(LC_CTYPE, $locale); |
| 1668 |
|
| 1669 |
if ($valid) { |
| 1670 |
$message = self::resolveMessage($message); |
| 1671 |
static::reportInvalidArgument(\sprintf( |
| 1672 |
$message ?: 'Expected a value to contain lowercase characters only. Got: %s', |
| 1673 |
static::valueToString($value) |
| 1674 |
)); |
| 1675 |
} |
| 1676 |
|
| 1677 |
return $value; |
| 1678 |
} |
| 1679 |
|
| 1680 |
|
| 1681 |
|
| 1682 |
|
| 1683 |
|
| 1684 |
|
| 1685 |
|
| 1686 |
|
| 1687 |
|
| 1688 |
|
| 1689 |
public static function upper(mixed $value, string|callable $message = ''): string |
| 1690 |
{ |
| 1691 |
static::string($value, $message); |
| 1692 |
|
| 1693 |
$locale = \setlocale(LC_CTYPE, '0'); |
| 1694 |
\setlocale(LC_CTYPE, 'C'); |
| 1695 |
$valid = !\ctype_upper($value); |
| 1696 |
\setlocale(LC_CTYPE, $locale); |
| 1697 |
|
| 1698 |
if ($valid) { |
| 1699 |
$message = self::resolveMessage($message); |
| 1700 |
static::reportInvalidArgument(\sprintf( |
| 1701 |
$message ?: 'Expected a value to contain uppercase characters only. Got: %s', |
| 1702 |
static::valueToString($value) |
| 1703 |
)); |
| 1704 |
} |
| 1705 |
|
| 1706 |
return $value; |
| 1707 |
} |
| 1708 |
|
| 1709 |
|
| 1710 |
|
| 1711 |
|
| 1712 |
|
| 1713 |
|
| 1714 |
|
| 1715 |
|
| 1716 |
public static function length(mixed $value, mixed $length, string|callable $message = ''): string |
| 1717 |
{ |
| 1718 |
static::string($value); |
| 1719 |
static::integerish($length); |
| 1720 |
|
| 1721 |
if ($length !== static::strlen($value)) { |
| 1722 |
$message = self::resolveMessage($message); |
| 1723 |
static::reportInvalidArgument(\sprintf( |
| 1724 |
$message ?: 'Expected a value to contain %2$s characters. Got: %s', |
| 1725 |
static::valueToString($value), |
| 1726 |
$length |
| 1727 |
)); |
| 1728 |
} |
| 1729 |
|
| 1730 |
return $value; |
| 1731 |
} |
| 1732 |
|
| 1733 |
|
| 1734 |
|
| 1735 |
|
| 1736 |
|
| 1737 |
|
| 1738 |
|
| 1739 |
|
| 1740 |
|
| 1741 |
|
| 1742 |
public static function minLength(mixed $value, mixed $min, string|callable $message = ''): string |
| 1743 |
{ |
| 1744 |
static::string($value); |
| 1745 |
static::integerish($min); |
| 1746 |
|
| 1747 |
if (static::strlen($value) < $min) { |
| 1748 |
$message = self::resolveMessage($message); |
| 1749 |
static::reportInvalidArgument(\sprintf( |
| 1750 |
$message ?: 'Expected a value to contain at least %2$s characters. Got: %s', |
| 1751 |
static::valueToString($value), |
| 1752 |
$min |
| 1753 |
)); |
| 1754 |
} |
| 1755 |
|
| 1756 |
return $value; |
| 1757 |
} |
| 1758 |
|
| 1759 |
|
| 1760 |
|
| 1761 |
|
| 1762 |
|
| 1763 |
|
| 1764 |
|
| 1765 |
|
| 1766 |
|
| 1767 |
|
| 1768 |
public static function maxLength(mixed $value, mixed $max, string|callable $message = ''): string |
| 1769 |
{ |
| 1770 |
static::string($value); |
| 1771 |
static::integerish($max); |
| 1772 |
|
| 1773 |
if (static::strlen($value) > $max) { |
| 1774 |
$message = self::resolveMessage($message); |
| 1775 |
static::reportInvalidArgument(\sprintf( |
| 1776 |
$message ?: 'Expected a value to contain at most %2$s characters. Got: %s', |
| 1777 |
static::valueToString($value), |
| 1778 |
$max |
| 1779 |
)); |
| 1780 |
} |
| 1781 |
|
| 1782 |
return $value; |
| 1783 |
} |
| 1784 |
|
| 1785 |
|
| 1786 |
|
| 1787 |
|
| 1788 |
|
| 1789 |
|
| 1790 |
|
| 1791 |
|
| 1792 |
|
| 1793 |
|
| 1794 |
public static function lengthBetween(mixed $value, mixed $min, mixed $max, string|callable $message = ''): string |
| 1795 |
{ |
| 1796 |
static::string($value); |
| 1797 |
static::integerish($min); |
| 1798 |
static::integerish($max); |
| 1799 |
|
| 1800 |
$length = static::strlen($value); |
| 1801 |
|
| 1802 |
if ($length < $min || $length > $max) { |
| 1803 |
$message = self::resolveMessage($message); |
| 1804 |
static::reportInvalidArgument(\sprintf( |
| 1805 |
$message ?: 'Expected a value to contain between %2$s and %3$s characters. Got: %s', |
| 1806 |
static::valueToString($value), |
| 1807 |
$min, |
| 1808 |
$max |
| 1809 |
)); |
| 1810 |
} |
| 1811 |
|
| 1812 |
return $value; |
| 1813 |
} |
| 1814 |
|
| 1815 |
|
| 1816 |
|
| 1817 |
|
| 1818 |
|
| 1819 |
|
| 1820 |
|
| 1821 |
|
| 1822 |
public static function fileExists(mixed $value, string|callable $message = ''): string |
| 1823 |
{ |
| 1824 |
static::string($value); |
| 1825 |
|
| 1826 |
if (!\file_exists($value)) { |
| 1827 |
$message = self::resolveMessage($message); |
| 1828 |
static::reportInvalidArgument(\sprintf( |
| 1829 |
$message ?: 'The path %s does not exist.', |
| 1830 |
static::valueToString($value) |
| 1831 |
)); |
| 1832 |
} |
| 1833 |
|
| 1834 |
return $value; |
| 1835 |
} |
| 1836 |
|
| 1837 |
|
| 1838 |
|
| 1839 |
|
| 1840 |
|
| 1841 |
|
| 1842 |
public static function file(mixed $value, string|callable $message = ''): string |
| 1843 |
{ |
| 1844 |
static::string($value); |
| 1845 |
|
| 1846 |
if (!\is_file($value)) { |
| 1847 |
$message = self::resolveMessage($message); |
| 1848 |
static::reportInvalidArgument(\sprintf( |
| 1849 |
$message ?: 'The path %s is not a file.', |
| 1850 |
static::valueToString($value) |
| 1851 |
)); |
| 1852 |
} |
| 1853 |
|
| 1854 |
return $value; |
| 1855 |
} |
| 1856 |
|
| 1857 |
|
| 1858 |
|
| 1859 |
|
| 1860 |
|
| 1861 |
|
| 1862 |
public static function directory(mixed $value, string|callable $message = ''): string |
| 1863 |
{ |
| 1864 |
static::string($value); |
| 1865 |
|
| 1866 |
if (!\is_dir($value)) { |
| 1867 |
$message = self::resolveMessage($message); |
| 1868 |
static::reportInvalidArgument(\sprintf( |
| 1869 |
$message ?: 'The path %s is not a directory.', |
| 1870 |
static::valueToString($value) |
| 1871 |
)); |
| 1872 |
} |
| 1873 |
|
| 1874 |
return $value; |
| 1875 |
} |
| 1876 |
|
| 1877 |
|
| 1878 |
|
| 1879 |
|
| 1880 |
|
| 1881 |
|
| 1882 |
public static function readable(mixed $value, string|callable $message = ''): string |
| 1883 |
{ |
| 1884 |
static::string($value); |
| 1885 |
|
| 1886 |
if (!\is_readable($value)) { |
| 1887 |
$message = self::resolveMessage($message); |
| 1888 |
static::reportInvalidArgument(\sprintf( |
| 1889 |
$message ?: 'The path %s is not readable.', |
| 1890 |
static::valueToString($value) |
| 1891 |
)); |
| 1892 |
} |
| 1893 |
|
| 1894 |
return $value; |
| 1895 |
} |
| 1896 |
|
| 1897 |
|
| 1898 |
|
| 1899 |
|
| 1900 |
|
| 1901 |
|
| 1902 |
public static function writable(mixed $value, string|callable $message = ''): string |
| 1903 |
{ |
| 1904 |
static::string($value); |
| 1905 |
|
| 1906 |
if (!\is_writable($value)) { |
| 1907 |
$message = self::resolveMessage($message); |
| 1908 |
static::reportInvalidArgument(\sprintf( |
| 1909 |
$message ?: 'The path %s is not writable.', |
| 1910 |
static::valueToString($value) |
| 1911 |
)); |
| 1912 |
} |
| 1913 |
|
| 1914 |
return $value; |
| 1915 |
} |
| 1916 |
|
| 1917 |
|
| 1918 |
|
| 1919 |
|
| 1920 |
|
| 1921 |
|
| 1922 |
|
| 1923 |
|
| 1924 |
|
| 1925 |
|
| 1926 |
public static function classExists(mixed $value, string|callable $message = ''): string |
| 1927 |
{ |
| 1928 |
static::string($value); |
| 1929 |
|
| 1930 |
if (!\class_exists($value)) { |
| 1931 |
$message = self::resolveMessage($message); |
| 1932 |
static::reportInvalidArgument(\sprintf( |
| 1933 |
$message ?: 'Expected an existing class name. Got: %s', |
| 1934 |
static::valueToString($value) |
| 1935 |
)); |
| 1936 |
} |
| 1937 |
|
| 1938 |
return $value; |
| 1939 |
} |
| 1940 |
|
| 1941 |
|
| 1942 |
|
| 1943 |
|
| 1944 |
|
| 1945 |
|
| 1946 |
|
| 1947 |
|
| 1948 |
|
| 1949 |
|
| 1950 |
|
| 1951 |
|
| 1952 |
|
| 1953 |
|
| 1954 |
|
| 1955 |
public static function subclassOf(mixed $value, mixed $class, string|callable $message = ''): string |
| 1956 |
{ |
| 1957 |
static::string($value); |
| 1958 |
static::string($class); |
| 1959 |
|
| 1960 |
if (!\is_subclass_of($value, $class)) { |
| 1961 |
$message = self::resolveMessage($message); |
| 1962 |
static::reportInvalidArgument(\sprintf( |
| 1963 |
$message ?: 'Expected a sub-class of %2$s. Got: %s', |
| 1964 |
static::valueToString($value), |
| 1965 |
static::valueToString($class) |
| 1966 |
)); |
| 1967 |
} |
| 1968 |
|
| 1969 |
return $value; |
| 1970 |
} |
| 1971 |
|
| 1972 |
|
| 1973 |
|
| 1974 |
|
| 1975 |
|
| 1976 |
|
| 1977 |
|
| 1978 |
|
| 1979 |
|
| 1980 |
|
| 1981 |
public static function interfaceExists(mixed $value, string|callable $message = ''): string |
| 1982 |
{ |
| 1983 |
static::string($value); |
| 1984 |
|
| 1985 |
if (!\interface_exists($value)) { |
| 1986 |
$message = self::resolveMessage($message); |
| 1987 |
static::reportInvalidArgument(\sprintf( |
| 1988 |
$message ?: 'Expected an existing interface name. got %s', |
| 1989 |
static::valueToString($value) |
| 1990 |
)); |
| 1991 |
} |
| 1992 |
|
| 1993 |
return $value; |
| 1994 |
} |
| 1995 |
|
| 1996 |
|
| 1997 |
|
| 1998 |
|
| 1999 |
|
| 2000 |
|
| 2001 |
|
| 2002 |
|
| 2003 |
|
| 2004 |
|
| 2005 |
|
| 2006 |
|
| 2007 |
|
| 2008 |
|
| 2009 |
|
| 2010 |
public static function implementsInterface(mixed $value, mixed $interface, string|callable $message = ''): object|string |
| 2011 |
{ |
| 2012 |
static::objectish($value); |
| 2013 |
|
| 2014 |
$implements = \class_implements($value); |
| 2015 |
|
| 2016 |
static::isArray($implements); |
| 2017 |
|
| 2018 |
if (!\in_array($interface, $implements, true)) { |
| 2019 |
$message = self::resolveMessage($message); |
| 2020 |
static::reportInvalidArgument(\sprintf( |
| 2021 |
$message ?: 'Expected an implementation of %2$s. Got: %s', |
| 2022 |
static::valueToString($value), |
| 2023 |
static::valueToString($interface) |
| 2024 |
)); |
| 2025 |
} |
| 2026 |
|
| 2027 |
return $value; |
| 2028 |
} |
| 2029 |
|
| 2030 |
|
| 2031 |
|
| 2032 |
|
| 2033 |
|
| 2034 |
|
| 2035 |
|
| 2036 |
|
| 2037 |
|
| 2038 |
public static function propertyExists(mixed $classOrObject, mixed $property, string|callable $message = ''): object|string |
| 2039 |
{ |
| 2040 |
static::objectish($classOrObject); |
| 2041 |
|
| 2042 |
if (!\property_exists($classOrObject, $property)) { |
| 2043 |
$message = self::resolveMessage($message); |
| 2044 |
static::reportInvalidArgument(\sprintf( |
| 2045 |
$message ?: 'Expected the property %s to exist.', |
| 2046 |
static::valueToString($property) |
| 2047 |
)); |
| 2048 |
} |
| 2049 |
|
| 2050 |
return $classOrObject; |
| 2051 |
} |
| 2052 |
|
| 2053 |
|
| 2054 |
|
| 2055 |
|
| 2056 |
|
| 2057 |
|
| 2058 |
|
| 2059 |
|
| 2060 |
|
| 2061 |
|
| 2062 |
|
| 2063 |
public static function propertyNotExists(mixed $classOrObject, mixed $property, string|callable $message = ''): mixed |
| 2064 |
{ |
| 2065 |
if (!(\is_string($classOrObject) || \is_object($classOrObject)) || \property_exists($classOrObject, $property)) { |
| 2066 |
$message = self::resolveMessage($message); |
| 2067 |
static::reportInvalidArgument(\sprintf( |
| 2068 |
$message ?: 'Expected the property %s to not exist.', |
| 2069 |
static::valueToString($property) |
| 2070 |
)); |
| 2071 |
} |
| 2072 |
|
| 2073 |
return $classOrObject; |
| 2074 |
} |
| 2075 |
|
| 2076 |
|
| 2077 |
|
| 2078 |
|
| 2079 |
|
| 2080 |
|
| 2081 |
|
| 2082 |
|
| 2083 |
|
| 2084 |
|
| 2085 |
|
| 2086 |
public static function methodExists(mixed $classOrObject, mixed $method, string|callable $message = ''): object|string |
| 2087 |
{ |
| 2088 |
static::objectish($classOrObject); |
| 2089 |
|
| 2090 |
if (!\method_exists($classOrObject, $method)) { |
| 2091 |
$message = self::resolveMessage($message); |
| 2092 |
static::reportInvalidArgument(\sprintf( |
| 2093 |
$message ?: 'Expected the method %s to exist.', |
| 2094 |
static::valueToString($method) |
| 2095 |
)); |
| 2096 |
} |
| 2097 |
|
| 2098 |
return $classOrObject; |
| 2099 |
} |
| 2100 |
|
| 2101 |
|
| 2102 |
|
| 2103 |
|
| 2104 |
|
| 2105 |
|
| 2106 |
|
| 2107 |
|
| 2108 |
|
| 2109 |
|
| 2110 |
|
| 2111 |
public static function methodNotExists(mixed $classOrObject, mixed $method, string|callable $message = ''): mixed |
| 2112 |
{ |
| 2113 |
static::objectish($classOrObject); |
| 2114 |
|
| 2115 |
if (\method_exists($classOrObject, $method)) { |
| 2116 |
$message = self::resolveMessage($message); |
| 2117 |
static::reportInvalidArgument(\sprintf( |
| 2118 |
$message ?: 'Expected the method %s to not exist.', |
| 2119 |
static::valueToString($method) |
| 2120 |
)); |
| 2121 |
} |
| 2122 |
|
| 2123 |
return $classOrObject; |
| 2124 |
} |
| 2125 |
|
| 2126 |
|
| 2127 |
|
| 2128 |
|
| 2129 |
|
| 2130 |
|
| 2131 |
|
| 2132 |
|
| 2133 |
|
| 2134 |
public static function keyExists(mixed $array, string|int $key, string|callable $message = ''): array |
| 2135 |
{ |
| 2136 |
static::isArray($array, $message); |
| 2137 |
|
| 2138 |
if (!(isset($array[$key]) || \array_key_exists($key, $array))) { |
| 2139 |
$message = self::resolveMessage($message); |
| 2140 |
static::reportInvalidArgument(\sprintf( |
| 2141 |
$message ?: 'Expected the key %s to exist.', |
| 2142 |
static::valueToString($key) |
| 2143 |
)); |
| 2144 |
} |
| 2145 |
|
| 2146 |
return $array; |
| 2147 |
} |
| 2148 |
|
| 2149 |
|
| 2150 |
|
| 2151 |
|
| 2152 |
|
| 2153 |
|
| 2154 |
|
| 2155 |
|
| 2156 |
|
| 2157 |
public static function keyNotExists(mixed $array, string|int $key, string|callable $message = ''): array |
| 2158 |
{ |
| 2159 |
static::isArray($array, $message); |
| 2160 |
|
| 2161 |
if (isset($array[$key]) || \array_key_exists($key, $array)) { |
| 2162 |
$message = self::resolveMessage($message); |
| 2163 |
static::reportInvalidArgument(\sprintf( |
| 2164 |
$message ?: 'Expected the key %s to not exist.', |
| 2165 |
static::valueToString($key) |
| 2166 |
)); |
| 2167 |
} |
| 2168 |
|
| 2169 |
return $array; |
| 2170 |
} |
| 2171 |
|
| 2172 |
|
| 2173 |
|
| 2174 |
|
| 2175 |
|
| 2176 |
|
| 2177 |
|
| 2178 |
|
| 2179 |
|
| 2180 |
|
| 2181 |
|
| 2182 |
|
| 2183 |
public static function validArrayKey(mixed $value, string|callable $message = ''): string|int |
| 2184 |
{ |
| 2185 |
if (!(\is_int($value) || \is_string($value))) { |
| 2186 |
$message = self::resolveMessage($message); |
| 2187 |
static::reportInvalidArgument(\sprintf( |
| 2188 |
$message ?: 'Expected string or integer. Got: %s', |
| 2189 |
static::typeToString($value) |
| 2190 |
)); |
| 2191 |
} |
| 2192 |
|
| 2193 |
return $value; |
| 2194 |
} |
| 2195 |
|
| 2196 |
|
| 2197 |
|
| 2198 |
|
| 2199 |
|
| 2200 |
|
| 2201 |
public static function count(mixed $array, mixed $number, string|callable $message = ''): array|Countable |
| 2202 |
{ |
| 2203 |
static::isCountable($array); |
| 2204 |
static::integerish($number); |
| 2205 |
|
| 2206 |
static::eq( |
| 2207 |
\count($array), |
| 2208 |
$number, |
| 2209 |
fn () => static::resolveMessage($message) ?: \sprintf( |
| 2210 |
$message ?: 'Expected an array to contain %d elements. Got: %d.', |
| 2211 |
$number, |
| 2212 |
\count($array) |
| 2213 |
) |
| 2214 |
); |
| 2215 |
|
| 2216 |
return $array; |
| 2217 |
} |
| 2218 |
|
| 2219 |
|
| 2220 |
|
| 2221 |
|
| 2222 |
|
| 2223 |
|
| 2224 |
public static function minCount(mixed $array, mixed $min, string|callable $message = ''): array|Countable |
| 2225 |
{ |
| 2226 |
static::isCountable($array); |
| 2227 |
static::integerish($min); |
| 2228 |
|
| 2229 |
if (\count($array) < $min) { |
| 2230 |
$message = self::resolveMessage($message); |
| 2231 |
static::reportInvalidArgument(\sprintf( |
| 2232 |
$message ?: 'Expected an array to contain at least %2$d elements. Got: %d', |
| 2233 |
\count($array), |
| 2234 |
$min |
| 2235 |
)); |
| 2236 |
} |
| 2237 |
|
| 2238 |
return $array; |
| 2239 |
} |
| 2240 |
|
| 2241 |
|
| 2242 |
|
| 2243 |
|
| 2244 |
|
| 2245 |
|
| 2246 |
public static function maxCount(mixed $array, mixed $max, string|callable $message = ''): array|Countable |
| 2247 |
{ |
| 2248 |
static::isCountable($array); |
| 2249 |
static::integerish($max); |
| 2250 |
|
| 2251 |
if (\count($array) > $max) { |
| 2252 |
$message = self::resolveMessage($message); |
| 2253 |
static::reportInvalidArgument(\sprintf( |
| 2254 |
$message ?: 'Expected an array to contain at most %2$d elements. Got: %d', |
| 2255 |
\count($array), |
| 2256 |
$max |
| 2257 |
)); |
| 2258 |
} |
| 2259 |
|
| 2260 |
return $array; |
| 2261 |
} |
| 2262 |
|
| 2263 |
|
| 2264 |
|
| 2265 |
|
| 2266 |
|
| 2267 |
|
| 2268 |
public static function countBetween(mixed $array, mixed $min, mixed $max, string|callable $message = ''): array|Countable |
| 2269 |
{ |
| 2270 |
static::isCountable($array); |
| 2271 |
static::integerish($min); |
| 2272 |
static::integerish($max); |
| 2273 |
|
| 2274 |
$count = \count($array); |
| 2275 |
|
| 2276 |
if ($count < $min || $count > $max) { |
| 2277 |
$message = self::resolveMessage($message); |
| 2278 |
static::reportInvalidArgument(\sprintf( |
| 2279 |
$message ?: 'Expected an array to contain between %2$d and %3$d elements. Got: %d', |
| 2280 |
$count, |
| 2281 |
$min, |
| 2282 |
$max |
| 2283 |
)); |
| 2284 |
} |
| 2285 |
|
| 2286 |
return $array; |
| 2287 |
} |
| 2288 |
|
| 2289 |
|
| 2290 |
|
| 2291 |
|
| 2292 |
|
| 2293 |
|
| 2294 |
|
| 2295 |
|
| 2296 |
|
| 2297 |
|
| 2298 |
|
| 2299 |
|
| 2300 |
public static function isList(mixed $array, string|callable $message = ''): array |
| 2301 |
{ |
| 2302 |
if (!\is_array($array) || !\array_is_list($array)) { |
| 2303 |
$message = self::resolveMessage($message); |
| 2304 |
static::reportInvalidArgument( |
| 2305 |
$message ?: 'Expected list - non-associative array.' |
| 2306 |
); |
| 2307 |
} |
| 2308 |
|
| 2309 |
return $array; |
| 2310 |
} |
| 2311 |
|
| 2312 |
|
| 2313 |
|
| 2314 |
|
| 2315 |
|
| 2316 |
|
| 2317 |
|
| 2318 |
|
| 2319 |
|
| 2320 |
|
| 2321 |
|
| 2322 |
|
| 2323 |
public static function isNonEmptyList(mixed $array, string|callable $message = ''): array |
| 2324 |
{ |
| 2325 |
static::isList($array, $message); |
| 2326 |
static::notEmpty($array, $message); |
| 2327 |
|
| 2328 |
return $array; |
| 2329 |
} |
| 2330 |
|
| 2331 |
|
| 2332 |
|
| 2333 |
|
| 2334 |
|
| 2335 |
|
| 2336 |
|
| 2337 |
|
| 2338 |
|
| 2339 |
|
| 2340 |
|
| 2341 |
|
| 2342 |
|
| 2343 |
|
| 2344 |
|
| 2345 |
public static function isMap(mixed $array, string|callable $message = ''): array |
| 2346 |
{ |
| 2347 |
static::isArray($array, $message); |
| 2348 |
|
| 2349 |
if (\count($array) > 0 && \array_is_list($array)) { |
| 2350 |
$message = self::resolveMessage($message); |
| 2351 |
static::reportInvalidArgument( |
| 2352 |
$message ?: 'Expected map - associative array with string keys.' |
| 2353 |
); |
| 2354 |
} |
| 2355 |
|
| 2356 |
return $array; |
| 2357 |
} |
| 2358 |
|
| 2359 |
|
| 2360 |
|
| 2361 |
|
| 2362 |
|
| 2363 |
|
| 2364 |
|
| 2365 |
|
| 2366 |
|
| 2367 |
public static function isStatic(mixed $callable, string|callable $message = ''): Closure|string |
| 2368 |
{ |
| 2369 |
static::isCallable($callable, $message); |
| 2370 |
|
| 2371 |
$callable = static::callableToClosure($callable); |
| 2372 |
|
| 2373 |
$reflection = new ReflectionFunction($callable); |
| 2374 |
|
| 2375 |
if (!$reflection->isStatic()) { |
| 2376 |
$message = self::resolveMessage($message); |
| 2377 |
static::reportInvalidArgument( |
| 2378 |
$message ?: 'Closure is not static.' |
| 2379 |
); |
| 2380 |
} |
| 2381 |
|
| 2382 |
return $callable; |
| 2383 |
} |
| 2384 |
|
| 2385 |
|
| 2386 |
|
| 2387 |
|
| 2388 |
|
| 2389 |
|
| 2390 |
|
| 2391 |
|
| 2392 |
|
| 2393 |
public static function notStatic(mixed $callable, string|callable $message = ''): Closure|string |
| 2394 |
{ |
| 2395 |
static::isCallable($callable, $message); |
| 2396 |
|
| 2397 |
$callable = static::callableToClosure($callable); |
| 2398 |
|
| 2399 |
$reflection = new ReflectionFunction($callable); |
| 2400 |
|
| 2401 |
if ($reflection->isStatic()) { |
| 2402 |
$message = self::resolveMessage($message); |
| 2403 |
static::reportInvalidArgument( |
| 2404 |
$message ?: 'Closure is not static.' |
| 2405 |
); |
| 2406 |
} |
| 2407 |
|
| 2408 |
return $callable; |
| 2409 |
} |
| 2410 |
|
| 2411 |
|
| 2412 |
|
| 2413 |
|
| 2414 |
|
| 2415 |
|
| 2416 |
|
| 2417 |
|
| 2418 |
|
| 2419 |
|
| 2420 |
|
| 2421 |
|
| 2422 |
|
| 2423 |
|
| 2424 |
|
| 2425 |
|
| 2426 |
public static function isNonEmptyMap(mixed $array, string|callable $message = ''): array |
| 2427 |
{ |
| 2428 |
static::isMap($array, $message); |
| 2429 |
static::notEmpty($array, $message); |
| 2430 |
|
| 2431 |
return $array; |
| 2432 |
} |
| 2433 |
|
| 2434 |
|
| 2435 |
|
| 2436 |
|
| 2437 |
|
| 2438 |
|
| 2439 |
|
| 2440 |
|
| 2441 |
public static function uuid(mixed $value, string|callable $message = ''): string |
| 2442 |
{ |
| 2443 |
static::string($value, $message); |
| 2444 |
|
| 2445 |
$originalValue = $value; |
| 2446 |
$value = \str_replace(['urn:', 'uuid:', '{', '}'], '', $value); |
| 2447 |
|
| 2448 |
|
| 2449 |
|
| 2450 |
if ('00000000-0000-0000-0000-000000000000' === $value) { |
| 2451 |
return $originalValue; |
| 2452 |
} |
| 2453 |
|
| 2454 |
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}$/D', $value)) { |
| 2455 |
$message = self::resolveMessage($message); |
| 2456 |
static::reportInvalidArgument(\sprintf( |
| 2457 |
$message ?: 'Value %s is not a valid UUID.', |
| 2458 |
static::valueToString($value) |
| 2459 |
)); |
| 2460 |
} |
| 2461 |
|
| 2462 |
return $originalValue; |
| 2463 |
} |
| 2464 |
|
| 2465 |
|
| 2466 |
|
| 2467 |
|
| 2468 |
|
| 2469 |
|
| 2470 |
|
| 2471 |
|
| 2472 |
|
| 2473 |
|
| 2474 |
|
| 2475 |
|
| 2476 |
public static function throws(mixed $expression, string $class = Throwable::class, string|callable $message = ''): callable |
| 2477 |
{ |
| 2478 |
static::string($class); |
| 2479 |
static::isCallable($expression); |
| 2480 |
|
| 2481 |
$actual = 'none'; |
| 2482 |
|
| 2483 |
try { |
| 2484 |
$expression(); |
| 2485 |
} catch (Throwable $e) { |
| 2486 |
$actual = \get_class($e); |
| 2487 |
if ($e instanceof $class) { |
| 2488 |
return $expression; |
| 2489 |
} |
| 2490 |
} |
| 2491 |
|
| 2492 |
$message = self::resolveMessage($message); |
| 2493 |
|
| 2494 |
static::reportInvalidArgument($message ?: \sprintf( |
| 2495 |
'Expected to throw "%s", got "%s"', |
| 2496 |
$class, |
| 2497 |
$actual |
| 2498 |
)); |
| 2499 |
} |
| 2500 |
|
| 2501 |
|
| 2502 |
|
| 2503 |
|
| 2504 |
|
| 2505 |
|
| 2506 |
protected static function callableToClosure(callable $callable): Closure|string |
| 2507 |
{ |
| 2508 |
if (\is_string($callable) && \function_exists($callable)) { |
| 2509 |
return $callable; |
| 2510 |
} |
| 2511 |
|
| 2512 |
if ($callable instanceof Closure) { |
| 2513 |
return $callable; |
| 2514 |
} |
| 2515 |
|
| 2516 |
return $callable(...); |
| 2517 |
} |
| 2518 |
|
| 2519 |
|
| 2520 |
|
| 2521 |
|
| 2522 |
protected static function valueToString(mixed $value): string |
| 2523 |
{ |
| 2524 |
if (null === $value) { |
| 2525 |
return 'null'; |
| 2526 |
} |
| 2527 |
|
| 2528 |
if (true === $value) { |
| 2529 |
return 'true'; |
| 2530 |
} |
| 2531 |
|
| 2532 |
if (false === $value) { |
| 2533 |
return 'false'; |
| 2534 |
} |
| 2535 |
|
| 2536 |
if (\is_array($value)) { |
| 2537 |
return 'array'; |
| 2538 |
} |
| 2539 |
|
| 2540 |
if (\is_object($value)) { |
| 2541 |
if (\method_exists($value, '__toString')) { |
| 2542 |
return \get_class($value).': '.self::valueToString($value->__toString()); |
| 2543 |
} |
| 2544 |
|
| 2545 |
if ($value instanceof DateTime || $value instanceof DateTimeImmutable) { |
| 2546 |
return \get_class($value).': '.self::valueToString($value->format('c')); |
| 2547 |
} |
| 2548 |
|
| 2549 |
if (\enum_exists(\get_class($value))) { |
| 2550 |
return \get_class($value).'::'.$value->name; |
| 2551 |
} |
| 2552 |
|
| 2553 |
return \get_class($value); |
| 2554 |
} |
| 2555 |
|
| 2556 |
if (\is_resource($value)) { |
| 2557 |
return 'resource'; |
| 2558 |
} |
| 2559 |
|
| 2560 |
if (\is_string($value)) { |
| 2561 |
return '"'.$value.'"'; |
| 2562 |
} |
| 2563 |
|
| 2564 |
return (string) $value; |
| 2565 |
} |
| 2566 |
|
| 2567 |
|
| 2568 |
|
| 2569 |
|
| 2570 |
protected static function typeToString(mixed $value): string |
| 2571 |
{ |
| 2572 |
return \is_object($value) ? \get_class($value) : \gettype($value); |
| 2573 |
} |
| 2574 |
|
| 2575 |
protected static function strlen(string $value): int |
| 2576 |
{ |
| 2577 |
if (!\function_exists('mb_detect_encoding')) { |
| 2578 |
return \strlen($value); |
| 2579 |
} |
| 2580 |
|
| 2581 |
if (false === $encoding = \mb_detect_encoding($value)) { |
| 2582 |
return \strlen($value); |
| 2583 |
} |
| 2584 |
|
| 2585 |
return \mb_strlen($value, $encoding); |
| 2586 |
} |
| 2587 |
|
| 2588 |
|
| 2589 |
|
| 2590 |
|
| 2591 |
|
| 2592 |
|
| 2593 |
protected static function reportInvalidArgument(string $message): never |
| 2594 |
{ |
| 2595 |
throw new InvalidArgumentException($message); |
| 2596 |
} |
| 2597 |
|
| 2598 |
|
| 2599 |
|
| 2600 |
|
| 2601 |
|
| 2602 |
|
| 2603 |
protected static function resolveMessage(string|callable $message): string |
| 2604 |
{ |
| 2605 |
return \is_callable($message) ? $message() : $message; |
| 2606 |
} |
| 2607 |
|
| 2608 |
private function __construct() |
| 2609 |
{ |
| 2610 |
} |
| 2611 |
} |