00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 require_once
PT_IP .
"/Types.php";
00024 require_once
PT_IP .
"/Types/Errors.php";
00025 require_once
PT_IP .
"/Types/Ref.php";
00026
00041 class OHash
00042 {
00043 var
$_hash;
00044
00052 function
__construct($array=array())
00053 {
00054 $this->_hash = array();
00055 $keys = array_keys($array);
00056 foreach ($
keys as $key) {
00057 $this->_hash[$key] =
ref($array[$key]);
00058 }
00059 }
00060
00068 function
OHash($array=array())
00069 {
00070 $this->__construct($array);
00071 }
00072
00079 function
set($key, $value)
00080 {
00081 $this->_hash[$key] =
ref($value);
00082 }
00083
00092 function
setRef($key, &$value)
00093 {
00094 $this->_hash[$key] =
ref($value);
00095 }
00096
00102 function
setAll($hash)
00103 {
00104
if (!is_array($hash) && is_a($hash, 'OHash')) {
00105 $hash = $hash->toHash();
00106 }
00107 $keys = array_keys($hash);
00108 foreach ($
keys as $key) {
00109 $this->_hash[$key] =
ref($hash[$key]);
00110 }
00111 }
00112
00119 function &
get($key)
00120 {
00121
if ($this->
containsKey($key)) {
00122
return $this->_hash[$key]->obj;
00123 }
00124
return null;
00125 }
00126
00132 function
remove($key)
00133 {
00134 $keys = $this->
keys();
00135
$i = array_search($key, $keys);
00136
if (
$i !==
false) {
00137
00138
00139 unset($this->_hash[$key]);
00140
00141 }
00142 }
00143
00149 function
removeElement(&$o)
00150 {
00151
$i = 0;
00152 $found =
false;
00153 foreach ($this->_hash as $key => $value) {
00154
if ($value->obj === $o || $value == $o) {
00155 $found =
$i;
00156
break;
00157 }
00158
$i++;
00159 }
00160
if ($found !==
false) {
00161
return array_splice($this->_hash, $found, 1);
00162 }
00163 }
00164
00170 function
isEmpty()
00171 {
00172
return $this->
size() == 0;
00173 }
00174
00180 function
size()
00181 {
00182
return count($this->_hash);
00183 }
00184
00190 function &
values()
00191 {
00192 $v = array();
00193 foreach ($this->_hash as $key => $
ref) {
00194 $v[] =& $ref->obj;
00195 }
00196
return $v;
00197 }
00198
00204 function
keys()
00205 {
00206
return array_keys($this->_hash);
00207 }
00208
00214 function
getNewIterator()
00215 {
00216
return new HashIterator($
this);
00217 }
00218
00225 function
containsKey($key)
00226 {
00227
return array_key_exists($key, $this->_hash);
00228 }
00229
00236 function
containsValue($value)
00237 {
00238 foreach ($this->_hash as $k => $v) {
00239
if ($v->obj === $value || $v == $value) {
00240
return true;
00241 }
00242 }
00243
return false;
00244 }
00245
00251 function &
toHash()
00252 {
00253 $result = array();
00254 foreach ($this->_hash as $key => $value) {
00255 $result[$key] =& $value->obj;
00256 }
00257
return $result;
00258 }
00259
00272 function
ArrayToHash($array)
00273 {
00274 $h =
new OHash();
00275
while (count($array) > 1) {
00276 $key = array_shift($array);
00277 $h->set($key, array_shift($array));
00278 }
00279
return $h;
00280 }
00281
00287 function
toString()
00288 {
00289
return Types::_hashToString($this->toHash());
00290 }
00291
00295 function
sort()
00296 {
00297 ksort($this->_hash);
00298 }
00299 }
00300
00301
00309 class HashIterator
00310 {
00311 var
$_src;
00312 var
$_values;
00313 var
$_keys;
00314 var
$_key;
00315 var
$_value;
00316 var
$_i = -1;
00317 var
$_end =
false;
00318
00324 function
HashIterator(&$hash)
00325 {
00326
return $this->__construct($hash);
00327 }
00328
00337 function __construct(&$hash)
00338 {
00339
if (is_array($hash)) {
00340 $this->_src =
new OHash($hash);
00341 }
else if (is_a($hash, 'ohash')) {
00342 $this->_src = &$hash;
00343 }
else {
00344 $err =
new TypeError('HashIterator requires associative array or OHash');
00345
00346 die($err->toString());
00347
return PEAR::raiseError($err);
00348 }
00349 $this->
reset();
00350 }
00351
00352
00353
00354
00355
00359 function
reset()
00360 {
00361
00362
00363 $this->_values = $this->_src->_hash;
00364 $this->_keys = $this->_src->keys();
00365
00366
if (count($this->_keys) == 0) {
00367 $this->_end =
true;
00368
return;
00369 }
00370
00371 $this->_i = 0;
00372 $this->_end =
false;
00373 $this->_key = $this->_keys[0];
00374 $this->_value = $this->_values[$this->_key];
00375 }
00376
00380 function
isValid()
00381 {
00382
return !$this->_end;
00383 }
00384
00392 function &
next()
00393 {
00394
if ($this->_end || ++$this->_i >= count($this->_keys)){
00395 $this->_end =
true;
00396
return null;
00397 }
00398
00399 $this->_key = $this->_keys[$this->_i];
00400 $this->_value = $this->_values[$this->_key];
00401
return $this->_value->obj;
00402 }
00403
00404
00405
00406
00407
00413 function
key()
00414 {
00415
return $this->_key;
00416 }
00417
00423 function
index()
00424 {
00425
return $this->_i;
00426 }
00427
00433 function &
value()
00434 {
00435
return $this->_value->obj;
00436 }
00437
00438
00439
00440
00441
00445 function
remove()
00446 {
00447 $this->_src->remove($this->_key);
00448 }
00449 }
00450
00451 ?>