Skip to content

Commit

Permalink
Conver null based on option (#226)
Browse files Browse the repository at this point in the history
* Convert null to xsi:nil based on setting

* Update ArrayToXml.php

* Update ArrayToXml.php

* Update ArrayToXmlTest__it_can_convert_an_array_with_empty_string_and_null_value_to_xml__1.xml

* Update ArrayToXml.php
  • Loading branch information
radeno authored Nov 14, 2023
1 parent 778ea25 commit 96be97e
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 5 deletions.
13 changes: 10 additions & 3 deletions src/ArrayToXml.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ class ArrayToXml

protected string $numericTagNamePrefix = 'numeric_';

protected array $options = ['convertNullToXsiNil' => false];

public function __construct(
array $array,
string | array $rootElement = '',
Expand All @@ -27,7 +29,8 @@ public function __construct(
string $xmlVersion = '1.0',
array $domProperties = [],
bool | null $xmlStandalone = null,
bool $addXmlDeclaration = true
bool $addXmlDeclaration = true,
array | null $options = ['convertNullToXsiNil' => false]
) {
$this->document = new DOMDocument($xmlVersion, $xmlEncoding ?? '');

Expand All @@ -41,6 +44,8 @@ public function __construct(

$this->addXmlDeclaration = $addXmlDeclaration;

$this->options = array_merge($this->options, $options);

$this->replaceSpacesByUnderScoresInKeyNames = $replaceSpacesByUnderScoresInKeyNames;

if (! empty($array) && $this->isArrayAllKeySequential($array)) {
Expand Down Expand Up @@ -68,6 +73,7 @@ public static function convert(
array $domProperties = [],
bool $xmlStandalone = null,
bool $addXmlDeclaration = true,
array $options = ['convertNullToXsiNil' => false]
): string {
$converter = new static(
$array,
Expand All @@ -77,7 +83,8 @@ public static function convert(
$xmlVersion,
$domProperties,
$xmlStandalone,
$addXmlDeclaration
$addXmlDeclaration,
$options
);

return $converter->toXml();
Expand Down Expand Up @@ -211,7 +218,7 @@ protected function addNode(DOMElement $element, string $key, mixed $value): void

protected function addNodeTypeAttribute(DOMElement $element, mixed $value): void
{
if (is_null($value)) {
if ($this->options['convertNullToXsiNil'] && is_null($value)) {
if (! $this->rootNode->hasAttribute('xmlns:xsi')) {
$this->rootNode->setAttribute('xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance');
}
Expand Down
9 changes: 9 additions & 0 deletions tests/ArrayToXmlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,15 @@
assertMatchesSnapshot($arrayToXml->dropXmlDeclaration()->toXml());
});

it('can convert an array with empty string and null value to xml with option', function () {
$arr = [
'testString' => '',
'testNull' => null,
];

assertMatchesXmlSnapshot(ArrayToXml::convert($arr, '', true, null, '1.0', [], null, true, ['convertNullToXsiNil' => true]));
});

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
@@ -1,5 +1,5 @@
<?xml version="1.0"?>
<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<root>
<testString/>
<testNull xsi:nil="true"/>
<testNull/>
</root>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0"?>
<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<testString/>
<testNull xsi:nil="true"/>
</root>

0 comments on commit 96be97e

Please sign in to comment.