Skip to content
This repository has been archived by the owner on May 12, 2018. It is now read-only.

Commit

Permalink
Use Sabre\Uri\resolve for URI resolution (#55)
Browse files Browse the repository at this point in the history
  • Loading branch information
Matt A committed Oct 27, 2016
1 parent 59aab3c commit a02915e
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 25 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).

* You can now use pointers when using the file loader, I.E. 'file://my-schema.json#/some/property'.
* Fixed a bug where non string values passed to `format` would fail when they should pass.
* Massive improvements to URI resolution. Now using sabre/uri (BSD-3) to resolve reference URIs.

## 0.3.3 - 2016-08-22

Expand Down
25 changes: 2 additions & 23 deletions src/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -185,37 +185,16 @@ function is_relative_ref($ref)
* Resolve the given id against the parent scope and return the resolved URI.
*
* @param string $id The id to resolve. This should be a valid relative or absolute URI.
* @param string $parentScope The parent scope to resolve against. Should be a valid URI or empty.z
* @param string $parentScope The parent scope to resolve against. Should be a valid URI or empty.
*
* @return string
*/
function resolve_uri($id, $parentScope)
{
// If the id is absolute, it doesn't need to be resolved.
if (!is_relative_ref($id)) {
return $id;
}

// If there is no parent scope, there is nothing to resolve against.
if ($parentScope === '') {
return $id;
}

$uri = Uri\parse($parentScope);
$parts = Uri\parse($id);

if (!empty($parts['path'])) {
// If the path ends in a slash, it shouldn't be replaced but instead appended to.
if ('/' === substr($uri['path'], -1)) {
$uri['path'] .= ltrim($parts['path'], '/');
} else {
$uri['path'] = '/' . ltrim($parts['path'], '/');
}
}

if (!empty($parts['fragment'])) {
$uri['fragment'] = $parts['fragment'];
}

return Uri\build($uri);
return Uri\resolve($parentScope, $id);
}
8 changes: 6 additions & 2 deletions tests/FunctionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,19 @@ public function testIsJsonInteger($jsonInteger, $isValid)
public function testUris()
{
return [
// Technically the spec adds the superfluous # at the end, but we don't need to enforce that.
['http://x.y.z/rootschema.json#', '', 'http://x.y.z/rootschema.json#'],
['#foo', 'http://x.y.z/rootschema.json#', 'http://x.y.z/rootschema.json#foo'],
// Technically the spec adds the superfluous # at the end, but we don't need to enforce that.
['otherschema.json', 'http://x.y.z/rootschema.json#', 'http://x.y.z/otherschema.json'],
['#bar', 'http://x.y.z/otherschema.json#', 'http://x.y.z/otherschema.json#bar'],
['t/inner.json#a', 'http://x.y.z/otherschema.json#', 'http://x.y.z/t/inner.json#a'],
['some://where.else/completely#', 'http://x.y.z/rootschema.json#', 'some://where.else/completely#'],
['some://where.else/completely#', 'http://x.y.z/rootschema.json#', 'some://where.else/completely'],
['folderInteger.json', 'http://localhost:1234/folder/', 'http://localhost:1234/folder/folderInteger.json'],
['some-id.json', '', 'some-id.json'],
['item.json', 'http://some/where/other-item.json', 'http://some/where/item.json'],
// @todo: fixed in https://github.com/fruux/sabre-uri/pull/10,
// we just need to wait for a PHP 5.5 compatible release to update composer.
// ['item.json', 'file:///schemas/other-item.json', 'file:///schemas/item.json'],
];
}

Expand Down

0 comments on commit a02915e

Please sign in to comment.