/[cvs]/nfo/php/libs/net.php.pear/Tree/Memory/Array.php
ViewVC logotype

Contents of /nfo/php/libs/net.php.pear/Tree/Memory/Array.php

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.1 - (show annotations)
Thu Feb 27 16:49:58 2003 UTC (21 years, 6 months ago) by joko
Branch: MAIN
+ initial commit, from PEAR

1 <?php
2 //
3 // +----------------------------------------------------------------------+
4 // | PHP Version 4 |
5 // +----------------------------------------------------------------------+
6 // | Copyright (c) 1997-2003 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: Wolfram Kriesing <wolfram@kriesing.de> |
17 // +----------------------------------------------------------------------+
18 //
19 // Id: Array.php,v 1.5 2003/01/18 15:36:22 cain Exp
20 // $Id: Array.php,v 1.5 2003/01/18 15:36:22 cain Exp $
21
22 require_once('Tree/Error.php');
23
24 /**
25 * EXPERIMENTAL
26 *
27 * @access public
28 * @author Wolfram Kriesing <wolfram@kriesing.de>
29 * @version 2002/08/30
30 * @package Tree
31 */
32 class Tree_Memory_Array
33 {
34
35 var $data = array();
36
37 /**
38 * this is the internal id that will be assigned if no id is given
39 * it simply counts from 1, so we can check if( $id ) i am lazy :-)
40 */
41 var $_id = 1;
42
43 /**
44 * set up this object
45 *
46 * @version 2002/08/30
47 * @access public
48 * @author Wolfram Kriesing <wolfram@kriesing.de>
49 * @param string $dsn the path on the filesystem
50 * @param array $options additional options you can set
51 */
52 function Tree_Memory_Array( &$array , $options=array() )
53 {
54 $this->_array = &$array;
55 $this->_options = $options; // not in use currently
56 } // end of function
57
58 /**
59 *
60 *
61 * @version 2002/08/30
62 * @access public
63 * @author Wolfram Kriesing <wolfram@kriesing.de>
64 * @return boolean true on success
65 */
66 function setup()
67 {
68 unset($this->data); // unset the data to be sure to get the real data again, no old data
69 if( is_array($this->_array) )
70 {
71 $this->data[0] = null;
72 $theData = array(&$this->_array);
73 $this->_setup($theData);
74 }
75
76 return $this->data;
77 }
78
79 /**
80 * we modify the $this->_array in here, we also add the id
81 * so methods like 'add' etc can find the elements they are searching for,
82 * if you dont like your data to be modified dont pass them as reference!
83 */
84 function _setup( &$array , $parentId=0 )
85 {
86 foreach( $array as $nodeKey=>$aNode )
87 {
88 $newData = $aNode;
89 if ( !isset($newData['id']) || !$newData['id'] ) {
90 $newData['id'] = $this->_id++;
91 $array[$nodeKey]['id'] = $newData['id'];
92 }
93
94 $newData['parentId'] = $parentId;
95 $children = null;
96 foreach ( $newData as $key=>$val ) // remove the 'children' array, since this is only info for this class
97 {
98 if( $key=='children' )
99 {
100 unset($newData[$key]);
101 }
102 }
103
104 $this->data[] = $newData;
105 if ( isset($aNode['children']) && $aNode['children'] ) {
106 if( !isset($array[$nodeKey]['children']) ) {
107 $array[$nodeKey]['children'] = array();
108 }
109 $this->_setup( $array[$nodeKey]['children'] , $newData['id'] );
110 }
111 }
112 }
113
114
115 /**
116 *
117 *
118 * @access private
119 * @version 2002/03/02
120 * @author Wolfram Kriesing <wolfram@kriesing.de>
121 * @param
122 * @return
123 */
124 function _throwError( $msg , $line , $mode=null )
125 {
126 return new Tree_Error( $msg , $line , __FILE__ , $mode , $this->db->last_query );
127 }
128
129 /**
130 * prepare multiple results
131 *
132 * @see _prepareResult()
133 * @access private
134 * @version 2002/03/03
135 * @author Wolfram Kriesing <wolfram@kriesing.de>
136 * @param
137 * @return
138 */
139 function _prepareResults( $results )
140 {
141 $newResults = array();
142 foreach( $results as $aResult )
143 $newResults[] = $this->_prepareResult($aResult);
144 return $newResults;
145 }
146
147 /**
148 * map back the index names to get what is expected
149 *
150 * @access private
151 * @version 2002/03/03
152 * @author Wolfram Kriesing <wolfram@kriesing.de>
153 * @param
154 * @return
155 */
156 function _prepareResult( $result )
157 {
158 $map = $this->getOption('columnNameMaps');
159
160 if( $map )
161 foreach( $map as $key=>$columnName )
162 {
163 $result[$key] = $result[$columnName];
164 unset($result[$columnName]);
165 }
166 return $result;
167 }
168
169 /**
170 *
171 */
172 function add( $data , $parentId , $previousId=0 )
173 {
174 $data['id'] = $this->_id++;
175 $data['parentId'] = $parentId;
176 $this->data[] = $data;
177
178 // add the element itself also in the source array, where we actually read the structure from
179 //$path = $this->getPathById($parentId);
180 array_walk($this->_array['children'],array(&$this,'_add'),array($data,$parentId));
181
182 //$this->_array
183 return $data['id'];
184 }
185
186 // this one was a real quicky !!!
187 function _add( &$val , $key , $data )
188 {
189 if( $val['id']==$data[1] )
190 $val['children'][] = $data[0];
191 else // if we havent found the new element go on searching
192 {
193 if( $val['children'] )
194 array_walk($val['children'],array(&$this,'_add'),$data);
195 }
196 }
197
198 } // end of class
199 ?>

MailToCvsAdmin">MailToCvsAdmin
ViewVC Help
Powered by ViewVC 1.1.26 RSS 2.0 feed