@@ -21,6 +21,9 @@ class PhpArrayToXml
21
21
protected $ _transform_tags = null ;
22
22
protected $ _format_output = false ;
23
23
protected $ _numeric_tag_suffix = null ;
24
+ protected $ _default_boolean_value_true = 'true ' ;
25
+ protected $ _default_boolean_value_false = 'false ' ;
26
+ protected $ _default_null_value = null ;
24
27
25
28
/**
26
29
* Set the version of the XML (Default = '1.0')
@@ -266,6 +269,69 @@ public function getNumericTagSuffix()
266
269
return $ this ->_numeric_tag_suffix ;
267
270
}
268
271
272
+ /**
273
+ * Cast real boolean (true) values to a given string
274
+ *
275
+ * @param string $value
276
+ * @return PhpArrayToXml
277
+ */
278
+ public function setCastBooleanValueTrue ($ value = 'true ' )
279
+ {
280
+ $ this ->_default_boolean_value_true = $ value ;
281
+
282
+ return $ this ;
283
+ }
284
+
285
+ /**
286
+ * @return string
287
+ */
288
+ public function getCastBooleanValueTrue ()
289
+ {
290
+ return $ this ->_default_boolean_value_true ;
291
+ }
292
+
293
+ /**
294
+ * Cast real boolean (false) values to a given string
295
+ *
296
+ * @param string $value
297
+ * @return PhpArrayToXml
298
+ */
299
+ public function setCastBooleanValueFalse ($ value = 'false ' )
300
+ {
301
+ $ this ->_default_boolean_value_false = $ value ;
302
+
303
+ return $ this ;
304
+ }
305
+
306
+ /**
307
+ * @return string
308
+ */
309
+ public function getCastBooleanValueFalse ()
310
+ {
311
+ return $ this ->_default_boolean_value_false ;
312
+ }
313
+
314
+ /**
315
+ * Cast real null values to a given string
316
+ *
317
+ * @param string $value
318
+ * @return PhpArrayToXml
319
+ */
320
+ public function setCastNullValue ($ value = null )
321
+ {
322
+ $ this ->_default_null_value = $ value ;
323
+
324
+ return $ this ;
325
+ }
326
+
327
+ /**
328
+ * @return null|string
329
+ */
330
+ public function getCastNullValue ()
331
+ {
332
+ return $ this ->_default_null_value ;
333
+ }
334
+
269
335
/**
270
336
* Validate if a given value has a proper tag starting character to be used in XML
271
337
*
@@ -405,7 +471,7 @@ protected function addArrayElements(DOMElement $parent, $array = [])
405
471
$ node = $ this ->createElement ($ name , null );
406
472
407
473
foreach ($ attributes as $ attribute_name => $ attribute_value ) {
408
- $ node ->setAttribute ($ attribute_name , $ attribute_value );
474
+ $ node ->setAttribute ($ attribute_name , $ this -> normalizeAttributeValue ( $ attribute_value) );
409
475
}
410
476
411
477
$ parent ->appendChild ($ node );
@@ -427,6 +493,48 @@ protected function addArrayElements(DOMElement $parent, $array = [])
427
493
}
428
494
}
429
495
496
+ /**
497
+ * Normalize a value (replace some characters)
498
+ *
499
+ * @param $value
500
+ * @return null|string
501
+ */
502
+ protected function normalizeValue ($ value )
503
+ {
504
+ if ($ value === true ) {
505
+ return $ this ->getCastBooleanValueTrue ();
506
+ }
507
+
508
+ if ($ value === false ) {
509
+ return $ this ->getCastBooleanValueFalse ();
510
+ }
511
+
512
+ if ($ value === null ) {
513
+ return $ this ->getCastNullValue ();
514
+ }
515
+
516
+ return $ value ;
517
+ }
518
+
519
+ /**
520
+ * Normalize an attribute value (replace some characters)
521
+ *
522
+ * @param $value
523
+ * @return string
524
+ */
525
+ protected function normalizeAttributeValue ($ value )
526
+ {
527
+ if ($ value === true ) {
528
+ return 'true ' ;
529
+ }
530
+
531
+ if ($ value === false ) {
532
+ return 'false ' ;
533
+ }
534
+
535
+ return $ value ;
536
+ }
537
+
430
538
/**
431
539
* See if a value matches an integer (could be a integer within a string)
432
540
*
@@ -458,16 +566,16 @@ protected function createElement($name, $value = null, $cdata = false, $attribut
458
566
$ element ->appendChild ($ this ->_doc ->createCDATASection ($ value ));
459
567
460
568
foreach ($ attributes as $ attribute_name => $ attribute_value ) {
461
- $ element ->setAttribute ($ attribute_name , $ attribute_value );
569
+ $ element ->setAttribute ($ attribute_name , $ this -> normalizeAttributeValue ( $ attribute_value) );
462
570
}
463
571
464
572
return $ element ;
465
573
}
466
574
467
- $ element = $ this ->_doc ->createElement ($ name , $ value );
575
+ $ element = $ this ->_doc ->createElement ($ name , $ this -> normalizeValue ( $ value) );
468
576
469
577
foreach ($ attributes as $ attribute_name => $ attribute_value ) {
470
- $ element ->setAttribute ($ attribute_name , $ attribute_value );
578
+ $ element ->setAttribute ($ attribute_name , $ this -> normalizeAttributeValue ( $ attribute_value) );
471
579
}
472
580
473
581
return $ element ;
0 commit comments