From 3b169fe46119eb733e697bdc5506d1d04e2208f8 Mon Sep 17 00:00:00 2001 From: Urban Müller Date: Thu, 21 Jan 2010 14:20:40 +0000 Subject: allow setting of subtrees --- it_xml.class | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) (limited to 'it_xml.class') diff --git a/it_xml.class b/it_xml.class index e38d7de..7dd82c7 100644 --- a/it_xml.class +++ b/it_xml.class @@ -327,6 +327,44 @@ function error() return is_a($this, "it_xml") ? $this->error : $GLOBALS['IT_XML_ERROR']; } +/** + * Set nodes as subnode of current node + * @param values assoc array of key => value pairs. May contain associative or numeric subarrays. + */ + +function set($vals) +{ + if (is_array($vals)) + { + foreach ($vals as $key => $val) + { + $classname = $this->_p['prefix'] . $this->_make_identifier($key); + if (!class_exists($classname)) + eval("class $classname extends it_xml {}"); + + if (is_array($val) && $val === array_values($val)) + { + $arr = (array)$this->$key; + foreach ($val as $i => $v) + { + if (!is_a($arr[$i], "it_xml")) + $arr[$i] = new $classname; + $arr[$i]->set($v); + } + $this->$key = $arr; + } + else + { + if (!is_a($this->$key, "it_xml")) + $this->$key = new $classname; + $this->$key->set($val); + } + } + } + else + $this->val = $vals; +} + } ?> -- cgit v1.2.3