vendor/webmozart/assert/src/Assert.php line 1973

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