Skip to content

Commit b0fc435

Browse files
committed
Fix, replace '\/' with '/'
1 parent 7f31e32 commit b0fc435

File tree

2 files changed

+22
-19
lines changed

2 files changed

+22
-19
lines changed

src/Rs/Json/Pointer.php

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
23
namespace Rs\Json;
34

45
use 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
});

tests/Rs/Json/PointerTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
<?php declare(strict_types=1);
2+
23
namespace Rs\Json;
34

45
use ArrayObject;
56
use PHPUnit\Framework\Attributes\DataProvider;
67
use PHPUnit\Framework\Attributes\Test;
78
use PHPUnit\Framework\TestCase;
8-
use Rs\Json\Pointer;
99
use Rs\Json\Pointer\InvalidJsonException;
1010
use Rs\Json\Pointer\InvalidPointerException;
1111
use Rs\Json\Pointer\NonexistentValueReferencedException;
@@ -258,7 +258,7 @@ public static function lastArrayElementsTestDataProvider(): array
258258
public static function specSpecialCaseProvider(): array
259259
{
260260
return array(
261-
array('{"foo":["bar","baz"],"":0,"a\/b":1,"c%d":2,"e^f":3,"g|h":4,"k\"l":6," ":7,"m~n":8}', ''),
261+
array('{"foo":["bar","baz"],"":0,"a/b":1,"c%d":2,"e^f":3,"g|h":4,"k\"l":6," ":7,"m~n":8}', ''),
262262
array(array('bar', 'baz'), '/foo'),
263263
array('bar', '/foo/0'),
264264
array(0, '/'),
@@ -268,7 +268,7 @@ public static function specSpecialCaseProvider(): array
268268
array(4, '/g|h'),
269269
array(6, "/k\"l"),
270270
array(7, '/ '),
271-
array(8, '/m~0n'),
271+
array(8, '/m~0n')
272272
);
273273
}
274274
/**

0 commit comments

Comments
 (0)