1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @package toolkit |
5
|
|
|
*/ |
6
|
|
|
/** |
7
|
|
|
* `XMLDocument` is a class used to simulate PHP's `DOMDocument` |
8
|
|
|
* class. Each object is a representation of a XML document |
9
|
|
|
* and can store it's children in an array. When an `XMLElement` |
10
|
|
|
* is generated, it is output as an XML string. |
11
|
|
|
* The `XMLDocument` extends `XMLElement` and adds properties the are specific |
12
|
|
|
* to documents. |
13
|
|
|
* |
14
|
|
|
* @since Symphony 3.0.0 |
15
|
|
|
*/ |
16
|
|
|
|
17
|
|
|
class XMLDocument extends XMLElement |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* Any processing instructions that the XSLT should know about when a |
21
|
|
|
* `XMLElement` is generated |
22
|
|
|
* @var array |
23
|
|
|
*/ |
24
|
|
|
private $processingInstructions = []; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* The DTD the should be output when a `XMLElement` is generated, defaults to null. |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
private $dtd = null; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* The encoding of the `XMLElement`, defaults to 'utf-8' |
34
|
|
|
* @var string |
35
|
|
|
*/ |
36
|
|
|
private $encoding = 'utf-8'; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* The version of the XML that is used for generation, defaults to '1.0' |
40
|
|
|
* @var string |
41
|
|
|
*/ |
42
|
|
|
private $version = '1.0'; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* When set to true this will include the XML declaration will be |
46
|
|
|
* output when the `XMLElement` is generated. Defaults to `false`. |
47
|
|
|
* @var boolean |
48
|
|
|
*/ |
49
|
|
|
private $includeHeader = false; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Accessor for `$encoding` |
53
|
|
|
* |
54
|
|
|
* @return string |
55
|
|
|
*/ |
56
|
|
|
public function getEncoding() |
57
|
|
|
{ |
58
|
|
|
return $this->encoding; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Accessor for `$version` |
63
|
|
|
* |
64
|
|
|
* @return string |
65
|
|
|
*/ |
66
|
|
|
public function getVersion() |
67
|
|
|
{ |
68
|
|
|
return $this->version; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Adds processing instructions to this `XMLElement` |
73
|
|
|
* |
74
|
|
|
* @param string $pi |
75
|
|
|
* @return XMLElement |
76
|
|
|
* The current instance |
77
|
|
|
*/ |
78
|
|
|
public function addProcessingInstruction($pi) |
79
|
|
|
{ |
80
|
|
|
$this->processingInstructions[] = $pi; |
81
|
|
|
return $this; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Sets the DTD for this `XMLElement` |
86
|
|
|
* |
87
|
|
|
* @param string $dtd |
88
|
|
|
* @return XMLElement |
89
|
|
|
* The current instance |
90
|
|
|
*/ |
91
|
|
|
public function setDTD($dtd) |
92
|
|
|
{ |
93
|
|
|
$this->dtd = $dtd; |
94
|
|
|
return $this; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Sets the encoding for this `XMLElement` for when |
99
|
|
|
* it's generated. |
100
|
|
|
* |
101
|
|
|
* @param string $value |
102
|
|
|
* @return XMLElement |
103
|
|
|
* The current instance |
104
|
|
|
*/ |
105
|
|
|
public function setEncoding($value) |
106
|
|
|
{ |
107
|
|
|
$this->encoding = $value; |
108
|
|
|
return $this; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Sets the version for the XML declaration of this |
113
|
|
|
* `XMLElement` |
114
|
|
|
* |
115
|
|
|
* @param string $value |
116
|
|
|
* @return XMLElement |
117
|
|
|
* The current instance |
118
|
|
|
*/ |
119
|
|
|
public function setVersion($value) |
120
|
|
|
{ |
121
|
|
|
$this->version = $value; |
122
|
|
|
return $this; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Sets whether this `XMLElement` needs to output an |
127
|
|
|
* XML declaration or not. This normally is only set to |
128
|
|
|
* true for the parent `XMLElement`, eg. 'html'. |
129
|
|
|
* |
130
|
|
|
* @param bool $value |
131
|
|
|
* @return XMLElement |
132
|
|
|
* The current instance |
133
|
|
|
*/ |
134
|
|
|
public function setIncludeHeader($value) |
135
|
|
|
{ |
136
|
|
|
$this->includeHeader = $value; |
137
|
|
|
return $this; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Makes this `XMLElement` output an XML declaration. |
142
|
|
|
* |
143
|
|
|
* @since Symphony 3.0.0 |
144
|
|
|
* @uses setIncludeHeader() |
145
|
|
|
* @return XMLElement |
146
|
|
|
* The current instance |
147
|
|
|
*/ |
148
|
|
|
public function renderHeader() |
149
|
|
|
{ |
150
|
|
|
return $this->setIncludeHeader(true); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* This function will turn the `XMLDocument` into a string |
155
|
|
|
* representing the document as it would appear in the markup. |
156
|
|
|
* The result is valid XML. |
157
|
|
|
* |
158
|
|
|
* @see XMLElement::generate() |
159
|
|
|
* @param boolean $indent |
160
|
|
|
* Defaults to false |
161
|
|
|
* @param integer $tabDepth |
162
|
|
|
* Defaults to 0, indicates the number of tabs (\t) that this |
163
|
|
|
* element should be indented by in the output string |
164
|
|
|
* @return string |
165
|
|
|
* The XML string |
166
|
|
|
*/ |
167
|
|
|
public function generate($indent = false, $tabDepth = 0) |
168
|
|
|
{ |
169
|
|
|
$result = null; |
170
|
|
|
$newline = ($indent ? PHP_EOL : null); |
171
|
|
|
|
172
|
|
|
if ($this->includeHeader) { |
173
|
|
|
$result .= sprintf( |
174
|
|
|
'<?xml version="%s" encoding="%s" ?>%s', |
175
|
|
|
$this->version, |
176
|
|
|
$this->encoding, |
177
|
|
|
$newline |
178
|
|
|
); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
if ($this->dtd) { |
182
|
|
|
$result .= $this->dtd . $newline; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
if (!empty($this->processingInstructions)) { |
186
|
|
|
$result .= implode($newline, $this->processingInstructions); |
|
|
|
|
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
return $result . parent::generate($indent, $tabDepth); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* Given a `DOMDocument`, this function will create a new `XMLDocument` |
194
|
|
|
* object, copy all attributes and children and return the result. |
195
|
|
|
* |
196
|
|
|
* @param DOMDocument $doc |
197
|
|
|
* A DOMDocument to copy from |
198
|
|
|
* @return XMLDocument |
199
|
|
|
* The new `XMLDocument` derived from `DOMDocument $doc`. |
200
|
|
|
*/ |
201
|
|
|
public static function fromDOMDocument(DOMDocument $doc) |
202
|
|
|
{ |
203
|
|
|
$root = new XMLDocument($doc->documentElement->nodeName); |
204
|
|
|
static::copyDOMNode($root, $doc->documentElement); |
205
|
|
|
return $root; |
206
|
|
|
} |
207
|
|
|
} |
208
|
|
|
|