1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Class XmlResponse | XmlResponse.php |
4
|
|
|
* @package Faulancer\Http |
5
|
|
|
* @author Florian Knapp <[email protected]> |
6
|
|
|
*/ |
7
|
|
|
namespace Faulancer\Http; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class XmlResponse |
11
|
|
|
*/ |
12
|
|
|
class XmlResponse extends Response |
13
|
|
|
{ |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* XmlResponse constructor. |
17
|
|
|
* |
18
|
|
|
* @param array $content |
19
|
|
|
*/ |
20
|
|
|
public function __construct ($content = []) |
21
|
|
|
{ |
22
|
|
|
parent::__construct($content); |
23
|
|
|
$this->setContent($content); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** @var array */ |
27
|
|
|
protected $content = []; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @param $dom |
31
|
|
|
* @param $data |
32
|
|
|
* @return bool|\DOMElement |
33
|
|
|
*/ |
34
|
|
|
private function generateXmlElement(\DOMDocument $dom, $data ) |
35
|
|
|
{ |
36
|
|
|
if (empty($data['name'])) { |
37
|
|
|
return false; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
// Create the element |
41
|
|
|
$elementValue = (!empty($data['value'])) ? $data['value'] : null; |
42
|
|
|
$element = $dom->createElement($data['name'], $elementValue); |
43
|
|
|
|
44
|
|
|
// Add any attributes |
45
|
|
|
if (!empty($data['attributes']) && is_array($data['attributes'])) { |
46
|
|
|
|
47
|
|
|
foreach ($data['attributes'] as $attributeKey => $attributeValue) { |
48
|
|
|
$element->setAttribute($attributeKey, $attributeValue); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
// Any other items in the data array should be child elements |
54
|
|
|
foreach ($data as $data_key => $childData) { |
55
|
|
|
|
56
|
|
|
if (!is_numeric($data_key)) { |
57
|
|
|
continue; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
$child = $this->generateXmlElement($dom, $childData); |
61
|
|
|
|
62
|
|
|
if ($child) { |
63
|
|
|
$element->appendChild($child); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
return $element; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param array $content |
73
|
|
|
* @return self |
74
|
|
|
*/ |
75
|
|
|
public function setContent($content = []) |
76
|
|
|
{ |
77
|
|
|
$this->setResponseHeader(['Content-Type' => 'text/xml']); |
78
|
|
|
|
79
|
|
|
$doc = new \DOMDocument(); |
80
|
|
|
$doc->xmlVersion = '1.0'; |
81
|
|
|
$doc->encoding = 'UTF-8'; |
82
|
|
|
$child = $this->generateXmlElement($doc, $content); |
83
|
|
|
|
84
|
|
|
if ( $child ) { |
85
|
|
|
$doc->appendChild($child); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
$doc->formatOutput = true; // Add line breaks to make it easier to read |
89
|
|
|
$xml = $doc->saveXML(); |
90
|
|
|
|
91
|
|
|
$this->content = trim($xml); |
|
|
|
|
92
|
|
|
return $this; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
} |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..