Passed
Branch develop (cd7fef)
by Mikaël
01:30
created

AbstractNodeHandler   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 131
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

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