forked from jsonrainbow/json-schema
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
jsonrainbow#240 Fix RefResolver and make it compatible with draft-04
- Loading branch information
1 parent
94d8d29
commit e45f2fe
Showing
10 changed files
with
617 additions
and
514 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,3 +7,4 @@ coverage | |
.settings | ||
composer.lock | ||
docs-api | ||
.idea |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the JsonSchema package. | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace JsonSchema\Entity; | ||
|
||
/** | ||
* @package JsonSchema\Entity | ||
* @author Joost Nijhuis <[email protected]> | ||
*/ | ||
class JsonPointer | ||
{ | ||
/** @var string */ | ||
private $filename; | ||
|
||
/** @var string[] */ | ||
private $propertyPaths = array(); | ||
|
||
/** | ||
* @param string $value | ||
*/ | ||
public function __construct($value) | ||
{ | ||
$this->validate($value); | ||
$splitRef = explode('#', $value, 2); | ||
$this->filename = $splitRef[0]; | ||
if (array_key_exists(1, $splitRef)) { | ||
$this->propertyPaths = $this->decodePropertyPaths($splitRef[1]); | ||
} | ||
} | ||
|
||
/** | ||
* @param string $value | ||
* @throws \InvalidArgumentException when $value is not a string | ||
* @throws \UnexpectedValueException when $value does not contain a hash sign | ||
*/ | ||
private function validate($value) | ||
{ | ||
if (!is_string($value)) { | ||
throw new \InvalidArgumentException('Ref value must be a string'); | ||
} | ||
|
||
if (false === strpos($value, '#')) { | ||
throw new \UnexpectedValueException('Ref value should contain a hash sign (#)'); | ||
} | ||
} | ||
|
||
/** | ||
* @param string $propertyPathString | ||
* @return string[] | ||
*/ | ||
private function decodePropertyPaths($propertyPathString) | ||
{ | ||
return array_reduce( | ||
explode('/', trim($propertyPathString, '/')), | ||
function (array $paths, $path) { | ||
$path = $this->decodePath($path); | ||
if ($path) { | ||
$paths[] = $path; | ||
} | ||
return $paths; | ||
}, | ||
array() | ||
); | ||
} | ||
|
||
/** | ||
* @return array | ||
*/ | ||
private function encodePropertyPaths() | ||
{ | ||
return array_map( | ||
[$this, 'encodePath'], | ||
$this->getPropertyPaths() | ||
); | ||
} | ||
|
||
/** | ||
* @param string $path | ||
* @return string | ||
*/ | ||
private function decodePath($path) | ||
{ | ||
return strtr($path, array('~1' => '/', '~0' => '~', '%25' => '%')); | ||
} | ||
|
||
/** | ||
* @param string $path | ||
* @return string | ||
*/ | ||
private function encodePath($path) | ||
{ | ||
return strtr($path, array('/' => '~1', '~' => '~0', '%' => '%25')); | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function getFilename() | ||
{ | ||
return $this->filename; | ||
} | ||
|
||
/** | ||
* @return string[] | ||
*/ | ||
public function getPropertyPaths() | ||
{ | ||
return $this->propertyPaths; | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function getPropertyPathAsString() | ||
{ | ||
return rtrim('#/' . implode('/', $this->encodePropertyPaths()), '/'); | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function __toString() | ||
{ | ||
return $this->getFilename() . $this->getPropertyPathAsString(); | ||
} | ||
} |
Oops, something went wrong.