コード例 #1
0
/**
 * Prepends a base IRI to the given relative IRI.
 *
 * @param mixed $base a string or the parsed base IRI.
 * @param string $iri the relative IRI.
 *
 * @return string the absolute IRI.
 */
function jsonld_prepend_base($base, $iri)
{
    // already an absolute IRI
    if (strpos($iri, ':') !== false) {
        return $iri;
    }
    // parse base if it is a string
    if (is_string($base)) {
        $base = jsonld_parse_url($base);
    }
    // parse given IRI
    $rel = jsonld_parse_url($iri);
    // start hierarchical part
    $hierPart = $base['protocol'];
    if ($rel['authority']) {
        $hierPart .= "//{$rel['authority']}";
    } else {
        if ($base['href'] !== '') {
            $hierPart .= "//{$base['authority']}";
        }
    }
    // per RFC3986 normalize
    // IRI represents an absolute path
    if (strpos($rel['path'], '/') === 0) {
        $path = $rel['path'];
    } else {
        $path = $base['path'];
        // append relative path to the end of the last directory from base
        if ($rel['path'] !== '') {
            $idx = strrpos($path, '/');
            $idx = $idx === false ? 0 : $idx + 1;
            $path = substr($path, 0, $idx);
            if (strlen($path) > 0 && substr($path, -1) !== '/') {
                $path .= '/';
            }
            $path .= $rel['path'];
        }
    }
    // remove slashes and dots in path
    $path = jsonld_remove_dot_segments($path, $hierPart !== '');
    // add query and hash
    if (isset($rel['query'])) {
        $path .= "?{$rel['query']}";
    }
    if (isset($rel['fragment'])) {
        $path .= "#{$rel['fragment']}";
    }
    $rval = $hierPart . $path;
    if ($rval === '') {
        $rval = './';
    }
    return $rval;
}
コード例 #2
0
ファイル: jsonld.php プロジェクト: Byrlyne/wordlift-plugin
/**
 * Prepends a base IRI to the given relative IRI.
 *
 * @param mixed $base a string or the parsed base IRI.
 * @param string $iri the relative IRI.
 *
 * @return string the absolute IRI.
 */
function jsonld_prepend_base($base, $iri)
{
    // skip IRI processing
    if ($base === null) {
        return $iri;
    }
    // already an absolute IRI
    if (strpos($iri, ':') !== false) {
        return $iri;
    }
    // parse base if it is a string
    if (is_string($base)) {
        $base = jsonld_parse_url($base);
    }
    // parse given IRI
    $rel = jsonld_parse_url($iri);
    // per RFC3986 5.2.2
    $transform = array('protocol' => $base['protocol']);
    if ($rel['authority'] !== null) {
        $transform['authority'] = $rel['authority'];
        $transform['path'] = $rel['path'];
        $transform['query'] = $rel['query'];
    } else {
        $transform['authority'] = $base['authority'];
        if ($rel['path'] === '') {
            $transform['path'] = $base['path'];
            if ($rel['query'] !== null) {
                $transform['query'] = $rel['query'];
            } else {
                $transform['query'] = $base['query'];
            }
        } else {
            if (strpos($rel['path'], '/') === 0) {
                // IRI represents an absolute path
                $transform['path'] = $rel['path'];
            } else {
                // merge paths
                $path = $base['path'];
                // append relative path to the end of the last directory from base
                if ($rel['path'] !== '') {
                    $idx = strrpos($path, '/');
                    $idx = $idx === false ? 0 : $idx + 1;
                    $path = substr($path, 0, $idx);
                    if (strlen($path) > 0 && substr($path, -1) !== '/') {
                        $path .= '/';
                    }
                    $path .= $rel['path'];
                }
                $transform['path'] = $path;
            }
            $transform['query'] = $rel['query'];
        }
    }
    // remove slashes and dots in path
    $transform['path'] = jsonld_remove_dot_segments($transform['path'], !!$transform['authority']);
    // construct URL
    $rval = $transform['protocol'];
    if ($transform['authority'] !== null) {
        $rval .= '//' . $transform['authority'];
    }
    $rval .= $transform['path'];
    if ($transform['query'] !== null) {
        $rval .= '?' . $transform['query'];
    }
    if ($rel['fragment'] !== null) {
        $rval .= '#' . $rel['fragment'];
    }
    // handle empty base
    if ($rval === '') {
        $rval = './';
    }
    return $rval;
}