secure-custom-fields
/
vendor
/
justinrainbow
/
json-schema
/
src
/
JsonSchema
/
Entity
/
JsonPointer.php
secure-custom-fields
/
vendor
/
justinrainbow
/
json-schema
/
src
/
JsonSchema
/
Entity
Last commit date
JsonPointer.php
8 months ago
JsonPointer.php
162 lines
| 1 | <?php |
| 2 | |
| 3 | /* |
| 4 | * This file is part of the JsonSchema package. |
| 5 | * |
| 6 | * For the full copyright and license information, please view the LICENSE |
| 7 | * file that was distributed with this source code. |
| 8 | */ |
| 9 | |
| 10 | namespace JsonSchema\Entity; |
| 11 | |
| 12 | use JsonSchema\Exception\InvalidArgumentException; |
| 13 | |
| 14 | /** |
| 15 | * @package JsonSchema\Entity |
| 16 | * |
| 17 | * @author Joost Nijhuis <jnijhuis81@gmail.com> |
| 18 | */ |
| 19 | class JsonPointer |
| 20 | { |
| 21 | /** @var string */ |
| 22 | private $filename; |
| 23 | |
| 24 | /** @var string[] */ |
| 25 | private $propertyPaths = array(); |
| 26 | |
| 27 | /** |
| 28 | * @var bool Whether the value at this path was set from a schema default |
| 29 | */ |
| 30 | private $fromDefault = false; |
| 31 | |
| 32 | /** |
| 33 | * @param string $value |
| 34 | * |
| 35 | * @throws InvalidArgumentException when $value is not a string |
| 36 | */ |
| 37 | public function __construct($value) |
| 38 | { |
| 39 | if (!is_string($value)) { |
| 40 | throw new InvalidArgumentException('Ref value must be a string'); |
| 41 | } |
| 42 | |
| 43 | $splitRef = explode('#', $value, 2); |
| 44 | $this->filename = $splitRef[0]; |
| 45 | if (array_key_exists(1, $splitRef)) { |
| 46 | $this->propertyPaths = $this->decodePropertyPaths($splitRef[1]); |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * @param string $propertyPathString |
| 52 | * |
| 53 | * @return string[] |
| 54 | */ |
| 55 | private function decodePropertyPaths($propertyPathString) |
| 56 | { |
| 57 | $paths = array(); |
| 58 | foreach (explode('/', trim($propertyPathString, '/')) as $path) { |
| 59 | $path = $this->decodePath($path); |
| 60 | if (is_string($path) && '' !== $path) { |
| 61 | $paths[] = $path; |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | return $paths; |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * @return array |
| 70 | */ |
| 71 | private function encodePropertyPaths() |
| 72 | { |
| 73 | return array_map( |
| 74 | array($this, 'encodePath'), |
| 75 | $this->getPropertyPaths() |
| 76 | ); |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * @param string $path |
| 81 | * |
| 82 | * @return string |
| 83 | */ |
| 84 | private function decodePath($path) |
| 85 | { |
| 86 | return strtr($path, array('~1' => '/', '~0' => '~', '%25' => '%')); |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * @param string $path |
| 91 | * |
| 92 | * @return string |
| 93 | */ |
| 94 | private function encodePath($path) |
| 95 | { |
| 96 | return strtr($path, array('/' => '~1', '~' => '~0', '%' => '%25')); |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * @return string |
| 101 | */ |
| 102 | public function getFilename() |
| 103 | { |
| 104 | return $this->filename; |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * @return string[] |
| 109 | */ |
| 110 | public function getPropertyPaths() |
| 111 | { |
| 112 | return $this->propertyPaths; |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * @param array $propertyPaths |
| 117 | * |
| 118 | * @return JsonPointer |
| 119 | */ |
| 120 | public function withPropertyPaths(array $propertyPaths) |
| 121 | { |
| 122 | $new = clone $this; |
| 123 | $new->propertyPaths = $propertyPaths; |
| 124 | |
| 125 | return $new; |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * @return string |
| 130 | */ |
| 131 | public function getPropertyPathAsString() |
| 132 | { |
| 133 | return rtrim('#/' . implode('/', $this->encodePropertyPaths()), '/'); |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * @return string |
| 138 | */ |
| 139 | public function __toString() |
| 140 | { |
| 141 | return $this->getFilename() . $this->getPropertyPathAsString(); |
| 142 | } |
| 143 | |
| 144 | /** |
| 145 | * Mark the value at this path as being set from a schema default |
| 146 | */ |
| 147 | public function setFromDefault() |
| 148 | { |
| 149 | $this->fromDefault = true; |
| 150 | } |
| 151 | |
| 152 | /** |
| 153 | * Check whether the value at this path was set from a schema default |
| 154 | * |
| 155 | * @return bool |
| 156 | */ |
| 157 | public function fromDefault() |
| 158 | { |
| 159 | return $this->fromDefault; |
| 160 | } |
| 161 | } |
| 162 |