Skip to content

Commit 0fdd0c0

Browse files
committed
add: better pagination handling
- fix problem with next_cursor handling for database queries - added raw extraction (->fillResult()) for 'has_more' and 'next_cursor' in EntityCollection (e.g. executed on querying databases) - added ->offsetByResponse(...) for handling the offset based on the previous query result (query result = EntityCollection) - added public functions for easy access to 'has_more' (->hasMoreEntries()) and 'next_cursor' (->nextCursor()) in EntityCollection
1 parent 21e07d4 commit 0fdd0c0

File tree

2 files changed

+59
-1
lines changed

2 files changed

+59
-1
lines changed

src/Endpoints/Database.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace FiveamCode\LaravelNotionApi\Endpoints;
44

5+
use FiveamCode\LaravelNotionApi\Entities\Collections\EntityCollection;
56
use FiveamCode\LaravelNotionApi\Entities\Collections\PageCollection;
67
use FiveamCode\LaravelNotionApi\Notion;
78
use FiveamCode\LaravelNotionApi\Query\Filters\Filter;
@@ -66,7 +67,7 @@ public function query(): PageCollection
6667
} // TODO Compound filters!
6768

6869
if ($this->startCursor !== null) {
69-
$postData['start_cursor'] = $this->startCursor;
70+
$postData['start_cursor'] = $this->startCursor->__toString();
7071
}
7172

7273
if ($this->pageSize !== null) {
@@ -104,4 +105,15 @@ public function sortBy(Collection $sorts): Database
104105

105106
return $this;
106107
}
108+
109+
/**
110+
* @param EntityCollection $entityCollection
111+
* @return $this
112+
*/
113+
public function offsetByResponse(EntityCollection $entityCollection): Database
114+
{
115+
$this->offset($entityCollection->nextCursor());
116+
117+
return $this;
118+
}
107119
}

src/Entities/Collections/EntityCollection.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use FiveamCode\LaravelNotionApi\Entities\Page;
88
use FiveamCode\LaravelNotionApi\Exceptions\HandlingException;
99
use FiveamCode\LaravelNotionApi\Exceptions\NotionException;
10+
use FiveamCode\LaravelNotionApi\Query\StartCursor;
1011
use Illuminate\Support\Arr;
1112
use Illuminate\Support\Collection;
1213

@@ -25,6 +26,16 @@ class EntityCollection
2526
*/
2627
protected array $rawResults = [];
2728

29+
/**
30+
* @var bool
31+
*/
32+
protected bool $hasMore = false;
33+
34+
/**
35+
* @var string
36+
*/
37+
protected ?string $nextCursor = null;
38+
2839
/**
2940
* @var Collection
3041
*/
@@ -96,13 +107,24 @@ protected function collectChildren(): void
96107
protected function fillFromRaw()
97108
{
98109
$this->fillResult();
110+
$this->fillCursorInformation();
99111
}
100112

101113
protected function fillResult()
102114
{
103115
$this->rawResults = $this->responseData['results'];
104116
}
105117

118+
protected function fillCursorInformation()
119+
{
120+
if (Arr::exists($this->responseData, 'has_more')) {
121+
$this->hasMore = $this->responseData['has_more'];
122+
}
123+
if (Arr::exists($this->responseData, 'next_cursor')) {
124+
$this->nextCursor = $this->responseData['next_cursor'];
125+
}
126+
}
127+
106128
/**
107129
* @return array
108130
*/
@@ -111,6 +133,14 @@ public function getRawResponse(): array
111133
return $this->responseData;
112134
}
113135

136+
/**
137+
* @return string
138+
*/
139+
public function getRawNextCursor(): ?string
140+
{
141+
return $this->nextCursor;
142+
}
143+
114144
/**
115145
* @return Collection
116146
*/
@@ -128,4 +158,20 @@ public function asJson(): string
128158
return $item->toArray();
129159
});
130160
}
161+
162+
/**
163+
* @return bool
164+
*/
165+
public function hasMoreEntries(): bool
166+
{
167+
return $this->hasMore;
168+
}
169+
170+
/**
171+
* @return StartCursor
172+
*/
173+
public function nextCursor(): StartCursor
174+
{
175+
return new StartCursor($this->getRawNextCursor());
176+
}
131177
}

0 commit comments

Comments
 (0)