Skip to content

Commit 96be97e

Browse files
authored
Conver null based on option (#226)
* 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
1 parent 778ea25 commit 96be97e

4 files changed

+26
-5
lines changed

src/ArrayToXml.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ class ArrayToXml
1919

2020
protected string $numericTagNamePrefix = 'numeric_';
2121

22+
protected array $options = ['convertNullToXsiNil' => false];
23+
2224
public function __construct(
2325
array $array,
2426
string | array $rootElement = '',
@@ -27,7 +29,8 @@ public function __construct(
2729
string $xmlVersion = '1.0',
2830
array $domProperties = [],
2931
bool | null $xmlStandalone = null,
30-
bool $addXmlDeclaration = true
32+
bool $addXmlDeclaration = true,
33+
array | null $options = ['convertNullToXsiNil' => false]
3134
) {
3235
$this->document = new DOMDocument($xmlVersion, $xmlEncoding ?? '');
3336

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

4245
$this->addXmlDeclaration = $addXmlDeclaration;
4346

47+
$this->options = array_merge($this->options, $options);
48+
4449
$this->replaceSpacesByUnderScoresInKeyNames = $replaceSpacesByUnderScoresInKeyNames;
4550

4651
if (! empty($array) && $this->isArrayAllKeySequential($array)) {
@@ -68,6 +73,7 @@ public static function convert(
6873
array $domProperties = [],
6974
bool $xmlStandalone = null,
7075
bool $addXmlDeclaration = true,
76+
array $options = ['convertNullToXsiNil' => false]
7177
): string {
7278
$converter = new static(
7379
$array,
@@ -77,7 +83,8 @@ public static function convert(
7783
$xmlVersion,
7884
$domProperties,
7985
$xmlStandalone,
80-
$addXmlDeclaration
86+
$addXmlDeclaration,
87+
$options
8188
);
8289

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

212219
protected function addNodeTypeAttribute(DOMElement $element, mixed $value): void
213220
{
214-
if (is_null($value)) {
221+
if ($this->options['convertNullToXsiNil'] && is_null($value)) {
215222
if (! $this->rootNode->hasAttribute('xmlns:xsi')) {
216223
$this->rootNode->setAttribute('xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance');
217224
}

tests/ArrayToXmlTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -480,6 +480,15 @@
480480
assertMatchesSnapshot($arrayToXml->dropXmlDeclaration()->toXml());
481481
});
482482

483+
it('can convert an array with empty string and null value to xml with option', function () {
484+
$arr = [
485+
'testString' => '',
486+
'testNull' => null,
487+
];
488+
489+
assertMatchesXmlSnapshot(ArrayToXml::convert($arr, '', true, null, '1.0', [], null, true, ['convertNullToXsiNil' => true]));
490+
});
491+
483492
it('can convert an array with empty string and null value to xml', function () {
484493
$arr = [
485494
'testString' => '',
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?xml version="1.0"?>
2-
<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
2+
<root>
33
<testString/>
4-
<testNull xsi:nil="true"/>
4+
<testNull/>
55
</root>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?xml version="1.0"?>
2+
<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
3+
<testString/>
4+
<testNull xsi:nil="true"/>
5+
</root>

0 commit comments

Comments
 (0)