Skip to content

Commit e67846d

Browse files
committed
rewrite with more objective approach (remove static functions), change assoc array detection in order to treat numeric assoc arrays same as sequential arrays
1 parent 8723508 commit e67846d

File tree

1 file changed

+66
-61
lines changed

1 file changed

+66
-61
lines changed

ArrayToXML.php

Lines changed: 66 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class ArrayToXML {
1010
* @return string XML String containig values
1111
* @return mixed Boolean false on failure, string XML result on success
1212
*/
13-
public static function buildXML($data, $startElement = 'data', $xml_version = '1.0', $xml_encoding = 'UTF-8'){
13+
public function buildXML($data, $startElement = 'data', $xml_version = '1.0', $xml_encoding = 'UTF-8'){
1414
if(!is_array($data)){
1515
$err = 'Invalid variable type supplied, expected array not found on line '.__LINE__." in Class: ".__CLASS__." Method: ".__METHOD__;
1616
trigger_error($err);
@@ -22,76 +22,81 @@ public static function buildXML($data, $startElement = 'data', $xml_version = '1
2222
$xml->startDocument($xml_version, $xml_encoding);
2323
$xml->startElement($startElement);
2424

25-
/**
26-
* Write keys in $data prefixed with @ as XML attributes, if $data is an array.
27-
* When an @ prefixed key is found, a '%' key is expected to indicate the element itself,
28-
* and '#' prefixed key indicates CDATA content
29-
*
30-
* @param object $xml XMLWriter Object
31-
* @param array $data with attributes filtered out
32-
*/
33-
function writeAttr(XMLWriter $xml, $data) {
34-
if(is_array($data)) {
35-
$nonAttributes = array();
36-
foreach($data as $key => $val) {
37-
//handle an attribute with elements
38-
if($key[0] == '@') {
39-
$xml->writeAttribute(substr($key, 1), $val);
40-
} else if($key[0] == '%') {
41-
if(is_array($val)) $nonAttributes = $val;
42-
else $xml->text($val);
43-
} elseif($key[0] == '#') {
44-
if(is_array($val)) $nonAttributes = $val;
45-
else {
46-
$xml->startElement(substr($key, 1));
47-
$xml->writeCData($val);
48-
$xml->endElement();
49-
}
50-
}
25+
$this->writeEl($xml, $data);
5126

52-
//ignore normal elements
53-
else $nonAttributes[$key] = $val;
27+
$xml->endElement();//write end element
28+
//returns the XML results
29+
return $xml->outputMemory(true);
30+
}
31+
/**
32+
* Write keys in $data prefixed with @ as XML attributes, if $data is an array.
33+
* When an @ prefixed key is found, a '%' key is expected to indicate the element itself,
34+
* and '#' prefixed key indicates CDATA content
35+
*
36+
* @param object $xml XMLWriter Object
37+
* @param array $data with attributes filtered out
38+
*/
39+
protected function writeAttr(XMLWriter $xml, $data) {
40+
if(is_array($data)) {
41+
$nonAttributes = array();
42+
foreach($data as $key => $val) {
43+
//handle an attribute with elements
44+
if($key[0] == '@') {
45+
$xml->writeAttribute(substr($key, 1), $val);
46+
} else if($key[0] == '%') {
47+
if(is_array($val)) $nonAttributes = $val;
48+
else $xml->text($val);
49+
} elseif($key[0] == '#') {
50+
if(is_array($val)) $nonAttributes = $val;
51+
else {
52+
$xml->startElement(substr($key, 1));
53+
$xml->writeCData($val);
54+
$xml->endElement();
55+
}
5456
}
55-
return $nonAttributes;
57+
58+
//ignore normal elements
59+
else $nonAttributes[$key] = $val;
5660
}
57-
else return $data;
61+
return $nonAttributes;
5862
}
63+
else return $data;
64+
}
5965

60-
/**
61-
* Write XML as per Associative Array
62-
* @param object $xml XMLWriter Object
63-
* @param array $data Associative Data Array
64-
*/
65-
function writeEl(XMLWriter $xml, $data) {
66-
foreach($data as $key => $value) {
67-
if(is_array($value) && isset($value[0])) { //numeric array
68-
foreach($value as $itemValue){
69-
if(is_array($itemValue)) {
70-
$xml->startElement($key);
71-
$itemValue = writeAttr($xml, $itemValue);
72-
writeEl($xml, $itemValue);
73-
$xml->endElement();
74-
} else {
75-
$itemValue = writeAttr($xml, $itemValue);
76-
$xml->writeElement($key, "$itemValue");
77-
}
66+
/**
67+
* Write XML as per Associative Array
68+
* @param object $xml XMLWriter Object
69+
* @param array $data Associative Data Array
70+
*/
71+
protected function writeEl(XMLWriter $xml, $data) {
72+
foreach($data as $key => $value) {
73+
if(is_array($value) && !$this->isAssoc($value)) { //numeric array
74+
foreach($value as $itemValue){
75+
if(is_array($itemValue)) {
76+
$xml->startElement($key);
77+
$itemValue = $this->writeAttr($xml, $itemValue);
78+
$this->writeEl($xml, $itemValue);
79+
$xml->endElement();
80+
} else {
81+
$itemValue = $this->writeAttr($xml, $itemValue);
82+
$xml->writeElement($key, "$itemValue");
7883
}
79-
} else if(is_array($value)) { //associative array
80-
$xml->startElement($key);
81-
$value = writeAttr($xml, $value);
82-
writeEl($xml, $value);
83-
$xml->endElement();
84-
} else { //scalar
85-
$value = writeAttr($xml, $value);
86-
$xml->writeElement($key, "$value");
8784
}
85+
} else if(is_array($value)) { //associative array
86+
$xml->startElement($key);
87+
$value = $this->writeAttr($xml, $value);
88+
$this->writeEl($xml, $value);
89+
$xml->endElement();
90+
} else { //scalar
91+
$value = $this->writeAttr($xml, $value);
92+
$xml->writeElement($key, "$value");
8893
}
8994
}
90-
writeEl($xml, $data);
95+
}
9196

92-
$xml->endElement();//write end element
93-
//returns the XML results
94-
return $xml->outputMemory(true);
97+
// Checks if array is an associative
98+
protected function isAssoc($array) {
99+
return (bool)count(array_filter(array_keys($array), 'is_string'));
95100
}
96101
}
97102

0 commit comments

Comments
 (0)