Skip to content

Commit

Permalink
Merge branch 'release/4.3.1'
Browse files Browse the repository at this point in the history
  • Loading branch information
lindyhopchris committed Oct 31, 2024
2 parents 05644f3 + 8d96752 commit 98899c6
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 3 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file. This projec

## Unreleased

## [4.3.1] - 2024-10-31

### Fixed

- [#41](https://github.com/laravel-json-api/eloquent/pull/41) Handle key column not existing in cursor paginator.

## [4.3.0] - 2024-10-13

### Added
Expand Down
7 changes: 4 additions & 3 deletions src/Pagination/Cursor/CursorParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,13 @@ public function __construct(private IdParser $idParser, private string $keyName)
*/
public function encode(LaravelCursor $cursor): string
{
$key = $cursor->parameter($this->keyName);

if ($key) {
try {
$key = $cursor->parameter($this->keyName);
$parameters = $this->withoutPrivate($cursor->toArray());
$parameters[$this->keyName] = $this->idParser->encode($key);
$cursor = new LaravelCursor($parameters, $cursor->pointsToNextItems());
} catch (\UnexpectedValueException $ex) {
// Do nothing as the cursor does not contain the key.
}

return $cursor->encode();
Expand Down
51 changes: 51 additions & 0 deletions tests/lib/Acceptance/Pagination/CursorPaginationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,57 @@ public function testWithoutCursor(): void
$this->assertPage($posts->reverse()->take(3), $page);
}

/**
* @return void
*/
public function testWithIdEncodingWithoutKeySort(): void
{
$this->withIdEncoding();
$this->paginator->withoutKeySort();

$posts = Post::factory()->count(4)->sequence(['title' => 'd'], ['title' => 'c'], ['title' => 'b'], ['title' => 'a'])->create();

$meta = [
'from' => $this->encodeCursor(
["posts.title"=> "a"],
pointsToNextItems: false,
),
'hasMore' => true,
'perPage' => 3,
'to' => $this->encodeCursor(
["posts.title"=> "c"],
pointsToNextItems: true,
),
];

$links = [
'first' => [
'href' => 'http://localhost/api/v1/posts?' . Arr::query([
'page' => ['limit' => '3'],
'sort' => 'title',
]),
],
'next' => [
'href' => 'http://localhost/api/v1/posts?' . Arr::query([
'page' => [
'after' => $this->encodeCursor(
["posts.title"=> "c"],
pointsToNextItems: true,
),
'limit' => '3',
],
'sort' => 'title',
]),
],
];

$page = $this->posts->repository()->queryAll()->sort('title')->paginate(['limit' => '3']);

$this->assertSame(['page' => $meta], $page->meta());
$this->assertSame($links, $page->links()->toArray());
$this->assertPage($posts->reverse()->take(3), $page);
}

/**
* @return void
*/
Expand Down

0 comments on commit 98899c6

Please sign in to comment.