Errors
1 year ago
Exceptions
1 year ago
Filters
1 year ago
Formats
1 year ago
Info
1 year ago
KeywordValidators
1 year ago
Keywords
1 year ago
Parsers
1 year ago
Pragmas
1 year ago
Resolvers
1 year ago
Schemas
1 year ago
Variables
1 year ago
CompliantValidator.php
1 year ago
ContentEncoding.php
1 year ago
ContentMediaType.php
1 year ago
Filter.php
1 year ago
Format.php
1 year ago
Helper.php
1 year ago
JsonPointer.php
1 year ago
Keyword.php
1 year ago
KeywordValidator.php
1 year ago
Pragma.php
1 year ago
Schema.php
1 year ago
SchemaLoader.php
7 months ago
SchemaValidator.php
1 year ago
Uri.php
1 year ago
ValidationContext.php
7 months ago
ValidationResult.php
1 year ago
Validator.php
1 year ago
Variables.php
1 year ago
JsonPointer.php
411 lines
| 1 | <?php |
| 2 | /* ============================================================================ |
| 3 | * Copyright 2020 Zindex Software |
| 4 | * |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | * ============================================================================ */ |
| 17 | |
| 18 | namespace Opis\JsonSchema; |
| 19 | |
| 20 | final class JsonPointer |
| 21 | { |
| 22 | /** @var string */ |
| 23 | protected const PATTERN = '~^(?:(?<level>0|[1-9][0-9]*)(?<shift>(?:\+|-)(?:0|[1-9][0-9]*))?)?(?<pointer>(?:/[^/#]*)*)(?<fragment>#)?$~'; |
| 24 | |
| 25 | /** @var string */ |
| 26 | protected const UNESCAPED = '/~([^01]|$)/'; |
| 27 | |
| 28 | protected int $level = -1; |
| 29 | |
| 30 | protected int $shift = 0; |
| 31 | |
| 32 | protected bool $fragment = false; |
| 33 | |
| 34 | /** @var string[]|int[] */ |
| 35 | protected array $path; |
| 36 | |
| 37 | protected ?string $str = null; |
| 38 | |
| 39 | final protected function __construct(array $path, int $level = -1, int $shift = 0, bool $fragment = false) |
| 40 | { |
| 41 | $this->path = $path; |
| 42 | $this->level = $level < 0 ? -1 : $level; |
| 43 | $this->shift = $shift; |
| 44 | $this->fragment = $level >= 0 && $fragment; |
| 45 | } |
| 46 | |
| 47 | public function isRelative(): bool |
| 48 | { |
| 49 | return $this->level >= 0; |
| 50 | } |
| 51 | |
| 52 | public function isAbsolute(): bool |
| 53 | { |
| 54 | return $this->level < 0; |
| 55 | } |
| 56 | |
| 57 | public function level(): int |
| 58 | { |
| 59 | return $this->level; |
| 60 | } |
| 61 | |
| 62 | public function shift(): int |
| 63 | { |
| 64 | return $this->shift; |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * @return string[] |
| 69 | */ |
| 70 | public function path(): array |
| 71 | { |
| 72 | return $this->path; |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * @return bool |
| 77 | */ |
| 78 | public function hasFragment(): bool |
| 79 | { |
| 80 | return $this->fragment; |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * @return string |
| 85 | */ |
| 86 | public function __toString(): string |
| 87 | { |
| 88 | if ($this->str === null) { |
| 89 | if ($this->level >= 0) { |
| 90 | $this->str = (string)$this->level; |
| 91 | |
| 92 | if ($this->shift !== 0) { |
| 93 | if ($this->shift > 0) { |
| 94 | $this->str .= '+'; |
| 95 | } |
| 96 | $this->str .= $this->shift; |
| 97 | } |
| 98 | |
| 99 | if ($this->path) { |
| 100 | $this->str .= '/'; |
| 101 | $this->str .= implode('/', self::encodePath($this->path)); |
| 102 | } |
| 103 | |
| 104 | if ($this->fragment) { |
| 105 | $this->str .= '#'; |
| 106 | } |
| 107 | } else { |
| 108 | $this->str = '/'; |
| 109 | $this->str .= implode('/', self::encodePath($this->path)); |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | return $this->str; |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * @param $data |
| 118 | * @param array|null $path |
| 119 | * @param mixed $default |
| 120 | * @return mixed |
| 121 | */ |
| 122 | public function data($data, ?array $path = null, $default = null) |
| 123 | { |
| 124 | if ($this->level < 0) { |
| 125 | return self::getData($data, $this->path, false, $default); |
| 126 | } |
| 127 | |
| 128 | if ($path !== null) { |
| 129 | $path = $this->absolutePath($path); |
| 130 | } |
| 131 | |
| 132 | if ($path === null) { |
| 133 | return $default; |
| 134 | } |
| 135 | |
| 136 | return self::getData($data, $path, $this->fragment, $default); |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * @param array $path |
| 141 | * @return array|null |
| 142 | */ |
| 143 | public function absolutePath(array $path = []): ?array |
| 144 | { |
| 145 | if ($this->level < 0) { |
| 146 | // Absolute pointer |
| 147 | return $this->path; |
| 148 | } |
| 149 | |
| 150 | if ($this->level === 0) { |
| 151 | if ($this->shift && !$this->handleShift($path)) { |
| 152 | return null; |
| 153 | } |
| 154 | return $this->path ? array_merge($path, $this->path) : $path; |
| 155 | } |
| 156 | |
| 157 | $count = count($path); |
| 158 | if ($count === $this->level) { |
| 159 | if ($this->shift) { |
| 160 | return null; |
| 161 | } |
| 162 | return $this->path; |
| 163 | } |
| 164 | |
| 165 | if ($count > $this->level) { |
| 166 | $count -= $this->level; |
| 167 | |
| 168 | /** @var array $path */ |
| 169 | $path = array_slice($path, 0, $count); |
| 170 | |
| 171 | if ($this->shift && !$this->handleShift($path, $count)) { |
| 172 | return null; |
| 173 | } |
| 174 | |
| 175 | return $this->path ? array_merge($path, $this->path) : $path; |
| 176 | } |
| 177 | |
| 178 | return null; |
| 179 | } |
| 180 | |
| 181 | protected function handleShift(array &$path, ?int $count = null): bool |
| 182 | { |
| 183 | if (!$path) { |
| 184 | return false; |
| 185 | } |
| 186 | |
| 187 | $count ??= count($path); |
| 188 | |
| 189 | $last = $path[$count - 1]; |
| 190 | |
| 191 | if (is_string($last) && preg_match('/^[1-9]\d*$/', $last)) { |
| 192 | $last = (int) $last; |
| 193 | } |
| 194 | |
| 195 | if (!is_int($last)) { |
| 196 | return false; |
| 197 | } |
| 198 | |
| 199 | $path[$count - 1] = $last + $this->shift; |
| 200 | |
| 201 | return true; |
| 202 | } |
| 203 | |
| 204 | public static function parse(string $pointer, bool $decode = true): ?self |
| 205 | { |
| 206 | if ($pointer === '' || !preg_match(self::PATTERN, $pointer, $m)) { |
| 207 | // Not a pointer |
| 208 | return null; |
| 209 | } |
| 210 | |
| 211 | $pointer = $m['pointer'] ?? null; |
| 212 | |
| 213 | // Check if the pointer is escaped |
| 214 | if ($decode && $pointer && preg_match(self::UNESCAPED, $pointer)) { |
| 215 | // Invalid pointer |
| 216 | return null; |
| 217 | } |
| 218 | |
| 219 | $level = isset($m['level']) && $m['level'] !== '' |
| 220 | ? (int)$m['level'] |
| 221 | : -1; |
| 222 | |
| 223 | $shift = 0; |
| 224 | if ($level >= 0 && isset($m['shift']) && $m['shift'] !== '') { |
| 225 | $shift = (int) $m['shift']; |
| 226 | } |
| 227 | |
| 228 | $fragment = isset($m['fragment']) && $m['fragment'] === '#'; |
| 229 | unset($m); |
| 230 | |
| 231 | if ($fragment && $level < 0) { |
| 232 | return null; |
| 233 | } |
| 234 | |
| 235 | if ($pointer === '') { |
| 236 | $pointer = null; |
| 237 | } elseif ($pointer !== null) { |
| 238 | // Remove leading slash |
| 239 | $pointer = substr($pointer, 1); |
| 240 | |
| 241 | if ($pointer !== '') { |
| 242 | $pointer = self::decodePath(explode('/', $pointer)); |
| 243 | } else { |
| 244 | $pointer = null; |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | return new self($pointer ?? [], $level, $shift, $fragment); |
| 249 | } |
| 250 | |
| 251 | /** |
| 252 | * @param $data |
| 253 | * @param array|null $path |
| 254 | * @param bool $fragment |
| 255 | * @param mixed $default |
| 256 | * @return mixed |
| 257 | */ |
| 258 | public static function getData($data, ?array $path = null, bool $fragment = false, $default = null) |
| 259 | { |
| 260 | if ($path === null) { |
| 261 | return $default; |
| 262 | } |
| 263 | |
| 264 | if (!$path) { |
| 265 | return $fragment ? $default : $data; |
| 266 | } |
| 267 | |
| 268 | if ($fragment) { |
| 269 | return end($path); |
| 270 | } |
| 271 | |
| 272 | foreach ($path as $key) { |
| 273 | if (is_array($data)) { |
| 274 | if (!array_key_exists($key, $data)) { |
| 275 | return $default; |
| 276 | } |
| 277 | $data = $data[$key]; |
| 278 | } elseif (is_object($data)) { |
| 279 | if (!property_exists($data, $key)) { |
| 280 | return $default; |
| 281 | } |
| 282 | $data = $data->{$key}; |
| 283 | } else { |
| 284 | return $default; |
| 285 | } |
| 286 | } |
| 287 | |
| 288 | return $data; |
| 289 | } |
| 290 | |
| 291 | /** |
| 292 | * @param string|string[] $path |
| 293 | * @return string|string[] |
| 294 | */ |
| 295 | public static function encodePath($path) |
| 296 | { |
| 297 | $path = str_replace('~', '~0', $path); |
| 298 | $path = str_replace('/', '~1', $path); |
| 299 | |
| 300 | if (is_array($path)) { |
| 301 | return array_map('rawurlencode', $path); |
| 302 | } |
| 303 | |
| 304 | return rawurlencode($path); |
| 305 | } |
| 306 | |
| 307 | /** |
| 308 | * @param string|string[] $path |
| 309 | * @return string|string[] |
| 310 | */ |
| 311 | public static function decodePath($path) |
| 312 | { |
| 313 | if (is_array($path)) { |
| 314 | $path = array_map('rawurldecode', $path); |
| 315 | } else { |
| 316 | $path = rawurldecode($path); |
| 317 | } |
| 318 | |
| 319 | $path = str_replace('~1', '/', $path); |
| 320 | $path = str_replace('~0', '~', $path); |
| 321 | |
| 322 | return $path; |
| 323 | } |
| 324 | |
| 325 | /** |
| 326 | * @param array $path |
| 327 | * @return string |
| 328 | */ |
| 329 | public static function pathToString(array $path): string |
| 330 | { |
| 331 | if (!$path) { |
| 332 | return '/'; |
| 333 | } |
| 334 | |
| 335 | return '/' . implode('/', self::encodePath($path)); |
| 336 | } |
| 337 | |
| 338 | /** |
| 339 | * @param array $path |
| 340 | * @return string |
| 341 | */ |
| 342 | public static function pathToFragment(array $path): string |
| 343 | { |
| 344 | if (!$path) { |
| 345 | return '#'; |
| 346 | } |
| 347 | |
| 348 | return '#/' . implode('/', self::encodePath($path)); |
| 349 | } |
| 350 | |
| 351 | /** |
| 352 | * @param string $pointer |
| 353 | * @return bool |
| 354 | */ |
| 355 | public static function isAbsolutePointer(string $pointer): bool |
| 356 | { |
| 357 | if ($pointer === '/') { |
| 358 | return true; |
| 359 | } |
| 360 | |
| 361 | if (!preg_match(self::PATTERN, $pointer, $m)) { |
| 362 | return false; |
| 363 | } |
| 364 | |
| 365 | if (isset($m['fragment']) || isset($m['level']) && $m['level'] !== '') { |
| 366 | return false; |
| 367 | } |
| 368 | |
| 369 | if (!isset($m['pointer']) || $m['pointer'] === '') { |
| 370 | return true; |
| 371 | } |
| 372 | |
| 373 | return !preg_match(self::UNESCAPED, $m['pointer']); |
| 374 | } |
| 375 | |
| 376 | /** |
| 377 | * @param string $pointer |
| 378 | * @return bool |
| 379 | */ |
| 380 | public static function isRelativePointer(string $pointer): bool |
| 381 | { |
| 382 | if ($pointer === '') { |
| 383 | return false; |
| 384 | } |
| 385 | |
| 386 | if (!preg_match(self::PATTERN, $pointer, $m)) { |
| 387 | return false; |
| 388 | } |
| 389 | |
| 390 | if (!isset($m['level']) || $m['level'] === '' || (int)$m['level'] < 0) { |
| 391 | return false; |
| 392 | } |
| 393 | |
| 394 | if (!isset($m['pointer']) || $m['pointer'] === '') { |
| 395 | return true; |
| 396 | } |
| 397 | |
| 398 | return !preg_match(self::UNESCAPED, $m['pointer']); |
| 399 | } |
| 400 | |
| 401 | public static function createAbsolute(array $path): self |
| 402 | { |
| 403 | return new self($path, -1, 0, false); |
| 404 | } |
| 405 | |
| 406 | public static function createRelative(int $level, array $path = [], int $shift = 0, bool $fragment = false): self |
| 407 | { |
| 408 | return new self($path, $level, $shift, $fragment); |
| 409 | } |
| 410 | } |
| 411 |