diff --git a/src/ArrayToXml.php b/src/ArrayToXml.php index cf43a99..b336d44 100644 --- a/src/ArrayToXml.php +++ b/src/ArrayToXml.php @@ -19,6 +19,8 @@ class ArrayToXml protected string $numericTagNamePrefix = 'numeric_'; + protected array $options = ['convertNullToXsiNil' => false]; + public function __construct( array $array, string | array $rootElement = '', @@ -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 ?? ''); @@ -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)) { @@ -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, @@ -77,7 +83,8 @@ public static function convert( $xmlVersion, $domProperties, $xmlStandalone, - $addXmlDeclaration + $addXmlDeclaration, + $options ); return $converter->toXml(); @@ -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'); } diff --git a/tests/ArrayToXmlTest.php b/tests/ArrayToXmlTest.php index 2fdd168..8c8e2f8 100644 --- a/tests/ArrayToXmlTest.php +++ b/tests/ArrayToXmlTest.php @@ -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' => '', diff --git a/tests/__snapshots__/ArrayToXmlTest__it_can_convert_an_array_with_empty_string_and_null_value_to_xml__1.xml b/tests/__snapshots__/ArrayToXmlTest__it_can_convert_an_array_with_empty_string_and_null_value_to_xml__1.xml index d2ef544..3e9415d 100644 --- a/tests/__snapshots__/ArrayToXmlTest__it_can_convert_an_array_with_empty_string_and_null_value_to_xml__1.xml +++ b/tests/__snapshots__/ArrayToXmlTest__it_can_convert_an_array_with_empty_string_and_null_value_to_xml__1.xml @@ -1,5 +1,5 @@ - + - + diff --git a/tests/__snapshots__/ArrayToXmlTest__it_can_convert_an_array_with_empty_string_and_null_value_to_xml_with_option__1.xml b/tests/__snapshots__/ArrayToXmlTest__it_can_convert_an_array_with_empty_string_and_null_value_to_xml_with_option__1.xml new file mode 100644 index 0000000..d2ef544 --- /dev/null +++ b/tests/__snapshots__/ArrayToXmlTest__it_can_convert_an_array_with_empty_string_and_null_value_to_xml_with_option__1.xml @@ -0,0 +1,5 @@ + + + + +