|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Cerbero\JsonParser\ValueObjects; |
|
4
|
|
|
|
|
5
|
|
|
use Cerbero\JsonParser\Pointers\Pointers; |
|
6
|
|
|
use Cerbero\JsonParser\Tokens\CompoundBegin; |
|
7
|
|
|
use Cerbero\JsonParser\Tokens\Parser; |
|
8
|
|
|
use Cerbero\JsonParser\Tokens\Token; |
|
9
|
|
|
use Cerbero\JsonParser\Tokens\Tokens; |
|
10
|
|
|
use Closure; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* The JSON parsing state. |
|
14
|
|
|
* |
|
15
|
|
|
*/ |
|
16
|
|
|
final class State |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* The JSON tree. |
|
20
|
|
|
* |
|
21
|
|
|
* @var Tree |
|
22
|
|
|
*/ |
|
23
|
|
|
public readonly Tree $tree; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* The JSON buffer. |
|
27
|
|
|
* |
|
28
|
|
|
* @var Parser|string |
|
29
|
|
|
*/ |
|
30
|
|
|
private Parser|string $buffer = ''; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Whether an object key is expected. |
|
34
|
|
|
* |
|
35
|
|
|
* @var bool |
|
36
|
|
|
*/ |
|
37
|
|
|
public bool $expectsKey = false; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* The expected token. |
|
41
|
|
|
* |
|
42
|
|
|
* @var int |
|
43
|
|
|
*/ |
|
44
|
|
|
public int $expectedToken = Tokens::COMPOUND_BEGIN; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Instantiate the class. |
|
48
|
|
|
* |
|
49
|
|
|
* @param Pointers $pointers |
|
50
|
|
|
* @param Closure $lazyLoad |
|
51
|
|
|
*/ |
|
52
|
357 |
|
public function __construct(private readonly Pointers $pointers, private readonly Closure $lazyLoad) |
|
53
|
|
|
{ |
|
54
|
357 |
|
$this->tree = new Tree($pointers); |
|
|
|
|
|
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Retrieve the JSON tree |
|
59
|
|
|
* |
|
60
|
|
|
* @return Tree |
|
61
|
|
|
*/ |
|
62
|
|
|
public function tree(): Tree |
|
63
|
|
|
{ |
|
64
|
|
|
return $this->tree; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Determine whether the parser can stop parsing |
|
69
|
|
|
* |
|
70
|
|
|
* @return bool |
|
71
|
|
|
*/ |
|
72
|
350 |
|
public function canStopParsing(): bool |
|
73
|
|
|
{ |
|
74
|
350 |
|
return $this->pointers->wereFoundInTree($this->tree); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Call the current pointer callback |
|
79
|
|
|
* |
|
80
|
|
|
* @param mixed $value |
|
81
|
|
|
* @param mixed $key |
|
82
|
|
|
* @return mixed |
|
83
|
|
|
*/ |
|
84
|
260 |
|
public function callPointer(mixed $value, mixed &$key): mixed |
|
85
|
|
|
{ |
|
86
|
260 |
|
return $this->pointers->matching()->call($value, $key); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Mutate state depending on the given token |
|
91
|
|
|
* |
|
92
|
|
|
* @param Token $token |
|
93
|
|
|
* @return void |
|
94
|
|
|
*/ |
|
95
|
353 |
|
public function mutateByToken(Token $token): void |
|
96
|
|
|
{ |
|
97
|
353 |
|
$this->tree->traverseToken($token, $this->expectsKey); |
|
98
|
|
|
|
|
99
|
353 |
|
if ($this->tree->isMatched() && ((!$this->expectsKey && $token->isValue()) || $this->tree->isDeep())) { |
|
100
|
261 |
|
$pointer = $this->pointers->markAsFound(); |
|
101
|
|
|
|
|
102
|
261 |
|
if ($token instanceof CompoundBegin && $pointer->isLazy) { |
|
103
|
30 |
|
$this->buffer = ($this->lazyLoad)(); |
|
104
|
30 |
|
$token->shouldLazyLoad = true; |
|
105
|
|
|
} else { |
|
106
|
|
|
/** @phpstan-ignore-next-line */ |
|
107
|
253 |
|
$this->buffer .= $token; |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
353 |
|
$token->mutateState($this); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* Determine whether the buffer contains tokens |
|
116
|
|
|
* |
|
117
|
|
|
* @return bool |
|
118
|
|
|
*/ |
|
119
|
351 |
|
public function hasBuffer(): bool |
|
120
|
|
|
{ |
|
121
|
351 |
|
return $this->buffer != ''; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* Retrieve the value from the buffer and reset it |
|
126
|
|
|
* |
|
127
|
|
|
* @return Parser|string |
|
128
|
|
|
*/ |
|
129
|
261 |
|
public function value(): Parser|string |
|
130
|
|
|
{ |
|
131
|
261 |
|
$buffer = $this->buffer; |
|
132
|
|
|
|
|
133
|
261 |
|
$this->buffer = ''; |
|
134
|
|
|
|
|
135
|
261 |
|
return $buffer; |
|
136
|
|
|
} |
|
137
|
|
|
} |
|
138
|
|
|
|