1
|
|
|
<?php |
2
|
|
|
namespace PHPDaemon\XMLStream; |
3
|
|
|
|
4
|
|
|
class XMLStream |
5
|
|
|
{ |
6
|
|
|
use \PHPDaemon\Traits\EventHandlers; |
7
|
|
|
use \PHPDaemon\Traits\ClassWatchdog; |
8
|
|
|
use \PHPDaemon\Traits\StaticObjectWatchdog; |
9
|
|
|
|
10
|
|
|
protected $parser; |
11
|
|
|
protected $xml_depth = 0; |
12
|
|
|
protected $current_ns = []; |
13
|
|
|
protected $idhandlers = []; |
14
|
|
|
protected $xpathhandlers = []; |
15
|
|
|
protected $default_ns; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Constructor |
19
|
|
|
* @return void |
|
|
|
|
20
|
|
|
*/ |
21
|
|
|
public function __construct() |
22
|
|
|
{ |
23
|
|
|
$this->parser = xml_parser_create('UTF-8'); |
24
|
|
|
xml_parser_set_option($this->parser, XML_OPTION_SKIP_WHITE, 1); |
25
|
|
|
xml_parser_set_option($this->parser, XML_OPTION_TARGET_ENCODING, 'UTF-8'); |
26
|
|
|
xml_set_object($this->parser, $this); |
27
|
|
|
xml_set_element_handler($this->parser, 'startXML', 'endXML'); |
28
|
|
|
xml_set_character_data_handler($this->parser, 'charXML'); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Set default namespace |
33
|
|
|
* @param string $ns |
34
|
|
|
* @return void |
35
|
|
|
*/ |
36
|
|
|
public function setDefaultNS($ns) |
37
|
|
|
{ |
38
|
|
|
$this->default_ns = $ns; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Finishes the stream |
43
|
|
|
* @return void |
44
|
|
|
*/ |
45
|
|
|
public function finish() |
46
|
|
|
{ |
47
|
|
|
$this->xml_depth = 0; |
48
|
|
|
$this->current_ns = []; |
49
|
|
|
$this->idhandlers = []; |
50
|
|
|
$this->xpathhandlers = []; |
51
|
|
|
$this->eventHandlers = []; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Destructor |
56
|
|
|
* @return void |
57
|
|
|
*/ |
58
|
|
|
public function __destroy() |
59
|
|
|
{ |
60
|
|
|
if ($this->parser) { |
61
|
|
|
xml_parse($this->parser, '', true); |
62
|
|
|
xml_parser_free($this->parser); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Feed stream |
68
|
|
|
* @param string $buf |
69
|
|
|
* @return void |
70
|
|
|
*/ |
71
|
|
|
public function feed($buf) |
72
|
|
|
{ |
73
|
|
|
xml_parse($this->parser, $buf, false); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Finalize stream |
78
|
|
|
* @return void |
79
|
|
|
*/ |
80
|
|
|
public function finalize() |
81
|
|
|
{ |
82
|
|
|
xml_parse($this->parser, '', true); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* XML start callback |
87
|
|
|
* |
88
|
|
|
* @see xml_set_element_handler |
89
|
|
|
* @param resource $parser |
90
|
|
|
* @param string $name |
91
|
|
|
* @param array $attr |
92
|
|
|
* @return void |
93
|
|
|
*/ |
94
|
|
|
public function startXML($parser, $name, $attr) |
|
|
|
|
95
|
|
|
{ |
96
|
|
|
++$this->xml_depth; |
97
|
|
|
if (array_key_exists('XMLNS', $attr)) { |
98
|
|
|
$this->current_ns[$this->xml_depth] = $attr['XMLNS']; |
99
|
|
|
} else { |
100
|
|
|
$this->current_ns[$this->xml_depth] = $this->current_ns[$this->xml_depth - 1]; |
101
|
|
|
if (!$this->current_ns[$this->xml_depth]) { |
102
|
|
|
$this->current_ns[$this->xml_depth] = $this->default_ns; |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
$ns = $this->current_ns[$this->xml_depth]; |
106
|
|
|
foreach ($attr as $key => $value) { |
107
|
|
View Code Duplication |
if (mb_orig_strpos($key, ':') !== false) { |
|
|
|
|
108
|
|
|
$key = explode(':', $key); |
109
|
|
|
$key = $key[1]; |
110
|
|
|
$this->ns_map[$key] = $value; |
|
|
|
|
111
|
|
|
} |
112
|
|
|
} |
113
|
|
View Code Duplication |
if (mb_orig_strpos($name, ':') !== false) { |
|
|
|
|
114
|
|
|
$name = explode(':', $name); |
115
|
|
|
$ns = $this->ns_map[$name[0]]; |
116
|
|
|
$name = $name[1]; |
117
|
|
|
} |
118
|
|
|
$obj = new XMLStreamObject($name, $ns, $attr); |
119
|
|
|
if ($this->xml_depth > 1) { |
120
|
|
|
$this->xmlobj[$this->xml_depth - 1]->subs[] = $obj; |
|
|
|
|
121
|
|
|
} |
122
|
|
|
$this->xmlobj[$this->xml_depth] = $obj; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* XML end callback |
127
|
|
|
* |
128
|
|
|
* @see xml_set_element_handler |
129
|
|
|
* |
130
|
|
|
* @param resource $parser |
131
|
|
|
* @param string $name |
132
|
|
|
* @return void |
133
|
|
|
*/ |
134
|
|
|
public function endXML($parser, $name) |
|
|
|
|
135
|
|
|
{ |
136
|
|
|
--$this->xml_depth; |
137
|
|
|
if ($this->xml_depth === 1) { |
138
|
|
|
foreach ($this->xpathhandlers as $handler) { |
139
|
|
|
if (is_array($this->xmlobj) && array_key_exists(2, $this->xmlobj)) { |
140
|
|
|
$searchxml = $this->xmlobj[2]; |
141
|
|
|
$nstag = array_shift($handler[0]); |
142
|
|
|
if (($nstag[0] === null or $searchxml->ns === $nstag[0]) and ($nstag[1] === "*" or $nstag[1] === $searchxml->name)) { |
143
|
|
|
foreach ($handler[0] as $nstag) { |
144
|
|
|
if ($searchxml !== null and $searchxml->hasSub($nstag[1], $ns = $nstag[0])) { |
145
|
|
|
$searchxml = $searchxml->sub($nstag[1], $ns = $nstag[0]); |
146
|
|
|
} else { |
147
|
|
|
$searchxml = null; |
148
|
|
|
break; |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
if ($searchxml !== null) { |
152
|
|
|
if ($handler[2] === null) { |
153
|
|
|
$handler[2] = $this; |
154
|
|
|
} |
155
|
|
|
$handler[1]($this->xmlobj[2]); |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
foreach ($this->idhandlers as $id => $handler) { |
161
|
|
|
if (array_key_exists('id', $this->xmlobj[2]->attrs) and $this->xmlobj[2]->attrs['id'] == $id) { |
162
|
|
|
$handler($this->xmlobj[2]); |
163
|
|
|
#id handlers are only used once |
164
|
|
|
unset($this->idhandlers[$id]); |
165
|
|
|
break; |
166
|
|
|
} |
167
|
|
|
} |
168
|
|
|
if (is_array($this->xmlobj)) { |
169
|
|
|
$this->xmlobj = array_slice($this->xmlobj, 0, 1); |
170
|
|
|
if (isset($this->xmlobj[0]) && $this->xmlobj[0] instanceof XMLStreamObject) { |
171
|
|
|
$this->xmlobj[0]->subs = null; |
|
|
|
|
172
|
|
|
} |
173
|
|
|
} |
174
|
|
|
unset($this->xmlobj[2]); |
175
|
|
|
} |
176
|
|
|
if ($this->xml_depth === 0) { |
177
|
|
|
$this->event('streamEnd'); |
178
|
|
|
} |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* XML character callback |
183
|
|
|
* @see xml_set_character_data_handler |
184
|
|
|
* |
185
|
|
|
* @param resource $parser |
186
|
|
|
* @param string $data |
187
|
|
|
*/ |
188
|
|
|
public function charXML($parser, $data) |
|
|
|
|
189
|
|
|
{ |
190
|
|
|
if (array_key_exists($this->xml_depth, $this->xmlobj)) { |
191
|
|
|
$this->xmlobj[$this->xml_depth]->data .= $data; |
192
|
|
|
} |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* Get next ID |
197
|
|
|
* |
198
|
|
|
* @return integer |
199
|
|
|
*/ |
200
|
|
|
public function getId() |
201
|
|
|
{ |
202
|
|
|
$this->lastid++; |
|
|
|
|
203
|
|
|
return $this->lastid; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* Add ID Handler |
208
|
|
|
* |
209
|
|
|
* @param integer $id |
210
|
|
|
* @param callable $cb |
211
|
|
|
*/ |
212
|
|
|
public function addIdHandler($id, $cb) |
213
|
|
|
{ |
214
|
|
|
if ($cb === null) { |
215
|
|
|
return; |
216
|
|
|
} |
217
|
|
|
$this->idhandlers[$id] = $cb; |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* Add XPath Handler |
222
|
|
|
* |
223
|
|
|
* @param string $xpath |
224
|
|
|
* @param \Closure $cb |
225
|
|
|
* @param null $obj |
226
|
|
|
*/ |
227
|
|
|
public function addXPathHandler($xpath, $cb, $obj = null) |
228
|
|
|
{ |
229
|
|
|
if (preg_match_all("/\(?{[^\}]+}\)?(\/?)[^\/]+/", $xpath, $regs)) { |
230
|
|
|
$ns_tags = $regs[0]; |
231
|
|
|
} else { |
232
|
|
|
$ns_tags = [$xpath]; |
233
|
|
|
} |
234
|
|
|
foreach ($ns_tags as $ns_tag) { |
235
|
|
|
list($l, $r) = explode("}", $ns_tag); |
236
|
|
|
if ($r !== null) { |
237
|
|
|
$xpart = [substr($l, 1), $r]; |
238
|
|
|
} else { |
239
|
|
|
$xpart = [null, $l]; |
240
|
|
|
} |
241
|
|
|
$xpath_array[] = $xpart; |
|
|
|
|
242
|
|
|
} |
243
|
|
|
$this->xpathhandlers[] = [$xpath_array, $cb, $obj, $xpath]; |
|
|
|
|
244
|
|
|
} |
245
|
|
|
} |
246
|
|
|
|
Adding a
@return
annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.