protected function __toXml(array $array = array(), $rootName = 'item', $addOpenTag = false, $addCdata = true)
{
$xml = '';
if ($addOpenTag) {
$xml .= '<?xml version="1.0" encoding="UTF-8"?>' . PHP_EOL;
}
if (empty($array)) {
$array = $this->toArray();
}
if (!empty($rootName) and !is_numeric($rootName)) {
$xml .= '<' . $rootName;
if (isset($array['id'])) {
$xml .= ' id="' . $array['id'] . '"';
unset($array['id']);
}
$xml .= '>' . PHP_EOL;
}
foreach ($array as $fieldName => $fieldValue) {
if (is_array($fieldValue)) {
if (!empty($fieldValue)) {
$xml .= $this->__toXml($fieldValue, $fieldName);
continue;
}
$fieldValue = '';
} elseif (is_object($fieldValue) and method_exists($fieldValue, 'toXml')) {
$xml .= $fieldValue->toXml(array(), $fieldValue->name);
continue;
}
if ($addCdata === true) {
$fieldValue = '<![CDATA[' . $fieldValue . ']]>';
$xml .= '<' . $fieldName . '>' . $fieldValue . '</' . $fieldName . '>' . PHP_EOL;
} else {
$fieldValue = htmlentities($fieldValue);
$xml .= '<' . $fieldName . '>' . $fieldValue . '</' . $fieldName . '>' . PHP_EOL;
}
}
if (!empty($rootName) and !is_numeric($rootName)) {
$xml .= '</' . $rootName . '>' . PHP_EOL;
}
return $xml;
}