Allow setting an empty-string pointer

This commit is contained in:
Andrea Marco Sartori 2022-12-08 21:52:28 +10:00
parent 00c0bf4a0b
commit 66229aeea4
3 changed files with 15 additions and 17 deletions

View File

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

View File

@ -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()]);
}
/**

View File

@ -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],