Completed
Branch master (32b148)
by Edgar
03:11
created

DOMWrapper::appendChild()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 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
    public function __construct()
25
    {
26
        $this->dom = new DOMDocument(Base::DOM_VERSION, Base::DOM_ENCODING);
27
        $this->dom->xmlStandalone = false;
28
        $this->dom->formatOutput = true;
29
    }
30
31
    public function setAttribute($name, $value = null)
32
    {
33
        $this->dom->documentElement->setAttribute($name, $value);
34
    }
35
36
    public function getAttribute($name)
37
    {
38
        return $this->dom->documentElement->getAttribute($name);
39
    }
40
41
    public function setAttributeNS($namespaceURI, $qualifiedName, $value)
42
    {
43
        // TODO: Implement setAttributeNS() method.
44
    }
45
46
    public function getAttributeNS($namespaceURI, $localName)
47
    {
48
        // TODO: Implement getAttributeNS() method.
49
    }
50
51
    public function appendChild(XMLDocumentInterface $newNode)
52
    {
53
        return $this->dom->appendChild($newNode->getElement());
54
    }
55
56
    public function createElement($name, $value = null)
57
    {
58
        // on PHP 5.4.* createElement method with null parameter will create empty DOMText object as child of created element
59
        $element = $value === null ? $this->dom->createElement($name) : $this->dom->createElement($name, $value);
60
61
        return new DOMElementWrapper($element);
62
    }
63
64
    public function createElementNS($namespaceURI, $qualifiedName, $value = null)
65
    {
66
        return $this->dom->createElementNS($namespaceURI, $qualifiedName, $value);
67
    }
68
69
    public static function isLoaded()
70
    {
71
        return extension_loaded('dom');
72
    }
73
74
    public function saveHTML()
75
    {
76
        return $this->dom->saveHTML();
77
    }
78
79
    public function getElement()
80
    {
81
        return $this->dom->documentElement;
82
    }
83
84
    /**
85
     * @param $value
86
     */
87
    public function setNodeValue($value)
88
    {
89
        throw new NotImplementedException();
90
    }
91
92
    /**
93
     * @inheritdoc
94
     */
95
    public function removeNode(XMLDocumentInterface $child)
96
    {
97
        return $this->dom->documentElement->removeChild($child->getElement());
98
    }
99
}