diff --git a/src/Parser.php b/src/Parser.php index 779cd16..2c0cb0c 100644 --- a/src/Parser.php +++ b/src/Parser.php @@ -74,10 +74,9 @@ class Parser implements \IteratorAggregate $this->lexer = $lexer; $this->jsonPointer = $jsonPointer; $this->jsonPointerPath = array_slice(array_map(function ($jsonPointerPart){ - $jsonPointerPart = str_replace( + return str_replace( '~0', '~', str_replace('~1', '/', $jsonPointerPart) ); - return is_numeric($jsonPointerPart) ? (int) $jsonPointerPart : $jsonPointerPart; }, explode('/', $jsonPointer)), 1); } @@ -105,11 +104,11 @@ class Parser implements \IteratorAggregate if ( ! isset($this->type[$firstChar]) || ! ($this->type[$firstChar] & $expectedType)) { $this->error("Unexpected symbol"); } - if ($currentPath == $this->jsonPointerPath && ($currentLevel > $iteratorLevel || ($currentLevel === $iteratorLevel && $expectedType & self::ANY_VALUE))) { + if ($currentPath === $this->jsonPointerPath && ($currentLevel > $iteratorLevel || ($currentLevel === $iteratorLevel && $expectedType & self::ANY_VALUE))) { $jsonBuffer .= $this->token; } if ($currentLevel < $iteratorLevel && $inArray && $expectedType & self::ANY_VALUE) { - $currentPath[$currentLevel] = isset($currentPath[$currentLevel]) ? (1+$currentPath[$currentLevel]) : 0; + $currentPath[$currentLevel] = isset($currentPath[$currentLevel]) ? (string)(1+(int)$currentPath[$currentLevel]) : "0"; } switch ($firstChar) { case '"': diff --git a/test/JsonMachineTest/ParserTest.php b/test/JsonMachineTest/ParserTest.php index d1f4036..dccb678 100644 --- a/test/JsonMachineTest/ParserTest.php +++ b/test/JsonMachineTest/ParserTest.php @@ -54,14 +54,27 @@ class ParserTest extends \PHPUnit_Framework_TestCase ]; } - public function testThrowsOnNotFoundPathSpec() + /** + * @dataProvider dataThrowsOnNotFoundJsonPointer + */ + public function testThrowsOnNotFoundJsonPointer($json, $jsonPointer) { - $parser = $this->createParser('{}', '/not/found'); + $parser = $this->createParser($json, $jsonPointer); $this->expectException(PathNotFoundException::class); - $this->expectExceptionMessage("Path '/not/found' was not found in json stream."); + $this->expectExceptionMessage("Path '$jsonPointer' was not found in json stream."); iterator_to_array($parser); } + public function dataThrowsOnNotFoundJsonPointer() + { + return [ + "non existing pointer" => ['{}', '/not/found'], + "empty string should not match '0'" => ['{"0":[]}', '/'], + "empty string should not match 0" => ['[[]]', '/'], + "0 should not match empty string" => ['{"":[]}', '/0'], + ]; + } + /** * @dataProvider dataGetPathSpec */