Passed
Push — develop ( a19bc2...cd7fef )
by Mikaël
07:31
created

AbstractNodeHandler::getDomDocumentHandler()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
ccs 0
cts 2
cp 0
rs 10
cc 1
nc 1
nop 0
crap 2
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