/
XMLElementIterator.php
158 lines (139 loc) · 3.82 KB
/
XMLElementIterator.php
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
<?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 <http://spdx.org/licenses/AGPL-3.0>
*/
/**
* Class XMLElementIterator
*
* Iterate over XMLReader element nodes
*/
class XMLElementIterator extends XMLReaderIterator
{
private $index;
private $name;
private $didRewind;
/**
* @param XMLReader $reader
* @param null|string $name element name, leave empty or use '*' for all elements
*/
public function __construct(XMLReader $reader, $name = null)
{
parent::__construct($reader);
$this->setName($name);
}
/**
* @return void
*/
public function rewind()
{
parent::rewind();
$this->ensureCurrentElementState();
$this->didRewind = true;
$this->index = 0;
}
/**
* @return XMLReaderNode|null
*/
public function current()
{
$this->didRewind || self::rewind();
$this->ensureCurrentElementState();
return self::valid() ? new XMLReaderNode($this->reader) : null;
}
public function key()
{
return $this->index;
}
public function next()
{
if (parent::valid()) {
$this->index++;
}
parent::next();
$this->ensureCurrentElementState();
}
/**
* @return array
*/
public function toArray()
{
$array = array();
$this->didRewind || $this->rewind();
if (!$this->valid()) {
return array();
}
$this->ensureCurrentElementState();
while ($this->valid()) {
$element = new XMLReaderNode($this->reader);
if ($this->reader->hasValue) {
$string = $this->reader->value;
} else {
$string = $element->readString();
}
if ($this->name) {
$array[] = $string;
} else {
$array[$element->name] = $string;
}
$this->moveToNextElementByName($this->name);
}
return $array;
}
/**
* @return string
*/
public function __toString()
{
return $this->readString();
}
/**
* decorate method calls
*/
public function __call($name, $args)
{
return call_user_func_array(array($this->current(), $name), $args);
}
/**
* decorate property get
*/
public function __get($name)
{
return $this->current()->$name;
}
/**
* @param null|string $name
*/
public function setName($name = null)
{
$this->name = '*' === $name ? null : $name;
}
/**
* take care the underlying XMLReader is at an element with a fitting name (if $this is looking for a name)
*/
private function ensureCurrentElementState()
{
if ($this->reader->nodeType !== XMLReader::ELEMENT) {
$this->moveToNextElementByName($this->name);
} elseif ($this->name && $this->name !== $this->reader->name) {
$this->moveToNextElementByName($this->name);
}
}
}