8000 [Bugfix] Fix cursor empty page meta and links · GIANTCRAB/laravel-json-api@41b12af · GitHub
[go: up one dir, main page]

Skip to content

Commit 41b12af

Browse files
committed
[Bugfix] Fix cursor empty page meta and links
Closes cloudcreativity#295
1 parent 28d127a commit 41b12af

File tree

4 files changed

+77
-4
lines changed

4 files changed

+77
-4
lines changed

CHANGELOG.md

Copy file name to clipboard
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22
All notable changes to this project will be documented in this file. This project adheres to
33
[Semantic Versioning](http://semver.org/) and [this changelog format](http://keepachangelog.com/).
44

5+
## Unreleased
6+
7+
### Fixed
8+
- [#295](https://github.com/cloudcreativity/laravel-json-api/issues/295)
9+
Use `null` for empty `from`/`to` fields in cursor page meta.
10+
511
## [1.0.0-rc.2] - 2019-02-05
612

713
### Added

src/Pagination/CursorPaginator.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,26 @@ public function getPerPage()
123123
return $this->cursor->getLimit();
124124
}
125125

126+
/**
127+
* @return string|null
128+
*/
129+
public function getFrom(): ?string
130+
{
131+
$first = $this->firstItem();
132+
133+
return $first ? (string) $first : null;
134+
}
135+
136+
/**
137+
* @return string|null
138+
*/
139+
public function getTo(): ?string
140+
{
141+
$last = $this->lastItem();
142+
143+
return $last ? (string) $last : null;
144+
}
145+
126146
/**
127147
* @inheritDoc
128148
*/
@@ -139,4 +159,20 @@ public function count()
139159
return $this->items->count();
140160
}
141161

162+
/**
163+
* @return bool
164+
*/
165+
public function isEmpty(): bool
166+
{
167+
return $this->items->isEmpty();
168+
}
169+
170+
/**
171+
* @return bool
172+
*/
173+
public function isNotEmpty(): bool
174+
{
175+
return !$this->isEmpty();
176+
}
177+
142178
}

src/Pagination/CursorStrategy.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ protected function createFirstLink(CursorPaginator $paginator, array $parameters
263263
/**
264264
* @param CursorPaginator $paginator
265265
* @param array $parameters
266-
* @return LinkInterface
266+
* @return LinkInterface|null
267267
*/
268268
protected function createNextLink(CursorPaginator $paginator, array $parameters = [])
269269
{
@@ -280,10 +280,14 @@ protected function createNextLink(CursorPaginator $paginator, array $parameters
280280
/**
281281
* @param CursorPaginator $paginator
282282
* @param array $parameters
283-
* @return LinkInterface
283+
* @return LinkInterface|null
284284
*/
285285
protected function createPrevLink(CursorPaginator $paginator, array $parameters = [])
286286
{
287+
if ($paginator->isEmpty()) {
288+
return null;
289+
}
290+
287291
return $this->createLink([
288292
$this->before => $paginator->firstItem(),
289293
$this->limit => $paginator->getPerPage(),
@@ -325,8 +329,8 @@ protected function createMeta(CursorPaginator $paginator)
325329
{
326330
$meta = [
327331
'per-page' => $paginator->getPerPage(),
328-
'from' => (string) $paginator->firstItem(),
329-
'to' => (string) $paginator->lastItem(),
332+
'from' => $paginator->getFrom(),
333+
'to' => $paginator->getTo(),
330334
'has-more' => $paginator->hasMore(),
331335
];
332336

tests/lib/Integration/Pagination/CursorPagingTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,33 @@ protected function setUp()
5353
$this->app->instance(CursorStrategy::class, $this->strategy);
5454
}
5555

56+
public function testNoPages()
57+
{
58+
$links = [
59+
'first' => $this->buildLink(
60+
'/api/v1/comments',
61+
[
62+
'page' => [
63+
'limit' => 10,
64+
],
65+
]
66+
),
67+
];
68+
69+
$page = [
70+
'per-page' => 10,
71+
'from' => null,
72+
'to' => null,
73+
'has-more' => false,
74+
];
75+
76+
$this->actingAsUser()
77+
->doSearch(['page' => ['limit' => 10]])
78+
->assertFetchedNone()
79+
->assertExactMeta(compact('page'))
80+
->assertExactLinks($links);
81+
}
82+
5683
public function testOnlyLimit()
5784
{
5885
/** @var Collection $comments */

0 commit comments

Comments
 (0)
0