11<?php
2+
23namespace Rs \Json ;
34
45use Rs \Json \Pointer \InvalidJsonException ;
@@ -19,12 +20,12 @@ class Pointer
1920 /**
2021 * @var string
2122 */
22- private $ pointer ;
23+ private string $ pointer ;
2324
2425 /**
2526 * @param string $json The Json structure to point through.
26- * @throws \Rs\Json\Pointer\ InvalidJsonException
27- * @throws \Rs\Json\Pointer\ NonWalkableJsonException
27+ * @throws InvalidJsonException
28+ * @throws NonWalkableJsonException
2829 */
2930 public function __construct ($ json )
3031 {
@@ -41,17 +42,17 @@ public function __construct($json)
4142
4243 /**
4344 * @param string $pointer The Json Pointer.
44- * @throws \Rs\Json\Pointer\ InvalidPointerException
45- * @throws \Rs\Json\Pointer\ NonexistentValueReferencedException
45+ * @throws InvalidPointerException
46+ * @throws NonexistentValueReferencedException
4647 *
4748 * @return mixed
4849 */
49- public function get ($ pointer )
50+ public function get ($ pointer ) : mixed
5051 {
5152 if ($ pointer === '' ) {
5253 $ output = \json_encode ($ this ->json , JSON_UNESCAPED_UNICODE );
5354 // workaround for https://bugs.php.net/bug.php?id=46600
54- return \str_replace (' "_empty_" ' , ' "" ' , $ output );
55+ return \str_replace ([ "_empty_ " , " \/ " ], [ "" , " / " ] , $ output );
5556 }
5657
5758 $ this ->validatePointer ($ pointer );
@@ -62,13 +63,14 @@ public function get($pointer)
6263 \array_map ('urldecode ' , \explode ('/ ' , $ pointer )),
6364 1
6465 );
66+
6567 return $ this ->traverse ($ this ->json , $ this ->evaluatePointerParts ($ plainPointerParts ));
6668 }
6769
6870 /**
6971 * @return string
7072 */
71- public function getPointer ()
73+ public function getPointer (): string
7274 {
7375 return $ this ->pointer ;
7476 }
@@ -77,12 +79,13 @@ public function getPointer()
7779 * @param array|\stdClass $json The json_decoded Json structure.
7880 * @param array $pointerParts The parts of the fed pointer.
7981 *
80- * @throws \Rs\Json\Pointer\ NonexistentValueReferencedException
82+ * @throws NonexistentValueReferencedException
8183 *
8284 * @return mixed
8385 */
84- private function traverse (&$ json , array $ pointerParts )
86+ private function traverse (&$ json , array $ pointerParts ) : mixed
8587 {
88+
8689 $ pointerPart = \array_shift ($ pointerParts );
8790
8891 if (\is_array ($ json ) && isset ($ json [$ pointerPart ])) {
@@ -124,7 +127,7 @@ private function traverse(&$json, array $pointerParts)
124127 /**
125128 * @return boolean
126129 */
127- private function isWalkableJson ()
130+ private function isWalkableJson (): bool
128131 {
129132 if ($ this ->json !== null && (\is_array ($ this ->json ) || $ this ->json instanceof \stdClass)) {
130133 return true ;
@@ -134,7 +137,7 @@ private function isWalkableJson()
134137
135138 /**
136139 * @param string $pointer The Json Pointer to validate.
137- * @throws \Rs\Json\Pointer\ InvalidPointerException
140+ * @throws InvalidPointerException
138141 */
139142 private function validatePointer ($ pointer )
140143 {
@@ -154,12 +157,12 @@ private function validatePointer($pointer)
154157 *
155158 * @return array
156159 */
157- private function evaluatePointerParts (array $ pointerParts )
160+ private function evaluatePointerParts (array $ pointerParts ): array
158161 {
159- $ searchables = array ( '~1 ' , '~0 ' ) ;
160- $ evaluations = array ( '/ ' , '~ ' ) ;
162+ $ searchables = [ '~1 ' , '~0 ' ] ;
163+ $ evaluations = [ '/ ' , '~ ' ] ;
161164
162- $ parts = array () ;
165+ $ parts = [] ;
163166 \array_filter ($ pointerParts , function ($ v ) use (&$ parts , &$ searchables , &$ evaluations ) {
164167 return $ parts [] = \str_replace ($ searchables , $ evaluations , $ v );
165168 });
0 commit comments