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
|
|
|
* @param callback|callable $callback callback for parsed collection item |
43
|
|
|
*/ |
44
|
|
|
public function __construct($callback) |
45
|
|
|
{ |
46
|
|
|
$this->callback = $callback; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function startDocument() |
50
|
|
|
{ |
51
|
|
|
$this->stack = []; |
52
|
|
|
$this->key = null; |
53
|
|
|
$this->keys = []; |
54
|
|
|
$this->objectLevel = 0; |
55
|
|
|
$this->level = 0; |
56
|
|
|
$this->objectKeys = []; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function endDocument() |
60
|
|
|
{ |
61
|
|
|
$this->stack = []; |
62
|
|
|
$this->keys = []; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function startObject() |
66
|
|
|
{ |
67
|
|
|
$this->objectLevel++; |
68
|
|
|
|
69
|
|
|
$this->startArrayCommon(); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function endObject() |
73
|
|
|
{ |
74
|
|
|
$this->endArrayCommon(); |
75
|
|
|
|
76
|
|
|
$this->objectLevel--; |
77
|
|
|
if ($this->objectLevel === 0) { |
78
|
|
|
$obj = $this->stack[0][0]; |
79
|
|
|
array_shift($this->stack[0]); |
80
|
|
|
|
81
|
|
|
call_user_func($this->callback, $obj); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public function startArray() |
86
|
|
|
{ |
87
|
|
|
$this->startArrayCommon(); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
public function startArrayCommon() |
91
|
|
|
{ |
92
|
|
|
$this->level++; |
93
|
|
|
$this->objectKeys[$this->level] = ($this->key) ? $this->key : null; |
94
|
|
|
$this->key = null; |
95
|
|
|
|
96
|
|
|
array_push($this->stack, []); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
public function endArray() |
100
|
|
|
{ |
101
|
|
|
$this->endArrayCommon(); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
public function endArrayCommon() |
105
|
|
|
{ |
106
|
|
|
$obj = array_pop($this->stack); |
107
|
|
|
|
108
|
|
|
if (!empty($this->stack)) { |
109
|
|
|
$parentObj = array_pop($this->stack); |
110
|
|
|
|
111
|
|
|
if ($this->objectKeys[$this->level]) { |
112
|
|
|
$parentObj[$this->objectKeys[$this->level]] = $obj; |
113
|
|
|
} else { |
114
|
|
|
array_push($parentObj, $obj); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
array_push($this->stack, $parentObj); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
$this->level--; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @param string $key |
125
|
|
|
*/ |
126
|
|
|
public function key($key) |
127
|
|
|
{ |
128
|
|
|
$this->key = $key; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @param mixed $value |
133
|
|
|
*/ |
134
|
|
|
public function value($value) |
135
|
|
|
{ |
136
|
|
|
$obj = array_pop($this->stack); |
137
|
|
|
|
138
|
|
|
if ($this->key) { |
139
|
|
|
$obj[$this->key] = $value; |
140
|
|
|
$this->key = null; |
141
|
|
|
} else { |
142
|
|
|
array_push($obj, $value); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
array_push($this->stack, $obj); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* @param string $whitespace |
150
|
|
|
*/ |
151
|
|
|
public function whitespace($whitespace) |
152
|
|
|
{ |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
|