|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the cfdi-xml project. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Kinedu |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Kinedu\CfdiXML\Common; |
|
13
|
|
|
|
|
14
|
|
|
use DOMElement; |
|
15
|
|
|
use DOMDocument; |
|
16
|
|
|
use DOMNodeList; |
|
17
|
|
|
|
|
18
|
|
|
class Node |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* Node document. |
|
22
|
|
|
* |
|
23
|
|
|
* @var DOMDocument |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $document; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Node element. |
|
29
|
|
|
* |
|
30
|
|
|
* @var DOMElement |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $element; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Define the parent node name. |
|
36
|
|
|
* |
|
37
|
|
|
* @var string|null |
|
38
|
|
|
*/ |
|
39
|
|
|
protected $parentNodeName = null; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Define the node name. |
|
43
|
|
|
* |
|
44
|
|
|
* @var string |
|
45
|
|
|
*/ |
|
46
|
|
|
protected $nodeName; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Node attributes. |
|
50
|
|
|
* |
|
51
|
|
|
* @var array |
|
52
|
|
|
*/ |
|
53
|
|
|
protected $attr = []; |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Create a new node instance. |
|
57
|
|
|
* |
|
58
|
|
|
* @param array $attr |
|
59
|
|
|
*/ |
|
60
|
|
|
public function __construct(array ...$attr) |
|
61
|
|
|
{ |
|
62
|
|
|
$this->attr = $attr; |
|
63
|
|
|
|
|
64
|
|
|
$this->document = new DOMDocument('1.0', 'UTF-8'); |
|
65
|
|
|
$this->document->preserveWhiteSpace = false; |
|
66
|
|
|
|
|
67
|
|
|
if ($nodeName = $this->getNodeName()) { |
|
68
|
|
|
$this->element = $this->document->createElement($nodeName); |
|
69
|
|
|
$this->document->appendChild($this->element); |
|
70
|
|
|
$this->setAttr($this->element, $this->getAttr()); |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Add a new node. |
|
76
|
|
|
* |
|
77
|
|
|
* @param \Kinedu\CfdiXML\Common\Node $node |
|
78
|
|
|
* |
|
79
|
|
|
* @return void |
|
80
|
|
|
*/ |
|
81
|
|
|
public function add(Node $node) |
|
82
|
|
|
{ |
|
83
|
|
|
$wrapperElement = null; |
|
84
|
|
|
|
|
85
|
|
|
if (isset($node->schemaDefinition) && $node->globalSchemaLocation) { |
|
|
|
|
|
|
86
|
|
|
$this->setSchemaDefinition( |
|
87
|
|
|
$node->getSchemaLocation() |
|
|
|
|
|
|
88
|
|
|
); |
|
89
|
|
|
|
|
90
|
|
|
$this->setNamespace($node); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
$nodeElement = $this->document->createElement($node->getNodeName()); |
|
94
|
|
|
$this->setAttr($nodeElement, $node->getAttr()); |
|
95
|
|
|
|
|
96
|
|
|
foreach ($node->element->childNodes as $child) { |
|
97
|
|
|
$nodeElement->appendChild( |
|
98
|
|
|
$this->document->importNode($child, true) |
|
99
|
|
|
); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
if ($wrapperName = $node->getWrapperNodeName()) { |
|
103
|
|
|
$wrapperElement = $this->getDirectChildElementByName( |
|
104
|
|
|
$this->element->childNodes, |
|
105
|
|
|
$wrapperName |
|
106
|
|
|
); |
|
107
|
|
|
|
|
108
|
|
|
if (!$wrapperElement) { |
|
109
|
|
|
$wrapperElement = $this->document->createElement($wrapperName); |
|
110
|
|
|
$this->element->appendChild($wrapperElement); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
$this->setAttr($wrapperElement, $node->getAttr('wrapper')); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
if ($parentName = $node->getParentNodeName()) { |
|
117
|
|
|
$currentElement = ($wrapperElement) ? $wrapperElement : $this->element; |
|
118
|
|
|
|
|
119
|
|
|
$parentNode = $this->getDirectChildElementByName( |
|
120
|
|
|
$currentElement->childNodes, |
|
121
|
|
|
$parentName |
|
122
|
|
|
); |
|
123
|
|
|
|
|
124
|
|
|
if (!$parentNode) { |
|
125
|
|
|
$parentElement = $this->document->createElement($parentName); |
|
126
|
|
|
$currentElement->appendChild($parentElement); |
|
127
|
|
|
$parentElement->appendChild($nodeElement); |
|
128
|
|
|
$this->setAttr($parentElement, $node->getAttr('parent')); |
|
129
|
|
|
} else { |
|
130
|
|
|
$parentNode->appendChild($nodeElement); |
|
131
|
|
|
} |
|
132
|
|
|
} else { |
|
133
|
|
|
$this->element->appendChild($nodeElement); |
|
134
|
|
|
} |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* Search the direct child of an element. |
|
139
|
|
|
* |
|
140
|
|
|
* @param DOMNodeList $children |
|
141
|
|
|
* @param string $find |
|
142
|
|
|
* |
|
143
|
|
|
* @return DOMElement|null |
|
144
|
|
|
*/ |
|
145
|
|
|
protected function getDirectChildElementByName(DOMNodeList $children, string $find) |
|
146
|
|
|
{ |
|
147
|
|
|
foreach ($children as $child) { |
|
148
|
|
|
if ($child->nodeName == $find) { |
|
149
|
|
|
return $child; |
|
150
|
|
|
} |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
return null; |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* @param \Kinedu\CfdiXML\Common\Node $node |
|
158
|
|
|
* @return void |
|
159
|
|
|
*/ |
|
160
|
|
|
public function setNamespace(Node $node) |
|
161
|
|
|
{ |
|
162
|
|
|
$element = $this->element; |
|
163
|
|
|
|
|
164
|
|
|
$namespaceKey = "xmlns:{$node->namespaceKey}"; |
|
|
|
|
|
|
165
|
|
|
$namespaceValue = $node->getNamespace(); |
|
|
|
|
|
|
166
|
|
|
|
|
167
|
|
|
$elementAttr = []; |
|
168
|
|
|
|
|
169
|
|
|
if ($element->hasAttributes()) { |
|
170
|
|
|
$lastAttrName = null; |
|
171
|
|
|
|
|
172
|
|
|
foreach (iterator_to_array($element->attributes) as $attr) { |
|
173
|
|
|
if ((substr($lastAttrName, 0, 5) == 'xmlns') && |
|
174
|
|
|
(substr($attr->name, 0, 5) != 'xmlns')) { |
|
175
|
|
|
$elementAttr[$namespaceKey] = $namespaceValue; |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
$elementAttr[$lastAttrName = $attr->name] = $attr->value; |
|
179
|
|
|
|
|
180
|
|
|
$element->removeAttributeNode($attr); |
|
181
|
|
|
} |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
$this->setAttr($element, $elementAttr); |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
/** |
|
188
|
|
|
* @param string $schemaDefinition |
|
189
|
|
|
* @return void |
|
190
|
|
|
*/ |
|
191
|
|
|
public function setSchemaDefinition(string $schemaDefinition) |
|
192
|
|
|
{ |
|
193
|
|
|
$attrName = 'xsi:schemaLocation'; |
|
194
|
|
|
|
|
195
|
|
|
$node = $this->element; |
|
196
|
|
|
|
|
197
|
|
|
if ($node->hasAttribute($attrName)) { |
|
198
|
|
|
$value = $node->getAttribute($attrName); |
|
199
|
|
|
$value = "{$value} {$schemaDefinition}"; |
|
200
|
|
|
|
|
201
|
|
|
$node->setAttribute($attrName, $value); |
|
202
|
|
|
} |
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
|
|
/** |
|
206
|
|
|
* Get node attributes. |
|
207
|
|
|
* |
|
208
|
|
|
* @param string $index |
|
209
|
|
|
* |
|
210
|
|
|
* @return array|null |
|
211
|
|
|
*/ |
|
212
|
|
|
public function getAttr(string $index = 'node') |
|
213
|
|
|
{ |
|
214
|
|
|
$attrIndex = ['node', 'parent', 'wrapper']; |
|
215
|
|
|
|
|
216
|
|
|
$index = (in_array($index, $attrIndex)) |
|
217
|
|
|
? array_search($index, $attrIndex) |
|
218
|
|
|
: 0; |
|
219
|
|
|
|
|
220
|
|
|
return $this->attr[$index] ?? null; |
|
221
|
|
|
} |
|
222
|
|
|
|
|
223
|
|
|
/** |
|
224
|
|
|
* Adds attributes to an element. |
|
225
|
|
|
* |
|
226
|
|
|
* @param DOMElement $element |
|
227
|
|
|
* @param array $attr |
|
228
|
|
|
* |
|
229
|
|
|
* @return void |
|
230
|
|
|
*/ |
|
231
|
|
|
public function setAttr(DOMElement $element, array $attr = null) |
|
232
|
|
|
{ |
|
233
|
|
|
if (!is_null($attr)) { |
|
234
|
|
|
foreach ($attr as $key => $value) { |
|
235
|
|
|
$element->setAttribute($key, $value); |
|
236
|
|
|
} |
|
237
|
|
|
} |
|
238
|
|
|
} |
|
239
|
|
|
|
|
240
|
|
|
/** |
|
241
|
|
|
* Get element. |
|
242
|
|
|
* |
|
243
|
|
|
* @return DOMElement |
|
244
|
|
|
*/ |
|
245
|
|
|
public function getElement(): DOMElement |
|
246
|
|
|
{ |
|
247
|
|
|
return $this->element; |
|
248
|
|
|
} |
|
249
|
|
|
|
|
250
|
|
|
/** |
|
251
|
|
|
* Get document. |
|
252
|
|
|
* |
|
253
|
|
|
* @return DOMDocument |
|
254
|
|
|
*/ |
|
255
|
|
|
public function getDocument(): DOMDocument |
|
256
|
|
|
{ |
|
257
|
|
|
return $this->document; |
|
258
|
|
|
} |
|
259
|
|
|
|
|
260
|
|
|
/** |
|
261
|
|
|
* Get wrapper node name. |
|
262
|
|
|
* |
|
263
|
|
|
* @return string|null |
|
264
|
|
|
*/ |
|
265
|
|
|
public function getWrapperNodeName() |
|
266
|
|
|
{ |
|
267
|
|
|
return $this->wrapperNodeName ?? null; |
|
|
|
|
|
|
268
|
|
|
} |
|
269
|
|
|
|
|
270
|
|
|
/** |
|
271
|
|
|
* Get parent node name. |
|
272
|
|
|
* |
|
273
|
|
|
* @return string|null |
|
274
|
|
|
*/ |
|
275
|
|
|
public function getParentNodeName() |
|
276
|
|
|
{ |
|
277
|
|
|
return $this->parentNodeName ?? null; |
|
278
|
|
|
} |
|
279
|
|
|
|
|
280
|
|
|
/** |
|
281
|
|
|
* Get node name. |
|
282
|
|
|
* |
|
283
|
|
|
* @return string |
|
284
|
|
|
*/ |
|
285
|
|
|
public function getNodeName() |
|
286
|
|
|
{ |
|
287
|
|
|
return $this->nodeName ?? null; |
|
288
|
|
|
} |
|
289
|
|
|
} |
|
290
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: