Skip to content

Commit c0a7bf9

Browse files
committed
Add support for custom arrays in XmlSeriaizer
1 parent 590d149 commit c0a7bf9

File tree

3 files changed

+26
-0
lines changed

3 files changed

+26
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1212
- New interface `Redmine\Http\HttpClient` for new minimalistic HTTP clients.
1313
- New interface `Redmine\Http\Request` for sending data with new minimalistic HTTP clients.
1414
- New method `Redmine\Api\...::getLastResponse()` to get the last response made by the API class.
15+
- Add support for custom arrays in `Redmine\Serializer\XmlSerializer`
1516

1617
### Changed
1718

src/Redmine/Serializer/XmlSerializer.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,12 @@ private function addChildToXmlElement(SimpleXMLElement $xml, $k, $v): void
182182
foreach ($v as $id) {
183183
$array->addChild($specialParams[$k], $id);
184184
}
185+
} elseif (is_array($v)) {
186+
$array = $xml->addChild($k, '');
187+
$array->addAttribute('type', 'array');
188+
foreach ($v as $id) {
189+
$array->addChild($k, $id);
190+
}
185191
} else {
186192
$xml->$k = $v;
187193
}

tests/Unit/Serializer/XmlSerializerTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,25 @@ public static function getNormalizedToEncodedData(): array
151151
</issue>
152152
XML,
153153
],
154+
'test with custom array' => [
155+
[
156+
'issue' => [
157+
'taglist' => [
158+
'approved',
159+
'finished',
160+
],
161+
],
162+
],
163+
<<< XML
164+
<?xml version="1.0"?>
165+
<issue>
166+
<taglist type="array">
167+
<taglist>approved</taglist>
168+
<taglist>finished</taglist>
169+
</taglist>
170+
</issue>
171+
XML,
172+
],
154173
'test with ignored elements' => [
155174
[
156175
'issue' => [

0 commit comments

Comments
 (0)