Skip to content

Commit 58642c2

Browse files
committed
Refactor: Remove duplicate code.
UriResolver instead of self. Additionally some minor CS fixes.
1 parent 51a2e53 commit 58642c2

File tree

2 files changed

+16
-52
lines changed

2 files changed

+16
-52
lines changed

src/JsonSchema/Uri/UriResolver.php

Lines changed: 1 addition & 1 deletion
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)

src/JsonSchema/Uri/UriRetriever.php

Lines changed: 15 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
use JsonSchema\Validator;
1515
use JsonSchema\Exception\InvalidSchemaMediaTypeException;
1616
use JsonSchema\Exception\JsonDecodingException;
17-
use JsonSchema\Exception\UriResolverException;
1817

1918
/**
2019
* Retrieves JSON Schema URIs
@@ -23,12 +22,23 @@
2322
*/
2423
class UriRetriever
2524
{
25+
/**
26+
* @var null|UriRetrieverInterface
27+
*/
2628
protected $uriRetriever = null;
2729

30+
/**
31+
* @var array|object[]
32+
* @see loadSchema
33+
*/
34+
private $schemaCache = array();
35+
2836
/**
2937
* Guarantee the correct media type was encountered
3038
*
31-
* @throws InvalidSchemaMediaTypeException
39+
* @param UriRetrieverInterface $uriRetriever
40+
* @param $uri
41+
* @return bool|void
3242
*/
3343
public function confirmMediaType($uriRetriever, $uri)
3444
{
@@ -120,8 +130,8 @@ public function resolvePointer($jsonSchema, $uri)
120130
* Retrieve a URI
121131
*
122132
* @param string $uri JSON Schema URI
133+
* @param null $baseUri
123134
* @return object JSON Schema contents
124-
* @throws InvalidSchemaMediaType for invalid media tyeps
125135
*/
126136
public function retrieve($uri, $baseUri = null)
127137
{
@@ -241,7 +251,7 @@ public function generate(array $components)
241251
* Resolves a URI
242252
*
243253
* @param string $uri Absolute or relative
244-
* @param type $baseUri Optional base URI
254+
* @param string $baseUri Optional base URI
245255
* @return string
246256
*/
247257
public function resolve($uri, $baseUri = null)
@@ -256,57 +266,11 @@ public function resolve($uri, $baseUri = null)
256266
$baseComponents = $this->parse($baseUri);
257267
$basePath = $baseComponents['path'];
258268

259-
$baseComponents['path'] = self::combineRelativePathWithBasePath($path, $basePath);
269+
$baseComponents['path'] = UriResolver::combineRelativePathWithBasePath($path, $basePath);
260270

261271
return $this->generate($baseComponents);
262272
}
263273

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

0 commit comments

Comments
 (0)