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

Add support for reverse pagination #797

Merged
merged 1 commit into from
Nov 6, 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
35 changes: 27 additions & 8 deletions lib/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class Collection extends StripeObject implements \IteratorAggregate

use ApiOperations\Request;

/** @var array */
protected $filters = [];

/**
Expand Down Expand Up @@ -46,8 +47,6 @@ public function getFilters()
public function setFilters($filters)
{
$this->filters = $filters;
unset($this->filters['starting_after']);
unset($this->filters['ending_before']);
}

public function offsetGet($k)
Expand Down Expand Up @@ -113,6 +112,15 @@ public function getIterator()
return new \ArrayIterator($this->data);
}

/**
* @return \ArrayIterator An iterator that can be used to iterate
* backwards across objects in the current page.
*/
public function getReverseIterator()
{
return new \ArrayIterator(array_reverse($this->data));
}

/**
* @return \Generator|StripeObject[] A generator that can be used to
* iterate across all objects across all pages. As page boundaries are
Expand All @@ -124,12 +132,19 @@ public function autoPagingIterator()
$page = $this;

while (true) {
foreach ($page as $item) {
yield $item;
if (array_key_exists('ending_before', $this->filters) &&
!array_key_exists('starting_after', $this->filters)) {
foreach ($page->getReverseIterator() as $item) {
yield $item;
}
$page = $page->previousPage();
} else {
foreach ($page as $item) {
yield $item;
}
$page = $page->nextPage();
}

$page = $page->nextPage();

if ($page->isEmpty()) {
break;
}
Expand Down Expand Up @@ -178,7 +193,7 @@ public function nextPage($params = null, $opts = null)
$lastId = end($this->data)->id;

$params = array_merge(
$this->filters,
$this->filters ?: [],
['starting_after' => $lastId],
$params ?: []
);
Expand All @@ -198,10 +213,14 @@ public function nextPage($params = null, $opts = null)
*/
public function previousPage($params = null, $opts = null)
{
if (!$this->has_more) {
return static::emptyCollection($opts);
}

$firstId = $this->data[0]->id;

$params = array_merge(
$this->filters,
$this->filters ?: [],
['ending_before' => $firstId],
$params ?: []
);
Expand Down
58 changes: 56 additions & 2 deletions tests/Stripe/CollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,34 @@ public function testCanCreate()

public function testCanIterate()
{
$collection = Collection::constructFrom([
'data' => [['id' => 1], ['id' => 2], ['id' => 3]],
'has_more' => true,
'url' => '/things',
]);

$seen = [];
foreach ($this->fixture as $item) {
foreach ($collection as $item) {
array_push($seen, $item['id']);
}

$this->assertSame([1], $seen);
$this->assertSame([1, 2, 3], $seen);
}

public function testCanIterateBackwards()
{
$collection = Collection::constructFrom([
'data' => [['id' => 1], ['id' => 2], ['id' => 3]],
'has_more' => true,
'url' => '/things',
]);

$seen = [];
foreach ($collection->getReverseIterator() as $item) {
array_push($seen, $item['id']);
}

$this->assertSame([3, 2, 1], $seen);
}

public function testSupportsIteratorToArray()
Expand Down Expand Up @@ -154,6 +176,38 @@ public function testAutoPagingIteratorSupportsIteratorToArray()
$this->assertSame([1, 2, 3], $seen);
}

public function testProvidesAutoPagingIteratorThatSupportsBackwardsPagination()
{
$this->stubRequest(
'GET',
'/things',
[
'ending_before' => 3,
],
null,
false,
[
'object' => 'list',
'data' => [['id' => 1], ['id' => 2]],
'has_more' => false,
]
);

$collection = Collection::constructFrom([
'data' => [['id' => 3]],
'has_more' => true,
'url' => '/things',
]);
$collection->setFilters(['ending_before' => 4]);

$seen = [];
foreach ($collection->autoPagingIterator() as $item) {
array_push($seen, $item['id']);
}

$this->assertSame([3, 2, 1], $seen);
}

public function testHeaders()
{
$this->stubRequest(
Expand Down