Completed
Push — develop ( 6eec50...8b93b9 )
by Mikaël
03:30
created

AbstractNodeHandler   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 162
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 20
lcom 1
cbo 1
dl 0
loc 162
ccs 59
cts 59
cp 1
rs 10
c 0
b 0
f 0

16 Methods

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