_dom->formatOutput = true; $this->_options = array(); } public function element($name, $value = '') { $elem = parent::element($name); $elem->push(); if (is_array($value)) { if (array_keys($value) === range(0, count($value) - 1)) $this->writeList($name, $value); else $this->writeHash($name, $value); } else if (is_object($value)) $this->writeObject($name, $value); else $this->writeText($name, $value); $elem->pop(); return $elem; } public function getTagOption($tag, $name, $default = false) { if (! array_key_exists($tag, $this->_options)) return $default; if (! array_key_exists($name, $this->_options[$tag])) return $default; return $this->_options[$tag][$name]; } public function hasTagOption($tag, $name) { if (! array_key_exists($tag, $this->_options)) return false; return array_key_exists($name, $this->_options[$tag]); } public function setTagOption($tag, $name, $value) { if (! array_key_exists($tag, $this->_options)) $this->_options[$tag] = array(); $this->_options[$tag][$name] = $value; } protected $_options; protected function writeList($tag, $list) { /* This is an ugly hack */ if ($this->hasTagOption($tag, 'child')) $child = $this->getTagOption($tag, 'child'); else if (preg_match('/(.*)s$/', $tag, $match)) $child = $match[1]; else if (preg_match('/(.*)_tab$/', $tag, $match)) $child = $match[1]; else if (preg_match('/(.*)0$/', $tag, $match)) $child = $match[1]; else if (preg_match('/(.*)_nesttab$/', $tag, $match)) $child = $match[1] . '_tab'; else $child = 'item'; foreach ($list as $item) $this->element($child, $item); } protected function writeHash($tag, $hash) { foreach ($hash as $name => $value) $this->element($name, $value); } protected function writeObject($tag, $object) { foreach (get_object_vars($object) as $name => $value) $this->element($name, $value); } protected function writeText($tag, $text) { if ($text !== '') { $type = gettype($text); if ($type == 'boolean') $text = $text ? 'true' : 'false'; /* Check if special processing is required */ if ($this->getTagOption($tag, 'html', false)) $this->writeHTML($text); else if ($this->getTagOption($tag, 'xml', false)) $this->writeXML($text); /* Deprecated: use 'xml' option instead */ else if ($this->getTagOption($tag, 'raw', false)) $this->writeXML($text); else $this->top()->text($text); } } protected function writeHTML($text) { /* Transform entities as these break the XML processing */ $text = preg_replace('/ /', ' ', $text); // TODO other transformations return $this->writeXML($text); } protected function writeXML($text) { $this->top()->fragment($text); } } ?>