|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace rdx\jsonpathstreamer; |
|
4
|
|
|
|
|
5
|
|
|
use JsonStreamingParser\Listener\IdleListener; |
|
6
|
|
|
|
|
7
|
|
|
abstract class PathAwareJsonListener extends IdleListener { |
|
8
|
|
|
|
|
9
|
|
|
protected $stopAfter = []; |
|
10
|
|
|
protected $separator = '/'; |
|
11
|
|
|
|
|
12
|
|
|
protected $value = []; |
|
13
|
|
|
|
|
14
|
|
|
protected $indent = -1; |
|
15
|
|
|
protected $array = [false]; |
|
16
|
|
|
protected $array_index = [-1]; |
|
17
|
|
|
protected $lastKey = null; |
|
18
|
|
|
protected $trace = []; |
|
19
|
|
|
protected $key = []; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* |
|
23
|
|
|
*/ |
|
24
|
|
|
public function __construct() { |
|
25
|
|
|
if ($regexes = $this->stopAfter()) { |
|
26
|
|
|
$this->stopAfter = array_combine($regexes, array_fill(0, count($regexes), false)); |
|
27
|
|
|
} |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Implements JsonStreamingParser\Listener::endDocument(). |
|
32
|
|
|
*/ |
|
33
|
|
|
public function endDocument() { |
|
34
|
|
|
if (!empty($this->stopAfter)) { |
|
35
|
|
|
$this->stop(false); |
|
36
|
|
|
} |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Implements JsonStreamingParser\Listener::startObject(). |
|
41
|
|
|
*/ |
|
42
|
|
|
public function startObject() { |
|
43
|
|
|
$this->arrayKey(); |
|
44
|
|
|
|
|
45
|
|
|
$this->indent++; |
|
46
|
|
|
$this->array[$this->indent] = false; |
|
47
|
|
|
if ($this->lastKey !== null) { |
|
48
|
|
|
$this->trace[] = $this->lastKey; |
|
49
|
|
|
} |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Implements JsonStreamingParser\Listener::endObject(). |
|
54
|
|
|
*/ |
|
55
|
|
|
public function endObject() { |
|
56
|
|
|
$this->indent--; |
|
57
|
|
|
array_pop($this->trace); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Implements JsonStreamingParser\Listener::startArray(). |
|
62
|
|
|
*/ |
|
63
|
|
|
public function startArray() { |
|
64
|
|
|
$this->arrayKey(); |
|
65
|
|
|
|
|
66
|
|
|
$this->indent++; |
|
67
|
|
|
$this->array[$this->indent] = true; |
|
68
|
|
|
$this->array_index[$this->indent] = -1; |
|
69
|
|
|
if ($this->lastKey !== null) { |
|
70
|
|
|
$this->trace[] = $this->lastKey; |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Implements JsonStreamingParser\Listener::endArray(). |
|
76
|
|
|
*/ |
|
77
|
|
|
public function endArray() { |
|
78
|
|
|
$this->array[$this->indent] = false; |
|
79
|
|
|
$this->indent--; |
|
80
|
|
|
array_pop($this->trace); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Implements JsonStreamingParser\Listener::key(). |
|
85
|
|
|
*/ |
|
86
|
|
|
public function key($key) { |
|
87
|
|
|
$this->lastKey = $key; |
|
88
|
|
|
$this->composeKey(); |
|
89
|
|
|
|
|
90
|
|
|
$this->gotPath($this->path); |
|
|
|
|
|
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Implements JsonStreamingParser\Listener::value(). |
|
95
|
|
|
*/ |
|
96
|
|
|
public function value($value) { |
|
97
|
|
|
$this->arrayKey(); |
|
98
|
|
|
$this->composeKey(); |
|
99
|
|
|
|
|
100
|
|
|
$this->touch($this->path); |
|
101
|
|
|
|
|
102
|
|
|
$this->gotValue($this->path, $value); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* |
|
107
|
|
|
*/ |
|
108
|
|
|
protected function touch(array $path) { |
|
109
|
|
|
if ($this->stopAfter) { |
|
|
|
|
|
|
110
|
|
|
$pathString = implode($this->separator, $path); |
|
111
|
|
|
foreach ($this->stopAfter as $regex => &$touched) { |
|
112
|
|
|
if (preg_match($regex, $pathString)) { |
|
113
|
|
|
$touched = true; |
|
114
|
|
|
} |
|
115
|
|
|
elseif ($touched) { |
|
116
|
|
|
unset($this->stopAfter[$regex]); |
|
117
|
|
|
|
|
118
|
|
|
if (empty($this->stopAfter)) { |
|
119
|
|
|
$this->stop(true); |
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
unset($touched); |
|
124
|
|
|
} |
|
125
|
|
|
} |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* |
|
130
|
|
|
*/ |
|
131
|
|
|
protected function stop($allTouched) { |
|
132
|
|
|
throw new DoneParsingException($allTouched); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* Create a complete key from the trace and current level. |
|
137
|
|
|
*/ |
|
138
|
|
|
protected function composeKey() { |
|
139
|
|
|
$this->path = array_merge($this->trace, [$this->lastKey]); |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* Trigger a new key, numeric or associative. |
|
144
|
|
|
*/ |
|
145
|
|
|
protected function arrayKey() { |
|
146
|
|
|
if (!empty($this->array[$this->indent])) { |
|
147
|
|
|
$this->array_index[$this->indent]++; |
|
148
|
|
|
$this->key($this->array_index[$this->indent]); |
|
149
|
|
|
} |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* Optional helper to save a scalar value into a non-scalar path. |
|
154
|
|
|
*/ |
|
155
|
|
|
protected function setValue(array &$container, array $key, $value) { |
|
156
|
|
|
$element = &$container; |
|
157
|
|
|
foreach ($key as $subkey) { |
|
158
|
|
|
$element =& $element[$subkey]; |
|
159
|
|
|
} |
|
160
|
|
|
$element = $value; |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
/** |
|
164
|
|
|
* Remember a value into the provided value property. |
|
165
|
|
|
*/ |
|
166
|
|
|
protected function rememberValue(array $path, $value) { |
|
167
|
|
|
$this->setValue($this->value, $path, $value); |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
/** |
|
171
|
|
|
* Return the entire saved value. |
|
172
|
|
|
*/ |
|
173
|
|
|
public function getValue() { |
|
174
|
|
|
return $this->value; |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
/** |
|
178
|
|
|
* Define a list of paths (regexes) that must be completed. Completely stops |
|
179
|
|
|
* the parser after these. |
|
180
|
|
|
* |
|
181
|
|
|
* @return array |
|
182
|
|
|
*/ |
|
183
|
|
|
public function stopAfter() { |
|
184
|
|
|
// Optional |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
/** |
|
188
|
|
|
* Get notified about a new key, value still unknown. |
|
189
|
|
|
* |
|
190
|
|
|
* @return void |
|
191
|
|
|
*/ |
|
192
|
|
|
abstract public function gotPath(array $path); |
|
193
|
|
|
|
|
194
|
|
|
/** |
|
195
|
|
|
* Get notified about a new value, including complete path. |
|
196
|
|
|
* |
|
197
|
|
|
* @return void |
|
198
|
|
|
*/ |
|
199
|
|
|
abstract public function gotValue(array $path, $value); |
|
200
|
|
|
|
|
201
|
|
|
} |
|
202
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: