| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Based on: http://stackoverflow.com/questions/99350/passing-php-associative-arrays-to-and-from-xml |
| 5 |
*/ |
| 6 |
class ArrayToXML |
| 7 |
{ |
| 8 |
private $version; |
| 9 |
private $encoding; |
| 10 |
|
| 11 |
/** |
| 12 |
* Construct ArrayToXML object with selected version and encoding |
| 13 |
* |
| 14 |
* for available values check XmlWriter docs http://www.php.net/manual/en/function.xmlwriter-start-document.php |
| 15 |
* @param string $xmlVersion XML Version, default 1.0 |
| 16 |
* @param string $xmlEncoding XML Encoding, default UTF-8 |
| 17 |
*/ |
| 18 |
public function __construct($xmlVersion = '1.0', $xmlEncoding = 'UTF-8') |
| 19 |
{ |
| 20 |
$this->version = $xmlVersion; |
| 21 |
$this->encoding = $xmlEncoding; |
| 22 |
} |
| 23 |
|
| 24 |
/** |
| 25 |
* Build an XML Data Set |
| 26 |
* |
| 27 |
* @param array $data Associative Array containing values to be parsed into an XML Data Set(s) |
| 28 |
* @param string $startElement Root Opening Tag, default data |
| 29 |
* @return string XML String containing values |
| 30 |
* @return mixed Boolean false on failure, string XML result on success |
| 31 |
*/ |
| 32 |
public function buildXML($data, $startElement = 'data') |
| 33 |
{ |
| 34 |
if (!is_array($data)) { |
| 35 |
$err = 'Invalid variable type supplied, expected array not found on line ' . __LINE__ . ' in Class: ' . __CLASS__ . ' Method: ' . __METHOD__; |
| 36 |
trigger_error($err); |
| 37 |
return false; //return false error occurred |
| 38 |
} |
| 39 |
$xml = new XmlWriter(); |
| 40 |
$xml->openMemory(); |
| 41 |
$xml->startDocument($this->version, $this->encoding); |
| 42 |
$xml->startElement($startElement); |
| 43 |
|
| 44 |
$data = $this->writeAttr($xml, $data); |
| 45 |
$this->writeEl($xml, $data); |
| 46 |
|
| 47 |
$xml->endElement(); //write end element |
| 48 |
//returns the XML results |
| 49 |
return $xml->outputMemory(true); |
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* Write keys in $data prefixed with @ as XML attributes, if $data is an array. |
| 54 |
* When an @ prefixed key is found, a '%' key is expected to indicate the element itself, |
| 55 |
* and '#' prefixed key indicates CDATA content |
| 56 |
* |
| 57 |
* @param XMLWriter $xml object |
| 58 |
* @param array $data with attributes filtered out |
| 59 |
* @return array $data | $nonAttributes |
| 60 |
*/ |
| 61 |
protected function writeAttr(XMLWriter $xml, $data) |
| 62 |
{ |
| 63 |
if (is_array($data)) { |
| 64 |
$nonAttributes = array(); |
| 65 |
foreach ($data as $key => $val) { |
| 66 |
//handle an attribute with elements |
| 67 |
if ($key[0] == '@') { |
| 68 |
$xml->writeAttribute(substr($key, 1), $val); |
| 69 |
} else if ($key[0] == '%') { |
| 70 |
if (is_array($val)) $nonAttributes = $val; |
| 71 |
else $xml->text($val); |
| 72 |
} elseif ($key[0] == '#') { |
| 73 |
if (is_array($val)) $nonAttributes = $val; |
| 74 |
else { |
| 75 |
$xml->startElement(substr($key, 1)); |
| 76 |
$xml->writeCData($val); |
| 77 |
$xml->endElement(); |
| 78 |
} |
| 79 |
}else if($key[0] == "!"){ |
| 80 |
if (is_array($val)) $nonAttributes = $val; |
| 81 |
else $xml->writeCData($val); |
| 82 |
} |
| 83 |
//ignore normal elements |
| 84 |
else $nonAttributes[$key] = $val; |
| 85 |
} |
| 86 |
return $nonAttributes; |
| 87 |
} else return $data; |
| 88 |
} |
| 89 |
|
| 90 |
/** |
| 91 |
* Write XML as per Associative Array |
| 92 |
* |
| 93 |
* @param XMLWriter $xml object |
| 94 |
* @param array $data Associative Data Array |
| 95 |
*/ |
| 96 |
protected function writeEl(XMLWriter $xml, $data) |
| 97 |
{ |
| 98 |
foreach ($data as $key => $value) { |
| 99 |
if (is_array($value) && !$this->isAssoc($value)) { //numeric array |
| 100 |
foreach ($value as $itemValue) { |
| 101 |
if (is_array($itemValue)) { |
| 102 |
$xml->startElement($key); |
| 103 |
$itemValue = $this->writeAttr($xml, $itemValue); |
| 104 |
$this->writeEl($xml, $itemValue); |
| 105 |
$xml->endElement(); |
| 106 |
} else { |
| 107 |
$itemValue = $this->writeAttr($xml, $itemValue); |
| 108 |
$xml->writeElement($key, "$itemValue"); |
| 109 |
} |
| 110 |
} |
| 111 |
} else if (is_array($value)) { //associative array |
| 112 |
$xml->startElement($key); |
| 113 |
$value = $this->writeAttr($xml, $value); |
| 114 |
$this->writeEl($xml, $value); |
| 115 |
$xml->endElement(); |
| 116 |
} else { //scalar |
| 117 |
$value = $this->writeAttr($xml, $value); |
| 118 |
$xml->writeElement($key, "$value"); |
| 119 |
} |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
/** |
| 124 |
* Check if array is associative with string based keys |
| 125 |
* FROM: http://stackoverflow.com/questions/173400/php-arrays-a-good-way-to-check-if-an-array-is-associative-or-sequential/4254008#4254008 |
| 126 |
* |
| 127 |
* @param array $array Array to check |
| 128 |
* @return bool |
| 129 |
*/ |
| 130 |
protected function isAssoc($array) |
| 131 |
{ |
| 132 |
return (bool)count(array_filter(array_keys($array), 'is_string')); |
| 133 |
} |
| 134 |
} |
| 135 |
|