10 defined(
'JPATH_PLATFORM') or die;
33 public function objectToString($object, $options = array())
35 $rootName = (isset($options[
'name'])) ? $options[
'name'] :
'registry';
36 $nodeName = (isset($options[
'nodeName'])) ? $options[
'nodeName'] :
'node';
39 $root = simplexml_load_string(
'<' . $rootName .
' />');
42 $this->getXmlChildren($root, $object, $nodeName);
44 return $root->asXML();
57 public function stringToObject($data, array $options = array())
62 $xml = simplexml_load_string($data);
64 foreach ($xml->children() as $node)
66 $obj->$node[
'name'] = $this->getValueFromNode($node);
81 protected function getValueFromNode($node)
83 switch ($node[
'type'])
86 $value = (string) $node;
91 return (
string) $node;
94 $value = (string) $node;
99 $value = (string) $node;
101 return (
float) $value;
106 foreach ($node->children() as $child)
108 $value[(string) $child[
'name']] = $this->getValueFromNode($child);
112 $value =
new stdClass;
114 foreach ($node->children() as $child)
116 $value->$child[
'name'] = $this->getValueFromNode($child);
135 protected function getXmlChildren(SimpleXMLElement $node, $var, $nodeName)
138 foreach ((array) $var as $k => $v)
142 $n = $node->addChild($nodeName, $v);
143 $n->addAttribute(
'name', $k);
144 $n->addAttribute(
'type', gettype($v));
148 $n = $node->addChild($nodeName);
149 $n->addAttribute(
'name', $k);
150 $n->addAttribute(
'type', gettype($v));
152 $this->getXmlChildren($n, $v, $nodeName);