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