@@ -121,9 +121,9 @@ public function testScalarValues(): void
121
121
$ this ->xml (
122
122
<<<EOF
123
123
<response>
124
- <item key="0" >true</item>
125
- <item key="1" >false</item>
126
- <item key="2" >100.2</item>
124
+ <item>true</item>
125
+ <item>false</item>
126
+ <item>100.2</item>
127
127
</response>
128
128
EOF
129
129
),
@@ -143,20 +143,20 @@ public function testArrayValues(): void
143
143
$ this ->assertSame (
144
144
$ this ->xml (
145
145
<<<EOF
146
- <response>
147
- <item key="0" >
148
- <item key="100" />
149
- <item key="200" />
150
- <item key="300" />
151
- </item>
152
- <item key="1" >
153
- <item key="0" >1</item>
154
- <item key="1" >1.1</item>
155
- <foo>bar</foo>
156
- <item key="2" >true</item>
157
- <item key="3" >false</item>
158
- </item>
159
- </response>
146
+ <response>
147
+ <item>
148
+ <item/>
149
+ <item/>
150
+ <item/>
151
+ </item>
152
+ <item>
153
+ <item>1</item>
154
+ <item>1.1</item>
155
+ <foo>bar</foo>
156
+ <item>true</item>
157
+ <item>false</item>
158
+ </item>
159
+ </response>
160
160
EOF
161
161
),
162
162
$ result ->getBody ()->getContents ()
@@ -171,6 +171,11 @@ public function xmlTagName(): string
171
171
return 'empty ' ;
172
172
}
173
173
174
+ public function xmlTagAttributes (): array
175
+ {
176
+ return [];
177
+ }
178
+
174
179
public function xmlData (): array
175
180
{
176
181
return [];
@@ -180,7 +185,7 @@ public function xmlData(): array
180
185
$ result ->getBody ()->rewind ();
181
186
182
187
$ this ->assertSame (
183
- $ this ->xml ('<response><object>< empty/></object ></response> ' ),
188
+ $ this ->xml ('<response><empty/></response> ' ),
184
189
$ result ->getBody ()->getContents ()
185
190
);
186
191
}
@@ -201,7 +206,7 @@ public function testObjectValues(): void
201
206
<int> $ object ->int </int>
202
207
<float> $ object ->float </float>
203
208
<array>
204
- <item key="0" >1</item>
209
+ <item>1</item>
205
210
<foo>bar</foo>
206
211
</array>
207
212
</dummy-class>
@@ -236,26 +241,26 @@ public function testArrayObjectValues(): void
236
241
$ this ->xml (
237
242
<<<EOF
238
243
<response>
239
- <dummy-class key="0" >
244
+ <dummy-class>
240
245
<string> $ object1 ->string </string>
241
246
<int> $ object1 ->int </int>
242
247
<float> $ object1 ->float </float>
243
248
<array>
244
- <item key="0" >foo</item>
245
- <item key="1" >1.1</item>
249
+ <item>foo</item>
250
+ <item>1.1</item>
246
251
</array>
247
252
</dummy-class>
248
- <dummy-class key="1" >
253
+ <dummy-class>
249
254
<string> $ object2 ->string </string>
250
255
<int> $ object2 ->int </int>
251
256
<float> $ object2 ->float </float>
252
257
<array>
253
- <item key="0" >1</item>
254
- <item key="1" >2</item>
255
- <item key="2" >3</item>
258
+ <item>1</item>
259
+ <item>2</item>
260
+ <item>3</item>
256
261
</array>
257
262
</dummy-class>
258
- <dummy-class key="2" >
263
+ <dummy-class>
259
264
<string> $ object3 ->string </string>
260
265
<int> $ object3 ->int </int>
261
266
<float> $ object3 ->float </float>
@@ -268,6 +273,52 @@ public function testArrayObjectValues(): void
268
273
);
269
274
}
270
275
276
+ public function testObjectValuesWithAttributes (): void
277
+ {
278
+ $ objects = [
279
+ $ object1 = $ this ->createDummyObject ('foo ' , 99 , 1.1 , [
280
+ $ object2 = $ this ->createDummyObject ('bar ' , 10 , 2.2 , [1 , 2 , 3 ], ['attribute ' => '25 ' ]),
281
+ $ object3 = $ this ->createDummyObject ('baz ' , 0 , 3.3 , ['bar ' => 'baz ' ], ['attribute ' => '' ]),
282
+ ], ['attribute ' => '22 ' ]),
283
+ ];
284
+ $ dataResponse = $ this ->createResponse ($ objects );
285
+ $ result = (new XmlDataResponseFormatter ())->format ($ dataResponse );
286
+ $ result ->getBody ()->rewind ();
287
+
288
+ $ this ->assertSame (
289
+ $ this ->xml (
290
+ <<<EOF
291
+ <response>
292
+ <dummy-class attribute="22">
293
+ <string> $ object1 ->string </string>
294
+ <int> $ object1 ->int </int>
295
+ <float> $ object1 ->float </float>
296
+ <array>
297
+ <dummy-class attribute="25">
298
+ <string> $ object2 ->string </string>
299
+ <int> $ object2 ->int </int>
300
+ <float> $ object2 ->float </float>
301
+ <array>
302
+ <item>1</item>
303
+ <item>2</item>
304
+ <item>3</item>
305
+ </array>
306
+ </dummy-class>
307
+ <dummy-class attribute="">
308
+ <string> $ object3 ->string </string>
309
+ <int> $ object3 ->int </int>
310
+ <float> $ object3 ->float </float>
311
+ <array><bar>baz</bar></array>
312
+ </dummy-class>
313
+ </array>
314
+ </dummy-class>
315
+ </response>
316
+ EOF
317
+ ),
318
+ $ result ->getBody ()->getContents ()
319
+ );
320
+ }
321
+
271
322
public function testNestedAndMixedValues (): void
272
323
{
273
324
$ dataResponse = $ this ->createResponse ([
@@ -291,7 +342,7 @@ public function testNestedAndMixedValues(): void
291
342
$ this ->xml (
292
343
<<<EOF
293
344
<response>
294
- <dummy-class key="0" >
345
+ <dummy-class>
295
346
<string> $ object1 ->string </string>
296
347
<int> $ object1 ->int </int>
297
348
<float> $ object1 ->float </float>
@@ -302,25 +353,25 @@ public function testNestedAndMixedValues(): void
302
353
<float> $ object2 ->float </float>
303
354
<array>
304
355
<foo>bar</foo>
305
- <item key="0" >1.1</item>
306
- <dummy-class key="1" >
356
+ <item>1.1</item>
357
+ <dummy-class>
307
358
<string> $ object3 ->string </string>
308
359
<int> $ object3 ->int </int>
309
360
<float> $ object3 ->float </float>
310
361
<array>
311
- <item key="0" >true</item>
312
- <item key="1" >false</item>
362
+ <item>true</item>
363
+ <item>false</item>
313
364
</array>
314
365
</dummy-class>
315
366
</array>
316
367
</dummy-class>
317
368
</array>
318
369
</dummy-class>
319
- <item key="1" >true</item>
370
+ <item>true</item>
320
371
<foo>bar</foo>
321
- <item key="2" >false</item>
322
- <item key="3" >11</item>
323
- <item key="4" >baz</item>
372
+ <item>false</item>
373
+ <item>11</item>
374
+ <item>baz</item>
324
375
</response>
325
376
EOF
326
377
),
@@ -342,7 +393,7 @@ public function testItemTagWhenNameIsEmptyOrInvalid(): void
342
393
$ this ->xml (
343
394
<<<EOF
344
395
<response>
345
- <item key="0" >test</item>
396
+ <item>test</item>
346
397
<validName>test</validName>
347
398
<item>test</item>
348
399
</response>
@@ -360,30 +411,37 @@ private function createResponse($data): DataResponse
360
411
private function xml (string $ data , string $ version = '1.0 ' , string $ encoding = 'UTF-8 ' ): string
361
412
{
362
413
$ startLine = sprintf ('<?xml version="%s" encoding="%s"?> ' , $ version , $ encoding );
363
- return $ startLine . "\n" . preg_replace ('/(?!item)\s(?!key )/ ' , '' , $ data ) . "\n" ;
414
+ return $ startLine . "\n" . preg_replace ('/(?!item)\s(?!attribute )/ ' , '' , $ data ) . "\n" ;
364
415
}
365
416
366
- private function createDummyObject (string $ string , int $ int , float $ float , array $ array ): object
417
+ private function createDummyObject (string $ string , int $ int , float $ float , array $ array, array $ attrs = [] ): object
367
418
{
368
- return new class ($ string , $ int , $ float , $ array ) implements XmlDataInterface {
419
+ return new class ($ string , $ int , $ float , $ array, $ attrs ) implements XmlDataInterface {
369
420
public string $ string ;
370
421
public int $ int ;
371
422
public float $ float ;
372
423
public array $ array ;
424
+ public array $ attrs ;
373
425
374
- public function __construct (string $ string , int $ int , float $ float , array $ array )
426
+ public function __construct (string $ string , int $ int , float $ float , array $ array, array $ attrs = [] )
375
427
{
376
428
$ this ->string = $ string ;
377
429
$ this ->int = $ int ;
378
430
$ this ->float = $ float ;
379
431
$ this ->array = $ array ;
432
+ $ this ->attrs = $ attrs ;
380
433
}
381
434
382
435
public function xmlTagName (): string
383
436
{
384
437
return 'dummy-class ' ;
385
438
}
386
439
440
+ public function xmlTagAttributes (): array
441
+ {
442
+ return $ this ->attrs ;
443
+ }
444
+
387
445
public function xmlData (): array
388
446
{
389
447
return [
0 commit comments