5 class AutoPagingIterator
implements \Iterator
7 private $lastId = null;
9 private $pageOffset = 0;
12 public function __construct($collection, $params)
14 $this->page
= $collection;
15 $this->params
= $params;
18 public function rewind()
20 // Actually rewinding would require making a copy of the original page.
23 public function current()
25 $item = current($this->page
->data
);
26 $this->lastId
= $item !== false ?
$item['id'] : null;
33 return key($this->page
->data
) +
$this->pageOffset
;
36 public function next()
38 $item = next($this->page
->data
);
39 if ($item === false) {
40 // If we've run out of data on the current page, try to fetch another one
41 // and increase the offset the new page would start at
42 $this->pageOffset +
= count($this->page
->data
);
43 if ($this->page
['has_more']) {
44 $this->params
= array_merge(
46 ['starting_after' => $this->lastId
]
48 $this->page
= $this->page
->all($this->params
);
55 public function valid()
57 $key = key($this->page
->data
);
58 $valid = ($key !== null && $key !== false);