@@ -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 }
0 commit comments