Skip to content

Commit af95da0

Browse files
committed
Empty values/attributes/cdata are now handled appropriately (Fixes [#3])
1 parent 9b8c037 commit af95da0

File tree

5 files changed

+68
-5
lines changed

5 files changed

+68
-5
lines changed

.gitattributes

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,5 @@
44
# Force text mode
55
*.php text diff=php
66
*.xml text
7+
8+
/tests/bugs/*.php diff=php eol=lf

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Version History
22

3+
**1.6** (2016/07/27)
4+
5+
* Empty values/attributes/cdata are now handled appropriately (Fixes [#3](https://github.com/rquadling/lalit/issues/3))
6+
37
**1.5** (2016/06/29)
48

59
* Reintroduce support for hhvm

src/Array2XML.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class Array2XML
3636
* Convert an Array to XML
3737
*
3838
* @param string $node_name - name of the root node to be converted
39-
* @param array $arr - aray to be converterd
39+
* @param array $arr - array to be converted
4040
*
4141
* @return DomDocument
4242
*/
@@ -98,7 +98,7 @@ private static function convert($node_name, $arr = [])
9898

9999
if (is_array($arr)) {
100100
// get the attributes first.;
101-
if (isset($arr['@attributes'])) {
101+
if (array_key_exists('@attributes', $arr)) {
102102
foreach ($arr['@attributes'] as $key => $value) {
103103
if (!self::isValidTagName($key)) {
104104
throw new Exception('[Array2XML] Illegal character in attribute name. attribute: ' . $key . ' in node: ' . $node_name);
@@ -110,12 +110,12 @@ private static function convert($node_name, $arr = [])
110110

111111
// check if it has a value stored in @value, if yes store the value and return
112112
// else check if its directly stored as string
113-
if (isset($arr['@value'])) {
113+
if (array_key_exists('@value', $arr)) {
114114
$node->appendChild($xml->createTextNode(self::bool2str($arr['@value'])));
115115
unset($arr['@value']); //remove the key from the array once done.
116116
//return from recursion, as a note with value cannot have child nodes.
117117
return $node;
118-
} else if (isset($arr['@cdata'])) {
118+
} else if (array_key_exists('@cdata', $arr)) {
119119
$node->appendChild($xml->createCDATASection(self::bool2str($arr['@cdata'])));
120120
unset($arr['@cdata']); //remove the key from the array once done.
121121
//return from recursion, as a note with cdata cannot have child nodes.

src/XML2Array.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ private static function convert(DOMNode $node)
111111
$t = $child->tagName;
112112

113113
// assume more nodes of same kind are coming
114-
if (!isset($output[$t])) {
114+
if (!array_key_exists($t, $output)) {
115115
$output[$t] = [];
116116
}
117117
$output[$t][] = $v;

tests/bugs/Bug003Test.php

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
namespace LaLit;
3+
4+
use PHPUnit_Framework_TestCase;
5+
6+
/**
7+
* Class Bug003
8+
*
9+
* @package LaLit
10+
* @group bugfixes
11+
*/
12+
class Bug003 extends PHPUnit_Framework_TestCase
13+
{
14+
public function testNullValues()
15+
{
16+
$array = [
17+
'container' => [
18+
'item' => [
19+
'term' => 'description',
20+
'label' => null,
21+
'emptyCData' => [
22+
'@cdata' => null,
23+
],
24+
'@attributes' => [
25+
'present' => 'none',
26+
],
27+
'node' => [
28+
'@attributes' => [
29+
'present' => 'years',
30+
'empty' => null,
31+
],
32+
'@value' => null,
33+
],
34+
],
35+
],
36+
];
37+
38+
$actualResults = Array2XML::createXML('root', $array)->saveXML();
39+
40+
$expectedResults = <<< END_XML
41+
<?xml version="1.0" encoding="utf-8" standalone="no"?>
42+
<root>
43+
<container>
44+
<item present="none">
45+
<term>description</term>
46+
<label></label>
47+
<emptyCData><![CDATA[]]></emptyCData>
48+
<node present="years" empty=""></node>
49+
</item>
50+
</container>
51+
</root>
52+
53+
END_XML;
54+
55+
self::assertEquals($expectedResults, $actualResults, 'Failed to handle empty @values');
56+
}
57+
}

0 commit comments

Comments
 (0)