/** * Checks that argument satisfies the required constraints otherwise throws the corresponding exception * * @param callable|callable[]|string|string[] $constraints Callable(s) which return(s) true if the argument satisfies the requirements or it also might contain the required class name(s) * @param mixed $arg * @param int|null $atPosition If null then calculated automatically * @param string $otherwiseThrow Exception class or exception object * @throws \Throwable */ function expects($constraints, $arg, $atPosition = null, $otherwiseThrow = '\\InvalidArgumentException') { // Backward compatibility if (null !== $atPosition && is_callable($atPosition)) { expectsToBe($constraints, $arg, $atPosition, is_int($otherwiseThrow) ? $otherwiseThrow : null, func_num_args() > 4 ? func_get_arg(4) : '\\InvalidArgumentException'); return; } if ((array) $constraints === $constraints) { $passedAnd = true; $passedOr = false; foreach ($constraints as $constraint) { if (is_callable($constraint)) { if (isset(_p\Checker::$isOr[$constraint])) { if (!$passedOr && $constraint($arg)) { $passedOr = true; } } else { if (!$constraint($arg)) { $passedAnd = false; break; } } } else { if (!$passedOr && (class_exists($constraint) || interface_exists($constraint)) && $arg instanceof $constraint) { $passedOr = true; } } } if (!$passedAnd || !$passedOr) { _p\throwExpectsException($arg, _p\ErrorMessage::getFor($constraints), $atPosition, $otherwiseThrow); } } else { if (!$constraints($arg)) { _p\throwExpectsException($arg, _p\ErrorMessage::getFor($constraints), $atPosition, $otherwiseThrow); } } }
/** * @return string */ public function __toString() { return ErrorMessage::getFor($this->constraints, true); }