nspl\args\_p\ErrorMessage::getFor PHP Method

getFor() public static method

public static getFor ( string | object | array $type, boolean $onlyOr = false ) : string
$type string | object | array
$onlyOr boolean
return string
    public static function getFor($type, $onlyOr = false)
    {
        if (is_array($type)) {
            $messagesFor = f\partial(a\map, ['\\nspl\\args\\_p\\ErrorMessage', 'getFor']);
            if ($onlyOr) {
                return implode(' or ', $messagesFor($type));
            } else {
                $isOr = function ($t) {
                    return isset(Checker::$isOr[$t]) || class_exists($t) || interface_exists($t);
                };
                list($orTypes, $andTypes) = a\partition($isOr, $type);
                return implode(' and ', array_filter([implode(' or ', $messagesFor($orTypes)), implode(' and ', $messagesFor($andTypes))]));
            }
        }
        if ($type instanceof Constraint) {
            return $type->__toString();
        }
        $default = class_exists($type) || interface_exists($type) ? $type : implode(' ', array_map('strtolower', preg_split('/(?=[A-Z])/', a\last(explode('\\', $type)))));
        return a\value(self::$messages, $type, 'be ' . $default);
    }

Usage Example

コード例 #1
0
ファイル: args.php プロジェクト: ihor/Nspl
/**
 * 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);
        }
    }
}
All Usage Examples Of nspl\args\_p\ErrorMessage::getFor
ErrorMessage