Skip to content

Commit 1bda63e

Browse files
committed
Curler: pass number of entities retrieved so far to getPage()
1 parent 4dd5143 commit 1bda63e

File tree

7 files changed

+12
-2
lines changed

7 files changed

+12
-2
lines changed

src/Toolkit/Contract/Curler/CurlerPagerInterface.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ public function getFirstRequest(
3535
*
3636
* @param mixed $data
3737
* @param TPage $previousPage
38+
* @param int|null $previousEntities The number of entities returned from
39+
* the endpoint via previous pages.
3840
* @param mixed[]|null $query The query applied to `$request` or returned by
3941
* {@see CurlerPageRequestInterface::getQuery()}, if applicable.
4042
* @return (TPage is null ? CurlerPageInterface : TPage)
@@ -45,6 +47,7 @@ public function getPage(
4547
ResponseInterface $response,
4648
CurlerInterface $curler,
4749
?CurlerPageInterface $previousPage = null,
50+
?int $previousEntities = null,
4851
?array $query = null
4952
): CurlerPageInterface;
5053
}

src/Toolkit/Curler/Curler.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,7 @@ private function process(string $method, ?array $query, $data = false)
382382
}
383383
$result = $this->getResponseBody($response);
384384
if ($pager) {
385-
$page = $pager->getPage($result, $request, $response, $this, null, $query);
385+
$page = $pager->getPage($result, $request, $response, $this, null, null, $query);
386386
return Arr::unwrap($page->getEntities(), 1);
387387
}
388388
return $result;
@@ -444,16 +444,18 @@ private function paginate(string $method, ?array $query, $data = false): iterabl
444444
$request = $this->createRequest($method, $query, $data);
445445
$request = $pager->getFirstRequest($request, $this, $query);
446446
$prev = null;
447+
$yielded = 0;
447448
do {
448449
if ($request instanceof CurlerPageRequestInterface) {
449450
$query = $request->getQuery() ?? $query;
450451
$request = $request->getRequest();
451452
}
452453
$response = $this->doSendRequest($request);
453454
$result = $this->getResponseBody($response);
454-
$page = $pager->getPage($result, $request, $response, $this, $prev, $query);
455+
$page = $pager->getPage($result, $request, $response, $this, $prev, $prev ? $yielded : null, $query);
455456
// Use `yield` instead of `yield from` so entities get unique keys
456457
foreach ($page->getEntities() as $entity) {
458+
$yielded++;
457459
yield $entity;
458460
}
459461
if (!$page->hasNextRequest()) {

src/Toolkit/Curler/Pager/LinkPager.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ public function getPage(
7575
ResponseInterface $response,
7676
CurlerInterface $curler,
7777
?CurlerPageInterface $previousPage = null,
78+
?int $previousEntities = null,
7879
?array $query = null
7980
): CurlerPageInterface {
8081
$data = ($this->EntitySelector)($data);

src/Toolkit/Curler/Pager/ODataPager.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ public function getPage(
6666
ResponseInterface $response,
6767
CurlerInterface $curler,
6868
?CurlerPageInterface $previousPage = null,
69+
?int $previousEntities = null,
6970
?array $query = null
7071
): CurlerPageInterface {
7172
if (!is_array($data)) {

src/Toolkit/Curler/Pager/QueryPager.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ public function getPage(
7373
ResponseInterface $response,
7474
CurlerInterface $curler,
7575
?CurlerPageInterface $previousPage = null,
76+
?int $previousEntities = null,
7677
?array $query = null
7778
): CurlerPageInterface {
7879
$data = ($this->EntitySelector)($data);

tests/fixtures/Toolkit/Sync/Provider/MockoonPager.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ public function getPage(
3333
ResponseInterface $response,
3434
CurlerInterface $curler,
3535
?CurlerPageInterface $previousPage = null,
36+
?int $previousEntities = null,
3637
?array $query = null
3738
): CurlerPageInterface {
3839
/** @var array<mixed[]> $data */

tests/unit/Toolkit/Curler/CurlerTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,7 @@ function (
214214
ResponseInterface $response,
215215
CurlerInterface $curler,
216216
?CurlerPageInterface $previousPage = null,
217+
?int $previousEntities = null,
217218
?array $query = null
218219
) use ($server, &$output): CurlerPage {
219220
$output[] = $server->getNewOutput();

0 commit comments

Comments
 (0)