2 /* vim: set expandtab tabstop=4 shiftwidth=4: */
3 // +----------------------------------------------------------------------+
5 // +----------------------------------------------------------------------+
6 // | Copyright (c) 1997-2002 The PHP Group |
7 // +----------------------------------------------------------------------+
8 // | This source file is subject to version 2.02 of the PHP license, |
9 // | that is bundled with this package in the file LICENSE, and is |
10 // | available at through the world-wide-web at |
11 // | http://www.php.net/license/2_02.txt. |
12 // | If you did not receive a copy of the PHP license and are unable to |
13 // | obtain it through the world-wide-web, please send a note to |
14 // | license@php.net so we can mail you a copy immediately. |
15 // +----------------------------------------------------------------------+
16 // | Authors: Alan Knowles <alan@akbkhome.com> |
17 // +----------------------------------------------------------------------+
19 // $Id: php_pear_headers,v 1.1 2002/04/22 09:51:27 alan_k Exp $
21 // This is a wrapper class for XML_Tree to lets you add callbacks to xml tags
22 // to enable data morphing (so you can get associative arrays and the like from
27 require_once 'XML/Tree.php';
37 * require_once 'XML/Tree/Morph.php';
40 * $x = new XML_Tree_Morph(
45 * //tag // either toObject/toArray or your function/method etc.
46 * 'project' => 'toObject',
47 * 'widget' => 'toObject',
48 * 'child' => 'toObject',
53 * $tree = $x->getTreeFromFile();
54 * print_r($tree->children[0]['project']);
55 * print_r($tree->children[1]['widget']);
61 * [name] => validatemanager
62 * [program_name] => validatemanage
64 * [source_directory] => src
65 * [pixmaps_directory] => pixmaps
67 * [gnome_support] => False
68 * [gettext_support] => False
72 * [class] => GtkWindow
74 * [title] => Gtk_ValidateManager
75 * [type] => GTK_WINDOW_TOPLEVEL
76 * [position] => GTK_WIN_POS_CENTER
78 * [default_width] => 600
79 * [default_height] => 400
80 * [allow_shrink] => False
81 * [allow_grow] => True
82 * [auto_shrink] => False
83 * [widget] => stdClass Object
87 * @version $Id: php_phpdoc_class,v 1.1 2002/04/22 10:20:29 alan_k Exp $
89 class XML_Tree_Morph extends XML_Tree {
103 * @param string Filename
104 * @param array $options
108 * tagname => callback
110 * where callback can be
111 * - toObject|toArary = built in converters
112 * - 'function', array($object,'method') or
113 * array('class','staticMethod');
119 function __construct($filename,$options) {
121 parent::__construct($filename,'1.0');
123 $this->_morphOptions = $options;
128 * Overridden endHandler which relays into callbacks..
130 * @see XML_Tree:endHandler();
135 function endHandler($elem) {
139 // recover the node created in StartHandler
140 $node = $this->obj[$obj_id];
142 //var_dump($this->cdata, typeof($node->children);
143 if (is_array($node->children) && count($node->children) > 0) {
144 if (!empty($this->cdata) && trim($this->cdata)) {
145 $node->children[] = new XML_Tree_Node(null, $this->cdata);
147 } else if ($this->cdata !== null) {
149 $node->setContent($this->cdata);
150 //var_dump(array($node, $this->cdata));
152 $node->setContent('');
154 $parent_id = ($this->i - 1);
155 $parent = $this->obj[$parent_id];
156 // attach the node to its parent node children array
157 // print_r($this);exit;
158 if (isset($this->_morphOptions['filter'][$node->name])) {
159 $f = $this->_morphOptions['filter'][$node->name];
160 if (is_string($f) && method_exists($this,'morph'.$f)) {
161 $parent->children[] = $this->{'morph'.$this->_morphOptions['filter'][$node->name]}($node);
165 if (is_callable($f)) {
166 $parent->children[] = call_user_func($f, $node);
174 if (@$this->_morphOptions['debug']) {
175 echo "SKIP: {$node->name}\n";
\r
178 $parent->children[] = $node;
190 * Converts standard <xxx>vvvv</xxxx> into
194 * @param object XML_Tree_Node
197 * @return array (name => array(keys=>values)
200 function morphToArray($node) {
202 foreach($node->children as $cnode) {
204 if (is_a($cnode,'xml_tree_node')) {
205 $ret[$cnode->name] = $cnode->content;
208 // otherwise it's an array...
211 foreach($cnode as $k=>$o) {
212 if (empty($ret[$k])) {
215 if (!is_array($ret[$k])) {
216 echo "OPPS: $k already in array ";print_r($ret);
217 echo "TRYING TO ADD "; print_r($cnode);
224 foreach($ret as $k=>$v) {
225 if (is_array($v) && count($v) == 1 ) {
231 return array($node->name => $ret);
236 * Converts standard <xxx>vvvv</xxxx> into
240 * @param object XML_Tree_Node
243 * @return array ($node->name => $object);
246 function morphToObject($node) {
248 foreach($node->children as $cnode) {
250 if (is_a($cnode,'xml_tree_node')) {
251 $ret->{$cnode->name} = $cnode->content;
254 // otherwise it's an array...
257 foreach($cnode as $k=>$o) {
258 if (empty($ret->{$k})) {
261 if (!is_array($ret->{$k})) {
262 echo "OPPS: $k already in array ";print_r($ret);
263 echo "TRYING TO ADD "; print_r($cnode);
270 foreach($ret as $k=>$v) {
271 if (is_array($v) && count($v) == 1 ) {
277 return array($node->name => $ret);