1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Thruster\Component\XMLIterator; |
4
|
|
|
|
5
|
|
|
use Iterator as IteratorInterface; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class XMLIterator |
9
|
|
|
* |
10
|
|
|
* @package Thruster\Component\XMLIterator |
11
|
|
|
* @author Aurimas Niekis <[email protected]> |
12
|
|
|
*/ |
13
|
|
|
class XMLIterator implements IteratorInterface |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var XMLReader |
17
|
|
|
*/ |
18
|
|
|
protected $reader; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var int |
22
|
|
|
*/ |
23
|
|
|
private $index; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var bool |
27
|
|
|
*/ |
28
|
|
|
private $lastRead; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var array |
32
|
|
|
*/ |
33
|
|
|
private $elementStack; |
34
|
|
|
|
35
|
16 |
|
public function __construct(XMLReader $reader) |
36
|
|
|
{ |
37
|
16 |
|
$this->reader = $reader; |
38
|
16 |
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @return XMLReader |
42
|
|
|
*/ |
43
|
|
|
public function getReader() : XMLReader |
44
|
|
|
{ |
45
|
|
|
return $this->reader; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param string $name |
50
|
|
|
* |
51
|
|
|
* @return bool|Node |
52
|
|
|
*/ |
53
|
8 |
|
public function moveToNextElementByName($name = null) |
54
|
|
|
{ |
55
|
8 |
|
while (self::moveToNextElement()) { |
56
|
6 |
|
if (!$name || $name === $this->reader->name) { |
|
|
|
|
57
|
6 |
|
break; |
58
|
|
|
} |
59
|
|
|
|
60
|
2 |
|
self::next(); |
61
|
|
|
}; |
62
|
|
|
|
63
|
8 |
|
return self::valid() ? self::current() : false; |
64
|
|
|
} |
65
|
|
|
|
66
|
8 |
|
public function moveToNextElement() |
67
|
|
|
{ |
68
|
8 |
|
return $this->moveToNextByNodeType(XMLReader::ELEMENT); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param int $nodeType |
73
|
|
|
* |
74
|
|
|
* @return bool|Node |
75
|
|
|
*/ |
76
|
8 |
|
public function moveToNextByNodeType($nodeType) |
77
|
|
|
{ |
78
|
8 |
|
if (null === self::valid()) { |
79
|
1 |
|
self::rewind(); |
80
|
8 |
|
} elseif (self::valid()) { |
81
|
8 |
|
self::next(); |
82
|
|
|
} |
83
|
|
|
|
84
|
8 |
|
while (self::valid()) { |
85
|
8 |
|
if ($this->reader->nodeType === $nodeType) { |
86
|
6 |
|
break; |
87
|
|
|
} |
88
|
|
|
|
89
|
7 |
|
self::next(); |
90
|
|
|
} |
91
|
|
|
|
92
|
8 |
|
return self::valid() ? self::current() : false; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @inheritdoc |
97
|
|
|
*/ |
98
|
16 |
|
public function rewind() |
99
|
|
|
{ |
100
|
|
|
// this iterator can not really rewind |
101
|
16 |
|
if ($this->reader->nodeType === XMLREADER::NONE) { |
102
|
16 |
|
self::next(); |
103
|
2 |
|
} elseif ($this->lastRead === null) { |
104
|
|
|
$this->lastRead = true; |
105
|
|
|
} |
106
|
|
|
|
107
|
16 |
|
$this->index = 0; |
108
|
16 |
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @inheritdoc |
112
|
|
|
*/ |
113
|
16 |
|
public function valid() |
114
|
|
|
{ |
115
|
16 |
|
return $this->lastRead; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @return Node |
120
|
|
|
*/ |
121
|
13 |
|
public function current() |
122
|
|
|
{ |
123
|
13 |
|
return new Node($this->reader); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @inheritdoc |
128
|
|
|
*/ |
129
|
7 |
|
public function key() |
130
|
|
|
{ |
131
|
7 |
|
return $this->index; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @inheritdoc |
136
|
|
|
*/ |
137
|
16 |
|
public function next() |
138
|
|
|
{ |
139
|
16 |
|
if ($this->lastRead = $this->reader->read() and $this->reader->nodeType === XMLReader::ELEMENT) { |
|
|
|
|
140
|
16 |
|
$depth = $this->reader->depth; |
141
|
16 |
|
$this->elementStack[$depth] = new Element($this->reader); |
142
|
|
|
|
143
|
16 |
|
if (count($this->elementStack) !== $depth + 1) { |
144
|
7 |
|
$this->elementStack = array_slice($this->elementStack, 0, $depth + 1); |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|
148
|
16 |
|
$this->index++; |
149
|
16 |
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* @return string |
153
|
|
|
*/ |
154
|
|
|
public function getNodePath() : string |
155
|
|
|
{ |
156
|
|
|
return '/' . implode('/', $this->elementStack); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* @return string |
161
|
|
|
*/ |
162
|
|
|
public function getNodeTree() : string |
163
|
|
|
{ |
164
|
|
|
$stack = $this->elementStack; |
165
|
|
|
$buffer = ''; |
166
|
|
|
|
167
|
|
|
/* @var $element Element */ |
168
|
|
|
while ($element = array_pop($stack)) { |
169
|
|
|
$buffer = $element->getXMLElementAround($buffer); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
return $buffer; |
173
|
|
|
} |
174
|
|
|
} |
175
|
|
|
|
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: