2 /* vim: set expandtab tabstop=4 shiftwidth=4: */
4 // Copyright (c) 2003 Laurent Bedubourg
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 // Authors: Laurent Bedubourg <laurent.bedubourg@free.fr>
23 require_once PT_IP
. "/Types.php";
24 require_once PT_IP
. "/Types/Errors.php";
25 require_once PT_IP
. "/Types/Ref.php";
26 require_once PT_IP
. "/Types/Iterator.php";
29 * Class wrapper for oriented object arrays.
31 * This class implements a simple interface quite like java to handle array in
34 * @author Laurent Bedubourg <laurent.bedubourg@free.fr>
36 class OArray
extends Iterable
44 * @param array $array optional
45 * The php array this vector will manage.
47 function OArray($array=array())
49 $this->__construct($array);
53 * OArray php4.4 compliant constructor.
55 * @param array $array optional
56 * The php array this vector will manage.
58 function __construct($array=array())
60 $this->_array
= array();
61 for ($i = 0; $i < count($array); $i++
) {
62 $this->pushRef($array[$i]);
67 * Retrieve an iterator ready to used.
68 * @return ArrayIterator
70 function getNewIterator()
72 return new ArrayIterator($this);
76 * Returns the vector number of elements.
81 return count($this->_array
);
85 * Returns true if this vector is empty, false otherwise.
90 return $this->size() == 0;
94 * Retrieve element at specified index.
100 if ($i > $this->size() ||
$i < 0) {
101 $err = new OutOfBounds("OArray index out of bounds ($i)");
102 // return EX::raise($err);
103 return PEAR
::raiseError($err);
105 return $this->_array
[$i]->obj
;
109 * Retrieve index of specified element.
111 * @return int The element index of false if element not in vector.
113 function indexOf($element)
115 for ($i=0; $i < count($this->_array
); $i++
) {
116 if ($this->_array
[$i]->obj
=== $element) {
124 * Set the item at specified index.
126 * @throws OutOfBounds if index is greated that vector limit
127 * @return Old element
129 function &set($i, $item)
131 return $this->setRef($i, $item);
135 * Set the item at specified index (by reference).
137 * @throws OutOfBounds if index is greated that vector limit
138 * @return Old element
140 function &setRef($i, &$item)
142 if ($i > $this->size() ||
$i < 0) {
143 $err = new OutOfBounds("OArray index out of bounds ($i)");
144 // return EX::raise($err);
145 return PEAR
::raiseError($err);
147 $temp = $this->_array
[$i];
148 $this->_array
[$i] = Ref($item);
153 * Test if the vector contains the specified element.
155 * @param mixed $item The item we look for.
157 function contains($o)
159 for ($i = 0; $i < $this->size(); $i++
) {
160 if ($this->_array
[$i]->obj
=== $o) return true;
166 * Add an element to the vector.
168 * @param mixed $o The item to add.
176 * Remove object from this vector.
178 * @param mixed $o Object to remove.
179 * @return boolean true if object removed false if not found
183 $i = $this->indexOf($o);
185 $this->removeIndex($i);
192 * Remove specified index from vector.
194 * @param int $i Index
195 * @throws OutOfBounds
197 function removeIndex($i)
199 if ($i > $this->size() ||
$i < 0 ) {
200 $err = new OutOfBounds("OArray index out of bounds ($i)");
201 // return EX::raise($err);
202 return PEAR
::raiseError($err);
205 // $this->_array = array_splice($this->_array, $i, 1);
206 array_splice($this->_array
, $i, 1);
214 $this->_array
= array();
218 * Add an element at the end of the vector (same as add()).
220 * @param mixed $o Item to append to vector.
224 array_push($this->_array
, Ref($o));
228 * Add an element at the end of the vector (same as add()).
230 * @param mixed $o Item to append to vector.
232 function pushRef(&$o)
234 $this->_array
[] = Ref($o);
238 * Retrieve vector values.
240 * The returned array contains references to internal data.
247 for ($i = 0; $i < $this->size(); $i++
) {
248 $v[] =& $this->_array
[$i]->obj
;
254 * Remove the last element of the vector and returns it.
260 $ref = array_pop($this->_array
);
265 * Extract and return first element of OArray.
270 $ref = array_shift($this->_array
);
275 * Retrieve a php array for this vector.
281 return $this->values();
285 * Retrieve a string representation of the array.
289 return Types
::_arrayToString($this->values());
294 * OArray itertor class.
296 * This kind of iterators are used to walk throug a OArray object.
298 * @author Laurent Bedubourg <laurent.bedubourg@free.fr>
300 class ArrayIterator
extends Iterator
308 * Iterator constructor.
310 * @param mixed $oarray OArray object or php array.
312 function ArrayIterator(&$oarray)
314 return $this->__construct($oarray);
318 * PHP 4.4 compliant constructor.
320 * @param mixed $oarray OArray object or php array.
322 function __construct(&$oarray)
324 if (is_array($oarray)) {
325 $this->_src
= new OArray($oarray);
326 } else if (is_a($oarray, 'OArray')) {
327 $this->_src
= &$oarray;
329 // ignore error, should throw a bad type error or something like
330 // that but pear does not handle errors in constructors yet
331 $err = new TypeError("ArrayIterator requires OArray object or php array.",
333 // return EX::raise($err);
334 return PEAR
::raiseError($err);
340 * Reset iterator to first array position.
346 $this->_values
= $this->_src
->_array
;
347 if (count($this->_values
) == 0) {
352 $this->_value
= $this->_values
[0];
356 * Returns next vector item value.
359 * @throws OutOfBounds if iterator overpass the vector size.
363 if ($this->_end || ++
$this->_i
>= count($this->_values
)) {
368 $this->_value
= $this->_values
[$this->_i
];
369 return $this->_value
->obj
;
373 * Test if the iteractor has not reached its end.
382 * Retrieve current iterator index.
391 * Retrieve the current iterator value.
396 return $this->_value
->obj
;
400 * Delete current index.
404 $this->_src
->remove($this->_value
->obj
);