Completed
Push — master ( 342540...c82ee6 )
by Joschi
02:27
created

DOMIterator::__construct()   B

Complexity

Conditions 4
Paths 3

Size

Total Lines 23
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 23
c 0
b 0
f 0
ccs 11
cts 11
cp 1
rs 8.7972
cc 4
eloc 12
nc 3
nop 3
crap 4
1
<?php
2
3
/**
4
 * rdfa-lite-microdata
5
 *
6
 * @category Jkphl
7
 * @package Jkphl\RdfaLiteMicrodata
8
 * @subpackage Jkphl\RdfaLiteMicrodata\Application
9
 * @author Joschi Kuphal <[email protected]> / @jkphl
10
 * @copyright Copyright © 2017 Joschi Kuphal <[email protected]> / @jkphl
11
 * @license http://opensource.org/licenses/MIT The MIT License (MIT)
12
 */
13
14
/***********************************************************************************
15
 *  The MIT License (MIT)
16
 *
17
 *  Copyright © 2017 Joschi Kuphal <[email protected]> / @jkphl
18
 *
19
 *  Permission is hereby granted, free of charge, to any person obtaining a copy of
20
 *  this software and associated documentation files (the "Software"), to deal in
21
 *  the Software without restriction, including without limitation the rights to
22
 *  use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
23
 *  the Software, and to permit persons to whom the Software is furnished to do so,
24
 *  subject to the following conditions:
25
 *
26
 *  The above copyright notice and this permission notice shall be included in all
27
 *  copies or substantial portions of the Software.
28
 *
29
 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
30
 *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
31
 *  FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
32
 *  COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
33
 *  IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
34
 *  CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35
 ***********************************************************************************/
36
37
namespace Jkphl\RdfaLiteMicrodata\Application\Parser;
38
39
use Jkphl\RdfaLiteMicrodata\Application\Context\ContextInterface;
40
use Jkphl\RdfaLiteMicrodata\Application\Contract\ElementProcessorInterface;
41
use Jkphl\RdfaLiteMicrodata\Application\Exceptions\RuntimeException;
42
43
/**
44
 * Recursive DOM node iterator
45
 *
46
 * @package Jkphl\RdfaLiteMicrodata
47
 * @subpackage Jkphl\RdfaLiteMicrodata\Application
48
 */
49
class DOMIterator extends \ArrayIterator implements \RecursiveIterator
50
{
51
    /**
52
     * Registered contexts
53
     *
54
     * @var ContextInterface[]
55
     */
56
    public $contexts = [];
57
    /**
58
     * Element processor
59
     *
60
     * @var ElementProcessorInterface
61
     */
62
    protected $elementProcessor;
63
    /**
64
     * Initial parser context
65
     *
66
     * @var ContextInterface
67
     */
68
    protected $initialContext;
69
    /**
70
     * Element context map
71
     *
72
     * @var array
73
     */
74
    protected $contextMap = [];
75
76
    /**
77
     * Recursive DOM node iterator constructor
78
     *
79
     * @param \DOMNodeList|array $nodeList Node list
80
     * @param ContextInterface $initialContext Initial parser context
81
     * @param ElementProcessorInterface $elementProcessor Element processor
82
     * @throws RuntimeException If the node list is invalid
83
     */
84 16
    public function __construct(
85
        $nodeList,
86
        ContextInterface $initialContext,
87
        ElementProcessorInterface $elementProcessor
88
    ) {
89
        // If the node list is invalid
90 16
        if (!is_array($nodeList) && !($nodeList instanceof \DOMNodeList)) {
91 1
            throw new RuntimeException(RuntimeException::INVALID_NODE_LIST_STR, RuntimeException::INVALID_NODE_LIST);
92
        }
93
94 15
        $this->elementProcessor = $elementProcessor;
95 15
        $this->initialContext = $initialContext;
96
97 15
        $nodes = [];
98
99
        // Run through and register all nodes
100
        /** @var \DOMNode $node */
101 15
        foreach ($nodeList as $node) {
102 15
            $nodes[spl_object_hash($node)] = $this->registerNode($node);
103 15
        }
104
105 15
        parent::__construct($nodes);
106 15
    }
107
108
    /**
109
     * Register an element node
110
     *
111
     * @param \DOMNode $node Node
112
     * @return \DOMNode Node
113
     */
114 15
    protected function registerNode(\DOMNode $node)
115
    {
116 15
        if ($node->nodeType == XML_ELEMENT_NODE) {
117
            /** @var \DOMElement $node */
118 15
            $localContext = $this->elementProcessor->processElement($node, $this->initialContext);
119
120
            // Register the node context
121 15
            $localContextId = spl_object_hash($localContext);
122 15
            if (empty($this->contexts[$localContextId])) {
123 15
                $this->contexts[$localContextId] = $localContext;
124 15
            }
125
126 15
            $this->contextMap[spl_object_hash($node)] = $localContextId;
127 15
        }
128
129 15
        return $node;
130
    }
131
132
    /**
133
     * Return the recursive iterator
134
     *
135
     * @return \RecursiveIteratorIterator Recursive iterator
136
     */
137 15
    public function getRecursiveIterator()
138
    {
139 15
        return new \RecursiveIteratorIterator($this, \RecursiveIteratorIterator::SELF_FIRST);
140
    }
141
142
    /**
143
     * Return whether the current node has child nodes
144
     *
145
     * This method gets called once per element and prior to the call to current(),
146
     * so this seems like the perfect place for the first processing steps (even
147
     * for elements without children).
148
     *
149
     * @return boolean Current node has child nodes
150
     */
151 15
    public function hasChildren()
152
    {
153 15
        return $this->current()->hasChildNodes();
154
    }
155
156
    /**
157
     * Return a child node iterator
158
     *
159
     * @return DOMIterator Child node iterator
160
     */
161 15
    public function getChildren()
162
    {
163 15
        $element = $this->current();
164 15
        $childContext = $this->elementProcessor->processElementChildren(
165 15
            $element,
166 15
            $this->contexts[$this->contextMap[$this->key()]]
167 15
        );
168 15
        return new static($element->childNodes, $childContext, $this->elementProcessor);
169
    }
170
171
    /**
172
     * Rewind array back to the start
173
     *
174
     * @return void
175
     */
176 15
    public function rewind()
177
    {
178 15
        parent::rewind();
179 15
    }
180
}
181