Skip to content

Commit 34e2982

Browse files
committed
Merge pull request jsonrainbow#105 from hakre/patch-2
Fix URI generic syntax delimit path components by slash ("`/`")
2 parents 0c16b51 + 0f3ca84 commit 34e2982

File tree

2 files changed

+27
-68
lines changed

2 files changed

+27
-68
lines changed

src/JsonSchema/Uri/UriResolver.php

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public function generate(array $components)
7272
* Resolves a URI
7373
*
7474
* @param string $uri Absolute or relative
75-
* @param type $baseUri Optional base URI
75+
* @param string $baseUri Optional base URI
7676
* @return string Absolute URI
7777
*/
7878
public function resolve($uri, $baseUri = null)
@@ -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
{
@@ -116,13 +116,14 @@ public static function combineRelativePathWithBasePath($relativePath, $basePath)
116116
return $relativePath;
117117
}
118118

119-
$basePathSegments = self::getPathSegments($basePath);
120-
119+
$basePathSegments = explode('/', $basePath);
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

@@ -143,14 +144,6 @@ private static function normalizePath($path)
143144
return $path;
144145
}
145146

146-
/**
147-
* @return array
148-
*/
149-
private static function getPathSegments($path) {
150-
151-
return explode('/', $path);
152-
}
153-
154147
/**
155148
* @param string $uri
156149
* @return boolean

src/JsonSchema/Uri/UriRetriever.php

Lines changed: 21 additions & 55 deletions
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\ResourceNotFoundException;
1718

1819
/**
1920
* Retrieves JSON Schema URIs
@@ -22,12 +23,23 @@
2223
*/
2324
class UriRetriever
2425
{
26+
/**
27+
* @var null|UriRetrieverInterface
28+
*/
2529
protected $uriRetriever = null;
2630

31+
/**
32+
* @var array|object[]
33+
* @see loadSchema
34+
*/
35+
private $schemaCache = array();
36+
2737
/**
2838
* Guarantee the correct media type was encountered
2939
*
30-
* @throws InvalidSchemaMediaTypeException
40+
* @param UriRetrieverInterface $uriRetriever
41+
* @param string $uri
42+
* @return bool|void
3143
*/
3244
public function confirmMediaType($uriRetriever, $uri)
3345
{
@@ -78,13 +90,13 @@ public function getUriRetriever()
7890
* @param string $uri JSON Schema URI
7991
* @return object JSON Schema after walking down the fragment pieces
8092
*
81-
* @throws \JsonSchema\Exception\ResourceNotFoundException
93+
* @throws ResourceNotFoundException
8294
*/
8395
public function resolvePointer($jsonSchema, $uri)
8496
{
8597
$resolver = new UriResolver();
8698
$parsed = $resolver->parse($uri);
87-
if (empty($parsed['fragment'])) {
99+
if (empty($parsed['fragment'])) {
88100
return $jsonSchema;
89101
}
90102

@@ -97,14 +109,14 @@ public function resolvePointer($jsonSchema, $uri)
97109
if (! empty($jsonSchema->$pathElement)) {
98110
$jsonSchema = $jsonSchema->$pathElement;
99111
} else {
100-
throw new \JsonSchema\Exception\ResourceNotFoundException(
112+
throw new ResourceNotFoundException(
101113
'Fragment "' . $parsed['fragment'] . '" not found'
102114
. ' in ' . $uri
103115
);
104116
}
105117

106118
if (! is_object($jsonSchema)) {
107-
throw new \JsonSchema\Exception\ResourceNotFoundException(
119+
throw new ResourceNotFoundException(
108120
'Fragment part "' . $pathElement . '" is no object '
109121
. ' in ' . $uri
110122
);
@@ -119,8 +131,8 @@ public function resolvePointer($jsonSchema, $uri)
119131
* Retrieve a URI
120132
*
121133
* @param string $uri JSON Schema URI
134+
* @param string|null $baseUri
122135
* @return object JSON Schema contents
123-
* @throws InvalidSchemaMediaType for invalid media tyeps
124136
*/
125137
public function retrieve($uri, $baseUri = null)
126138
{
@@ -167,6 +179,7 @@ protected function loadSchema($fetchUri)
167179
}
168180

169181
$this->schemaCache[$fetchUri] = $jsonSchema;
182+
170183
return $jsonSchema;
171184
}
172185

@@ -240,7 +253,7 @@ public function generate(array $components)
240253
* Resolves a URI
241254
*
242255
* @param string $uri Absolute or relative
243-
* @param type $baseUri Optional base URI
256+
* @param string $baseUri Optional base URI
244257
* @return string
245258
*/
246259
public function resolve($uri, $baseUri = null)
@@ -255,58 +268,11 @@ public function resolve($uri, $baseUri = null)
255268
$baseComponents = $this->parse($baseUri);
256269
$basePath = $baseComponents['path'];
257270

258-
$baseComponents['path'] = self::combineRelativePathWithBasePath($path, $basePath);
271+
$baseComponents['path'] = UriResolver::combineRelativePathWithBasePath($path, $basePath);
259272

260273
return $this->generate($baseComponents);
261274
}
262275

263-
/**
264-
* Tries to glue a relative path onto an absolute one
265-
*
266-
* @param string $relativePath
267-
* @param string $basePath
268-
* @return string Merged path
269-
* @throws UriResolverException
270-
*/
271-
private static function combineRelativePathWithBasePath($relativePath, $basePath)
272-
{
273-
$relativePath = self::normalizePath($relativePath);
274-
$basePathSegments = self::getPathSegments($basePath);
275-
276-
preg_match('|^/?(\.\./(?:\./)*)*|', $relativePath, $match);
277-
$numLevelUp = strlen($match[0]) /3 + 1;
278-
if ($numLevelUp >= count($basePathSegments)) {
279-
throw new \JsonSchema\Exception\UriResolverException(sprintf("Unable to resolve URI '%s' from base '%s'", $relativePath, $basePath));
280-
}
281-
282-
$basePathSegments = array_slice($basePathSegments, 0, -$numLevelUp);
283-
$path = preg_replace('|^/?(\.\./(\./)*)*|', '', $relativePath);
284-
285-
return implode('/', $basePathSegments) . '/' . $path;
286-
}
287-
288-
/**
289-
* Normalizes a URI path component by removing dot-slash and double slashes
290-
*
291-
* @param string $path
292-
* @return string
293-
*/
294-
private static function normalizePath($path)
295-
{
296-
$path = preg_replace('|((?<!\.)\./)*|', '', $path);
297-
$path = preg_replace('|//|', '/', $path);
298-
299-
return $path;
300-
}
301-
302-
/**
303-
* @return array
304-
*/
305-
private static function getPathSegments($path)
306-
{
307-
return explode('/', $path);
308-
}
309-
310276
/**
311277
* @param string $uri
312278
* @return boolean

0 commit comments

Comments
 (0)