Open Monograph Press  1.1
 All Classes Namespaces Functions Variables Groups Pages
ArrayItemIterator.inc.php
1 <?php
2 
17 import('lib.pkp.classes.core.ItemIterator');
18 
21  var $theArray;
22 
25 
27  var $page;
28 
30  var $count;
31 
33  var $wasEmpty;
34 
41  function ArrayItemIterator(&$theArray, $page=-1, $itemsPerPage=-1) {
42  if ($page>=1 && $itemsPerPage>=1) {
43  $this->theArray = $this->array_slice_key($theArray, ($page-1) * $itemsPerPage, $itemsPerPage);
44  $this->page = $page;
45  } else {
46  $this->theArray =& $theArray;
47  $this->page = 1;
48  $this->itemsPerPage = max(count($this->theArray),1);
49  }
50  $this->count = count($theArray);
51  $this->itemsPerPage = $itemsPerPage;
52  $this->wasEmpty = count($this->theArray)==0;
53  reset($this->theArray);
54  }
55 
61  function &fromRangeInfo(&$theArray, &$theRange) {
62  if ($theRange && $theRange->isValid()) {
63  $theIterator = new ArrayItemIterator($theArray, $theRange->getPage(), $theRange->getCount());
64  } else {
65  $theIterator = new ArrayItemIterator($theArray);
66  }
67  return $theIterator;
68  }
69 
74  function &next() {
75  if (!is_array($this->theArray)) {
76  $value = null;
77  return $value;
78  }
79  $value = current($this->theArray);
80  if (next($this->theArray)===false) {
81  $this->theArray = null;
82  }
83  return $value;
84  }
85 
90  function nextWithKey() {
91  $key = key($this->theArray);
92  $value = $this->next();
93  return array($key, $value);
94  }
95 
100  function atFirstPage() {
101  return $this->page==1;
102  }
103 
108  function atLastPage() {
109  return ($this->page * $this->itemsPerPage) + 1 > $this->count;
110  }
111 
116  function getPage() {
117  return $this->page;
118  }
119 
124  function getCount() {
125  return $this->count;
126  }
127 
132  function getPageCount() {
133  return max(1, ceil($this->count / $this->itemsPerPage));
134  }
135 
140  function eof() {
141  return (($this->theArray == null) || (count($this->theArray)==0));
142  }
143 
148  function wasEmpty() {
149  return $this->wasEmpty;
150  }
151 
156  function &toArray() {
157  return $this->theArray;
158  }
159 
164  function isInBounds() {
165  return ($this->getPageCount() >= $this->page);
166  }
167 
172  function &getLastPageRangeInfo() {
173  import('lib.pkp.classes.db.DBResultRange');
174  $returner = new DBResultRange(
175  $this->itemsPerPage,
176  $this->getPageCount()
177  );
178  return $returner;
179  }
180 
189  function array_slice_key($array, $offset, $len=-1) {
190  if (!is_array($array)) return false;
191 
192  $return = array();
193  $length = $len >= 0? $len: count($array);
194  $keys = array_slice(array_keys($array), $offset, $length);
195  foreach($keys as $key) {
196  $return[$key] = $array[$key];
197  }
198 
199  return $return;
200  }
201 }
202 
203 ?>
Provides paging and iteration for arrays.
array_slice_key($array, $offset, $len=-1)
Container class for range information when retrieving a result set.
Generic iterator class; needs to be overloaded by subclasses providing specific implementations.
& fromRangeInfo(&$theArray, &$theRange)
ArrayItemIterator(&$theArray, $page=-1, $itemsPerPage=-1)