Debian Codegraph @

lib/3rdParty/composer/webmozart/assert/src/Assert.php

raw symbols SCIP
1 <?php
2
3 declare(strict_types=1);
4
5 /*
6 * This file is part of the webmozart/assert package.
7 *
8 * (c) Bernhard Schussek <[email protected]>
9 *
10 * For the full copyright and license information, please view the LICENSE
11 * file that was distributed with this source code.
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 * Efficient assertions to validate the input/output of your methods.
28 *
29 * @since 1.0
30 *
31 * @author Bernhard Schussek <[email protected]>
32 */
33 class Assert
34 {
35 use Mixin;
36
37 /**
38 * @psalm-pure
39 *
40 * @psalm-assert string $value
41 * @param string|callable():string $message
42 *
43 * @throws InvalidArgumentException
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 * @psalm-pure
61 *
62 * @psalm-assert non-empty-string $value
63 * @param string|callable():string $message
64 *
65 * @return non-empty-string
66 *
67 * @throws InvalidArgumentException
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 * @psalm-pure
79 *
80 * @psalm-assert int $value
81 *
82 * @param string|callable():string $message
83 *
84 * @throws InvalidArgumentException
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 * @psalm-pure
101 *
102 * @psalm-assert numeric $value
103 * @param string|callable():string $message
104 *
105 * @throws InvalidArgumentException
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 * @psalm-pure
122 *
123 * @psalm-assert positive-int $value
124 *
125 * @param string|callable():string $message
126 *
127 * @return positive-int
128 *
129 * @throws InvalidArgumentException
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 * @psalm-pure
148 * @psalm-assert non-negative-int $value
149 * @param string|callable():string $message
150 *
151 * @return non-negative-int
152 *
153 * @throws InvalidArgumentException
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 * @psalm-pure
172 * @psalm-assert negative-int $value
173 * @param string|callable():string $message
174 *
175 * @return negative-int
176 *
177 * @throws InvalidArgumentException
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 * @psalm-pure
196 *
197 * @psalm-assert float $value
198 *
199 * @param string|callable():string $message
200 *
201 * @throws InvalidArgumentException
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 * @psalm-pure
218 *
219 * @psalm-assert numeric $value
220 *
221 * @param string|callable():string $message
222 *
223 * @throws InvalidArgumentException
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 * @psalm-pure
240 *
241 * @psalm-assert positive-int|0 $value
242 *
243 * @param string|callable():string $message
244 *
245 * @return positive-int|0
246 *
247 * @throws InvalidArgumentException
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 * @psalm-pure
264 *
265 * @psalm-assert bool $value
266 *
267 * @param string|callable():string $message
268 *
269 * @throws InvalidArgumentException
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 * @psalm-pure
286 *
287 * @psalm-assert scalar $value
288 *
289 * @param string|callable():string $message
290 *
291 * @throws InvalidArgumentException
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 * @psalm-pure
308 *
309 * @psalm-assert object $value
310 *
311 * @param string|callable():string $message
312 *
313 * @throws InvalidArgumentException
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 * @psalm-pure
330 *
331 * @psalm-assert object|class-string $value
332 *
333 * @param string|callable():string $message
334 *
335 * @return object|class-string
336 *
337 * @throws InvalidArgumentException
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 * @psalm-pure
362 *
363 * @psalm-assert resource $value
364 *
365 * @param string|callable():string $message
366 *
367 * @see https://www.php.net/manual/en/function.get-resource-type.php
368 *
369 * @return resource
370 *
371 * @throws InvalidArgumentException
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 // User supplied message might include the second placeholder.
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 * @psalm-pure
398 *
399 * @psalm-assert object $value
400 *
401 * @param string|callable():string $message
402 *
403 * @throws InvalidArgumentException
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 * @psalm-pure
424 *
425 * @psalm-assert callable $value
426 *
427 * @param string|callable():string $message
428 *
429 * @throws InvalidArgumentException
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 * @psalm-pure
446 *
447 * @psalm-assert array $value
448 *
449 * @param string|callable():string $message
450 *
451 * @throws InvalidArgumentException
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 * @psalm-pure
468 *
469 * @psalm-assert array|ArrayAccess $value
470 *
471 * @param string|callable():string $message
472 *
473 * @throws InvalidArgumentException
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 * @psalm-pure
490 *
491 * @psalm-assert countable $value
492 *
493 * @param string|callable():string $message
494 *
495 * @throws InvalidArgumentException
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 * @psalm-pure
512 *
513 * @psalm-assert iterable $value
514 *
515 * @param string|callable():string $message
516 *
517 * @throws InvalidArgumentException
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 * @psalm-pure
534 *
535 * @template T of object
536 *
537 * @psalm-assert T $value
538 *
539 * @param string|callable():string $message
540 * @param class-string<T> $class
541 *
542 * @return T
543 *
544 * @throws InvalidArgumentException
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 * @template T of object
564 *
565 * @psalm-assert object $value
566 *
567 * @param string|callable():string $message
568 * @param class-string<T> $class
569 *
570 * @throws InvalidArgumentException
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 * @template T of object
590 *
591 * @psalm-assert T $value
592 *
593 * @param iterable<class-string<T>> $classes
594 * @param string|callable():string $message
595 *
596 * @return T
597 *
598 * @throws InvalidArgumentException
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 * @template T
622 *
623 * @psalm-assert object|class-string $value
624 *
625 * @param T $value
626 * @param string|callable():string $message
627 *
628 * @return T
629 *
630 * @throws InvalidArgumentException
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 * @psalm-pure
654 *
655 * @template T of object
656 *
657 * @psalm-assert T|class-string<T> $value
658 *
659 * @param string|callable():string $message
660 * @param class-string<T> $class
661 *
662 * @return T|class-string<T>
663 *
664 * @throws InvalidArgumentException
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 * @psalm-pure
684 *
685 * @template T
686 *
687 * @psalm-assert object|class-string $value
688 *
689 * @param T $value
690 * @param string|callable():string $message
691 *
692 * @return object|class-string
693 *
694 * @throws InvalidArgumentException
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 * @psalm-pure
715 *
716 * @psalm-assert T $value
717 *
718 * @template T as object
719 *
720 * @param array<class-string<T>> $classes
721 * @param string|callable():string $message
722 *
723 * @return T
724 * @throws InvalidArgumentException
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 * @psalm-pure
749 *
750 * @psalm-assert empty $value
751 *
752 * @param string|callable():string $message
753 *
754 * @return empty
755 *
756 * @throws InvalidArgumentException
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 * @psalm-pure
773 *
774 * @psalm-assert !empty $value
775 *
776 * @param string|callable():string $message
777 *
778 * @throws InvalidArgumentException
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 * @psalm-pure
795 *
796 * @psalm-assert null $value
797 *
798 * @param string|callable():string $message
799 *
800 * @throws InvalidArgumentException
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 * @psalm-pure
817 *
818 * @psalm-assert !null $value
819 *
820 * @param string|callable():string $message
821 *
822 * @template T
823 * @param T|null $value
824 * @return T
825 * @throws InvalidArgumentException
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 * @psalm-pure
841 *
842 * @psalm-assert true $value
843 *
844 * @param string|callable():string $message
845 *
846 * @throws InvalidArgumentException
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 * @psalm-pure
863 *
864 * @psalm-assert false $value
865 *
866 * @param string|callable():string $message
867 *
868 * @throws InvalidArgumentException
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 * @psalm-pure
885 *
886 * @psalm-assert !false $value
887 *
888 * @param string|callable():string $message
889 *
890 * @template T
891 * @param T|false $value
892 * @return T
893 * @throws InvalidArgumentException
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 * @psalm-pure
909 *
910 * @param string|callable():string $message
911 * @param string $value
912 *
913 * @throws InvalidArgumentException
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 * @psalm-pure
932 *
933 * @param string|callable():string $message
934 * @param string $value
935 *
936 * @throws InvalidArgumentException
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 * @psalm-pure
955 *
956 * @param string|callable():string $message
957 * @param string $value
958 *
959 * @throws InvalidArgumentException
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 * @psalm-pure
978 *
979 * @param string|callable():string $message
980 * @param string $value
981 *
982 * @throws InvalidArgumentException
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 * Does non-strict comparisons on the items, so ['3', 3] will not pass the assertion.
1001 * Note: objects with identical properties are also considered equal.
1002 *
1003 * @psalm-assert array $values
1004 *
1005 * @param string|callable():string $message
1006 *
1007 * @throws InvalidArgumentException
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 * @param string|callable():string $message
1032 *
1033 * @throws InvalidArgumentException
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 * @param string|callable():string $message
1051 *
1052 * @throws InvalidArgumentException
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 * @psalm-pure
1069 *
1070 * @param string|callable():string $message
1071 *
1072 * @throws InvalidArgumentException
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 * @psalm-pure
1090 *
1091 * @param string|callable():string $message
1092 *
1093 * @throws InvalidArgumentException
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 * @psalm-pure
1110 *
1111 * @param string|callable():string $message
1112 *
1113 * @throws InvalidArgumentException
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 * @psalm-pure
1131 *
1132 * @param string|callable():string $message
1133 *
1134 * @throws InvalidArgumentException
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 * @psalm-pure
1152 *
1153 * @param string|callable():string $message
1154 *
1155 * @throws InvalidArgumentException
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 * @psalm-pure
1173 *
1174 * @param string|callable():string $message
1175 *
1176 * @throws InvalidArgumentException
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 * Inclusive range, so Assert::(3, 3, 5) passes.
1194 *
1195 * @psalm-pure
1196 *
1197 * @param string|callable():string $message
1198 *
1199 * @throws InvalidArgumentException
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 * A more human-readable alias of Assert::inArray().
1218 *
1219 * @psalm-pure
1220 *
1221 * @param string|callable():string $message
1222 *
1223 * @throws InvalidArgumentException
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 * Does strict comparison, so Assert::inArray(3, ['3']) does not pass the assertion.
1234 *
1235 * @psalm-pure
1236 *
1237 * @param string|callable():string $message
1238 *
1239 * @throws InvalidArgumentException
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 * A more human-readable alias of Assert::notInArray().
1259 *
1260 * @psalm-pure
1261 *
1262 * @param string|callable():string $message
1263 *
1264 * @throws InvalidArgumentException
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 * Check that a value is not present
1275 *
1276 * Does strict comparison, so Assert::notInArray(3, [1, 2, 3]) will not pass
1277 * the assertion, but Assert::notInArray(3, ['3']) will.
1278 *
1279 * @psalm-pure
1280 *
1281 * @param string|callable():string $message
1282 *
1283 * @throws InvalidArgumentException
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 * @psalm-pure
1303 *
1304 * @param string|callable():string $message
1305 *
1306 * @throws InvalidArgumentException
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 * @psalm-pure
1327 *
1328 * @param string|callable():string $message
1329 *
1330 * @throws InvalidArgumentException
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 * @psalm-pure
1351 *
1352 * @param string|callable():string $message
1353 *
1354 * @throws InvalidArgumentException
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 * @psalm-pure
1373 *
1374 * @param string|callable():string $message
1375 *
1376 * @throws InvalidArgumentException
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 * @psalm-pure
1397 *
1398 * @param string|callable():string $message
1399 *
1400 * @throws InvalidArgumentException
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 * @psalm-pure
1421 *
1422 * @param string|callable():string $message
1423 *
1424 * @throws InvalidArgumentException
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 * @psalm-pure
1452 *
1453 * @param string|callable():string $message
1454 *
1455 * @throws InvalidArgumentException
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 * @psalm-pure
1476 *
1477 * @param string|callable():string $message
1478 *
1479 * @throws InvalidArgumentException
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 * @psalm-pure
1500 *
1501 * @param string|callable():string $message
1502 *
1503 * @throws InvalidArgumentException
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 * @psalm-pure
1523 *
1524 * @param string|callable():string $message
1525 *
1526 * @throws InvalidArgumentException
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 * @psalm-pure
1548 *
1549 * @param string|callable():string $message
1550 *
1551 * @throws InvalidArgumentException
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 * @psalm-pure
1570 *
1571 * @param string|callable():string $message
1572 *
1573 * @throws InvalidArgumentException
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 * @psalm-pure
1597 *
1598 * @param string|callable():string $message
1599 *
1600 * @throws InvalidArgumentException
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 * @psalm-pure
1624 *
1625 * @param string|callable():string $message
1626 *
1627 * @throws InvalidArgumentException
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 * @psalm-pure
1651 *
1652 * @psalm-assert lowercase-string $value
1653 *
1654 * @param string|callable():string $message
1655 *
1656 * @return lowercase-string
1657 *
1658 * @throws InvalidArgumentException
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 * @psalm-pure
1682 *
1683 * @psalm-assert string $value
1684 *
1685 * @param string|callable():string $message
1686 *
1687 * @throws InvalidArgumentException
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 * @psalm-pure
1711 *
1712 * @param string|callable():string $message
1713 *
1714 * @throws InvalidArgumentException
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 * Inclusive min.
1735 *
1736 * @psalm-pure
1737 *
1738 * @param string|callable():string $message
1739 *
1740 * @throws InvalidArgumentException
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 * Inclusive max.
1761 *
1762 * @psalm-pure
1763 *
1764 * @param string|callable():string $message
1765 *
1766 * @throws InvalidArgumentException
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 * Inclusive, so Assert::lengthBetween('asd', 3, 5); passes the assertion.
1787 *
1788 * @psalm-pure
1789 *
1790 * @param string|callable():string $message
1791 *
1792 * @throws InvalidArgumentException
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 * Will also pass if $value is a directory, use Assert::file() instead if you need to be sure it is a file.
1817 *
1818 * @param string|callable():string $message
1819 *
1820 * @throws InvalidArgumentException
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 * @param string|callable():string $message
1839 *
1840 * @throws InvalidArgumentException
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 * @param string|callable():string $message
1859 *
1860 * @throws InvalidArgumentException
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 * @param string|callable():string $message
1879 *
1880 * @throws InvalidArgumentException
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 * @param string|callable():string $message
1899 *
1900 * @throws InvalidArgumentException
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 * @psalm-assert class-string $value
1919 *
1920 * @param string|callable():string $message
1921 *
1922 * @return class-string
1923 *
1924 * @throws InvalidArgumentException
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 * @psalm-pure
1943 *
1944 * @template ExpectedType of object
1945 *
1946 * @psalm-assert class-string<ExpectedType> $value
1947 *
1948 * @param class-string<ExpectedType> $class
1949 * @param string|callable():string $message
1950 *
1951 * @return class-string<ExpectedType>
1952 *
1953 * @throws InvalidArgumentException
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 * @psalm-assert class-string $value
1974 *
1975 * @param string|callable():string $message
1976 *
1977 * @return class-string
1978 *
1979 * @throws InvalidArgumentException
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 * @psalm-pure
1998 *
1999 * @template ExpectedType of object
2000 *
2001 * @psalm-assert class-string<ExpectedType>|ExpectedType $value
2002 *
2003 * @param class-string<ExpectedType> $interface
2004 * @param string|callable():string $message
2005 *
2006 * @return class-string<ExpectedType>|ExpectedType
2007 *
2008 * @throws InvalidArgumentException
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 * @psalm-pure
2032 *
2033 * @param string|object $classOrObject
2034 * @param string|callable():string $message
2035 *
2036 * @throws InvalidArgumentException
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 * @psalm-pure
2055 *
2056 * @template T as class-string|object
2057 * @param T $classOrObject
2058 * @param string|callable():string $message
2059 *
2060 * @return T
2061 * @throws InvalidArgumentException
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 * @psalm-pure
2078 *
2079 * @template T as class-string|object
2080 * @param T $classOrObject
2081 * @param string|callable():string $message
2082 *
2083 * @return T
2084 * @throws InvalidArgumentException
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 * @psalm-pure
2103 *
2104 * @template T as class-string|object
2105 * @param T $classOrObject
2106 * @param string|callable():string $message
2107 *
2108 * @return T
2109 * @throws InvalidArgumentException
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 * @psalm-pure
2128 *
2129 * @param string|int $key
2130 * @param string|callable():string $message
2131 *
2132 * @throws InvalidArgumentException
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 * @psalm-pure
2151 *
2152 * @param string|int $key
2153 * @param string|callable():string $message
2154 *
2155 * @throws InvalidArgumentException
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 * Checks if a value is a valid array key (int or string).
2174 *
2175 * @psalm-pure
2176 *
2177 * @psalm-assert array-key $value
2178 *
2179 * @param string|callable():string $message
2180 *
2181 * @throws InvalidArgumentException
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 * @param string|callable():string $message
2198 *
2199 * @throws InvalidArgumentException
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 * @param string|callable():string $message
2221 *
2222 * @throws InvalidArgumentException
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 * @param string|callable():string $message
2243 *
2244 * @throws InvalidArgumentException
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 * @param string|callable():string $message
2265 *
2266 * @throws InvalidArgumentException
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 * @psalm-pure
2291 *
2292 * @psalm-assert list<mixed> $array
2293 *
2294 * @param string|callable():string $message
2295 *
2296 * @return list<mixed>
2297 *
2298 * @throws InvalidArgumentException
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 * @psalm-pure
2314 *
2315 * @psalm-assert non-empty-list<mixed> $array
2316 *
2317 * @param string|callable():string $message
2318 *
2319 * @return non-empty-list<mixed>
2320 *
2321 * @throws InvalidArgumentException
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 * @psalm-pure
2333 *
2334 * @template T
2335 *
2336 * @psalm-assert array<string, T> $array
2337 *
2338 * @param mixed|array<array-key, T> $array
2339 * @param string|callable():string $message
2340 *
2341 * @return array<string, T>
2342 *
2343 * @throws InvalidArgumentException
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 * @param callable $callable
2361 * @param string|callable():string $message
2362 *
2363 * @return Closure|callable-string
2364 *
2365 * @throws InvalidArgumentException
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 * @param callable $callable
2387 * @param string|callable():string $message
2388 *
2389 * @return Closure|callable-string
2390 *
2391 * @throws InvalidArgumentException
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 * @psalm-pure
2413 *
2414 * @template T
2415 *
2416 * @psalm-assert array<string, T> $array
2417 * @psalm-assert !empty $array
2418 *
2419 * @param array<string, T> $array
2420 * @param string|callable():string $message
2421 *
2422 * @return non-empty-array<string, T>
2423 *
2424 * @throws InvalidArgumentException
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 * @psalm-pure
2436 *
2437 * @param string|callable():string $message
2438 *
2439 * @throws InvalidArgumentException
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 // The nil UUID is special form of UUID that is specified to have all
2449 // 128 bits set to zero.
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 * @template T as callable
2467 *
2468 * @param T $expression
2469 * @param string|callable():string $message
2470 * @param class-string<Throwable> $class
2471 *
2472 * @return T
2473 *
2474 * @throws InvalidArgumentException
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 * @psalm-pure
2503 *
2504 * @return Closure|callable-string
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 * @psalm-pure
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 * @psalm-pure
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 * @psalm-pure this method is not supposed to perform side effects
2590 *
2591 * @throws InvalidArgumentException
2592 */
2593 protected static function reportInvalidArgument(string $message): never
2594 {
2595 throw new InvalidArgumentException($message);
2596 }
2597
2598 /**
2599 * @psalm-pure
2600 *
2601 * @param string|callable():string $message
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 }