00001 <?php
00002
00015
00016
00017
00018 import('core.ItemIterator');
00019
00020 class ArrayItemIterator extends ItemIterator {
00022 var $theArray;
00023
00025 var $itemsPerPage;
00026
00028 var $page;
00029
00031 var $count;
00032
00034 var $wasEmpty;
00035
00042 function ArrayItemIterator(&$theArray, $page=-1, $itemsPerPage=-1) {
00043 if ($page>=1 && $itemsPerPage>=1) {
00044 $this->theArray = $this->array_slice_key($theArray, ($page-1) * $itemsPerPage, $itemsPerPage);
00045 $this->page = $page;
00046 } else {
00047 $this->theArray = &$theArray;
00048 $this->page = 1;
00049 $this->itemsPerPage = max(count($this->theArray),1);
00050 }
00051 $this->count = count($theArray);
00052 $this->itemsPerPage = $itemsPerPage;
00053 $this->wasEmpty = count($this->theArray)==0;
00054 reset($this->theArray);
00055 }
00056
00061 function &next() {
00062 if (!is_array($this->theArray)) {
00063 $value = null;
00064 return $value;
00065 }
00066 $value = current($this->theArray);
00067 if (next($this->theArray)==null) {
00068 $this->theArray = null;
00069 }
00070 return $value;
00071 }
00072
00077 function &nextWithKey() {
00078 $key = key($this->theArray);
00079 $value = $this->next();
00080 return array($key, $value);
00081 }
00082
00087 function atFirstPage() {
00088 return $this->page==1;
00089 }
00090
00095 function atLastPage() {
00096 return ($this->page * $this->itemsPerPage) + 1 > $this->count;
00097 }
00098
00103 function getPage() {
00104 return $this->page;
00105 }
00106
00111 function getCount() {
00112 return $this->count;
00113 }
00114
00119 function getPageCount() {
00120 return max(1, ceil($this->count / $this->itemsPerPage));
00121 }
00122
00127 function eof() {
00128 return (($this->theArray == null) || (count($this->theArray)==0));
00129 }
00130
00135 function wasEmpty() {
00136 return $this->wasEmpty;
00137 }
00138
00143 function &toArray() {
00144 return $this->theArray;
00145 }
00146
00147 function array_slice_key($array, $offset, $len=-1) {
00148
00149
00150
00151
00152
00153
00154
00155 if (!is_array($array)) return false;
00156
00157 $return = array();
00158 $length = $len >= 0? $len: count($array);
00159 $keys = array_slice(array_keys($array), $offset, $length);
00160 foreach($keys as $key) {
00161 $return[$key] = $array[$key];
00162 }
00163
00164 return $return;
00165 }
00166
00171 function isInBounds() {
00172 return ($this->getPageCount() >= $this->page);
00173 }
00174
00179 function &getLastPageRangeInfo() {
00180 import('db.DBResultRange');
00181 $returner =& new DBResultRange(
00182 $this->itemsPerPage,
00183 $this->getPageCount()
00184 );
00185 return $returner;
00186 }
00187 }
00188
00189 ?>