-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathXmlResponse.php
executable file
·281 lines (233 loc) · 7.42 KB
/
XmlResponse.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
<?php
namespace SymfonyXmlResponse\Responses;
/**
* Simple XML response writer in the Symfony Response model.
*
* For the origin of much of this code, @see http://php.net/manual/en/ref.xmlwriter.php. Proper credit should go
* to massimo71, Alexandre Arica and others.
*
* @author Michal Carson <[email protected]>
*/
use Symfony\Component\HttpFoundation\Response;
class XmlResponse extends Response
{
protected $data = '';
/**
* instance of the built-in PHP XMLWriter.
* @var \XMLWriter
*/
protected $xml_writer;
/**
* Name for the root element of the XML document.
* @var string
*/
public $root_element_name = 'document';
/**
* File path for XSL Transform file. Will be included in XML header if set.
* @var string
*/
public $xslt_file_path = '';
/**
* Array of objects that implement the XmlDecoratorInterface, each of which
* will be run as the content is rendered.
* @var array
*/
protected $decorators = array();
/**
* Constructor.
*
* @param mixed $data The response data
* @param int $status The response status code
* @param array $headers An array of response headers
*/
public function __construct($data = null, $status = 200, $headers = array())
{
parent::__construct('', $status, $headers);
if (null === $data) {
$data = new \ArrayObject();
}
$this->xml_writer = new \XMLWriter();
if (!is_null($data)) {
$this->setData($data);
}
}
/**
* {@inheritdoc}
*/
public static function create($data = null, $status = 200, $headers = array())
{
return new static($data, $status, $headers);
}
/**
* Sets the data to be sent as XML.
*
* @param mixed $data
*
* @return XmlResponse
*
* @throws \InvalidArgumentException
*/
public function setData($data = array())
{
try {
$this->startDocument($this->root_element_name, $this->xslt_file_path);
$this->fromArray($data, $this->root_element_name);
$this->data = $this->getDocument();
} catch (\Exception $exception) {
throw $exception;
}
return $this->update();
}
/**
* Updates the content and headers
*
* @return XmlResponse
*/
protected function update()
{
// Only set the header when there is none
// in order to not overwrite a custom definition.
if (!$this->headers->has('Content-Type')) {
$this->headers->set('Content-Type', 'application/xml');
}
return $this->setContent($this->data);
}
/**
* Constructor.
* @author Alexandre Arica
* @param string $prm_rootElementName A root element's name of a current xml document
* @param string $prm_xsltFilePath Path of a XSLT file.
* @access protected
* @param null
*/
protected function startDocument($prm_rootElementName, $prm_xsltFilePath = '')
{
$this->xml_writer->openMemory();
$this->xml_writer->setIndent(true);
$this->xml_writer->setIndentString(' ');
$this->xml_writer->startDocument('1.0', 'UTF-8');
if ($prm_xsltFilePath) {
$this->xml_writer->writePi('xml-stylesheet', 'type="text/xsl" href="' . $prm_xsltFilePath . '"');
}
$this->xml_writer->startElement($prm_rootElementName);
}
/**
* Set an element with a text to a current xml document.
* @author Alexandre Arica
* @access protected
* @param string $prm_elementName An element's name
* @param string $prm_ElementText An element's text
* @throws \InvalidArgumentException
* @return null
*/
protected function setElement($prm_elementName, $prm_ElementText)
{
if (!isset($prm_elementName)) {
throw new \InvalidArgumentException('Element name cannot be null. ' . var_export($prm_elementName, true));
}
if (preg_match('/[a-zA-Z]/', substr($prm_elementName, 0, 1)) !== 1) {
throw new \InvalidArgumentException(
'Element name must begin with alpha character. ' . var_export($prm_elementName, true)
);
}
$this->xml_writer->startElement($prm_elementName);
$this->xml_writer->text($prm_ElementText);
$this->xml_writer->endElement();
}
/**
* Construct elements and texts from an array.
* The array should contain an attribute's name in index part
* and a attribute's text in value part.
* @author Alexandre Arica
* @author massimo71
* @access protected
* @param array $prm_array Contains attributes and texts
* @param string $prm_name Name of the element described by this array
* @return null
*/
protected function fromArray($prm_array, $prm_name)
{
if (is_array($prm_array)) {
foreach ($prm_array as $index => $element) {
if (is_array($element)) {
$this->xml_writer->startElement($index);
$this->fromArray($element, $index);
$this->xml_writer->endElement();
} elseif (substr($index, 0, 1) == '@') {
$this->xml_writer->writeAttribute(substr($index, 1), $element);
} elseif ($index == $prm_name) {
$this->xml_writer->text($element);
} else {
$this->setElement($index, $element);
}
}
}
}
/**
* Return the content of a current xml document.
* @author Alexandre Arica
* @access protected
* @param null
* @return string Xml document
*/
protected function getDocument()
{
$this->xml_writer->endElement();
$this->xml_writer->endDocument();
return $this->xml_writer->outputMemory();
}
/**
* Return an XML fragment (rather than a fully formated XML file).
*
* @param string $name outermost element name
* @param mixed $content array or string content for the element
* @return string
*/
public function getFragment($name, $content)
{
$this->xml_writer->openMemory();
$this->xml_writer->setIndent(true);
$this->xml_writer->setIndentString(' ');
if (is_array($content)) {
$this->xml_writer->startElement($name);
$this->fromArray($content, $name);
$this->xml_writer->endElement();
} else {
$this->setElement($name, $content);
}
return $this->xml_writer->outputMemory();
}
/**
* Sends content for the current web response.
* Process all decorators before sending the content.
*
* @return Response
*/
public function sendContent()
{
foreach ($this->decorators as $decor) {
$this->content = $decor->run($this->content);
}
return parent::sendContent();
}
/**
* Add a decorator to the stack.
*
* @param XmlDecoratorInterface $decorator
* @return XmlResponse
*/
public function addDecorator(XmlDecoratorInterface $decorator)
{
$this->decorators[] = $decorator;
return $this;
}
/**
* Retrieve the decorator stack for inspection.
*
* @return array
*/
public function getDecorators()
{
return $this->decorators;
}
}