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