00001 <?php
00002
00016
00017
00018
00019 import('core.ItemIterator');
00020
00021 class VirtualArrayIterator extends ItemIterator {
00023 var $theArray;
00024
00026 var $itemsPerPage;
00027
00029 var $page;
00030
00032 var $count;
00033
00035 var $wasEmpty;
00036
00044 function VirtualArrayIterator(&$theArray, $totalItems, $page=-1, $itemsPerPage=-1) {
00045 if ($page>=1 && $itemsPerPage>=1) {
00046 $this->page = $page;
00047 } else {
00048 $this->page = 1;
00049 $this->itemsPerPage = max(count($this->theArray),1);
00050 }
00051 $this->theArray = &$theArray;
00052 $this->count = $totalItems;
00053 $this->itemsPerPage = $itemsPerPage;
00054 $this->wasEmpty = count($this->theArray)==0;
00055 reset($this->theArray);
00056 }
00057
00062 function &next() {
00063 if (!is_array($this->theArray)) {
00064 $nullVar = null;
00065 return $nullVar;
00066 }
00067 $value = ¤t($this->theArray);
00068 if (next($this->theArray)==null) {
00069 $this->theArray = null;
00070 }
00071 return $value;
00072 }
00073
00078 function &nextWithKey() {
00079 $key = key($this->theArray);
00080 $value = $this->next();
00081 return array($key, $value);
00082 }
00083
00088 function atFirstPage() {
00089 return $this->page==1;
00090 }
00091
00096 function atLastPage() {
00097 return ($this->page * $this->itemsPerPage) + 1 > $this->count;
00098 }
00099
00104 function getPage() {
00105 return $this->page;
00106 }
00107
00112 function getCount() {
00113 return $this->count;
00114 }
00115
00120 function getPageCount() {
00121 return max(1, ceil($this->count / $this->itemsPerPage));
00122 }
00123
00130 function eof() {
00131 return (($this->theArray == null) || (count($this->theArray)==0));
00132 }
00133
00138 function wasEmpty() {
00139 return $this->wasEmpty;
00140 }
00141
00146 function &toArray() {
00147 return $this->theArray;
00148 }
00149
00150 function array_slice_key($array, $offset, $len=-1) {
00151
00152
00153
00154
00155
00156
00157
00158 if (!is_array($array)) return false;
00159
00160 $return = array();
00161 $length = $len >= 0? $len: count($array);
00162 $keys = array_slice(array_keys($array), $offset, $length);
00163 foreach($keys as $key) {
00164 $return[$key] = $array[$key];
00165 }
00166
00167 return $return;
00168 }
00169 }
00170
00171 ?>