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