diff --git a/init.php b/init.php index aaca08f3d..7431d2fd4 100644 --- a/init.php +++ b/init.php @@ -4,7 +4,6 @@ require(dirname(__FILE__) . '/lib/Stripe.php'); // Utilities -require(dirname(__FILE__) . '/lib/Util/AutoPagingIterator.php'); require(dirname(__FILE__) . '/lib/Util/CaseInsensitiveArray.php'); require(dirname(__FILE__) . '/lib/Util/LoggerInterface.php'); require(dirname(__FILE__) . '/lib/Util/DefaultLogger.php'); diff --git a/lib/Collection.php b/lib/Collection.php index 38d3835ff..f51a3ce41 100644 --- a/lib/Collection.php +++ b/lib/Collection.php @@ -77,14 +77,30 @@ public function getIterator() } /** - * @return Util\AutoPagingIterator An iterator that can be used to iterate - * across all objects across all pages. As page boundaries are + * @return \Generator|StripeObject[] A generator that can be used to + * iterate across all objects across all pages. As page boundaries are * encountered, the next page will be fetched automatically for * continued iteration. */ public function autoPagingIterator() { - return new Util\AutoPagingIterator($this, $this->_requestParams); + $page = $this; + $params = $this->_requestParams; + + while (true) { + $itemId = null; + foreach ($page as $item) { + $itemId = $item['id']; + yield $item; + } + + if (!$page['has_more'] || is_null($itemId)) { + return; + } + + $params = array_merge($params ?: [], ['starting_after' => $itemId]); + $page = $this->all($params, $this->_opts); + } } private function extractPathAndUpdateParams($params) diff --git a/lib/Util/AutoPagingIterator.php b/lib/Util/AutoPagingIterator.php deleted file mode 100644 index 167d1258a..000000000 --- a/lib/Util/AutoPagingIterator.php +++ /dev/null @@ -1,61 +0,0 @@ -page = $collection; - $this->params = $params; - } - - public function rewind() - { - // Actually rewinding would require making a copy of the original page. - } - - public function current() - { - $item = current($this->page->data); - $this->lastId = $item !== false ? $item['id'] : null; - - return $item; - } - - public function key() - { - return key($this->page->data) + $this->pageOffset; - } - - public function next() - { - $item = next($this->page->data); - if ($item === false) { - // If we've run out of data on the current page, try to fetch another one - // and increase the offset the new page would start at - $this->pageOffset += count($this->page->data); - if ($this->page['has_more']) { - $this->params = array_merge( - $this->params ?: [], - ['starting_after' => $this->lastId] - ); - $this->page = $this->page->all($this->params); - } else { - return false; - } - } - } - - public function valid() - { - $key = key($this->page->data); - $valid = ($key !== null && $key !== false); - return $valid; - } -} diff --git a/tests/Stripe/CollectionTest.php b/tests/Stripe/CollectionTest.php index 14b710d2b..35b110653 100644 --- a/tests/Stripe/CollectionTest.php +++ b/tests/Stripe/CollectionTest.php @@ -25,6 +25,7 @@ public function testCanList() null, false, [ + 'object' => 'list', 'data' => [['id' => 1]], 'has_more' => true, 'url' => '/things', @@ -102,6 +103,7 @@ public function testProvidesAutoPagingIterator() null, false, [ + 'object' => 'list', 'data' => [['id' => 2], ['id' => 3]], 'has_more' => false, ] @@ -126,6 +128,7 @@ public function testAutoPagingIteratorSupportsIteratorToArray() null, false, [ + 'object' => 'list', 'data' => [['id' => 2], ['id' => 3]], 'has_more' => false, ]