Completed
Push — master ( a959b0...c973b9 )
by Joschi
03:02
created

DOMIterator::getRecursiveIterator()   A

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 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
/**
4
 * rdfa-lite
5
 *
6
 * @category Jkphl
7
 * @package Jkphl\Rdfalite
8
 * @subpackage Jkphl\Rdfalite\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\Rdfalite\Application\Parser;
38
39
use Jkphl\Rdfalite\Application\Contract\ElementProcessorInterface;
40
41
/**
42
 * Recursive DOM node iterator
43
 *
44
 * @package Jkphl\Rdfalite
45
 * @subpackage Jkphl\Rdfalite\Application
46
 */
47
class DOMIterator extends \ArrayIterator implements \RecursiveIterator
48
{
49
    /**
50
     * Element processor
51
     *
52
     * @var ElementProcessorInterface
53
     */
54
    protected $elementProcessor;
55
    /**
56
     * Initial parser context
57
     *
58
     * @var Context
59
     */
60
    protected $initialContext;
61
    /**
62
     * Registered contexts
63
     *
64
     * @var Context[]
65
     */
66
    public $contexts = [];
67
    /**
68
     * Element context map
69
     *
70
     * @var array
71
     */
72
    protected $contextMap = [];
73
74
    /**
75
     * Recursive DOM node iterator constructor
76
     *
77
     * @param \DOMNodeList $nodeList Node list
78
     * @param Context $initialContext Initial parser context
79
     * @param ElementProcessorInterface $elementProcessor Element processor
80
     */
81 3
    public function __construct(
82
        \DOMNodeList $nodeList,
83
        Context $initialContext,
84
        ElementProcessorInterface $elementProcessor
85
    ) {
86 3
        $this->elementProcessor = $elementProcessor;
87 3
        $this->initialContext = $initialContext;
88
89 3
        $nodes = [];
90
91
        // Run through all nodes
92
        /** @var \DOMNode $node */
93 3
        foreach ($nodeList as $node) {
94 3
            $nodeId = spl_object_hash($node);
95
96
            // If it's an element node: Process it and register it's local context
97 3
            if ($node->nodeType == XML_ELEMENT_NODE) {
98
                /** @var \DOMElement $node */
99 3
                $localContext = $this->elementProcessor->processElement($node, $this->initialContext);
100
101
                // Register the node context
102 3
                $localContextId = spl_object_hash($localContext);
103 3
                if (empty($this->contexts[$localContextId])) {
104 3
                    $this->contexts[$localContextId] = $localContext;
105 3
                }
106
107 3
                $this->contextMap[$nodeId] = $localContextId;
108 3
            }
109
110
            // Register the node
111 3
            $nodes[$nodeId] = $node;
112 3
        }
113
114 3
        parent::__construct($nodes);
115 3
    }
116
117
    /**
118
     * Return the recursive iterator
119
     *
120
     * @return \RecursiveIteratorIterator Recursive iterator
121
     */
122 3
    public function getRecursiveIterator()
123
    {
124 3
        return new \RecursiveIteratorIterator($this, \RecursiveIteratorIterator::SELF_FIRST);
125
    }
126
127
    /**
128
     * Return whether the current node has child nodes
129
     *
130
     * This method gets called once per element and prior to the call to current(),
131
     * so this seems like the perfect place for the first processing steps (even
132
     * for elements without children).
133
     *
134
     * @return boolean Current node has child nodes
135
     */
136 3
    public function hasChildren()
137
    {
138 3
        return $this->current()->hasChildNodes();
139
    }
140
141
    /**
142
     * Return a child node iterator
143
     *
144
     * @return DOMIterator Child node iterator
145
     */
146 3
    public function getChildren()
147
    {
148 3
        $element = $this->current();
149 3
        $childContext = $this->elementProcessor->processElementChildren(
150 3
            $element,
151 3
            $this->contexts[$this->contextMap[$this->key()]]
152 3
        );
153 3
        return new static($element->childNodes, $childContext, $this->elementProcessor);
154
    }
155
156
    /**
157
     * Rewind array back to the start
158
     *
159
     * @return void
160
     */
161 3
    public function rewind()
162
    {
163 3
        parent::rewind();
164 3
    }
165
}
166