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/OArray.php";
00024 require_once
PT_IP .
"/Types/OHash.php";
00025 require_once
PT_IP .
"/Types/Iterator.php";
00026
00058 class PHPTAL_LoopControler
00059 {
00060 var
$_context;
00061 var
$_data;
00062 var
$_data_name;
00063 var
$_iterator;
00064 var
$_error;
00065
00076 function
PHPTAL_LoopControler(&$context, $data_name, $data)
00077 {
00078 $this->_context =& $context;
00079 $this->_data =& $data;
00080 $this->_data_name = $data_name;
00081
00082
00083
if (PEAR::isError($
data)) {
00084 $this->_error =& $data;
00085
return $data;
00086 }
00087
00088
00089
00090
00091
00092
00093
00094
if (is_object($
data)) {
00095
if (method_exists($
data,
"getNewIterator")) {
00096
00097 $this->_iterator =& $data->getNewIterator();
00098
00099 } elseif (is_a(
"iterator", $
data)
00100 || (method_exists($
data, '
next')
00101 && method_exists($
data, '
isValid')
00102 && method_exists($
data, '
index'))) {
00103
00104 $this->_iterator =& $data;
00105
00106 } elseif (get_class($
data) == 'db_result') {
00107
00108 $this->_iterator =
new PHPTAL_DBResultIterator($
data);
00109
00110 }
else {
00111
00112 $err =
new TypeError(
"PHPTAL loop controler received a non Iterable object ("
00113 . get_class($
data) .
")");
00114 $this->_error =& $err;
00115
return PEAR::raiseError($err);
00116 }
00117 } elseif (is_array($
data)) {
00118
00119
00120
00121 reset($
data);
00122
if (count($
data) > 0 && array_key_exists(0, $
data)) {
00123
00124 $this->_data =
new OArray($
data);
00125 $this->_iterator =& $this->_data->getNewIterator();
00126 }
else {
00127
00128 $this->_data =
new OHash($
data);
00129 $this->_iterator =& $this->_data->getNewIterator();
00130 }
00131 }
else {
00132 $err =
new TypeError(
"phptal loop controler received a non Iterable value ("
00133 . gettype($
data) .
")");
00134 $this->_error =& $err;
00135
return PEAR::raiseError($err);
00136 }
00137
00138
00139
00140
00141 $repeat =& $this->_context->get(
"repeat");
00142
if (array_key_exists($this->_data_name, $repeat)) {
00143 unset($repeat[$this->_data_name]);
00144 }
00145 $repeat[$this->_data_name] =& $this;
00146
00147
00148 $temp =& $this->_iterator->value();
00149 $this->_context->set($this->_data_name, $temp);
00150
return $temp;
00151 }
00152
00158 function
index()
00159 {
00160
return $this->_iterator->index();
00161 }
00162
00168 function
key()
00169 {
00170
if (method_exists($this->_iterator,
"key")) {
00171
return $this->_iterator->key();
00172 }
else {
00173
return $this->_iterator->index();
00174 }
00175 }
00176
00182 function
number()
00183 {
00184
return $this->
index() + 1;
00185 }
00186
00192 function
even()
00193 {
00194
return !$this->
odd();
00195 }
00196
00202 function
odd()
00203 {
00204
return ($this->
index() % 2);
00205 }
00206
00212 function
start()
00213 {
00214
return ($this->
index() == 0);
00215 }
00216
00222 function
end()
00223 {
00224
return ($this->
length() == $this->
number());
00225 }
00226
00227 function
isValid()
00228 {
00229
return $this->_iterator->isValid();
00230 }
00231
00237 function
length()
00238 {
00239
return $this->_data->size();
00240 }
00241
00247 function &
next()
00248 {
00249 $temp =& $this->_iterator->next();
00250
if (!$this->_iterator->isValid()) {
00251
return false;
00252 }
00253
00254
00255 $this->_context->set($this->_data_name, $temp);
00256
return $temp;
00257 }
00258 };
00259
00260
00269 class PHPTAL_DBResultIterator extends Iterator
00270 {
00271 var
$_src;
00272 var
$_index = -1;
00273 var
$_end =
false;
00274 var
$_value;
00275
00282 function
PHPTAL_DBResultIterator(&$result)
00283 {
00284 $this->_src =& $result;
00285 $this->
reset();
00286 }
00287
00288 function
reset()
00289 {
00290
if ($this->
size() == 0) {
00291 $this->_end =
true;
00292
return;
00293 }
00294
00295 $this->_index = 0;
00296 $this->_end =
false;
00297 unset($this->_value);
00298 $this->_value = $this->_src->fetchRow();
00299 }
00300
00306 function
size()
00307 {
00308
if (!isset($this->_size)) {
00309 $this->_size = $this->_src->numRows();
00310 }
00311
return $this->_size;
00312 }
00313
00317 function
isValid()
00318 {
00319
return !$this->_end;
00320 }
00321
00331 function &
next()
00332 {
00333
if ($this->_end || ++ $this->_index >= $this->
size()) {
00334 $this->_end =
true;
00335
return false;
00336 }
00337
00338 unset($this->_value);
00339 $this->_value = $this->_src->fetchRow();
00340
return $this->_value;
00341 }
00342
00348 function &
value()
00349 {
00350
return $this->_value;
00351 }
00352
00358 function
index()
00359 {
00360
return $this->_index;
00361 }
00362 }
00363
00364 ?>