Do not change IRIs if base is empty.

This commit is contained in:
Dave Longley 2013-02-28 17:10:21 -05:00
parent e112743436
commit 5847ac58b1
1 changed files with 34 additions and 38 deletions

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,32 +411,29 @@ 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 // remove as many '..' as possible
if($base['scheme'] !== '') { for($i = 0; $i < count($segments);) {
// remove as many '..' as possible $segment = $segments[$i];
for($i = 0; $i < count($segments);) { if($segment === '..') {
$segment = $segments[$i]; // too many reverse dots
if($segment === '..') { if($i === 0) {
// too many reverse dots $last = $segments[count($segments) - 1];
if($i === 0) { if($last !== '..') {
$last = $segments[count($segments) - 1]; $segments = array($last);
if($last !== '..') {
$segments = array($last);
}
else {
$segments = array();
}
break;
} }
else {
$segments = array();
}
break;
}
// remove '..' and previous segment // remove '..' and previous segment
array_splice($segments, $i - 1, 2); array_splice($segments, $i - 1, 2);
$segments = array_values($segments); $segments = array_values($segments);
$i -= 1; $i -= 1;
} }
else { else {
$i += 1; $i += 1;
}
} }
} }
@ -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(isset($base['auth'])) {
if(strpos($iri, '//') === 0) { $absolute_iri .= "{$base['auth']}@";
$absolute_iri .= '//';
}
$absolute_iri .= $path;
}
else {
$absolute_iri .= "{$base['scheme']}://";
if(isset($base['auth'])) {
$absolute_iri .= "{$base['auth']}@";
}
$absolute_iri .= "$authority/$path";
} }
$absolute_iri .= "$authority/$path";
return $absolute_iri; return $absolute_iri;
} }