composer package updates
[openemr.git] / vendor / stripe / stripe-php / lib / Util / AutoPagingIterator.php
blob167d1258a265efa4eb7ff67a9294a24bac06a76b
1 <?php
3 namespace Stripe\Util;
5 class AutoPagingIterator implements \Iterator
7 private $lastId = null;
8 private $page = null;
9 private $pageOffset = 0;
10 private $params = [];
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;
28 return $item;
31 public function key()
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(
45 $this->params ?: [],
46 ['starting_after' => $this->lastId]
48 $this->page = $this->page->all($this->params);
49 } else {
50 return false;
55 public function valid()
57 $key = key($this->page->data);
58 $valid = ($key !== null && $key !== false);
59 return $valid;