Completed
Push — master ( c5f2a4...b3cdb4 )
by Edgar
04:10
created

DOMWrapper   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 132
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 45.71%

Importance

Changes 5
Bugs 0 Features 1
Metric Value
wmc 16
c 5
b 0
f 1
lcom 1
cbo 4
dl 0
loc 132
ccs 16
cts 35
cp 0.4571
rs 10

15 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A setAttribute() 0 4 1
A getAttribute() 0 4 1
A setAttributeNS() 0 4 1
A getAttributeNS() 0 4 1
A appendChild() 0 4 1
A createElement() 0 7 2
A createElementNS() 0 4 1
A isLoaded() 0 4 1
A saveHTML() 0 4 1
A getElement() 0 4 1
A setNodeValue() 0 4 1
A getNodeValue() 0 4 1
A removeNode() 0 4 1
A saveXML() 0 6 1
1
<?php
2
namespace nstdio\svg\xml;
3
4
use DOMDocument;
5
use nstdio\svg\Base;
6
use nstdio\svg\traits\ElementTrait;
7
use nstdio\svg\XMLDocumentInterface;
8
9
/**
10
 * Class DomWrapper
11
 *
12
 * @package nstdio\svg\xml
13
 * @author  Edgar Asatryan <[email protected]>
14
 */
15
final class DOMWrapper implements XMLDocumentInterface
16
{
17
    use ElementTrait;
18
19
    /**
20
     * @var DOMDocument
21
     */
22
    private $dom;
23
24 127
    public function __construct()
25
    {
26 127
        $this->dom = new DOMDocument(Base::DOM_VERSION, Base::DOM_ENCODING);
27 127
        $this->dom->xmlStandalone = false;
28 127
    }
29
30
    /**
31
     * @inheritdoc
32
     */
33
    public function setAttribute($name, $value = null)
34
    {
35
        $this->dom->documentElement->setAttribute($name, $value);
36
    }
37
38
    /**
39
     * @inheritdoc
40
     */
41
    public function getAttribute($name)
42
    {
43
        return $this->dom->documentElement->getAttribute($name);
44
    }
45
46
    /**
47
     * @inheritdoc
48
     */
49
    public function setAttributeNS($namespaceURI, $qualifiedName, $value)
50
    {
51
        // TODO: Implement setAttributeNS() method.
52
    }
53
54
    /**
55
     * @inheritdoc
56
     */
57
    public function getAttributeNS($namespaceURI, $localName)
58
    {
59
        // TODO: Implement getAttributeNS() method.
60
    }
61
62
    /**
63
     * @inheritdoc
64
     */
65 127
    public function appendChild(XMLDocumentInterface $newNode)
66
    {
67 127
        return $this->dom->appendChild($newNode->getElement());
68
    }
69
70
    /**
71
     * @inheritdoc
72
     */
73 127
    public function createElement($name, $value = null)
74
    {
75
        // on PHP 5.4.* createElement method with null parameter will create empty DOMText object as child of created element
76 127
        $element = $value === null ? $this->dom->createElement($name) : $this->dom->createElement($name, $value);
77
78 127
        return new DOMElementWrapper($element);
79
    }
80
81
    /**
82
     * @inheritdoc
83
     */
84
    public function createElementNS($namespaceURI, $qualifiedName, $value = null)
85
    {
86
        return $this->dom->createElementNS($namespaceURI, $qualifiedName, $value);
87
    }
88
89
    /**
90
     * @inheritdoc
91
     */
92 127
    public static function isLoaded()
93
    {
94 127
        return extension_loaded('dom');
95
    }
96
97
    /**
98
     * @inheritdoc
99
     */
100 45
    public function saveHTML()
101
    {
102 45
        return $this->dom->saveHTML();
103
    }
104
105
    /**
106
     * @inheritdoc
107
     */
108 2
    public function saveXML($formatOutput = false)
109
    {
110 2
        $this->dom->formatOutput = (bool) $formatOutput;
111
112 2
        return trim($this->dom->saveXML());
113
    }
114
115
    /**
116
     * @inheritdoc
117
     */
118
    public function getElement()
119
    {
120
        return $this->dom->documentElement;
121
    }
122
123
    /**
124
     * @inheritdoc
125
     */
126
    public function setNodeValue($value)
127
    {
128
        throw NotImplementedException::newInstance();
129
    }
130
131
    /**
132
     * @inheritdoc
133
     */
134
    public function getNodeValue()
135
    {
136
        throw NotImplementedException::newInstance();
137
    }
138
139
    /**
140
     * @inheritdoc
141
     */
142
    public function removeNode(XMLDocumentInterface $child)
143
    {
144
        return $this->dom->documentElement->removeChild($child->getElement());
145
    }
146
}