Skip to content

Fix URI generic syntax delimit path components by slash ("/") #105

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 28, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 6 additions & 13 deletions src/JsonSchema/Uri/UriResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public function generate(array $components)
* Resolves a URI
*
* @param string $uri Absolute or relative
* @param type $baseUri Optional base URI
* @param string $baseUri Optional base URI
* @return string Absolute URI
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return annotation should be separated due to PHPDoc

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

more context.

*/
public function resolve($uri, $baseUri = null)
Expand Down Expand Up @@ -100,11 +100,11 @@ public function resolve($uri, $baseUri = null)

/**
* Tries to glue a relative path onto an absolute one
*
*
* @param string $relativePath
* @param string $basePath
* @return string Merged path
* @throws UriResolverException
* @throws UriResolverException
*/
public static function combineRelativePathWithBasePath($relativePath, $basePath)
{
Expand All @@ -116,13 +116,14 @@ public static function combineRelativePathWithBasePath($relativePath, $basePath)
return $relativePath;
}

$basePathSegments = self::getPathSegments($basePath);
$basePathSegments = explode('/', $basePath);

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

$basePathSegments = array_slice($basePathSegments, 0, -$numLevelUp);
$path = preg_replace('|^/?(\.\./(\./)*)*|', '', $relativePath);

Expand All @@ -143,14 +144,6 @@ private static function normalizePath($path)
return $path;
}

/**
* @return array
*/
private static function getPathSegments($path) {

return explode('/', $path);
}

/**
* @param string $uri
* @return boolean
Expand Down
76 changes: 21 additions & 55 deletions src/JsonSchema/Uri/UriRetriever.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use JsonSchema\Validator;
use JsonSchema\Exception\InvalidSchemaMediaTypeException;
use JsonSchema\Exception\JsonDecodingException;
use JsonSchema\Exception\ResourceNotFoundException;

/**
* Retrieves JSON Schema URIs
Expand All @@ -22,12 +23,23 @@
*/
class UriRetriever
{
/**
* @var null|UriRetrieverInterface
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

short descr is missing

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

varname - no need for waste.

*/
protected $uriRetriever = null;

/**
* @var array|object[]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in't it array of objects ? therefore only object[].

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

short descr is missing

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

follows this projects convention.

@see + varname, no waste needed.

* @see loadSchema
*/
private $schemaCache = array();

/**
* Guarantee the correct media type was encountered
*
* @throws InvalidSchemaMediaTypeException
* @param UriRetrieverInterface $uriRetriever
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

then parameter should be typehinted

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done.

* @param string $uri
* @return bool|void
*/
public function confirmMediaType($uriRetriever, $uri)
{
Expand Down Expand Up @@ -78,13 +90,13 @@ public function getUriRetriever()
* @param string $uri JSON Schema URI
* @return object JSON Schema after walking down the fragment pieces
*
* @throws \JsonSchema\Exception\ResourceNotFoundException
* @throws ResourceNotFoundException
*/
public function resolvePointer($jsonSchema, $uri)
{
$resolver = new UriResolver();
$parsed = $resolver->parse($uri);
if (empty($parsed['fragment'])) {
if (empty($parsed['fragment'])) {
return $jsonSchema;
}

Expand All @@ -97,14 +109,14 @@ public function resolvePointer($jsonSchema, $uri)
if (! empty($jsonSchema->$pathElement)) {
$jsonSchema = $jsonSchema->$pathElement;
} else {
throw new \JsonSchema\Exception\ResourceNotFoundException(
throw new ResourceNotFoundException(
'Fragment "' . $parsed['fragment'] . '" not found'
. ' in ' . $uri
);
}

if (! is_object($jsonSchema)) {
throw new \JsonSchema\Exception\ResourceNotFoundException(
throw new ResourceNotFoundException(
'Fragment part "' . $pathElement . '" is no object '
. ' in ' . $uri
);
Expand All @@ -119,8 +131,8 @@ public function resolvePointer($jsonSchema, $uri)
* Retrieve a URI
*
* @param string $uri JSON Schema URI
* @param string|null $baseUri
* @return object JSON Schema contents
* @throws InvalidSchemaMediaType for invalid media tyeps
*/
public function retrieve($uri, $baseUri = null)
{
Expand Down Expand Up @@ -167,6 +179,7 @@ protected function loadSchema($fetchUri)
}

$this->schemaCache[$fetchUri] = $jsonSchema;

return $jsonSchema;
}

Expand Down Expand Up @@ -240,7 +253,7 @@ public function generate(array $components)
* Resolves a URI
*
* @param string $uri Absolute or relative
* @param type $baseUri Optional base URI
* @param string $baseUri Optional base URI
* @return string
*/
public function resolve($uri, $baseUri = null)
Expand All @@ -255,58 +268,11 @@ public function resolve($uri, $baseUri = null)
$baseComponents = $this->parse($baseUri);
$basePath = $baseComponents['path'];

$baseComponents['path'] = self::combineRelativePathWithBasePath($path, $basePath);
$baseComponents['path'] = UriResolver::combineRelativePathWithBasePath($path, $basePath);

return $this->generate($baseComponents);
}

/**
* Tries to glue a relative path onto an absolute one
*
* @param string $relativePath
* @param string $basePath
* @return string Merged path
* @throws UriResolverException
*/
private static function combineRelativePathWithBasePath($relativePath, $basePath)
{
$relativePath = self::normalizePath($relativePath);
$basePathSegments = self::getPathSegments($basePath);

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

$basePathSegments = array_slice($basePathSegments, 0, -$numLevelUp);
$path = preg_replace('|^/?(\.\./(\./)*)*|', '', $relativePath);

return implode('/', $basePathSegments) . '/' . $path;
}

/**
* Normalizes a URI path component by removing dot-slash and double slashes
*
* @param string $path
* @return string
*/
private static function normalizePath($path)
{
$path = preg_replace('|((?<!\.)\./)*|', '', $path);
$path = preg_replace('|//|', '/', $path);

return $path;
}

/**
* @return array
*/
private static function getPathSegments($path)
{
return explode('/', $path);
}

/**
* @param string $uri
* @return boolean
Expand Down