Fix bugs w/expanding/compacting with empty base.

This commit is contained in:
Dave Longley 2013-02-28 16:42:32 -05:00
parent b57d0797e8
commit 24fa461705

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.9 * Version: 0.0.10
* *
* @author Dave Longley * @author Dave Longley
* *
@ -429,7 +429,7 @@ function jsonld_prepend_base($base, $iri) {
} }
} }
$path = '/' . implode('/', $segments); $path = implode('/', $segments);
// add query and hash // add query and hash
if(isset($rel['query'])) { if(isset($rel['query'])) {
@ -439,11 +439,20 @@ function jsonld_prepend_base($base, $iri) {
$path .= "#{$rel['fragment']}"; $path .= "#{$rel['fragment']}";
} }
$absolute_iri = "{$base['scheme']}://"; $absolute_iri = '';
if(isset($base['auth'])) { if($base['scheme'] === '') {
$absolute_iri .= "{$base['auth']}@"; if(strpos($iri, '//') === 0) {
$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;
} }
@ -462,16 +471,23 @@ function jsonld_remove_base($base, $iri) {
$base = jsonld_parse_url($base); $base = jsonld_parse_url($base);
} }
// base is empty
if($base['scheme'] === '') {
return $iri;
}
// establish base root // establish base root
$root = "{$base['scheme']}://"; $root = '';
if(isset($base['auth'])) { if($base['scheme'] !== '') {
$root .= "{$base['auth']}@"; $root = "{$base['scheme']}://";
if(isset($base['auth'])) {
$root .= "{$base['auth']}@";
}
$root .= $base['host'];
if(isset($base['port'])) {
$root .= ":{$base['port']}";
}
} }
$authority = $base['host'];
if(isset($base['port'])) {
$authority .= ":{$base['port']}";
}
$root .= $authority;
// IRI not relative to base // IRI not relative to base
if(strpos($iri, $root) !== 0) { if(strpos($iri, $root) !== 0) {