1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Cerbero\JsonParser; |
4
|
|
|
|
5
|
|
|
use function count; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* The JSON tree. |
9
|
|
|
* |
10
|
|
|
*/ |
11
|
|
|
final class Tree |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* The original JSON tree. |
15
|
|
|
* |
16
|
|
|
* @var array<int, string|int> |
17
|
|
|
*/ |
18
|
|
|
private array $original = []; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* The wildcarded JSON tree. |
22
|
|
|
* |
23
|
|
|
* @var array<int, string|int> |
24
|
|
|
*/ |
25
|
|
|
private array $wildcarded = []; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Whether a depth is within an object. |
29
|
|
|
* |
30
|
|
|
* @var array<int, bool> |
31
|
|
|
*/ |
32
|
|
|
private array $inObjectByDepth = []; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* The JSON tree depth. |
36
|
|
|
* |
37
|
|
|
* @var int |
38
|
|
|
*/ |
39
|
|
|
private int $depth = -1; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Whether the tree changed. |
43
|
|
|
* |
44
|
|
|
* @var bool |
45
|
|
|
*/ |
46
|
|
|
public bool $changed = false; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Retrieve the original JSON tree |
50
|
|
|
* |
51
|
|
|
* @return array<int, string|int> |
52
|
|
|
*/ |
53
|
130 |
|
public function original(): array |
54
|
|
|
{ |
55
|
130 |
|
return $this->original; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Retrieve the wildcarded JSON tree |
60
|
|
|
* |
61
|
|
|
* @return array<int, string|int> |
62
|
|
|
*/ |
63
|
79 |
|
public function wildcarded(): array |
64
|
|
|
{ |
65
|
79 |
|
return $this->wildcarded; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Determine whether the current depth is within an object |
70
|
|
|
* |
71
|
|
|
* @return bool |
72
|
|
|
*/ |
73
|
126 |
|
public function inObject(): bool |
74
|
|
|
{ |
75
|
126 |
|
return $this->inObjectByDepth[$this->depth] ?? false; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Retrieve the JSON tree depth |
80
|
|
|
* |
81
|
|
|
* @return int |
82
|
|
|
*/ |
83
|
126 |
|
public function depth(): int |
84
|
|
|
{ |
85
|
126 |
|
return $this->depth; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Increase the tree depth by entering an object or an array |
90
|
|
|
* |
91
|
|
|
* @param bool $inObject |
92
|
|
|
* @return void |
93
|
|
|
*/ |
94
|
126 |
|
public function deepen(bool $inObject): void |
95
|
|
|
{ |
96
|
126 |
|
$this->depth++; |
97
|
126 |
|
$this->inObjectByDepth[$this->depth] = $inObject; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Decrease the tree depth |
102
|
|
|
* |
103
|
|
|
* @return void |
104
|
|
|
*/ |
105
|
93 |
|
public function emerge(): void |
106
|
|
|
{ |
107
|
93 |
|
$this->depth--; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Traverse the given object key |
112
|
|
|
* |
113
|
|
|
* @param string $key |
114
|
|
|
* @return void |
115
|
|
|
*/ |
116
|
86 |
|
public function traverseKey(string $key): void |
117
|
|
|
{ |
118
|
86 |
|
$trimmedKey = substr($key, 1, -1); |
119
|
|
|
|
120
|
86 |
|
$this->original[$this->depth] = $trimmedKey; |
121
|
86 |
|
$this->wildcarded[$this->depth] = $trimmedKey; |
122
|
86 |
|
$this->changed = true; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Traverse an array |
127
|
|
|
* |
128
|
|
|
* @param string[] $referenceTokens |
129
|
|
|
* @return void |
130
|
|
|
*/ |
131
|
126 |
|
public function traverseArray(array $referenceTokens): void |
132
|
|
|
{ |
133
|
126 |
|
$referenceToken = $referenceTokens[$this->depth] ?? null; |
134
|
126 |
|
$index = $this->original[$this->depth] ?? null; |
135
|
|
|
|
136
|
126 |
|
$this->original[$this->depth] = is_int($index) ? $index + 1 : 0; |
137
|
126 |
|
$this->wildcarded[$this->depth] = $referenceToken == '-' ? '-' : $this->original[$this->depth]; |
138
|
126 |
|
$this->changed = true; |
139
|
|
|
|
140
|
126 |
|
if (count($this->original) > $this->depth + 1) { |
141
|
126 |
|
array_splice($this->original, $this->depth + 1); |
142
|
126 |
|
array_splice($this->wildcarded, $this->depth + 1); |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Retrieve the current key |
148
|
|
|
* |
149
|
|
|
* @return string|int |
150
|
|
|
*/ |
151
|
92 |
|
public function currentKey(): string|int |
152
|
|
|
{ |
153
|
92 |
|
$key = $this->original[$this->depth]; |
154
|
|
|
|
155
|
92 |
|
return is_string($key) ? "\"$key\"" : $key; |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|