1
|
|
|
<?php |
2
|
|
|
namespace JsonCollectionParser; |
3
|
|
|
|
4
|
|
|
class Listener implements \JsonStreamingParser\Listener |
5
|
|
|
{ |
6
|
|
|
/** |
7
|
|
|
* @var array |
8
|
|
|
*/ |
9
|
|
|
protected $stack; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* @var string |
13
|
|
|
*/ |
14
|
|
|
protected $key; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var array |
18
|
|
|
*/ |
19
|
|
|
protected $keys; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var int |
23
|
|
|
*/ |
24
|
|
|
protected $level; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var int |
28
|
|
|
*/ |
29
|
|
|
protected $objectLevel; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var array |
33
|
|
|
*/ |
34
|
|
|
protected $objectKeys; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var callback|callable |
38
|
|
|
*/ |
39
|
|
|
protected $callback; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var bool |
43
|
|
|
*/ |
44
|
|
|
protected $assoc; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param callback|callable $callback callback for parsed collection item |
48
|
|
|
* @param bool $assoc When @c true, returned objects will be converted into associative arrays. |
49
|
|
|
*/ |
50
|
|
|
public function __construct($callback, $assoc = false) |
51
|
|
|
{ |
52
|
|
|
$this->callback = $callback; |
53
|
|
|
$this->assoc = $assoc; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function startDocument() |
57
|
|
|
{ |
58
|
|
|
$this->stack = []; |
59
|
|
|
$this->key = null; |
60
|
|
|
$this->keys = []; |
61
|
|
|
$this->objectLevel = 0; |
62
|
|
|
$this->level = 0; |
63
|
|
|
$this->objectKeys = []; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function endDocument() |
67
|
|
|
{ |
68
|
|
|
$this->stack = []; |
69
|
|
|
$this->keys = []; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function startObject() |
73
|
|
|
{ |
74
|
|
|
$this->objectLevel++; |
75
|
|
|
|
76
|
|
|
$this->startCommon(); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function endObject() |
80
|
|
|
{ |
81
|
|
|
$this->endCommon(true); |
82
|
|
|
|
83
|
|
|
$this->objectLevel--; |
84
|
|
|
if ($this->objectLevel === 0) { |
85
|
|
|
$obj = $this->stack[0][0]; |
86
|
|
|
array_shift($this->stack[0]); |
87
|
|
|
|
88
|
|
|
call_user_func($this->callback, $obj); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
public function startArray() |
93
|
|
|
{ |
94
|
|
|
$this->startCommon(); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
public function startCommon() |
98
|
|
|
{ |
99
|
|
|
$this->level++; |
100
|
|
|
$this->objectKeys[$this->level] = ($this->key) ? $this->key : null; |
101
|
|
|
$this->key = null; |
102
|
|
|
|
103
|
|
|
array_push($this->stack, []); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
public function endArray() |
107
|
|
|
{ |
108
|
|
|
$this->endCommon(false); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
public function endCommon($isObject) |
112
|
|
|
{ |
113
|
|
|
$obj = array_pop($this->stack); |
114
|
|
|
|
115
|
|
|
if ($isObject && !$this->assoc) { |
116
|
|
|
$obj = (object)$obj; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
if (!empty($this->stack)) { |
120
|
|
|
$parentObj = array_pop($this->stack); |
121
|
|
|
|
122
|
|
|
if ($this->objectKeys[$this->level]) { |
123
|
|
|
$parentObj[$this->objectKeys[$this->level]] = $obj; |
124
|
|
|
} else { |
125
|
|
|
array_push($parentObj, $obj); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
array_push($this->stack, $parentObj); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
$this->level--; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @param string $key |
136
|
|
|
*/ |
137
|
|
|
public function key($key) |
138
|
|
|
{ |
139
|
|
|
$this->key = $key; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @param mixed $value |
144
|
|
|
*/ |
145
|
|
|
public function value($value) |
146
|
|
|
{ |
147
|
|
|
$obj = array_pop($this->stack); |
148
|
|
|
|
149
|
|
|
if ($this->key) { |
150
|
|
|
$obj[$this->key] = $value; |
151
|
|
|
$this->key = null; |
152
|
|
|
} else { |
153
|
|
|
array_push($obj, $value); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
array_push($this->stack, $obj); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* @param string $whitespace |
161
|
|
|
*/ |
162
|
|
|
public function whitespace($whitespace) |
163
|
|
|
{ |
164
|
|
|
} |
165
|
|
|
} |
166
|
|
|
|