XmlIterator::valid()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace XmlIterator;
4
5
use Iterator;
6
7
class XmlIterator implements Iterator
8
{
9
    /**
10
     * @var int
11
     */
12
    protected $position = 0;
13
14
    /**
15
     * @var string
16
     */
17
    protected $xmlFileUri;
18
19
    /**
20
     * @var string The name of the tag that delimits/separates each iterated element/row
21
     */
22
    protected $delimiterTagName;
23
24
    /**
25
     * @var array
26
     */
27
    protected $options = array(
28
        /**
29
         * @var string Encoding of source file
30
         */
31
        "encoding"      => null,
32
33
        /**
34
         * @var null See __construct() for the default
35
         */
36
        "readerOptions" => null,
37
38
        /**
39
         * @var bool Activate UTF8 filter agains invalid (e.g. "out of allowed range") characters?
40
         *
41
         * Disable for performance gain if you know that the xml is clean of bad characters.
42
         */
43
        "utf8Filter"    => true,
44
45
        /**
46
         * @var bool Return current element as an array for ease of use?
47
         *
48
         * Disable for performance gain. Bu you need to do (string)$current->something for each sub-element.
49
         */
50
        "asArray"       => true,
51
    );
52
53
    /**
54
     * @var \XMLReader
55
     */
56
    protected $reader;
57
58
    /**
59
     * @var \DOMDocument
60
     */
61
    protected $doc;
62
63
    /**
64
     * @param string $xmlFileUri
65
     * @param string $delimiterTagName
66
     * @param array $options
67
     *
68
     * @throws \Exception
69
     */
70 2
    public function __construct(
71
        $xmlFileUri,
72
        $delimiterTagName,
73
        $options = array()
74
    ) {
75 2
        $this->xmlFileUri = $xmlFileUri;
76 2
        $this->delimiterTagName = $delimiterTagName;
77
78
        // work-around for non-scalar default value
79 2
        $this->options["readerOptions"] = \XMLReader::VALIDATE | \XMLReader::SUBST_ENTITIES | LIBXML_NOCDATA;
80 2
        $this->options = array_replace_recursive($this->options, $options);
81
82 2
        $this->reader = new \XMLReader();
83 2
        $this->doc = new \DOMDocument();
84
85 2
        if ($this->options["utf8Filter"]) {
86 2
            require_once "Utf8Filter.php";
87 2
            stream_filter_register('xmlutf8', __NAMESPACE__ . "\\Utf8Filter");
88 1
        }
89 2
    }
90
91
    /**
92
     * Return the current element, <b>FALSE</b> on error
93
     * @link http://php.net/manual/en/iterator.current.php
94
     * @link http://stackoverflow.com/a/1835324/372654
95
     * @return false|array|\SimpleXMLElement
96
     */
97 2
    public function current()
98
    {
99 2
        $node = $this->reader->expand();
100 2
        if ($node === false) {
101
            return false;
102
        }
103 2
        $node = $this->doc->importNode($node, true);
104 2
        if ($node === false) {
105
            return false;
106
        }
107 2
        $current = simplexml_import_dom($node);
108 2
        if ($current === false) {
109
            return false;
110
        }
111
112 2
        if ($this->options["asArray"]) {
113 2
            return json_decode(json_encode($current), true);
114
        } else {
115
            return $current;
116
        }
117
    }
118
119
    /**
120
     * Move forward to next element
121
     * @link http://php.net/manual/en/iterator.next.php
122
     * @return void Any returned value is ignored.
123
     */
124 2
    public function next()
125
    {
126 2
        if ($this->reader->next($this->delimiterTagName)) {
127 2
            ++$this->position;
128 1
        }
129 2
    }
130
131
    /**
132
     * Return the key of the current element
133
     * @link http://php.net/manual/en/iterator.key.php
134
     * @return int scalar on success, or null on failure.
135
     */
136
    public function key()
137
    {
138
        return $this->position;
139
    }
140
141
    /**
142
     * Checks if current position is valid
143
     * @link http://php.net/manual/en/iterator.valid.php
144
     * @return boolean The return value will be casted to boolean and then evaluated.
145
     *       Returns true on success or false on failure.
146
     */
147 2
    public function valid()
148
    {
149 2
        return $this->reader->name === $this->delimiterTagName;
150
    }
151
152
    /**
153
     * Rewind the Iterator to the first element
154
     * @link http://php.net/manual/en/iterator.rewind.php
155
     * @throws \Exception
156
     * @return void Any returned value is ignored.
157
     */
158 2
    public function rewind()
159
    {
160 2
        $uri = $this->xmlFileUri;
161
162 2
        if ($this->options["utf8Filter"]) {
163 2
            $uri = "php://filter/read=xmlutf8/resource=" . $uri;
164 1
        }
165
166 2
        if (!$this->reader
167 2
            ->open(
168 1
                $uri,
169 2
                $this->options["encoding"],
170 2
                $this->options["readerOptions"]
171 1
            )
172 1
        ) {
173
            throw new \Exception("$this->xmlFileUri cannot be opened");
174
        }
175
176
        // move to the first element
177 2
        while ($this->reader->read() && $this->reader->name !== $this->delimiterTagName) {
0 ignored issues
show
Unused Code introduced by
This while loop is empty and can be removed.

This check looks for while loops that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

Consider removing the loop.

Loading history...
178
            // intentionally empty
179 1
        }
180 2
    }
181
}
182