public function to_xml($data = NULL, $structure = NULL, $basenode = 'xml')
{
if ($data === NULL && func_num_args() === 0) {
$data = $this->_data;
}
if ($structure === NULL) {
$structure = simplexml_load_string("<?xml version='1.0' encoding='utf-8'?><{$basenode} />");
}
// Force it to be something useful
if (is_array($data) === FALSE && is_object($data) === FALSE) {
$data = (array) $data;
}
foreach ($data as $key => $value) {
//change false/true to 0/1
if (is_bool($value)) {
$value = (int) $value;
}
// no numeric keys in our xml please!
if (is_numeric($key)) {
// make string key...
$key = singular($basenode) != $basenode ? singular($basenode) : 'item';
}
// replace anything not alpha numeric
$key = preg_replace('/[^a-z_\\-0-9]/i', '', $key);
if ($key === '_attributes' && (is_array($value) || is_object($value))) {
$attributes = $value;
if (is_object($attributes)) {
$attributes = get_object_vars($attributes);
}
foreach ($attributes as $attribute_name => $attribute_value) {
$structure->addAttribute($attribute_name, $attribute_value);
}
} elseif (is_array($value) || is_object($value)) {
$node = $structure->addChild($key);
// recursive call.
$this->to_xml($value, $node, $key);
} else {
// add single node.
$value = htmlspecialchars(html_entity_decode($value, ENT_QUOTES, 'UTF-8'), ENT_QUOTES, 'UTF-8');
$structure->addChild($key, $value);
}
}
return $structure->asXML();
}