mirror of
https://github.com/cerbero90/json-parser.git
synced 2025-01-29 10:38:09 +01:00
Allow setting an empty-string pointer
This commit is contained in:
parent
00c0bf4a0b
commit
66229aeea4
@ -109,11 +109,12 @@ class Pointer implements Stringable
|
||||
*/
|
||||
public function includesTree(Tree $tree): bool
|
||||
{
|
||||
if (($firstNest = array_search('-', $this->referenceTokens)) === false) {
|
||||
return false;
|
||||
if ($this->pointer == '') {
|
||||
return true;
|
||||
}
|
||||
|
||||
return array_slice($this->referenceTokens, 0, $firstNest) == array_slice($tree->original(), 0, $firstNest);
|
||||
return (($firstNest = array_search('-', $this->referenceTokens)) !== false)
|
||||
&& array_slice($this->referenceTokens, 0, $firstNest) == array_slice($tree->original(), 0, $firstNest);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -19,13 +19,6 @@ class State
|
||||
*/
|
||||
protected Tree $tree;
|
||||
|
||||
/**
|
||||
* Whether the tree changed.
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
protected bool $treeChanged = false;
|
||||
|
||||
/**
|
||||
* The JSON pointers.
|
||||
*
|
||||
@ -139,22 +132,22 @@ class State
|
||||
*/
|
||||
public function mutateByToken(Token $token): void
|
||||
{
|
||||
$this->treeChanged = false;
|
||||
$treeChanged = false;
|
||||
$shouldTrackTree = $this->pointer == '' || $this->tree->depth() < $this->pointer->depth();
|
||||
|
||||
if ($shouldTrackTree && $token->isValue() && !$this->inObject()) {
|
||||
$this->tree->traverseArray($this->pointer->referenceTokens());
|
||||
$this->treeChanged = true;
|
||||
$treeChanged = true;
|
||||
}
|
||||
|
||||
if ($shouldTrackTree && $this->expectsKey) {
|
||||
$this->tree->traverseKey($token);
|
||||
$this->treeChanged = true;
|
||||
$treeChanged = true;
|
||||
}
|
||||
|
||||
$this->bufferToken($token);
|
||||
|
||||
if ($this->treeChanged && $this->pointers->count() > 1) {
|
||||
if ($treeChanged && $this->pointers->count() > 1) {
|
||||
$this->pointer = $this->pointers->matchTree($this->tree);
|
||||
}
|
||||
|
||||
@ -186,8 +179,7 @@ class State
|
||||
*/
|
||||
protected function pointerMatchesTree(): bool
|
||||
{
|
||||
return $this->pointer == ''
|
||||
|| in_array($this->pointer->referenceTokens(), [$this->tree->original(), $this->tree->wildcarded()]);
|
||||
return in_array($this->pointer->referenceTokens(), [[], $this->tree->original(), $this->tree->wildcarded()]);
|
||||
}
|
||||
|
||||
/**
|
||||
|
7
tests/fixtures/pointers/single_pointer.php
vendored
7
tests/fixtures/pointers/single_pointer.php
vendored
@ -2,6 +2,7 @@
|
||||
|
||||
return [
|
||||
'complex_array' => [
|
||||
'' => require __DIR__ . '/../parsing/complex_array.php',
|
||||
'/-' => [
|
||||
[
|
||||
"id" => "0001",
|
||||
@ -248,6 +249,7 @@ return [
|
||||
'/-/batters/batter/-/id' => ['id' => ["1001", "1002", "1003", "1004", "1001", "1001", "1002"]],
|
||||
],
|
||||
'complex_object' => [
|
||||
'' => require __DIR__ . '/../parsing/complex_object.php',
|
||||
'/id' => ['id' => '0001'],
|
||||
'/batters' => [
|
||||
'batters' => [
|
||||
@ -312,19 +314,21 @@ return [
|
||||
'/batters/batter/-/id' => ['id' => ["1001", "1002", "1003", "1004"]],
|
||||
],
|
||||
'empty_array' => [
|
||||
'' => [],
|
||||
'/-' => [],
|
||||
'/-1' => [],
|
||||
'/0' => [],
|
||||
'/foo' => [],
|
||||
],
|
||||
'empty_object' => [
|
||||
'' => [],
|
||||
'/-' => [],
|
||||
'/-1' => [],
|
||||
'/0' => [],
|
||||
'/foo' => [],
|
||||
],
|
||||
'simple_array' => [
|
||||
// '' => [1, '', 'foo', '"bar"', 'hej då', 3.14, false, null, [], []],
|
||||
'' => require __DIR__ . '/../parsing/simple_array.php',
|
||||
'/-' => [1, '', 'foo', '"bar"', 'hej då', 3.14, false, null, [], []],
|
||||
'/-1' => [],
|
||||
'/0' => [1],
|
||||
@ -341,6 +345,7 @@ return [
|
||||
'/foo' => [],
|
||||
],
|
||||
'simple_object' => [
|
||||
'' => require __DIR__ . '/../parsing/simple_object.php',
|
||||
'/-' => [],
|
||||
'/-1' => [],
|
||||
'/int' => ['int' => 1],
|
||||
|
Loading…
x
Reference in New Issue
Block a user