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
|
|
|
} |