diff --git a/src/Paginator.php b/src/Paginator.php index 9be4cf0..0edfe22 100644 --- a/src/Paginator.php +++ b/src/Paginator.php @@ -30,6 +30,11 @@ abstract class Paginator implements Iterator, Countable */ protected Request $request; + /** + * The start page when the paginator is rewound + */ + protected int $startPage = 1; + /** * Internal Marker For The Current Page */ @@ -199,7 +204,7 @@ public function valid(): bool */ public function rewind(): void { - $this->page = 1; + $this->page = $this->startPage; $this->currentResponse = null; $this->totalResults = 0; $this->totalPages = null; @@ -310,6 +315,18 @@ public function getPage(): int return $this->page; } + /** + * Set the start page of the paginator + * + * Used when the paginator is rewound + */ + public function setStartPage(int $startPage): static + { + $this->startPage = $startPage; + + return $this; + } + /** * Count the iterator */ diff --git a/tests/Feature/StartPageTest.php b/tests/Feature/StartPageTest.php new file mode 100644 index 0000000..67305f0 --- /dev/null +++ b/tests/Feature/StartPageTest.php @@ -0,0 +1,55 @@ +paginate($request); + + $superheroes = []; + $iteratorCounter = 0; + + foreach ($paginator as $item) { + $iteratorCounter++; + $superheroes = array_merge($superheroes, $item->json('data')); + } + + expect($iteratorCounter)->toBe(2); + expect($paginator->getTotalResults())->toEqual(10); + + $mapped = array_map(static fn (array $superhero) => $superhero['id'], $superheroes); + + expect($mapped)->toEqual([11, 12, 13, 14, 15, 16, 17, 18, 19, 20,]); +}); + +test('you can specify a start page on a paginator instance', function () { + $connector = new PagedConnector; + $request = new SuperheroPagedRequest(); + $paginator = $connector->paginate($request); + + $superheroes = []; + $iteratorCounter = 0; + + $paginator->setStartPage(3); + + foreach ($paginator as $item) { + $iteratorCounter++; + $superheroes = array_merge($superheroes, $item->json('data')); + } + + expect($iteratorCounter)->toBe(2); + expect($paginator->getTotalResults())->toEqual(10); + + $mapped = array_map(static fn (array $superhero) => $superhero['id'], $superheroes); + + expect($mapped)->toEqual([11, 12, 13, 14, 15, 16, 17, 18, 19, 20,]); +}); diff --git a/tests/Fixtures/Connectors/CustomStartPagePagedConnector.php b/tests/Fixtures/Connectors/CustomStartPagePagedConnector.php new file mode 100644 index 0000000..d5c7f4f --- /dev/null +++ b/tests/Fixtures/Connectors/CustomStartPagePagedConnector.php @@ -0,0 +1,47 @@ +paginate($this); + } + + return new class(connector: $this, request: $request) extends PagedPaginator { + /** + * Set a custom start page + */ + protected int $startPage = 3; + + /** + * Check if we are on the last page + */ + protected function isLastPage(Response $response): bool + { + return empty($response->json('next_page_url')); + } + + /** + * Get the results from the page + */ + protected function getPageItems(Response $response, Request $request): array + { + return $response->json('data') ?? []; + } + }; + } +}