Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use yield for autopagination #703

Merged
merged 1 commit into from
Aug 22, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion init.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
22 changes: 19 additions & 3 deletions lib/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
ob-stripe marked this conversation as resolved.
Show resolved Hide resolved
* 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)
Expand Down
61 changes: 0 additions & 61 deletions lib/Util/AutoPagingIterator.php

This file was deleted.

3 changes: 3 additions & 0 deletions tests/Stripe/CollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public function testCanList()
null,
false,
[
'object' => 'list',
'data' => [['id' => 1]],
'has_more' => true,
'url' => '/things',
Expand Down Expand Up @@ -102,6 +103,7 @@ public function testProvidesAutoPagingIterator()
null,
false,
[
'object' => 'list',
'data' => [['id' => 2], ['id' => 3]],
'has_more' => false,
]
Expand All @@ -126,6 +128,7 @@ public function testAutoPagingIteratorSupportsIteratorToArray()
null,
false,
[
'object' => 'list',
'data' => [['id' => 2], ['id' => 3]],
'has_more' => false,
]
Expand Down