Skip to content

Commit c95fd4d

Browse files
authored
Add proper boolean conversion to xml representation (#228)
* Add proper boolean conversion to xml representation * Update CHANGELOG.md * Update ArrayToXmlTest.php
1 parent aec6e0b commit c95fd4d

File tree

5 files changed

+48
-2
lines changed

5 files changed

+48
-2
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22

33
All notable changes to `array-to-xml` will be documented in this file
44

5+
## 3.2.3 - 2024-XX-XX
6+
7+
### What's Changed
8+
9+
- Convert boolean values to proper xml representation by @radeno in https://github.com/spatie/array-to-xml/pull/228
10+
511
## 3.2.2 - 2023-11-14
612

713
### What's Changed

src/ArrayToXml.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class ArrayToXml
1919

2020
protected string $numericTagNamePrefix = 'numeric_';
2121

22-
protected array $options = ['convertNullToXsiNil' => false];
22+
protected array $options = ['convertNullToXsiNil' => false, 'convertBoolToString' => false];
2323

2424
public function __construct(
2525
array $array,
@@ -30,7 +30,7 @@ public function __construct(
3030
array $domProperties = [],
3131
bool | null $xmlStandalone = null,
3232
bool $addXmlDeclaration = true,
33-
array | null $options = ['convertNullToXsiNil' => false]
33+
array | null $options = ['convertNullToXsiNil' => false, 'convertBoolToString' => false]
3434
) {
3535
$this->document = new DOMDocument($xmlVersion, $xmlEncoding ?? '');
3636

@@ -213,9 +213,21 @@ protected function addNode(DOMElement $element, string $key, mixed $value): void
213213
$this->addNodeTypeAttribute($child, $value);
214214

215215
$element->appendChild($child);
216+
217+
$value = $this->convertNodeValue($child, $value);
218+
216219
$this->convertElement($child, $value);
217220
}
218221

222+
protected function convertNodeValue(DOMElement $element, mixed $value): mixed
223+
{
224+
if ($this->options['convertBoolToString'] && is_bool($value)) {
225+
$value = $value ? 'true' : 'false';
226+
}
227+
228+
return $value;
229+
}
230+
219231
protected function addNodeTypeAttribute(DOMElement $element, mixed $value): void
220232
{
221233
if ($this->options['convertNullToXsiNil'] && is_null($value)) {

tests/ArrayToXmlTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -489,6 +489,24 @@
489489
assertMatchesXmlSnapshot(ArrayToXml::convert($arr, '', true, null, '1.0', [], null, true, ['convertNullToXsiNil' => true]));
490490
});
491491

492+
it('can convert boolean values to xml representation', function () {
493+
$arr = [
494+
'testFalse' => false,
495+
'testTrue' => true,
496+
];
497+
498+
assertMatchesXmlSnapshot(ArrayToXml::convert($arr, '', true, null, '1.0', [], null, true, ['convertBoolToString' => true]));
499+
});
500+
501+
it('can not convert boolean values to xml representation', function () {
502+
$arr = [
503+
'testFalse' => false,
504+
'testTrue' => true,
505+
];
506+
507+
assertMatchesXmlSnapshot(ArrayToXml::convert($arr));
508+
});
509+
492510
it('can convert an array with empty string and null value to xml', function () {
493511
$arr = [
494512
'testString' => '',
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?xml version="1.0"?>
2+
<root>
3+
<testFalse>false</testFalse>
4+
<testTrue>true</testTrue>
5+
</root>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?xml version="1.0"?>
2+
<root>
3+
<testFalse/>
4+
<testTrue>1</testTrue>
5+
</root>

0 commit comments

Comments
 (0)