From dc763526bd56f887e17aa56c3dcd570a596e2e26 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20Wolfsj=C3=A4ger?= Date: Wed, 19 Jan 2022 15:27:36 +0100 Subject: [PATCH] Fix JSON stream parser test for root JSON pointer --- src/Parser.php | 8 +++++--- src/ValidJsonPointers.php | 3 +++ test/JsonMachineTest/ParserTest.php | 1 + 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/src/Parser.php b/src/Parser.php index 795b3fd..80d5b91 100644 --- a/src/Parser.php +++ b/src/Parser.php @@ -368,11 +368,13 @@ class Parser implements \IteratorAggregate, PositionAware throw new JsonMachineException('getCurrentJsonPointer() must not be called outside of a loop'); } - $currentJsonPointer = implode('/', array_map(function ($jsonPointerPart) { + $currentJsonPointer = array_map(function ($jsonPointerPart) { return str_replace(['~', '/'], ['~0', '~1'], $jsonPointerPart); - }, $this->currentJsonPath)); + }, $this->currentJsonPath); - return ($currentJsonPointer !== '') ? '/'.$currentJsonPointer : ''; + array_unshift($currentJsonPointer, ''); + + return implode('/', $currentJsonPointer); } public function getMatchedJsonPointer(): string diff --git a/src/ValidJsonPointers.php b/src/ValidJsonPointers.php index e515739..fd678d5 100644 --- a/src/ValidJsonPointers.php +++ b/src/ValidJsonPointers.php @@ -17,6 +17,9 @@ final class ValidJsonPointers $this->jsonPointers = array_values($jsonPointers); } + /** + * @throws InvalidArgumentException + */ public function toArray(): array { if (! $this->validated) { diff --git a/test/JsonMachineTest/ParserTest.php b/test/JsonMachineTest/ParserTest.php index 82c96de..8a03478 100644 --- a/test/JsonMachineTest/ParserTest.php +++ b/test/JsonMachineTest/ParserTest.php @@ -324,6 +324,7 @@ class ParserTest extends \PHPUnit_Framework_TestCase { return [ ['', '{"c":1,"d":2}', ['', '']], + ['/', '{"":{"c":1,"d":2}}', ['/', '/']], ['/~0', '{"~":{"c":1,"d":2}}', ['/~0', '/~0']], ['/~1', '{"/":{"c":1,"d":2}}', ['/~1', '/~1']], ['/~1/c', '{"/":{"c":[1,2],"d":2}}', ['/~1/c', '/~1/c']],