4 * This file is part of the JsonSchema package.
6 * For the full copyright and license information, please view the LICENSE
7 * file that was distributed with this source code.
10 namespace JsonSchema\Entity;
12 use JsonSchema\Exception\InvalidArgumentException;
15 * @package JsonSchema\Entity
17 * @author Joost Nijhuis <jnijhuis81@gmail.com>
25 private $propertyPaths = array();
28 * @var bool Whether the value at this path was set from a schema default
30 private $fromDefault = false;
33 * @param string $value
35 * @throws InvalidArgumentException when $value is not a string
37 public function __construct($value)
39 if (!is_string($value)) {
40 throw new InvalidArgumentException('Ref value must be a string');
43 $splitRef = explode('#', $value, 2);
44 $this->filename = $splitRef[0];
45 if (array_key_exists(1, $splitRef)) {
46 $this->propertyPaths = $this->decodePropertyPaths($splitRef[1]);
51 * @param string $propertyPathString
55 private function decodePropertyPaths($propertyPathString)
58 foreach (explode('/', trim($propertyPathString, '/')) as $path) {
59 $path = $this->decodePath($path);
60 if (is_string($path) && '' !== $path) {
71 private function encodePropertyPaths()
74 array($this, 'encodePath'),
75 $this->getPropertyPaths()
84 private function decodePath($path)
86 return strtr($path, array('~1' => '/', '~0' => '~', '%25' => '%'));
94 private function encodePath($path)
96 return strtr($path, array('/' => '~1', '~' => '~0', '%' => '%25'));
102 public function getFilename()
104 return $this->filename;
110 public function getPropertyPaths()
112 return $this->propertyPaths;
116 * @param array $propertyPaths
118 * @return JsonPointer
120 public function withPropertyPaths(array $propertyPaths)
123 $new->propertyPaths = $propertyPaths;
131 public function getPropertyPathAsString()
133 return rtrim('#/' . implode('/', $this->encodePropertyPaths()), '/');
139 public function __toString()
141 return $this->getFilename() . $this->getPropertyPathAsString();
145 * Mark the value at this path as being set from a schema default
147 public function setFromDefault()
149 $this->fromDefault = true;
153 * Check whether the value at this path was set from a schema default
157 public function fromDefault()
159 return $this->fromDefault;