-
Notifications
You must be signed in to change notification settings - Fork 357
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #245 from jojo1981/feature/ref-resolver
#240 Fix RefResolver and make it compatible with draft-04
- Loading branch information
Showing
19 changed files
with
1,294 additions
and
579 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
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
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,118 @@ | ||
<?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 | ||
* @throws \InvalidArgumentException when $value is not a string | ||
*/ | ||
public function __construct($value) | ||
{ | ||
if (!is_string($value)) { | ||
throw new \InvalidArgumentException('Ref value must be a string'); | ||
} | ||
|
||
$splitRef = explode('#', $value, 2); | ||
$this->filename = $splitRef[0]; | ||
if (array_key_exists(1, $splitRef)) { | ||
$this->propertyPaths = $this->decodePropertyPaths($splitRef[1]); | ||
} | ||
} | ||
|
||
/** | ||
* @param string $propertyPathString | ||
* @return string[] | ||
*/ | ||
private function decodePropertyPaths($propertyPathString) | ||
{ | ||
$paths = array(); | ||
foreach (explode('/', trim($propertyPathString, '/')) as $path) { | ||
$path = $this->decodePath($path); | ||
if (is_string($path) && '' !== $path) { | ||
$paths[] = $path; | ||
} | ||
} | ||
|
||
return $paths; | ||
} | ||
|
||
/** | ||
* @return array | ||
*/ | ||
private function encodePropertyPaths() | ||
{ | ||
return array_map( | ||
array($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(); | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
src/JsonSchema/Exception/UnresolvableJsonPointerException.php
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,18 @@ | ||
<?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\Exception; | ||
|
||
/** | ||
* @package JsonSchema\Exception | ||
* @author Joost Nijhuis <[email protected]> | ||
*/ | ||
class UnresolvableJsonPointerException extends \InvalidArgumentException | ||
{ | ||
} |
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,147 @@ | ||
<?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\Iterator; | ||
|
||
/** | ||
* @package JsonSchema\Iterator | ||
* @author Joost Nijhuis <[email protected]> | ||
*/ | ||
class ObjectIterator implements \Iterator, \Countable | ||
{ | ||
/** @var object */ | ||
private $object; | ||
|
||
/** @var int */ | ||
private $position = 0; | ||
|
||
/** @var array */ | ||
private $data = array(); | ||
|
||
/** @var bool */ | ||
private $initialized = false; | ||
|
||
/** | ||
* @param object $object | ||
*/ | ||
public function __construct($object) | ||
{ | ||
$this->object = $object; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function current() | ||
{ | ||
$this->initialize(); | ||
|
||
return $this->data[$this->position]; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function next() | ||
{ | ||
$this->initialize(); | ||
$this->position++; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function key() | ||
{ | ||
$this->initialize(); | ||
|
||
return $this->position; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function valid() | ||
{ | ||
$this->initialize(); | ||
|
||
return isset($this->data[$this->position]); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function rewind() | ||
{ | ||
$this->initialize(); | ||
$this->position = 0; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function count() | ||
{ | ||
$this->initialize(); | ||
|
||
return count($this->data); | ||
} | ||
|
||
/** | ||
* Initializer | ||
*/ | ||
private function initialize() | ||
{ | ||
if (!$this->initialized) { | ||
$this->data = $this->buildDataFromObject($this->object); | ||
$this->initialized = true; | ||
} | ||
} | ||
|
||
/** | ||
* @param object $object | ||
* @return array | ||
*/ | ||
private function buildDataFromObject($object) | ||
{ | ||
$result = array(); | ||
|
||
$stack = new \SplStack(); | ||
$stack->push($object); | ||
|
||
while (!$stack->isEmpty()) { | ||
|
||
$current = $stack->pop(); | ||
if (is_object($current)) { | ||
array_push($result, $current); | ||
} | ||
|
||
foreach ($this->getDataFromItem($current) as $propertyName => $propertyValue) { | ||
if (is_object($propertyValue) || is_array($propertyValue)) { | ||
$stack->push($propertyValue); | ||
} | ||
} | ||
} | ||
|
||
return $result; | ||
} | ||
|
||
/** | ||
* @param object|array $item | ||
* @return array | ||
*/ | ||
private function getDataFromItem($item) | ||
{ | ||
if (!is_object($item) && !is_array($item)) { | ||
return array(); | ||
} | ||
|
||
return is_object($item) ? get_object_vars($item) : $item; | ||
} | ||
} |
Oops, something went wrong.