1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace WsdlToPhp\DomHandler; |
6
|
|
|
|
7
|
|
|
abstract class AbstractNodeHandler |
8
|
|
|
{ |
9
|
|
|
protected \DOMNode $node; |
10
|
|
|
|
11
|
|
|
protected int $index; |
12
|
|
|
|
13
|
|
|
protected AbstractDomDocumentHandler $domDocumentHandler; |
14
|
|
|
|
15
|
98 |
|
public function __construct(\DOMNode $node, AbstractDomDocumentHandler $domDocumentHandler, int $index = 0) |
16
|
|
|
{ |
17
|
98 |
|
$this->node = $node; |
18
|
98 |
|
$this->index = $index; |
19
|
98 |
|
$this->domDocumentHandler = $domDocumentHandler; |
20
|
|
|
} |
21
|
|
|
|
22
|
86 |
|
public function getNode(): \DOMNode |
23
|
|
|
{ |
24
|
86 |
|
return $this->node; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @return \DOMNodeList<\DOMElement|\DOMNode> |
29
|
|
|
*/ |
30
|
2 |
|
public function getChildNodes(): \DOMNodeList |
31
|
|
|
{ |
32
|
2 |
|
return $this->getNode()->childNodes; |
33
|
|
|
} |
34
|
|
|
|
35
|
8 |
|
public function getParent(): ?AbstractNodeHandler |
36
|
|
|
{ |
37
|
8 |
|
if ($this->getNode()->parentNode instanceof \DOMNode) { |
38
|
6 |
|
return $this->getDomDocumentHandler()->getHandler($this->getNode()->parentNode); |
39
|
|
|
} |
40
|
|
|
|
41
|
4 |
|
return null; |
42
|
|
|
} |
43
|
|
|
|
44
|
2 |
|
public function getIndex(): int |
45
|
|
|
{ |
46
|
2 |
|
return $this->index; |
47
|
|
|
} |
48
|
|
|
|
49
|
64 |
|
public function getDomDocumentHandler(): AbstractDomDocumentHandler |
50
|
|
|
{ |
51
|
64 |
|
return $this->domDocumentHandler; |
52
|
|
|
} |
53
|
|
|
|
54
|
8 |
|
public function getName(): string |
55
|
|
|
{ |
56
|
8 |
|
$name = $this->getNode()->nodeName; |
57
|
8 |
|
if (false !== strpos($name, ':')) { |
58
|
6 |
|
$name = implode('', array_slice(explode(':', $name), -1, 1)); |
59
|
|
|
} |
60
|
|
|
|
61
|
8 |
|
return $name; |
62
|
|
|
} |
63
|
|
|
|
64
|
4 |
|
public function getNamespace(): ?string |
65
|
|
|
{ |
66
|
4 |
|
$name = $this->getNode()->nodeName; |
67
|
4 |
|
if (false !== strpos($name, ':')) { |
68
|
2 |
|
return implode('', array_slice(explode(':', $name), 0, -1)); |
69
|
|
|
} |
70
|
|
|
|
71
|
2 |
|
return null; |
72
|
|
|
} |
73
|
|
|
|
74
|
2 |
|
public function hasAttributes(): bool |
75
|
|
|
{ |
76
|
2 |
|
return $this->getNode()->hasAttributes(); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @return AbstractAttributeHandler[]|AbstractNodeHandler[] |
81
|
|
|
*/ |
82
|
2 |
|
public function getAttributes(): array |
83
|
|
|
{ |
84
|
2 |
|
return $this->getHandlers($this->getNode()->attributes); |
85
|
|
|
} |
86
|
|
|
|
87
|
8 |
|
public function hasChildren(): bool |
88
|
|
|
{ |
89
|
8 |
|
return $this->getNode()->hasChildNodes(); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @return AbstractAttributeHandler[]|AbstractElementHandler[]|AbstractNodeHandler[] |
94
|
|
|
*/ |
95
|
4 |
|
public function getChildren(): array |
96
|
|
|
{ |
97
|
4 |
|
return $this->getHandlers($this->getNode()->childNodes); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @return mixed |
102
|
|
|
*/ |
103
|
2 |
|
public function getNodeValue() |
104
|
|
|
{ |
105
|
2 |
|
$nodeValue = trim((string) $this->getNode()->nodeValue); |
106
|
2 |
|
$nodeValue = str_replace([ |
107
|
2 |
|
"\r", |
108
|
2 |
|
"\n", |
109
|
2 |
|
"\t", |
110
|
2 |
|
], [ |
111
|
2 |
|
'', |
112
|
2 |
|
'', |
113
|
2 |
|
' ', |
114
|
2 |
|
], $nodeValue); |
115
|
|
|
|
116
|
2 |
|
return preg_replace('[\s+]', ' ', $nodeValue); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Alias for AbstractNodeHandler::getNodeValue(). |
121
|
|
|
* |
122
|
|
|
* @return mixed |
123
|
|
|
*/ |
124
|
2 |
|
public function getValue() |
125
|
|
|
{ |
126
|
2 |
|
return $this->getNodeValue(); |
127
|
|
|
} |
128
|
|
|
|
129
|
2 |
|
public function getValueNamespace(): ?string |
130
|
|
|
{ |
131
|
2 |
|
return null; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @param null|\Traversable<\DOMNode> $nodes |
136
|
|
|
* |
137
|
|
|
* @return AbstractAttributeHandler[]|AbstractElementHandler[]|AbstractNodeHandler[] |
138
|
|
|
*/ |
139
|
6 |
|
private function getHandlers(?\Traversable $nodes): array |
140
|
|
|
{ |
141
|
6 |
|
if (is_null($nodes)) { |
142
|
|
|
return []; |
143
|
|
|
} |
144
|
|
|
|
145
|
6 |
|
$handlers = []; |
146
|
6 |
|
$index = 0; |
147
|
6 |
|
foreach ($nodes as $node) { |
148
|
6 |
|
$handlers[] = $this->getDomDocumentHandler()->getHandler($node, $index++); |
149
|
|
|
} |
150
|
|
|
|
151
|
6 |
|
return $handlers; |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|