Skip to content

Commit a434578

Browse files
committed
Fixed an issue with empty arrays within arrays
1 parent aa804d5 commit a434578

File tree

3 files changed

+40
-2
lines changed

3 files changed

+40
-2
lines changed

src/Exporters/ArrayExporter.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,8 @@ private function walkArray(array $values, string $name, DOMDocument $document, D
106106
$element = $document->createElement($fieldName);
107107

108108
if (empty($value)) {
109-
$element = $document->createElement($name);
110-
$parent = $root->appendChild($element);
109+
$element = $document->createElement($fieldName);
110+
$parent = $rootElement->appendChild($element);
111111
} elseif (! Arr::isAssoc($value)) {
112112
if ($rootElement->parentNode === null) {
113113
$element = $document->createElement($name);

tests/Features/Export/ExportFromDataTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,4 +124,30 @@ public function test_exports_from_nested_string_array()
124124
->toString();
125125
$this->assertMatchesXmlSnapshot($xml);
126126
}
127+
128+
129+
/**
130+
* Test exporting a nested string array.
131+
*/
132+
public function test_exports_from_nested_arrays()
133+
{
134+
$data = [
135+
'key1' => [
136+
'foo' => 'bar',
137+
'bar' => 'baz',
138+
'baz' => []
139+
],
140+
'key2' => [
141+
'a' => 'b',
142+
'c' => 'd'
143+
]
144+
];
145+
146+
$xml = XML::export($data)
147+
->setRootTag('export')
148+
->version('1.0')
149+
->encoding('UTF-8')
150+
->toString();
151+
$this->assertMatchesXmlSnapshot($xml);
152+
}
127153
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<export>
3+
<key1>
4+
<foo>bar</foo>
5+
<bar>baz</bar>
6+
<baz/>
7+
</key1>
8+
<key2>
9+
<a>b</a>
10+
<c>d</c>
11+
</key2>
12+
</export>

0 commit comments

Comments
 (0)