1 | <?php |
||
26 | class XmlResponseFormatter extends Component implements ResponseFormatterInterface |
||
27 | { |
||
28 | /** |
||
29 | * @var string the Content-Type header for the response |
||
30 | */ |
||
31 | public $contentType = 'application/xml'; |
||
32 | /** |
||
33 | * @var string the XML version |
||
34 | */ |
||
35 | public $version = '1.0'; |
||
36 | /** |
||
37 | * @var string the XML encoding. If not set, it will use the value of [[Response::charset]]. |
||
38 | */ |
||
39 | public $encoding; |
||
40 | /** |
||
41 | * @var string the name of the root element. If set to false, null or is empty then no root tag should be added. |
||
42 | */ |
||
43 | public $rootTag = 'response'; |
||
44 | /** |
||
45 | * @var string the name of the elements that represent the array elements with numeric keys. |
||
46 | */ |
||
47 | public $itemTag = 'item'; |
||
48 | /** |
||
49 | * @var bool whether to interpret objects implementing the [[\Traversable]] interface as arrays. |
||
50 | * Defaults to `true`. |
||
51 | * @since 2.0.7 |
||
52 | */ |
||
53 | public $useTraversableAsArray = true; |
||
54 | /** |
||
55 | * @var bool if object tags should be added |
||
56 | * @since 2.0.11 |
||
57 | */ |
||
58 | public $useObjectTags = true; |
||
59 | |||
60 | |||
61 | /** |
||
62 | * Formats the specified response. |
||
63 | * @param Response $response the response to be formatted. |
||
64 | */ |
||
65 | 20 | public function format($response) |
|
84 | |||
85 | /** |
||
86 | * @param DOMElement $element |
||
87 | * @param mixed $data |
||
88 | */ |
||
89 | 19 | protected function buildXml($element, $data) |
|
90 | { |
||
91 | 19 | if (is_array($data) || |
|
92 | 19 | ($data instanceof \Traversable && $this->useTraversableAsArray && !$data instanceof Arrayable) |
|
93 | ) { |
||
94 | 12 | foreach ($data as $name => $value) { |
|
95 | 11 | if (is_int($name) && is_object($value)) { |
|
96 | 3 | $this->buildXml($element, $value); |
|
97 | 11 | } elseif ((is_array($value) && !isset($value['xml-attributes'])) || is_object($value)) { |
|
98 | 3 | $child = new DOMElement($this->getValidXmlElementName($name)); |
|
99 | 3 | $element->appendChild($child); |
|
100 | 3 | $this->buildXml($child, $value); |
|
101 | } else { |
||
102 | 11 | $child = new DOMElement($this->getValidXmlElementName($name)); |
|
103 | 11 | $element->appendChild($child); |
|
104 | 11 | if(is_array($value) && isset($value['xml-attributes'])){ |
|
105 | 2 | foreach($value['xml-attributes'] as $attribute => $val){ |
|
106 | 2 | $child->setAttribute($attribute, $val); |
|
107 | } |
||
108 | 2 | $child->appendChild(new DOMText($this->formatScalarValue($value['value']))); |
|
109 | }else{ |
||
110 | 12 | $child->appendChild(new DOMText($this->formatScalarValue($value))); |
|
111 | } |
||
112 | } |
||
113 | } |
||
114 | 13 | } elseif (is_object($data)) { |
|
115 | 6 | if ($this->useObjectTags) { |
|
116 | 5 | $child = new DOMElement(StringHelper::basename(get_class($data))); |
|
117 | 5 | $element->appendChild($child); |
|
118 | } else { |
||
119 | 1 | $child = $element; |
|
120 | } |
||
121 | 6 | if ($data instanceof Arrayable) { |
|
122 | 1 | $this->buildXml($child, $data->toArray()); |
|
123 | } else { |
||
124 | 5 | $array = []; |
|
125 | 5 | foreach ($data as $name => $value) { |
|
126 | 5 | $array[$name] = $value; |
|
127 | } |
||
128 | 6 | $this->buildXml($child, $array); |
|
129 | } |
||
130 | } else { |
||
131 | 7 | $element->appendChild(new DOMText($this->formatScalarValue($data))); |
|
132 | } |
||
133 | 19 | } |
|
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 | * @since 2.0.11 |
||
141 | */ |
||
142 | 18 | protected function formatScalarValue($value) |
|
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 | * @since 2.0.12 |
||
165 | */ |
||
166 | 11 | protected function getValidXmlElementName($name) |
|
174 | |||
175 | /** |
||
176 | * Checks if name is valid to be used in XML. |
||
177 | * |
||
178 | * @param mixed $name |
||
179 | * @return bool |
||
180 | * @see http://stackoverflow.com/questions/2519845/how-to-check-if-string-is-a-valid-xml-element-name/2519943#2519943 |
||
181 | * @since 2.0.12 |
||
182 | */ |
||
183 | 9 | protected function isValidXmlName($name) |
|
192 | } |
||
193 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: