Пример #1
0
function type_check($src, $dst, $namespace = '') : bool
{
    global $classes;
    // Fast-track most common cases first
    if ($src === $dst) {
        return true;
    }
    if (empty($dst) || empty($src)) {
        return true;
    }
    if (strpos("|{$src}|", '|mixed|') !== false) {
        return true;
    }
    if (strpos("|{$dst}|", '|mixed|') !== false) {
        return true;
    }
    if ($src === 'int' && $dst === 'float') {
        return true;
    }
    $src = type_map(strtolower($src));
    $dst = type_map(strtolower($dst));
    // our own union types
    foreach (explode('|', $src) as $s) {
        if (empty($s)) {
            continue;
        }
        foreach (explode('|', $dst) as $d) {
            if (empty($d)) {
                continue;
            }
            if (substr($s, 0, 9) == 'callable:') {
                $s = 'callable';
            }
            if (substr($d, 0, 9) == 'callable:') {
                $d = 'callable';
            }
            if ($s[0] == '\\') {
                $s = substr($s, 1);
            }
            if ($d[0] == '\\') {
                $d = substr($d, 1);
            }
            if ($s === $d) {
                return true;
            }
            if ($s === 'int' && $d === 'float') {
                return true;
            }
            // int->float is ok
            if (($s === 'array' || $s === 'string') && $d === 'callable') {
                return true;
            }
            if ($s === 'object' && !type_scalar($d) && $d !== 'array') {
                return true;
            }
            if ($d === 'object' && !type_scalar($s) && $s !== 'array') {
                return true;
            }
            if (strpos($s, '[]') !== false && $d === 'array') {
                return true;
            }
            if (strpos($d, '[]') !== false && $s === 'array') {
                return true;
            }
            if (($pos = strrpos($d, '\\')) !== false) {
                if (!empty($namespace)) {
                    if (trim(strtolower($namespace . $s), '\\') == $d) {
                        return true;
                    }
                } else {
                    if (substr($d, $pos + 1) === $s) {
                        return true;
                    }
                    // Lazy hack, but...
                }
            }
            if (($pos = strrpos($s, '\\')) !== false) {
                if (!empty($namespace)) {
                    if (trim(strtolower($namespace . $d), '\\') == $s) {
                        return true;
                    }
                } else {
                    if (substr($s, $pos + 1) === $d) {
                        return true;
                    }
                    // Lazy hack, but...
                }
            }
        }
    }
    return false;
}
Пример #2
0
function type_check($src, $dst) : bool
{
    global $classes;
    static $typemap = [['integer', 'double', 'boolean', 'false', 'true', 'callback'], ['int', 'float', 'bool', 'bool', 'bool', 'callable']];
    // Fast-track most common cases first
    if ($src === $dst) {
        return true;
    }
    if (empty($dst) || empty($src)) {
        return true;
    }
    if (strpos("|{$src}|", '|mixed|') !== false) {
        return true;
    }
    if (strpos("|{$dst}|", '|mixed|') !== false) {
        return true;
    }
    if ($src === 'int' && $dst === 'float') {
        return true;
    }
    $src = str_replace($typemap[0], $typemap[1], strtolower($src));
    $dst = str_replace($typemap[0], $typemap[1], strtolower($dst));
    $src = str_replace('object:', '', $src);
    $dst = str_replace('object:', '', $dst);
    // our own union types
    foreach (explode('|', $src) as $s) {
        foreach (explode('|', $dst) as $d) {
            if (substr($s, 0, 9) == 'callable:') {
                $s = 'callable';
            }
            if (substr($d, 0, 9) == 'callable:') {
                $d = 'callable';
            }
            if ($s === $d) {
                return true;
            }
            if ($s === 'int' && $d === 'float') {
                return true;
            }
            // int->float is ok
            if (($s === 'array' || $s === 'string') && $d === 'callable') {
                return true;
            }
            if ($s === 'object' && !type_scalar($d) && $d !== 'array') {
                return true;
            }
            if ($d === 'object' && !type_scalar($s) && $s !== 'array') {
                return true;
            }
            if (strpos($s, '[]') !== false && $d === 'array') {
                return true;
            }
            if (strpos($d, '[]') !== false && $s === 'array') {
                return true;
            }
        }
    }
    return false;
}