Skip to content

Commit 0fc5efe

Browse files
author
Refactor Studio
authoredApr 15, 2018
Merge pull request #2 from refactorstudio/cast-boolean-and-null
Cast boolean and null
2 parents d23f5ab + 0528ad8 commit 0fc5efe

9 files changed

+311
-5
lines changed
 

‎README.md

+85
Original file line numberDiff line numberDiff line change
@@ -365,3 +365,88 @@ Result (prettified):
365365
</node_3>
366366
</root>
367367
```
368+
369+
370+
371+
## Cast boolean values
372+
> By default boolean values from the array will be cast to the string 'true' or 'false'. You can choose to cast it to any (string) value you like. This method only works on real boolean values, so strings with the value 'true' and 'false' are untouched.
373+
>
374+
> `->setCastBooleanValueTrue(string $value = 'true')`
375+
376+
> `->setCastBooleanValueFalse(string $value = 'false')`
377+
378+
```php
379+
$array = [
380+
'StringTrue' => 'true',
381+
'StringFalse' => 'false',
382+
'BooleanTrue' => true,
383+
'BooleanFalse' => false
384+
];
385+
```
386+
387+
Default:
388+
```xml
389+
<?xml version="1.0" encoding="UTF-8"?>
390+
<root>
391+
<StringTrue>true</StringTrue>
392+
<StringFalse>false</StringFalse>
393+
<BooleanTrue>true</BooleanTrue>
394+
<BooleanFalse>false</BooleanFalse>
395+
</root>
396+
```
397+
398+
Usage:
399+
```php
400+
$xml_string = $converter->setCastBooleanTrue('Yes')->setCastBooleanFalse('No')->toXmlString($array);
401+
```
402+
403+
Result:
404+
```xml
405+
<?xml version="1.0" encoding="UTF-8"?>
406+
<root>
407+
<StringTrue>true</StringTrue>
408+
<StringFalse>false</StringFalse>
409+
<BooleanTrue>Yes</BooleanTrue>
410+
<BooleanFalse>No</BooleanFalse>
411+
</root>
412+
```
413+
414+
415+
416+
## Cast NULL values
417+
> By default null values from the array will have no value in the XML, so the tag looks something like this: `<MyTag/>`. You can choose to cast it to any (string) value you like. This method only works on real 'null' values, so strings with the value `'null'` or empty strings `''` are untouched.
418+
>
419+
> `->setCastNullValue(null|string $value = null)`
420+
421+
```php
422+
$array = [
423+
'StringNull' => 'null',
424+
'StringEmpty' => '',
425+
'RealNull' => null
426+
];
427+
```
428+
429+
Default:
430+
```xml
431+
<?xml version="1.0" encoding="UTF-8"?>
432+
<root>
433+
<StringNull>null</StringNull>
434+
<StringEmpty/>
435+
<RealNull/>
436+
</root>
437+
```
438+
439+
Usage:
440+
```php
441+
$xml_string = $converter->setCastNullValue('__NULL__')->setCastBooleanFalse('No')->toXmlString($array);
442+
```
443+
444+
Result:
445+
```xml
446+
<?xml version="1.0" encoding="UTF-8"?>
447+
<root>
448+
<StringNull>null</StringNull>
449+
<StringEmpty/>
450+
<RealNull>__NULL__</RealNull>
451+
</root>
452+
```

‎src/PhpArrayToXml.php

+112-4
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ class PhpArrayToXml
2121
protected $_transform_tags = null;
2222
protected $_format_output = false;
2323
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;
2427

2528
/**
2629
* Set the version of the XML (Default = '1.0')
@@ -266,6 +269,69 @@ public function getNumericTagSuffix()
266269
return $this->_numeric_tag_suffix;
267270
}
268271

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+
269335
/**
270336
* Validate if a given value has a proper tag starting character to be used in XML
271337
*
@@ -405,7 +471,7 @@ protected function addArrayElements(DOMElement $parent, $array = [])
405471
$node = $this->createElement($name, null);
406472

407473
foreach($attributes as $attribute_name => $attribute_value) {
408-
$node->setAttribute($attribute_name, $attribute_value);
474+
$node->setAttribute($attribute_name, $this->normalizeAttributeValue($attribute_value));
409475
}
410476

411477
$parent->appendChild($node);
@@ -427,6 +493,48 @@ protected function addArrayElements(DOMElement $parent, $array = [])
427493
}
428494
}
429495

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+
430538
/**
431539
* See if a value matches an integer (could be a integer within a string)
432540
*
@@ -458,16 +566,16 @@ protected function createElement($name, $value = null, $cdata = false, $attribut
458566
$element->appendChild($this->_doc->createCDATASection($value));
459567

460568
foreach($attributes as $attribute_name => $attribute_value) {
461-
$element->setAttribute($attribute_name, $attribute_value);
569+
$element->setAttribute($attribute_name, $this->normalizeAttributeValue($attribute_value));
462570
}
463571

464572
return $element;
465573
}
466574

467-
$element = $this->_doc->createElement($name, $value);
575+
$element = $this->_doc->createElement($name, $this->normalizeValue($value));
468576

469577
foreach($attributes as $attribute_name => $attribute_value) {
470-
$element->setAttribute($attribute_name, $attribute_value);
578+
$element->setAttribute($attribute_name, $this->normalizeAttributeValue($attribute_value));
471579
}
472580

473581
return $element;

‎tests/PhpArrayToXmlTest.php

+31-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public function check_if_custom_helper_methods_actually_work()
4141
public function check_if_every_stub_is_being_tested()
4242
{
4343
$missing = null;
44-
$stubs = glob('stubs' . DIRECTORY_SEPARATOR . '*.*');
44+
$stubs = glob(__DIR__ . DIRECTORY_SEPARATOR . 'stubs' . DIRECTORY_SEPARATOR . '*.*');
4545

4646
foreach($stubs as $stub) {
4747
$path_info = pathinfo($stub);
@@ -61,6 +61,16 @@ public function check_if_all_constants_are_available()
6161
$this->assertEquals('uppercase', PhpArrayToXml::UPPERCASE);
6262
}
6363

64+
public function test_default_values()
65+
{
66+
$array = $this->getArrayStub(__FUNCTION__);
67+
$expected = $this->getXmlStub(__FUNCTION__);
68+
69+
$result = (new PhpArrayToXml)->prettify()->toXmlString($array);
70+
71+
$this->assertEquals($expected, $result);
72+
}
73+
6474
public function test_version_encoding()
6575
{
6676
$array = $this->getArrayStub(__FUNCTION__);
@@ -312,4 +322,24 @@ public function test_attributes_and_cdata()
312322

313323
$this->assertEquals($expected, $result);
314324
}
325+
326+
public function test_cast_boolean_values()
327+
{
328+
$array = $this->getArrayStub(__FUNCTION__);
329+
$expected = $this->getXmlStub(__FUNCTION__);
330+
331+
$result = (new PhpArrayToXml)->setCastBooleanValueTrue('Yes')->setCastBooleanValueFalse('No')->prettify()->toXmlString($array);
332+
333+
$this->assertEquals($expected, $result);
334+
}
335+
336+
public function test_cast_null_values()
337+
{
338+
$array = $this->getArrayStub(__FUNCTION__);
339+
$expected = $this->getXmlStub(__FUNCTION__);
340+
341+
$result = (new PhpArrayToXml)->setCastNullValue('__NULL__')->prettify()->toXmlString($array);
342+
343+
$this->assertEquals($expected, $result);
344+
}
315345
}
+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
return [
4+
'StringTrue' => 'true',
5+
'StringFalse' => 'false',
6+
'BooleanTrue' => true,
7+
'BooleanFalse' => false
8+
];
+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<root>
3+
<StringTrue>true</StringTrue>
4+
<StringFalse>false</StringFalse>
5+
<BooleanTrue>Yes</BooleanTrue>
6+
<BooleanFalse>No</BooleanFalse>
7+
</root>

‎tests/stubs/test_cast_null_values.php

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
return [
4+
'StringNull' => 'null',
5+
'StringEmpty' => '',
6+
'RealNull' => null,
7+
];

‎tests/stubs/test_cast_null_values.xml

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<root>
3+
<StringNull>null</StringNull>
4+
<StringEmpty/>
5+
<RealNull>__NULL__</RealNull>
6+
</root>

‎tests/stubs/test_default_values.php

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
return [
4+
'StringTrue' => 'true',
5+
'StringFalse' => 'false',
6+
'BooleanTrue' => true,
7+
'BooleanFalse' => false,
8+
'StringNull' => 'null',
9+
'StringEmpty' => '',
10+
'RealNull' => null,
11+
'Float' => 0.987654321,
12+
'Integer' => 12345,
13+
'String' => 'Example',
14+
'CData' => [
15+
'@value' => '<span>CData Example</span>',
16+
'@cdata' => true
17+
],
18+
'Attributes' => [
19+
'@value' => 'Test',
20+
'@attr' => [
21+
'AttrBooleanTrue' => true,
22+
'AttrBooleanFalse' => false,
23+
'AttrString' => 'StringValue',
24+
'AttrStringNull' => 'null',
25+
'AttrStringEmpty' => '',
26+
'AttrRealNull' => null,
27+
'AttrFloat' => 0.987654321,
28+
'AttrInteger' => 12345
29+
]
30+
],
31+
'CDataWithAttributes' => [
32+
'@value' => '<span>Value</span>',
33+
'@cdata' => true,
34+
'@attr' => [
35+
'Attr1' => 'First',
36+
'Attr2' => 'Second'
37+
]
38+
]
39+
];

‎tests/stubs/test_default_values.xml

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<root>
3+
<StringTrue>true</StringTrue>
4+
<StringFalse>false</StringFalse>
5+
<BooleanTrue>true</BooleanTrue>
6+
<BooleanFalse>false</BooleanFalse>
7+
<StringNull>null</StringNull>
8+
<StringEmpty/>
9+
<RealNull/>
10+
<Float>0.987654321</Float>
11+
<Integer>12345</Integer>
12+
<String>Example</String>
13+
<CData><![CDATA[<span>CData Example</span>]]></CData>
14+
<Attributes AttrBooleanTrue="true" AttrBooleanFalse="false" AttrString="StringValue" AttrStringNull="null" AttrStringEmpty="" AttrRealNull="" AttrFloat="0.987654321" AttrInteger="12345">Test</Attributes>
15+
<CDataWithAttributes Attr1="First" Attr2="Second"><![CDATA[<span>Value</span>]]></CDataWithAttributes>
16+
</root>

0 commit comments

Comments
 (0)
Please sign in to comment.