-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathXMLChildElementIterator.php
More file actions
215 lines (189 loc) · 5.25 KB
/
XMLChildElementIterator.php
File metadata and controls
215 lines (189 loc) · 5.25 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
<?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 XMLChildElementIterator
*
* Iterate over child element nodes of the current XMLReader node
*/
class XMLChildElementIterator extends XMLElementIterator
{
/**
* @var null|int
*/
private $stopDepth;
/**
* @var bool
*/
private $descendTree;
/**
* @var bool
*/
private $didRewind;
/**
* @var int
*/
private $index;
/**
* @var string|null
*/
private $name;
/**
* @var int
*/
private $innerDepth;
/**
* @inheritdoc
*
* @param bool $descendantAxis traverse children of children
*/
public function __construct(XMLReader $reader, $name = null, $descendantAxis = false)
{
parent::__construct($reader);
$this->name = $name;
$this->descendTree = $descendantAxis;
}
/**
* @throws UnexpectedValueException
* @return void
*/
public function rewind()
{
// this iterator can not really rewind. instead it places itself onto the
// first child element - if any.
if ($this->didRewind) {
return;
}
if ($this->reader->nodeType === XMLReader::NONE) {
!$this->moveToNextByNodeType(XMLReader::ELEMENT);
}
// handles e.g. <parent/> -> no children available
if ($this->reader->isEmptyElement) {
return;
}
if ($this->stopDepth === null) {
$this->stopDepth = $this->reader->depth;
}
// move to first child element - if any
$result = $this->nextChildElementByName($this->name);
$this->index = $result ? 0 : null;
$this->innerDepth = 1;
$this->didRewind = true;
}
public function next()
{
if (!$this->valid()) {
return;
}
$this->index++;
$this->nextChildElementByName($this->name);
}
public function valid()
{
if (!$this->didRewind) {
return false;
}
$depth = $this->reader->depth;
if ($depth <= $this->stopDepth) {
return false;
}
if (!$this->descendTree && $depth !== $this->stopDepth + 1) {
return false;
}
if ($this->name === null || $this->reader->name === $this->name) {
return $this->reader->nodeType === XMLReader::ELEMENT; // always true here if reader in sync with $this
}
return false;
}
/**
* @return XMLReaderNode|null
*/
public function current()
{
$this->didRewind || $this->rewind();
return $this->valid() ? parent::current() : null;
}
/**
* @return int
*/
public function key()
{
return $this->index;
}
/**
* move to next child element by name
*
* @param string|null $name
* @return bool
*/
private function nextChildElementByName($name = null)
{
while ($next = $this->nextElement()) {
$depth = $this->reader->depth;
if ($depth <= $this->stopDepth) {
return false;
}
if (!$this->descendTree && $depth !== $this->stopDepth + 1) {
continue;
}
if ($name === null || $this->reader->name === $name) {
break;
}
}
return $next;
}
/**
* @return bool
*/
private function nextElement()
{
while ($this->readNext()) {
if (XMLReader::ELEMENT !== $this->reader->nodeType) {
continue;
}
$this->touchElementStack();
return true;
}
return false;
}
/**
* Wrap reading to track opening and closing tags to prevent reading not-children nodes
*
* @return bool
*/
protected function readNext()
{
// update inner depth
if ($this->reader->nodeType === XMLReader::ELEMENT && !$this->reader->isEmptyElement) {
$this->innerDepth++;
} elseif ($this->reader->nodeType === XMLReader::END_ELEMENT) {
$this->innerDepth--;
// all children read? Abort to prevent reading next node
if ($this->innerDepth === 0) {
// set pointer behind closing-tag
parent::readNext();
return false;
}
}
return parent::readNext();
}
}