1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of the php-epp2 library. |
5
|
|
|
* |
6
|
|
|
* (c) Gunter Grodotzki <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE file |
9
|
|
|
* that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace AfriCC\EPP\DOM; |
13
|
|
|
|
14
|
|
|
use DOMElement as PHP_DOMElement; |
15
|
|
|
|
16
|
|
|
class DOMTools |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* transform epp style DOMElement to a php-array |
20
|
|
|
* |
21
|
|
|
* @param DOMElement $node |
22
|
|
|
* @param array $forceArrayKeys force certain tags into an array that are |
23
|
|
|
* expected to be iterated (forex: street). |
24
|
|
|
* @param array $ignoreAttributeKeys ignore certain attributes as they |
25
|
|
|
* contain duplicate information (forex: ipType). |
26
|
|
|
*/ |
27
|
|
|
public static function nodeToArray( |
28
|
|
|
PHP_DOMElement $node, |
29
|
|
|
$forceArrayKeys = ['hostAttr', 'hostObj', 'street', 'hostAddr'], |
30
|
|
|
$ignoreAttributeKeys = ['hostAddr'] |
31
|
|
|
) { |
32
|
|
|
$tmp = []; |
33
|
|
|
foreach ($node->childNodes as $each) { |
34
|
|
|
if ($each->nodeType !== XML_ELEMENT_NODE) { |
35
|
|
|
continue; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
// if node only has a type attribute lets distinguish them directly |
39
|
|
|
// and then ignore the attribtue |
40
|
|
|
if ($each->hasAttribute('type')) { |
41
|
|
|
$key = $each->localName . '@' . $each->getAttribute('type'); |
42
|
|
|
$ignore_attributes = true; |
43
|
|
|
} else { |
44
|
|
|
$key = $each->localName; |
45
|
|
|
$ignore_attributes = false; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
// in case of special keys, always create array |
49
|
|
|
if (in_array($key, $forceArrayKeys)) { |
50
|
|
|
$current = &$tmp[$key][]; |
51
|
|
|
end($tmp[$key]); |
52
|
|
|
$insert_key = key($tmp[$key]); |
53
|
|
|
} |
54
|
|
|
// if key already exists, dynamically create an array |
55
|
|
|
elseif (isset($tmp[$key])) { |
56
|
|
|
if (!is_array($tmp[$key]) || !isset($tmp[$key][0])) { |
57
|
|
|
$tmp[$key] = [$tmp[$key]]; |
58
|
|
|
} |
59
|
|
|
$current = &$tmp[$key][]; |
60
|
|
|
end($tmp[$key]); |
61
|
|
|
$insert_key = key($tmp[$key]); |
62
|
|
|
} |
63
|
|
|
// key was not yet set, so lets start off with a string |
64
|
|
|
else { |
65
|
|
|
$current = &$tmp[$key]; |
66
|
|
|
$insert_key = null; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
if ($each->hasChildNodes()) { |
70
|
|
|
$current = static::nodeToArray($each, $forceArrayKeys, $ignoreAttributeKeys); |
71
|
|
|
} else { |
72
|
|
|
$current = $each->nodeValue; |
73
|
|
|
|
74
|
|
|
if (!$ignore_attributes && !in_array($each->localName, $ignoreAttributeKeys) && $each->hasAttributes()) { |
75
|
|
|
foreach ($each->attributes as $attr) { |
76
|
|
|
|
77
|
|
|
// single attribute with empty node, use the attr-value directly |
78
|
|
|
if ($each->localName === 'status' || ($each->attributes->length === 1 && $each->nodeValue === '')) { |
79
|
|
|
$current = $attr->nodeValue; |
80
|
|
|
break; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
if ($insert_key) { |
84
|
|
|
if (isset($tmp['@' . $key][$attr->nodeName]) && !is_array($tmp['@' . $key][$attr->nodeName])) { |
85
|
|
|
$tmp['@' . $key][$attr->nodeName] = [$tmp['@' . $key][$attr->nodeName]]; |
86
|
|
|
} |
87
|
|
|
$tmp['@' . $key][$attr->nodeName][$insert_key] = $attr->nodeValue; |
88
|
|
|
} else { |
89
|
|
|
$tmp['@' . $key][$attr->nodeName] = $attr->nodeValue; |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
return $tmp; |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|