1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Yiisoft\Yii\Web\Formatter; |
4
|
|
|
|
5
|
|
|
use DOMDocument; |
6
|
|
|
use DOMElement; |
7
|
|
|
use DOMException; |
8
|
|
|
use DOMText; |
9
|
|
|
use Psr\Http\Message\ResponseInterface; |
10
|
|
|
use Yiisoft\Strings\StringHelper; |
11
|
|
|
use Yiisoft\Yii\Web\WebResponse; |
12
|
|
|
|
13
|
|
|
final class XmlResponseFormatter implements ResponseFormatterInterface |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var string the Content-Type header for the response |
17
|
|
|
*/ |
18
|
|
|
private string $contentType = 'application/xml'; |
19
|
|
|
/** |
20
|
|
|
* @var string the XML version |
21
|
|
|
*/ |
22
|
|
|
private string $version = '1.0'; |
23
|
|
|
/** |
24
|
|
|
* @var string the XML encoding. |
25
|
|
|
*/ |
26
|
|
|
private string $encoding = 'UTF-8'; |
27
|
|
|
/** |
28
|
|
|
* @var string the name of the root element. If set to false, null or is empty then no root tag should be added. |
29
|
|
|
*/ |
30
|
|
|
private string $rootTag = 'response'; |
31
|
|
|
/** |
32
|
|
|
* @var string the name of the elements that represent the array elements with numeric keys. |
33
|
|
|
*/ |
34
|
|
|
private string $itemTag = 'item'; |
35
|
|
|
/** |
36
|
|
|
* @var bool whether to interpret objects implementing the [[\Traversable]] interface as arrays. |
37
|
|
|
* Defaults to `true`. |
38
|
|
|
*/ |
39
|
|
|
private bool $useTraversableAsArray = true; |
40
|
|
|
/** |
41
|
|
|
* @var bool if object tags should be added |
42
|
|
|
*/ |
43
|
|
|
private bool $useObjectTags = true; |
44
|
|
|
|
45
|
|
|
public function format(WebResponse $webResponse): ResponseInterface |
46
|
|
|
{ |
47
|
|
|
$content = ''; |
48
|
|
|
$data = $webResponse->getData(); |
49
|
|
|
if ($data !== null) { |
50
|
|
|
$dom = new DOMDocument($this->version, $this->encoding); |
51
|
|
|
if (!empty($this->rootTag)) { |
52
|
|
|
$root = new DOMElement($this->rootTag); |
|
|
|
|
53
|
|
|
$dom->appendChild($root); |
54
|
|
|
$this->buildXml($root, $data); |
55
|
|
|
} else { |
56
|
|
|
$this->buildXml($dom, $data); |
|
|
|
|
57
|
|
|
} |
58
|
|
|
$content = $dom->saveXML(); |
59
|
|
|
} |
60
|
|
|
$response = $webResponse->getResponse(); |
61
|
|
|
$response->getBody()->write($content); |
62
|
|
|
|
63
|
|
|
return $response->withHeader('Content-Type', $this->contentType . '; ' . $this->encoding); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function setVersion(string $version): void |
67
|
|
|
{ |
68
|
|
|
$this->version = $version; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function setEncoding(string $encoding): void |
72
|
|
|
{ |
73
|
|
|
$this->encoding = $encoding; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function setRootTag(string $rootTag): void |
77
|
|
|
{ |
78
|
|
|
$this->rootTag = $rootTag; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public function setItemTag(string $itemTag): void |
82
|
|
|
{ |
83
|
|
|
$this->itemTag = $itemTag; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public function setUseTraversableAsArray(bool $useTraversableAsArray): void |
87
|
|
|
{ |
88
|
|
|
$this->useTraversableAsArray = $useTraversableAsArray; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function setUseObjectTags(bool $useObjectTags): void |
92
|
|
|
{ |
93
|
|
|
$this->useObjectTags = $useObjectTags; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @param DOMElement $element |
98
|
|
|
* @param mixed $data |
99
|
|
|
*/ |
100
|
|
|
protected function buildXml($element, $data): void |
101
|
|
|
{ |
102
|
|
|
if (is_array($data) || |
103
|
|
|
($data instanceof \Traversable && $this->useTraversableAsArray) |
104
|
|
|
) { |
105
|
|
|
foreach ($data as $name => $value) { |
106
|
|
|
if (is_int($name) && is_object($value)) { |
107
|
|
|
$this->buildXml($element, $value); |
108
|
|
|
} elseif (is_array($value) || is_object($value)) { |
109
|
|
|
$child = new DOMElement($this->getValidXmlElementName($name)); |
|
|
|
|
110
|
|
|
$element->appendChild($child); |
111
|
|
|
$this->buildXml($child, $value); |
112
|
|
|
} else { |
113
|
|
|
$child = new DOMElement($this->getValidXmlElementName($name)); |
114
|
|
|
$element->appendChild($child); |
115
|
|
|
$child->appendChild(new DOMText($this->formatScalarValue($value))); |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
} elseif (is_object($data)) { |
119
|
|
|
if ($this->useObjectTags) { |
120
|
|
|
$child = new DOMElement(StringHelper::basename(get_class($data))); |
121
|
|
|
$element->appendChild($child); |
122
|
|
|
} else { |
123
|
|
|
$child = $element; |
124
|
|
|
} |
125
|
|
|
$array = []; |
126
|
|
|
foreach ($data as $name => $value) { |
127
|
|
|
$array[$name] = $value; |
128
|
|
|
} |
129
|
|
|
$this->buildXml($child, $array); |
130
|
|
|
} else { |
131
|
|
|
$element->appendChild(new DOMText($this->formatScalarValue($data))); |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Formats scalar value to use in XML text node. |
137
|
|
|
* |
138
|
|
|
* @param int|string|bool|float $value a scalar value. |
139
|
|
|
* @return string string representation of the value. |
140
|
|
|
*/ |
141
|
|
|
protected function formatScalarValue($value): string |
142
|
|
|
{ |
143
|
|
|
if ($value === true) { |
144
|
|
|
return 'true'; |
145
|
|
|
} |
146
|
|
|
if ($value === false) { |
147
|
|
|
return 'false'; |
148
|
|
|
} |
149
|
|
|
if (is_float($value)) { |
150
|
|
|
return StringHelper::floatToString($value); |
151
|
|
|
} |
152
|
|
|
return (string)$value; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Returns element name ready to be used in DOMElement if |
157
|
|
|
* name is not empty, is not int and is valid. |
158
|
|
|
* |
159
|
|
|
* Falls back to [[itemTag]] otherwise. |
160
|
|
|
* |
161
|
|
|
* @param mixed $name |
162
|
|
|
* @return string |
163
|
|
|
*/ |
164
|
|
|
protected function getValidXmlElementName($name): string |
165
|
|
|
{ |
166
|
|
|
if (empty($name) || is_int($name) || !$this->isValidXmlName($name)) { |
167
|
|
|
return $this->itemTag; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
return $name; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* Checks if name is valid to be used in XML. |
175
|
|
|
* |
176
|
|
|
* @param mixed $name |
177
|
|
|
* @return bool |
178
|
|
|
* @see http://stackoverflow.com/questions/2519845/how-to-check-if-string-is-a-valid-xml-element-name/2519943#2519943 |
179
|
|
|
*/ |
180
|
|
|
protected function isValidXmlName($name): bool |
181
|
|
|
{ |
182
|
|
|
try { |
183
|
|
|
new DOMElement($name); |
|
|
|
|
184
|
|
|
return true; |
185
|
|
|
} catch (DOMException $e) { |
186
|
|
|
return false; |
187
|
|
|
} |
188
|
|
|
} |
189
|
|
|
} |
190
|
|
|
|
This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.