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
|
|
|
*/ |
83
|
19 |
|
public function __construct( |
84
|
|
|
$nodeList, |
85
|
|
|
ContextInterface $initialContext, |
86
|
|
|
ElementProcessorInterface $elementProcessor |
87
|
|
|
) { |
88
|
19 |
|
$this->elementProcessor = $elementProcessor; |
89
|
19 |
|
$this->initialContext = $initialContext; |
90
|
|
|
|
91
|
19 |
|
parent::__construct($this->registerNodes($nodeList)); |
92
|
18 |
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Recursive DOM node iterator constructor |
96
|
|
|
* |
97
|
|
|
* @param \DOMNodeList|array $nodeList Node list |
98
|
|
|
* @throws RuntimeException If the node list is invalid |
99
|
|
|
* @return array Nodes |
100
|
|
|
*/ |
101
|
19 |
|
protected function registerNodes($nodeList) |
102
|
|
|
{ |
103
|
|
|
// If the node list is invalid |
104
|
19 |
|
if (!is_array($nodeList) && !($nodeList instanceof \DOMNodeList)) { |
105
|
1 |
|
throw new RuntimeException(RuntimeException::INVALID_NODE_LIST_STR, RuntimeException::INVALID_NODE_LIST); |
106
|
|
|
} |
107
|
|
|
|
108
|
18 |
|
$nodes = []; |
109
|
|
|
|
110
|
|
|
// Run through and register all nodes |
111
|
|
|
/** @var \DOMNode $node */ |
112
|
18 |
|
foreach ($nodeList as $node) { |
113
|
18 |
|
$nodes[spl_object_hash($node)] = $this->registerNode($node); |
114
|
|
|
} |
115
|
|
|
|
116
|
18 |
|
return $nodes; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Register an element node |
121
|
|
|
* |
122
|
|
|
* @param \DOMNode $node Node |
123
|
|
|
* @return \DOMNode Node |
124
|
|
|
*/ |
125
|
18 |
|
protected function registerNode(\DOMNode $node) |
126
|
|
|
{ |
127
|
18 |
|
if ($node->nodeType == XML_ELEMENT_NODE) { |
128
|
|
|
/** @var \DOMElement $node */ |
129
|
18 |
|
$localContext = $this->elementProcessor->processElement($node, $this->initialContext); |
130
|
|
|
|
131
|
|
|
// Register the node context |
132
|
18 |
|
$localContextId = spl_object_hash($localContext); |
133
|
18 |
|
if (empty($this->contexts[$localContextId])) { |
134
|
18 |
|
$this->contexts[$localContextId] = $localContext; |
135
|
|
|
} |
136
|
|
|
|
137
|
18 |
|
$this->contextMap[spl_object_hash($node)] = $localContextId; |
138
|
|
|
} |
139
|
|
|
|
140
|
18 |
|
return $node; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Return the recursive iterator |
145
|
|
|
* |
146
|
|
|
* @return \RecursiveIteratorIterator Recursive iterator |
147
|
|
|
*/ |
148
|
18 |
|
public function getRecursiveIterator() |
149
|
|
|
{ |
150
|
18 |
|
return new \RecursiveIteratorIterator($this, \RecursiveIteratorIterator::SELF_FIRST); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Return whether the current node has child nodes |
155
|
|
|
* |
156
|
|
|
* This method gets called once per element and prior to the call to current(), |
157
|
|
|
* so this seems like the perfect place for the first processing steps (even |
158
|
|
|
* for elements without children). |
159
|
|
|
* |
160
|
|
|
* @return boolean Current node has child nodes |
161
|
|
|
*/ |
162
|
18 |
|
public function hasChildren() |
163
|
|
|
{ |
164
|
18 |
|
return $this->current()->hasChildNodes(); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Return a child node iterator |
169
|
|
|
* |
170
|
|
|
* @return DOMIterator Child node iterator |
171
|
|
|
*/ |
172
|
18 |
|
public function getChildren() |
173
|
|
|
{ |
174
|
18 |
|
$element = $this->current(); |
175
|
18 |
|
$childContext = $this->elementProcessor->processElementChildren( |
176
|
|
|
$element, |
177
|
18 |
|
$this->contexts[$this->contextMap[$this->key()]] |
178
|
|
|
); |
179
|
18 |
|
return new static($element->childNodes, $childContext, $this->elementProcessor); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* Rewind array back to the start |
184
|
|
|
* |
185
|
|
|
* @return void |
186
|
|
|
*/ |
187
|
18 |
|
public function rewind() |
188
|
|
|
{ |
189
|
18 |
|
parent::rewind(); |
190
|
18 |
|
} |
191
|
|
|
} |
192
|
|
|
|