diff --git a/CHANGELOG.md b/CHANGELOG.md index 8402b6f..7a3e7a6 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,12 @@ All notable changes to `array-to-xml` will be documented in this file +## 3.2.3 - 2024-XX-XX + +### What's Changed + +- Convert boolean values to proper xml representation by @radeno in https://github.com/spatie/array-to-xml/pull/228 + ## 3.2.2 - 2023-11-14 ### What's Changed diff --git a/src/ArrayToXml.php b/src/ArrayToXml.php index b336d44..6879692 100644 --- a/src/ArrayToXml.php +++ b/src/ArrayToXml.php @@ -19,7 +19,7 @@ class ArrayToXml protected string $numericTagNamePrefix = 'numeric_'; - protected array $options = ['convertNullToXsiNil' => false]; + protected array $options = ['convertNullToXsiNil' => false, 'convertBoolToString' => false]; public function __construct( array $array, @@ -30,7 +30,7 @@ public function __construct( array $domProperties = [], bool | null $xmlStandalone = null, bool $addXmlDeclaration = true, - array | null $options = ['convertNullToXsiNil' => false] + array | null $options = ['convertNullToXsiNil' => false, 'convertBoolToString' => false] ) { $this->document = new DOMDocument($xmlVersion, $xmlEncoding ?? ''); @@ -213,9 +213,21 @@ protected function addNode(DOMElement $element, string $key, mixed $value): void $this->addNodeTypeAttribute($child, $value); $element->appendChild($child); + + $value = $this->convertNodeValue($child, $value); + $this->convertElement($child, $value); } + protected function convertNodeValue(DOMElement $element, mixed $value): mixed + { + if ($this->options['convertBoolToString'] && is_bool($value)) { + $value = $value ? 'true' : 'false'; + } + + return $value; + } + protected function addNodeTypeAttribute(DOMElement $element, mixed $value): void { if ($this->options['convertNullToXsiNil'] && is_null($value)) { diff --git a/tests/ArrayToXmlTest.php b/tests/ArrayToXmlTest.php index 8c8e2f8..df1401a 100644 --- a/tests/ArrayToXmlTest.php +++ b/tests/ArrayToXmlTest.php @@ -489,6 +489,24 @@ assertMatchesXmlSnapshot(ArrayToXml::convert($arr, '', true, null, '1.0', [], null, true, ['convertNullToXsiNil' => true])); }); +it('can convert boolean values to xml representation', function () { + $arr = [ + 'testFalse' => false, + 'testTrue' => true, + ]; + + assertMatchesXmlSnapshot(ArrayToXml::convert($arr, '', true, null, '1.0', [], null, true, ['convertBoolToString' => true])); +}); + +it('can not convert boolean values to xml representation', function () { + $arr = [ + 'testFalse' => false, + 'testTrue' => true, + ]; + + assertMatchesXmlSnapshot(ArrayToXml::convert($arr)); +}); + it('can convert an array with empty string and null value to xml', function () { $arr = [ 'testString' => '', diff --git a/tests/__snapshots__/ArrayToXmlTest__it_can_convert_boolean_values_to_xml_representation__1.xml b/tests/__snapshots__/ArrayToXmlTest__it_can_convert_boolean_values_to_xml_representation__1.xml new file mode 100644 index 0000000..4215e24 --- /dev/null +++ b/tests/__snapshots__/ArrayToXmlTest__it_can_convert_boolean_values_to_xml_representation__1.xml @@ -0,0 +1,5 @@ + + + false + true + diff --git a/tests/__snapshots__/ArrayToXmlTest__it_can_not_convert_boolean_values_to_xml_representation__1.xml b/tests/__snapshots__/ArrayToXmlTest__it_can_not_convert_boolean_values_to_xml_representation__1.xml new file mode 100644 index 0000000..d87b292 --- /dev/null +++ b/tests/__snapshots__/ArrayToXmlTest__it_can_not_convert_boolean_values_to_xml_representation__1.xml @@ -0,0 +1,5 @@ + + + + 1 +