Skip to content

Commit 51a2e53

Browse files
committed
Refactor: Align Uri::combineRelativePathWithBasePath
Concrete in **UriRetriever** and **UriResolver**.
1 parent 6c92ec1 commit 51a2e53

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

src/JsonSchema/Uri/UriResolver.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,11 +100,11 @@ public function resolve($uri, $baseUri = null)
100100

101101
/**
102102
* Tries to glue a relative path onto an absolute one
103-
*
103+
*
104104
* @param string $relativePath
105105
* @param string $basePath
106106
* @return string Merged path
107-
* @throws UriResolverException
107+
* @throws UriResolverException
108108
*/
109109
public static function combineRelativePathWithBasePath($relativePath, $basePath)
110110
{
@@ -117,12 +117,13 @@ public static function combineRelativePathWithBasePath($relativePath, $basePath)
117117
}
118118

119119
$basePathSegments = explode('/', $basePath);
120-
120+
121121
preg_match('|^/?(\.\./(?:\./)*)*|', $relativePath, $match);
122122
$numLevelUp = strlen($match[0]) /3 + 1;
123123
if ($numLevelUp >= count($basePathSegments)) {
124124
throw new UriResolverException(sprintf("Unable to resolve URI '%s' from base '%s'", $relativePath, $basePath));
125125
}
126+
126127
$basePathSegments = array_slice($basePathSegments, 0, -$numLevelUp);
127128
$path = preg_replace('|^/?(\.\./(\./)*)*|', '', $relativePath);
128129

src/JsonSchema/Uri/UriRetriever.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use JsonSchema\Validator;
1515
use JsonSchema\Exception\InvalidSchemaMediaTypeException;
1616
use JsonSchema\Exception\JsonDecodingException;
17+
use JsonSchema\Exception\UriResolverException;
1718

1819
/**
1920
* Retrieves JSON Schema URIs
@@ -271,12 +272,19 @@ public function resolve($uri, $baseUri = null)
271272
private static function combineRelativePathWithBasePath($relativePath, $basePath)
272273
{
273274
$relativePath = self::normalizePath($relativePath);
275+
if ($relativePath == '') {
276+
return $basePath;
277+
}
278+
if ($relativePath[0] == '/') {
279+
return $relativePath;
280+
}
281+
274282
$basePathSegments = explode('/', $basePath);
275283

276284
preg_match('|^/?(\.\./(?:\./)*)*|', $relativePath, $match);
277285
$numLevelUp = strlen($match[0]) /3 + 1;
278286
if ($numLevelUp >= count($basePathSegments)) {
279-
throw new \JsonSchema\Exception\UriResolverException(sprintf("Unable to resolve URI '%s' from base '%s'", $relativePath, $basePath));
287+
throw new UriResolverException(sprintf("Unable to resolve URI '%s' from base '%s'", $relativePath, $basePath));
280288
}
281289

282290
$basePathSegments = array_slice($basePathSegments, 0, -$numLevelUp);

0 commit comments

Comments
 (0)