Passed
Push — develop ( 10444e...4631c2 )
by Mikaël
01:29
created

AbstractNodeHandler   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 145
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 21
eloc 45
c 2
b 0
f 0
dl 0
loc 145
ccs 0
cts 58
cp 0
rs 10

16 Methods

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