9
9
use DOMException ;
10
10
use DOMText ;
11
11
use Psr \Http \Message \ResponseInterface ;
12
- use Traversable ;
12
+ use RuntimeException ;
13
13
use Yiisoft \DataResponse \ResponseContentTrait ;
14
14
use Yiisoft \Strings \NumericHelper ;
15
- use Yiisoft \Strings \StringHelper ;
16
15
use Yiisoft \DataResponse \DataResponse ;
17
16
use Yiisoft \DataResponse \DataResponseFormatterInterface ;
18
17
21
20
use function is_float ;
22
21
use function is_int ;
23
22
use function is_object ;
24
- use function iterator_to_array ;
25
- use function strpos ;
23
+ use function sprintf ;
26
24
27
25
final class XmlDataResponseFormatter implements DataResponseFormatterInterface
28
26
{
@@ -51,12 +49,6 @@ final class XmlDataResponseFormatter implements DataResponseFormatterInterface
51
49
*/
52
50
private string $ rootTag = 'response ' ;
53
51
54
- /**
55
- * @var bool If true, the object tags will be formed from the class names,
56
- * otherwise the {@see DEFAULT_ITEM_TAG_NAME} value will be used.
57
- */
58
- private bool $ useObjectTags = true ;
59
-
60
52
public function format (DataResponse $ dataResponse ): ResponseInterface
61
53
{
62
54
if ($ dataResponse ->hasData ()) {
@@ -106,21 +98,6 @@ public function withRootTag(string $rootTag): self
106
98
return $ new ;
107
99
}
108
100
109
- /**
110
- * Returns a new instance with the specified value, whether to use class names as tags or not.
111
- *
112
- * @param bool $useObjectTags If true, the object tags will be formed from the class names,
113
- * otherwise the {@see DEFAULT_ITEM_TAG_NAME} value will be used. Default is true.
114
- *
115
- * @return self
116
- */
117
- public function withUseObjectTags (bool $ useObjectTags ): self
118
- {
119
- $ new = clone $ this ;
120
- $ new ->useObjectTags = $ useObjectTags ;
121
- return $ new ;
122
- }
123
-
124
101
/**
125
102
* Builds the data to use in XML.
126
103
*
@@ -134,12 +111,11 @@ private function buildXml(DOMDocument $dom, $element, $data): void
134
111
return ;
135
112
}
136
113
137
- if (is_array ($ data ) || $ data instanceof Traversable) {
138
- $ data = $ data instanceof Traversable ? iterator_to_array ($ data ) : $ data ;
114
+ if (is_array ($ data )) {
139
115
$ dataSize = count ($ data );
140
116
141
117
foreach ($ data as $ name => $ value ) {
142
- if (is_int ($ name ) && is_object ($ value ) && !( $ value instanceof Traversable) ) {
118
+ if (is_int ($ name ) && is_object ($ value )) {
143
119
$ this ->buildObject ($ dom , $ element , $ value , $ dataSize > 1 ? $ name : null );
144
120
continue ;
145
121
}
@@ -181,25 +157,22 @@ private function buildXml(DOMDocument $dom, $element, $data): void
181
157
*/
182
158
private function buildObject (DOMDocument $ dom , $ element , object $ object , int $ key = null ): void
183
159
{
184
- if ($ this ->useObjectTags ) {
185
- $ class = get_class ($ object );
186
- $ class = strpos ($ class , 'class@anonymous ' ) === false ? StringHelper::baseName ($ class ) : 'AnonymousClass ' ;
160
+ if (!($ object instanceof XmlDataInterface)) {
161
+ throw new RuntimeException (sprintf (
162
+ 'The "%s" object must implement the "%s" interface. ' ,
163
+ get_class ($ object ),
164
+ XmlDataInterface::class,
165
+ ));
187
166
}
188
167
189
- $ child = $ this ->safeCreateDomElement ($ dom , $ class ?? self :: DEFAULT_ITEM_TAG_NAME );
168
+ $ child = $ this ->safeCreateDomElement ($ dom , $ object -> xmlTagName () );
190
169
191
170
if ($ key !== null ) {
192
171
$ child ->setAttribute (self ::KEY_ATTRIBUTE_NAME , (string ) $ key );
193
172
}
194
173
195
174
$ element ->appendChild ($ child );
196
- $ array = [];
197
-
198
- foreach ($ object as $ property => $ value ) {
199
- $ array [$ property ] = $ value ;
200
- }
201
-
202
- $ this ->buildXml ($ dom , $ child , $ array );
175
+ $ this ->buildXml ($ dom , $ child , $ object ->xmlData ());
203
176
}
204
177
205
178
/**
0 commit comments