-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathXMLReaderIterator.php
More file actions
222 lines (196 loc) · 5.16 KB
/
XMLReaderIterator.php
File metadata and controls
222 lines (196 loc) · 5.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
<?php
/*
* This file is part of the XMLReaderIterator package.
*
* Copyright (C) 2012, 2013 hakre <http://hakre.wordpress.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* @author hakre <http://hakre.wordpress.com>
* @license AGPL-3.0-or-later <https://spdx.org/licenses/AGPL-3.0-or-later>
*/
/**
* Class XMLReaderIterator
*
* Iterate over all nodes of a reader
*/
class XMLReaderIterator implements Iterator, XMLReaderAggregate
{
/**
* @var XMLReader
*/
protected $reader;
/**
* @var int
*/
private $index;
/**
* stores the result of the last XMLReader::read() operation.
*
* additionally, it's set to true if not initialized (null) on @see XMLReaderIterator::rewind()
*
* @var bool
*/
private $lastRead;
/**
* @var bool
*/
private $skipNextRead;
/**
* @var array
*/
private $elementStack;
public function __construct(XMLReader $reader)
{
$this->reader = $reader;
}
public function getReader()
{
return $this->reader;
}
/**
* skip the next read on next "next()"
*
* compare {@see XMLReaderIteration::skipNextRead()}
*
* @see XMLReaderIterator::next()
*
*/
public function skipNextRead()
{
$this->skipNextRead = true;
}
public function moveToNextElementByName($name = null)
{
while (self::moveToNextElement()) {
if (!$name || $name === $this->reader->name) {
break;
}
self::next();
}
;
return self::valid() ? self::current() : false;
}
public function moveToNextElement()
{
return $this->moveToNextByNodeType(XMLReader::ELEMENT);
}
/**
* @param int $nodeType
*
* @return bool|\XMLReaderNode
*/
public function moveToNextByNodeType($nodeType)
{
if (null === self::valid()) {
self::rewind();
} elseif (self::valid()) {
self::next();
}
while (self::valid()) {
if ($this->reader->nodeType === $nodeType) {
break;
}
self::next();
}
return self::valid() ? self::current() : false;
}
#[\ReturnTypeWillChange]
public function rewind()
{
$this->skipNextRead = false;
// this iterator can not really rewind
if ($this->reader->nodeType === XMLREADER::NONE) {
self::next();
} elseif ($this->lastRead === null) {
$this->lastRead = true;
}
$this->index = 0;
}
/**
* @return bool
*/
#[\ReturnTypeWillChange]
public function valid()
{
return $this->lastRead;
}
/**
* @return XMLReaderNode
*/
#[\ReturnTypeWillChange]
public function current()
{
return $this->lastRead ? new XMLReaderNode($this->reader) : null;
}
#[\ReturnTypeWillChange]
public function key()
{
return $this->index;
}
#[\ReturnTypeWillChange]
public function next()
{
$this->index ++;
if ($this->skipNextRead) {
$this->skipNextRead = false;
$this->lastRead = $this->reader->nodeType !== XMLReader::NONE;
} elseif ($this->lastRead = $this->readNext() and $this->reader->nodeType === XMLReader::ELEMENT) {
$this->touchElementStack();
}
}
#[\ReturnTypeWillChange]
protected function readNext()
{
return $this->reader->read();
}
/**
* @return string
* @since 0.0.19
*/
public function getNodePath()
{
return '/' . implode('/', $this->elementStack);
}
/**
* @return string
* @since 0.0.19
*/
public function getNodeTree()
{
$stack = $this->elementStack;
$buffer = '';
/* @var $element XMLReaderElement */
while ($element = array_pop($stack)) {
$buffer = $element->getXMLElementAround($buffer);
}
return $buffer;
}
/**
* touch the internal element-stack
*
* update the element-stack for the current reader node - which must be
* of type XMLReader::ELEMENT otherwise undefined.
*
* @return void
*/
protected function touchElementStack()
{
$depth = $this->reader->depth;
$this->elementStack[$depth] = new XMLReaderElement($this->reader);
if (count($this->elementStack) !== $depth + 1) {
$this->elementStack = array_slice($this->elementStack, 0, $depth + 1);
}
}
}