|
| 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 | +/** |
| 13 | + * @package JsonSchema\Entity |
| 14 | + * @author Joost Nijhuis <[email protected]> |
| 15 | + */ |
| 16 | +class JsonPointer |
| 17 | +{ |
| 18 | + /** @var string */ |
| 19 | + private $filename; |
| 20 | + |
| 21 | + /** @var string[] */ |
| 22 | + private $propertyPaths = array(); |
| 23 | + |
| 24 | + /** |
| 25 | + * @param string $value |
| 26 | + * @throws \InvalidArgumentException when $value is not a string |
| 27 | + */ |
| 28 | + public function __construct($value) |
| 29 | + { |
| 30 | + if (!is_string($value)) { |
| 31 | + throw new \InvalidArgumentException('Ref value must be a string'); |
| 32 | + } |
| 33 | + |
| 34 | + $splitRef = explode('#', $value, 2); |
| 35 | + $this->filename = $splitRef[0]; |
| 36 | + if (array_key_exists(1, $splitRef)) { |
| 37 | + $this->propertyPaths = $this->decodePropertyPaths($splitRef[1]); |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * @param string $propertyPathString |
| 43 | + * @return string[] |
| 44 | + */ |
| 45 | + private function decodePropertyPaths($propertyPathString) |
| 46 | + { |
| 47 | + $paths = array(); |
| 48 | + foreach (explode('/', trim($propertyPathString, '/')) as $path) { |
| 49 | + $path = $this->decodePath($path); |
| 50 | + if (is_string($path) && '' !== $path) { |
| 51 | + $paths[] = $path; |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + return $paths; |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * @return array |
| 60 | + */ |
| 61 | + private function encodePropertyPaths() |
| 62 | + { |
| 63 | + return array_map( |
| 64 | + array($this, 'encodePath'), |
| 65 | + $this->getPropertyPaths() |
| 66 | + ); |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * @param string $path |
| 71 | + * @return string |
| 72 | + */ |
| 73 | + private function decodePath($path) |
| 74 | + { |
| 75 | + return strtr($path, array('~1' => '/', '~0' => '~', '%25' => '%')); |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * @param string $path |
| 80 | + * @return string |
| 81 | + */ |
| 82 | + private function encodePath($path) |
| 83 | + { |
| 84 | + return strtr($path, array('/' => '~1', '~' => '~0', '%' => '%25')); |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * @return string |
| 89 | + */ |
| 90 | + public function getFilename() |
| 91 | + { |
| 92 | + return $this->filename; |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * @return string[] |
| 97 | + */ |
| 98 | + public function getPropertyPaths() |
| 99 | + { |
| 100 | + return $this->propertyPaths; |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * @return string |
| 105 | + */ |
| 106 | + public function getPropertyPathAsString() |
| 107 | + { |
| 108 | + return rtrim('#/' . implode('/', $this->encodePropertyPaths()), '/'); |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * @return string |
| 113 | + */ |
| 114 | + public function __toString() |
| 115 | + { |
| 116 | + return $this->getFilename() . $this->getPropertyPathAsString(); |
| 117 | + } |
| 118 | +} |
0 commit comments