ex
Fork of mbed-os-example-mbed5-blinky by
dumi_doc-master/schema/vendor/justinrainbow/json-schema/src/JsonSchema/Entity/JsonPointer.php@44:c1d8923072ba, 2017-07-18 (annotated)
- Committer:
- TMBOY
- Date:
- Tue Jul 18 16:27:22 2017 +0800
- Revision:
- 44:c1d8923072ba
?
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| TMBOY | 44:c1d8923072ba | 1 | <?php |
| TMBOY | 44:c1d8923072ba | 2 | |
| TMBOY | 44:c1d8923072ba | 3 | /* |
| TMBOY | 44:c1d8923072ba | 4 | * This file is part of the JsonSchema package. |
| TMBOY | 44:c1d8923072ba | 5 | * |
| TMBOY | 44:c1d8923072ba | 6 | * For the full copyright and license information, please view the LICENSE |
| TMBOY | 44:c1d8923072ba | 7 | * file that was distributed with this source code. |
| TMBOY | 44:c1d8923072ba | 8 | */ |
| TMBOY | 44:c1d8923072ba | 9 | |
| TMBOY | 44:c1d8923072ba | 10 | namespace JsonSchema\Entity; |
| TMBOY | 44:c1d8923072ba | 11 | |
| TMBOY | 44:c1d8923072ba | 12 | /** |
| TMBOY | 44:c1d8923072ba | 13 | * @package JsonSchema\Entity |
| TMBOY | 44:c1d8923072ba | 14 | * @author Joost Nijhuis <jnijhuis81@gmail.com> |
| TMBOY | 44:c1d8923072ba | 15 | */ |
| TMBOY | 44:c1d8923072ba | 16 | class JsonPointer |
| TMBOY | 44:c1d8923072ba | 17 | { |
| TMBOY | 44:c1d8923072ba | 18 | /** @var string */ |
| TMBOY | 44:c1d8923072ba | 19 | private $filename; |
| TMBOY | 44:c1d8923072ba | 20 | |
| TMBOY | 44:c1d8923072ba | 21 | /** @var string[] */ |
| TMBOY | 44:c1d8923072ba | 22 | private $propertyPaths = array(); |
| TMBOY | 44:c1d8923072ba | 23 | |
| TMBOY | 44:c1d8923072ba | 24 | /** |
| TMBOY | 44:c1d8923072ba | 25 | * @param string $value |
| TMBOY | 44:c1d8923072ba | 26 | * @throws \InvalidArgumentException when $value is not a string |
| TMBOY | 44:c1d8923072ba | 27 | */ |
| TMBOY | 44:c1d8923072ba | 28 | public function __construct($value) |
| TMBOY | 44:c1d8923072ba | 29 | { |
| TMBOY | 44:c1d8923072ba | 30 | if (!is_string($value)) { |
| TMBOY | 44:c1d8923072ba | 31 | throw new \InvalidArgumentException('Ref value must be a string'); |
| TMBOY | 44:c1d8923072ba | 32 | } |
| TMBOY | 44:c1d8923072ba | 33 | |
| TMBOY | 44:c1d8923072ba | 34 | $splitRef = explode('#', $value, 2); |
| TMBOY | 44:c1d8923072ba | 35 | $this->filename = $splitRef[0]; |
| TMBOY | 44:c1d8923072ba | 36 | if (array_key_exists(1, $splitRef)) { |
| TMBOY | 44:c1d8923072ba | 37 | $this->propertyPaths = $this->decodePropertyPaths($splitRef[1]); |
| TMBOY | 44:c1d8923072ba | 38 | } |
| TMBOY | 44:c1d8923072ba | 39 | } |
| TMBOY | 44:c1d8923072ba | 40 | |
| TMBOY | 44:c1d8923072ba | 41 | /** |
| TMBOY | 44:c1d8923072ba | 42 | * @param string $propertyPathString |
| TMBOY | 44:c1d8923072ba | 43 | * @return string[] |
| TMBOY | 44:c1d8923072ba | 44 | */ |
| TMBOY | 44:c1d8923072ba | 45 | private function decodePropertyPaths($propertyPathString) |
| TMBOY | 44:c1d8923072ba | 46 | { |
| TMBOY | 44:c1d8923072ba | 47 | $paths = array(); |
| TMBOY | 44:c1d8923072ba | 48 | foreach (explode('/', trim($propertyPathString, '/')) as $path) { |
| TMBOY | 44:c1d8923072ba | 49 | $path = $this->decodePath($path); |
| TMBOY | 44:c1d8923072ba | 50 | if (is_string($path) && '' !== $path) { |
| TMBOY | 44:c1d8923072ba | 51 | $paths[] = $path; |
| TMBOY | 44:c1d8923072ba | 52 | } |
| TMBOY | 44:c1d8923072ba | 53 | } |
| TMBOY | 44:c1d8923072ba | 54 | |
| TMBOY | 44:c1d8923072ba | 55 | return $paths; |
| TMBOY | 44:c1d8923072ba | 56 | } |
| TMBOY | 44:c1d8923072ba | 57 | |
| TMBOY | 44:c1d8923072ba | 58 | /** |
| TMBOY | 44:c1d8923072ba | 59 | * @return array |
| TMBOY | 44:c1d8923072ba | 60 | */ |
| TMBOY | 44:c1d8923072ba | 61 | private function encodePropertyPaths() |
| TMBOY | 44:c1d8923072ba | 62 | { |
| TMBOY | 44:c1d8923072ba | 63 | return array_map( |
| TMBOY | 44:c1d8923072ba | 64 | array($this, 'encodePath'), |
| TMBOY | 44:c1d8923072ba | 65 | $this->getPropertyPaths() |
| TMBOY | 44:c1d8923072ba | 66 | ); |
| TMBOY | 44:c1d8923072ba | 67 | } |
| TMBOY | 44:c1d8923072ba | 68 | |
| TMBOY | 44:c1d8923072ba | 69 | /** |
| TMBOY | 44:c1d8923072ba | 70 | * @param string $path |
| TMBOY | 44:c1d8923072ba | 71 | * @return string |
| TMBOY | 44:c1d8923072ba | 72 | */ |
| TMBOY | 44:c1d8923072ba | 73 | private function decodePath($path) |
| TMBOY | 44:c1d8923072ba | 74 | { |
| TMBOY | 44:c1d8923072ba | 75 | return strtr($path, array('~1' => '/', '~0' => '~', '%25' => '%')); |
| TMBOY | 44:c1d8923072ba | 76 | } |
| TMBOY | 44:c1d8923072ba | 77 | |
| TMBOY | 44:c1d8923072ba | 78 | /** |
| TMBOY | 44:c1d8923072ba | 79 | * @param string $path |
| TMBOY | 44:c1d8923072ba | 80 | * @return string |
| TMBOY | 44:c1d8923072ba | 81 | */ |
| TMBOY | 44:c1d8923072ba | 82 | private function encodePath($path) |
| TMBOY | 44:c1d8923072ba | 83 | { |
| TMBOY | 44:c1d8923072ba | 84 | return strtr($path, array('/' => '~1', '~' => '~0', '%' => '%25')); |
| TMBOY | 44:c1d8923072ba | 85 | } |
| TMBOY | 44:c1d8923072ba | 86 | |
| TMBOY | 44:c1d8923072ba | 87 | /** |
| TMBOY | 44:c1d8923072ba | 88 | * @return string |
| TMBOY | 44:c1d8923072ba | 89 | */ |
| TMBOY | 44:c1d8923072ba | 90 | public function getFilename() |
| TMBOY | 44:c1d8923072ba | 91 | { |
| TMBOY | 44:c1d8923072ba | 92 | return $this->filename; |
| TMBOY | 44:c1d8923072ba | 93 | } |
| TMBOY | 44:c1d8923072ba | 94 | |
| TMBOY | 44:c1d8923072ba | 95 | /** |
| TMBOY | 44:c1d8923072ba | 96 | * @return string[] |
| TMBOY | 44:c1d8923072ba | 97 | */ |
| TMBOY | 44:c1d8923072ba | 98 | public function getPropertyPaths() |
| TMBOY | 44:c1d8923072ba | 99 | { |
| TMBOY | 44:c1d8923072ba | 100 | return $this->propertyPaths; |
| TMBOY | 44:c1d8923072ba | 101 | } |
| TMBOY | 44:c1d8923072ba | 102 | |
| TMBOY | 44:c1d8923072ba | 103 | /** |
| TMBOY | 44:c1d8923072ba | 104 | * @param array $propertyPaths |
| TMBOY | 44:c1d8923072ba | 105 | * @return JsonPointer |
| TMBOY | 44:c1d8923072ba | 106 | */ |
| TMBOY | 44:c1d8923072ba | 107 | public function withPropertyPaths(array $propertyPaths) |
| TMBOY | 44:c1d8923072ba | 108 | { |
| TMBOY | 44:c1d8923072ba | 109 | $new = clone $this; |
| TMBOY | 44:c1d8923072ba | 110 | $new->propertyPaths = $propertyPaths; |
| TMBOY | 44:c1d8923072ba | 111 | return $new; |
| TMBOY | 44:c1d8923072ba | 112 | } |
| TMBOY | 44:c1d8923072ba | 113 | |
| TMBOY | 44:c1d8923072ba | 114 | /** |
| TMBOY | 44:c1d8923072ba | 115 | * @return string |
| TMBOY | 44:c1d8923072ba | 116 | */ |
| TMBOY | 44:c1d8923072ba | 117 | public function getPropertyPathAsString() |
| TMBOY | 44:c1d8923072ba | 118 | { |
| TMBOY | 44:c1d8923072ba | 119 | return rtrim('#/' . implode('/', $this->encodePropertyPaths()), '/'); |
| TMBOY | 44:c1d8923072ba | 120 | } |
| TMBOY | 44:c1d8923072ba | 121 | |
| TMBOY | 44:c1d8923072ba | 122 | /** |
| TMBOY | 44:c1d8923072ba | 123 | * @return string |
| TMBOY | 44:c1d8923072ba | 124 | */ |
| TMBOY | 44:c1d8923072ba | 125 | public function __toString() |
| TMBOY | 44:c1d8923072ba | 126 | { |
| TMBOY | 44:c1d8923072ba | 127 | return $this->getFilename() . $this->getPropertyPathAsString(); |
| TMBOY | 44:c1d8923072ba | 128 | } |
| TMBOY | 44:c1d8923072ba | 129 | } |
