Do not change IRIs if base is empty.

This commit is contained in:
Dave Longley 2013-02-28 17:10:21 -05:00
commit 5847ac58b1

View file

@ -1,7 +1,7 @@
<?php <?php
/** /**
* PHP implementation of the JSON-LD API. * PHP implementation of the JSON-LD API.
* Version: 0.0.11 * Version: 0.0.12
* *
* @author Dave Longley * @author Dave Longley
* *
@ -360,15 +360,23 @@ function jsonld_prepend_base($base, $iri) {
return $iri; return $iri;
} }
// parse base if it is a string
if(is_string($base)) { if(is_string($base)) {
$base = jsonld_parse_url($base); $base = jsonld_parse_url($base);
} }
// if base is empty, do not change iri
if($base['scheme'] === '') {
return $iri;
}
$authority = $base['host']; $authority = $base['host'];
if(isset($base['port'])) { if(isset($base['port'])) {
$authority .= ":{$base['port']}"; $authority .= ":{$base['port']}";
} }
$rel = jsonld_parse_url($iri); $rel = jsonld_parse_url($iri);
// per RFC3986 normalize slashes and dots in path // per RFC3986 normalize slashes and dots in path
// IRI contains authority // IRI contains authority
@ -403,8 +411,6 @@ function jsonld_prepend_base($base, $iri) {
}; };
$segments = array_values(array_filter($segments, $filter)); $segments = array_values(array_filter($segments, $filter));
// do not remove '..' for empty base
if($base['scheme'] !== '') {
// remove as many '..' as possible // remove as many '..' as possible
for($i = 0; $i < count($segments);) { for($i = 0; $i < count($segments);) {
$segment = $segments[$i]; $segment = $segments[$i];
@ -430,7 +436,6 @@ function jsonld_prepend_base($base, $iri) {
$i += 1; $i += 1;
} }
} }
}
$path = implode('/', $segments); $path = implode('/', $segments);
@ -442,20 +447,11 @@ function jsonld_prepend_base($base, $iri) {
$path .= "#{$rel['fragment']}"; $path .= "#{$rel['fragment']}";
} }
$absolute_iri = ''; $absolute_iri = "{$base['scheme']}://";
if($base['scheme'] === '') {
if(strpos($iri, '//') === 0) {
$absolute_iri .= '//';
}
$absolute_iri .= $path;
}
else {
$absolute_iri .= "{$base['scheme']}://";
if(isset($base['auth'])) { if(isset($base['auth'])) {
$absolute_iri .= "{$base['auth']}@"; $absolute_iri .= "{$base['auth']}@";
} }
$absolute_iri .= "$authority/$path"; $absolute_iri .= "$authority/$path";
}
return $absolute_iri; return $absolute_iri;
} }