Skip to content

Commit

Permalink
Add proper boolean conversion to xml representation (#228)
Browse files Browse the repository at this point in the history
* Add proper boolean conversion to xml representation

* Update CHANGELOG.md

* Update ArrayToXmlTest.php
  • Loading branch information
radeno committed Feb 7, 2024
1 parent aec6e0b commit c95fd4d
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 2 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 14 additions & 2 deletions src/ArrayToXml.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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 ?? '');

Expand Down Expand Up @@ -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)) {
Expand Down
18 changes: 18 additions & 0 deletions tests/ArrayToXmlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' => '',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0"?>
<root>
<testFalse>false</testFalse>
<testTrue>true</testTrue>
</root>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0"?>
<root>
<testFalse/>
<testTrue>1</testTrue>
</root>

0 comments on commit c95fd4d

Please sign in to comment.