Passed
Push — develop ( 26cb25...0f2b7b )
by Mikaël
07:29
created

AbstractNodeHandler::hasAttributes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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