1
0
mirror of https://github.com/halaxa/json-machine.git synced 2025-07-18 21:11:16 +02:00

Fixes #6 - falsy pointer does not match falsy path

This commit is contained in:
Filip Halaxa
2019-12-13 22:02:29 +01:00
parent 0d128d676a
commit b7ffc662c9
2 changed files with 19 additions and 7 deletions

View File

@@ -74,10 +74,9 @@ class Parser implements \IteratorAggregate
$this->lexer = $lexer; $this->lexer = $lexer;
$this->jsonPointer = $jsonPointer; $this->jsonPointer = $jsonPointer;
$this->jsonPointerPath = array_slice(array_map(function ($jsonPointerPart){ $this->jsonPointerPath = array_slice(array_map(function ($jsonPointerPart){
$jsonPointerPart = str_replace( return str_replace(
'~0', '~', str_replace('~1', '/', $jsonPointerPart) '~0', '~', str_replace('~1', '/', $jsonPointerPart)
); );
return is_numeric($jsonPointerPart) ? (int) $jsonPointerPart : $jsonPointerPart;
}, explode('/', $jsonPointer)), 1); }, explode('/', $jsonPointer)), 1);
} }
@@ -105,11 +104,11 @@ class Parser implements \IteratorAggregate
if ( ! isset($this->type[$firstChar]) || ! ($this->type[$firstChar] & $expectedType)) { if ( ! isset($this->type[$firstChar]) || ! ($this->type[$firstChar] & $expectedType)) {
$this->error("Unexpected symbol"); $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; $jsonBuffer .= $this->token;
} }
if ($currentLevel < $iteratorLevel && $inArray && $expectedType & self::ANY_VALUE) { 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) { switch ($firstChar) {
case '"': case '"':

View File

@@ -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->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); 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 * @dataProvider dataGetPathSpec
*/ */