Skip to content

[12.x] Allow rendering paginated data where cursor parameters are not in the response #55524

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

Closed
Closed
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
32 changes: 31 additions & 1 deletion src/Illuminate/Pagination/CursorPaginator.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ class CursorPaginator extends AbstractCursorPaginator implements Arrayable, Arra
*/
protected $hasMore;

/**
* The function that transforms before rendering.
*
* @var \Closure(TValue): mixed
*/
protected $transformer;

/**
* Create a new paginator instance.
*
Expand Down Expand Up @@ -142,6 +149,29 @@ public function onLastPage()
return ! $this->hasMorePages();
}

/**
* Set how data is transformed before rendering to JSON.
*
* @param \Closure(TValue): mixed $tranformer
* @return $this
*/
public function setTransformer($tranformer)
{
$this->transformer = $tranformer;

return $this;
}

/**
* Prepare data for rendering.
*
* @return array
*/
protected function transformedData()
{
return $this->transformer ? $this->items->map($this->transformer)->toArray() : $this->items->toArray();
}

/**
* Get the instance as an array.
*
Expand All @@ -150,7 +180,7 @@ public function onLastPage()
public function toArray()
{
return [
'data' => $this->items->toArray(),
'data' => $this->transformedData(),
'path' => $this->path(),
'per_page' => $this->perPage(),
'next_cursor' => $this->nextCursor()?->encode(),
Expand Down
31 changes: 29 additions & 2 deletions tests/Pagination/CursorPaginatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,14 @@ public function testCanTransformPaginatorItems()
$options = ['path' => 'http://website.com/test', 'parameters' => ['id']]);

$p->through(function ($item) {
$item['id'] = $item['id'] + 2;
$item['zid'] = $item['id'] + 2;
unset($item['id']);

return $item;
});

$this->assertInstanceOf(CursorPaginator::class, $p);
$this->assertSame([['id' => 6], ['id' => 7]], $p->items());
$this->assertSame([['zid' => 6], ['zid' => 7]], $p->items());
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note: If I were to call $p->toArray() it would produce a warning.

}

public function testCursorPaginatorOnFirstAndLastPage()
Expand Down Expand Up @@ -120,6 +121,32 @@ public function testReturnEmptyCursorWhenItemsAreEmpty()
], $p->toArray());
}

public function testCanTransformOutput()
{
$p = new CursorPaginator([['id' => 4], ['id' => 5], ['id' => 6]], 2, null,
['path' => 'http://website.com/test', 'parameters' => ['id']]
);

$p->setTransformer(function ($item) {
return [
'slug' => 'slug-'.$item['id'],
'name' => 'Test '.$item['id'],
];
});

$this->assertInstanceOf(CursorPaginator::class, $p);
$this->assertEquals([
'data' => [['slug' => 'slug-4', 'name' => 'Test 4'], ['slug' => 'slug-5', 'name' => 'Test 5']],
'path' => 'http://website.com/test',
'per_page' => 2,
'next_cursor' => 'eyJpZCI6NSwiX3BvaW50c1RvTmV4dEl0ZW1zIjp0cnVlfQ',
'next_page_url' => 'http://website.com/test?cursor=eyJpZCI6NSwiX3BvaW50c1RvTmV4dEl0ZW1zIjp0cnVlfQ',
'prev_cursor' => null,
'prev_page_url' => null,
], $p->toArray());
$this->assertEquals([ ['id' => 4], ['id' => 5]], $p->items());
}

protected function getCursor($params, $isNext = true)
{
return (new Cursor($params, $isNext))->encode();
Expand Down