Completed
Push — master ( 7c66eb...e496eb )
by Edgar
06:39
created

DOMWrapper::createElement()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 7
ccs 3
cts 3
cp 1
rs 9.4285
cc 2
eloc 3
nc 2
nop 2
crap 2
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 121
    public function __construct()
25
    {
26 121
        $this->dom = new DOMDocument(Base::DOM_VERSION, Base::DOM_ENCODING);
27 121
        $this->dom->xmlStandalone = false;
28 121
    }
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 121
    public function appendChild(XMLDocumentInterface $newNode)
66
    {
67 121
        return $this->dom->appendChild($newNode->getElement());
68
    }
69
70
    /**
71
     * @inheritdoc
72
     */
73 121
    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 121
        $element = $value === null ? $this->dom->createElement($name) : $this->dom->createElement($name, $value);
77
78 121
        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 121
    public static function isLoaded()
93
    {
94 121
        return extension_loaded('dom');
95
    }
96
97
    /**
98
     * @inheritdoc
99
     */
100 42
    public function saveHTML()
101
    {
102 42
        return $this->dom->saveHTML();
103
    }
104
105
    /**
106
     * @inheritdoc
107
     */
108
    public function saveXML($formatOutput = false)
109
    {
110
        $this->dom->formatOutput = (bool) $formatOutput;
111
112
        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
}