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