diff --git a/CHANGELOG.md b/CHANGELOG.md index 433d3cd..f5aa5f7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Changelog +# 0.9.3 + +* fix: renumbering of pages if anchored to the first page. + # 0.9.2 * build: limit `zenstruck/foundry` to 1.37.* for now diff --git a/packages/rekapager-core/src/Pager/Internal/ProximityPager.php b/packages/rekapager-core/src/Pager/Internal/ProximityPager.php index 6e6bbee..f86c1c7 100644 --- a/packages/rekapager-core/src/Pager/Internal/ProximityPager.php +++ b/packages/rekapager-core/src/Pager/Internal/ProximityPager.php @@ -231,30 +231,6 @@ public function __construct( $this->nextPage = $this->nextNeighboringPages[0] ?? $this->lastPage; - // renumber pages if there is no gap from first page to the current page - - if ($this->hasHiddenPagesBefore === false) { - $currentPageNumber = 1; - - if ($this->firstPage !== null) { - $this->firstPage = $this->firstPage->withPageNumber($currentPageNumber); - $currentPageNumber++; - } - - foreach ($this->previousNeighboringPages as $page) { - $page = $page->withPageNumber($currentPageNumber); - $currentPageNumber++; - } - - $this->currentPage = $this->currentPage->withPageNumber($currentPageNumber); - $currentPageNumber++; - - foreach ($this->nextNeighboringPages as $page) { - $page = $page->withPageNumber($currentPageNumber); - $currentPageNumber++; - } - } - // if we have page number larger than the last page, then the count // must be wrong @@ -363,6 +339,34 @@ public function __construct( ) { // $this->nextPage = $this->lastPage; } + + // renumber pages if there is no gap from first page to the current page + + if ($this->hasHiddenPagesBefore === false) { + $currentPageNumber = 1; + + if ($this->firstPage !== null) { + $this->firstPage = $this->firstPage->withPageNumber($currentPageNumber); + $currentPageNumber++; + } + + $newPreviousNeighboringPages = []; + foreach ($this->previousNeighboringPages as $page) { + $newPreviousNeighboringPages[] = $page->withPageNumber($currentPageNumber); + $currentPageNumber++; + } + $this->previousNeighboringPages = $newPreviousNeighboringPages; + + $this->currentPage = $this->currentPage->withPageNumber($currentPageNumber); + $currentPageNumber++; + + $newNextNeighboringPages = []; + foreach ($this->nextNeighboringPages as $page) { + $newNextNeighboringPages[] = $page->withPageNumber($currentPageNumber); + $currentPageNumber++; + } + $this->nextNeighboringPages = $newNextNeighboringPages; + } } public function withProximity(int $proximity): static diff --git a/packages/rekapager-core/src/Pager/Pager.php b/packages/rekapager-core/src/Pager/Pager.php index 28771dd..6240f79 100644 --- a/packages/rekapager-core/src/Pager/Pager.php +++ b/packages/rekapager-core/src/Pager/Pager.php @@ -104,7 +104,7 @@ private function getPager(): PagerInterface public function getCurrentPage(): PagerItemInterface { return new PagerItem( - $this->page, + $this->getPager()->getCurrentPage(), $this->pagerUrlGenerator ); } diff --git a/tests/src/IntegrationTests/Pager/KeysetPagerTraversalTest.php b/tests/src/IntegrationTests/Pager/KeysetPagerTraversalTest.php index 3275f24..4868ff6 100644 --- a/tests/src/IntegrationTests/Pager/KeysetPagerTraversalTest.php +++ b/tests/src/IntegrationTests/Pager/KeysetPagerTraversalTest.php @@ -40,4 +40,40 @@ public function testLastPageFromSecondFromLastPage(string $pageableGeneratorClas static::assertEquals(BoundaryType::Lower, $nextPageIdentifier->getBoundaryType()); static::assertCount(3, $nextPage); } + + #[DataProviderExternal(PageableGeneratorProvider::class, 'keyset')] + public function testTraversalFromLastToStart(string $pageableGeneratorClass): void + { + $pageable = $this->createPageableFromGenerator($pageableGeneratorClass); + + $lastPage = $pageable->getLastPage(); + self::assertNotNull($lastPage); + $pager = $this->createPagerFromPage($lastPage); + + foreach (range(1, 19) as $i) { + $previousPage = $pager->getPreviousPage(); + self::assertNotNull($previousPage); + + $pager = $this->createPagerFromPage($previousPage); + } + + $this->assertPager( + $pager, + proximity: 2, + hasPrevious: true, + hasNext: true, + hasFirst: true, + hasLast: true, + hasGapToFirstPage: false, + hasGapToLastPage: true, + numOfPreviousNeighboringPages: 0, + numOfNextNeighboringPages: 3, + firstPageNumber: 1, + lastPageNumber: -1, + currentPageNumber: 2, + previousPageNumbers: [], + nextPageNumbers: [3, 4, 5], + currentCount: 5, + ); + } }