AutoPagingIterator.php
6 years ago
CaseInsensitiveArray.php
6 years ago
DefaultLogger.php
6 years ago
LoggerInterface.php
6 years ago
RandomGenerator.php
6 years ago
RequestOptions.php
6 years ago
Set.php
6 years ago
Util.php
6 years ago
AutoPagingIterator.php
62 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Stripe\Util; |
| 4 | |
| 5 | class AutoPagingIterator implements \Iterator |
| 6 | { |
| 7 | private $lastId = null; |
| 8 | private $page = null; |
| 9 | private $pageOffset = 0; |
| 10 | private $params = []; |
| 11 | |
| 12 | public function __construct($collection, $params) |
| 13 | { |
| 14 | $this->page = $collection; |
| 15 | $this->params = $params; |
| 16 | } |
| 17 | |
| 18 | public function rewind() |
| 19 | { |
| 20 | // Actually rewinding would require making a copy of the original page. |
| 21 | } |
| 22 | |
| 23 | public function current() |
| 24 | { |
| 25 | $item = current($this->page->data); |
| 26 | $this->lastId = $item !== false ? $item['id'] : null; |
| 27 | |
| 28 | return $item; |
| 29 | } |
| 30 | |
| 31 | public function key() |
| 32 | { |
| 33 | return key($this->page->data) + $this->pageOffset; |
| 34 | } |
| 35 | |
| 36 | public function next() |
| 37 | { |
| 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] |
| 47 | ); |
| 48 | $this->page = $this->page->all($this->params); |
| 49 | } else { |
| 50 | return false; |
| 51 | } |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | public function valid() |
| 56 | { |
| 57 | $key = key($this->page->data); |
| 58 | $valid = ($key !== null && $key !== false); |
| 59 | return $valid; |
| 60 | } |
| 61 | } |
| 62 |