Open Journal Systems  3.3.0
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 __construct(&$theArray, $page=-1, $itemsPerPage=-1) {
42  parent::__construct();
43  if ($page>=1 && $itemsPerPage>=1) {
44  $this->theArray = $this->array_slice_key($theArray, ($page-1) * $itemsPerPage, $itemsPerPage);
45  $this->page = $page;
46  } else {
47  $this->theArray =& $theArray;
48  $this->page = 1;
49  $this->itemsPerPage = max(count($this->theArray),1);
50  }
51  $this->count = count($theArray);
52  $this->itemsPerPage = $itemsPerPage;
53  $this->wasEmpty = count($this->theArray)==0;
54  reset($this->theArray);
55  }
56 
62  function &fromRangeInfo(&$theArray, &$theRange) {
63  if ($theRange && $theRange->isValid()) {
64  $theIterator = new ArrayItemIterator($theArray, $theRange->getPage(), $theRange->getCount());
65  } else {
66  $theIterator = new ArrayItemIterator($theArray);
67  }
68  return $theIterator;
69  }
70 
75  function &next() {
76  if (!is_array($this->theArray)) {
77  $value = null;
78  return $value;
79  }
80  $value = current($this->theArray);
81  if (next($this->theArray)===false) {
82  $this->theArray = null;
83  }
84  return $value;
85  }
86 
91  function nextWithKey() {
92  $key = key($this->theArray);
93  $value = $this->next();
94  return array($key, $value);
95  }
96 
101  function atFirstPage() {
102  return $this->page==1;
103  }
104 
109  function atLastPage() {
110  return ($this->page * $this->itemsPerPage) + 1 > $this->count;
111  }
112 
117  function getPage() {
118  return $this->page;
119  }
120 
125  function getCount() {
126  return $this->count;
127  }
128 
133  function getPageCount() {
134  return max(1, ceil($this->count / $this->itemsPerPage));
135  }
136 
141  function eof() {
142  return (($this->theArray == null) || (count($this->theArray)==0));
143  }
144 
149  function wasEmpty() {
150  return $this->wasEmpty;
151  }
152 
157  function &toArray() {
158  return $this->theArray;
159  }
160 
164  function &toAssociativeArray() {
165  return $this->theArray;
166  }
167 
176  function array_slice_key($array, $offset, $len=-1) {
177  if (!is_array($array)) return false;
178 
179  $return = array();
180  $length = $len >= 0? $len: count($array);
181  $keys = array_slice(array_keys($array), $offset, $length);
182  foreach($keys as $key) {
183  $return[$key] = $array[$key];
184  }
185 
186  return $return;
187  }
188 }
189 
190 
ArrayItemIterator\atFirstPage
atFirstPage()
Definition: ArrayItemIterator.inc.php:113
ArrayItemIterator\atLastPage
atLastPage()
Definition: ArrayItemIterator.inc.php:121
Seboettg\Collection\current
current()
Definition: ArrayListTrait.php:53
ArrayItemIterator\getPageCount
getPageCount()
Definition: ArrayItemIterator.inc.php:145
ArrayItemIterator\array_slice_key
array_slice_key($array, $offset, $len=-1)
Definition: ArrayItemIterator.inc.php:188
ArrayItemIterator\$itemsPerPage
$itemsPerPage
Definition: ArrayItemIterator.inc.php:30
ArrayItemIterator\getCount
getCount()
Definition: ArrayItemIterator.inc.php:137
ArrayItemIterator\toArray
& toArray()
Definition: ArrayItemIterator.inc.php:169
ArrayItemIterator\toAssociativeArray
& toAssociativeArray()
Definition: ArrayItemIterator.inc.php:176
ArrayItemIterator
Provides paging and iteration for arrays.
Definition: ArrayItemIterator.inc.php:19
ArrayItemIterator\$wasEmpty
$wasEmpty
Definition: ArrayItemIterator.inc.php:45
ArrayItemIterator\eof
eof()
Definition: ArrayItemIterator.inc.php:153
Seboettg\Collection\count
count()
Definition: ArrayListTrait.php:253
ArrayItemIterator\wasEmpty
wasEmpty()
Definition: ArrayItemIterator.inc.php:161
ItemIterator\__construct
__construct()
Definition: ItemIterator.inc.php:22
ArrayItemIterator\fromRangeInfo
& fromRangeInfo(&$theArray, &$theRange)
Definition: ArrayItemIterator.inc.php:74
ArrayItemIterator\__construct
__construct(&$theArray, $page=-1, $itemsPerPage=-1)
Definition: ArrayItemIterator.inc.php:53
ArrayItemIterator\nextWithKey
nextWithKey()
Definition: ArrayItemIterator.inc.php:103
ArrayItemIterator\$page
$page
Definition: ArrayItemIterator.inc.php:36
ArrayItemIterator\next
& next()
Definition: ArrayItemIterator.inc.php:87
ArrayItemIterator\$theArray
$theArray
Definition: ArrayItemIterator.inc.php:24
ArrayItemIterator\$count
$count
Definition: ArrayItemIterator.inc.php:42
ItemIterator
Generic iterator class; needs to be overloaded by subclasses providing specific implementations.
Definition: ItemIterator.inc.php:18
ArrayItemIterator\getPage
getPage()
Definition: ArrayItemIterator.inc.php:129