diff --git a/system/Pager/PagerRenderer.php b/system/Pager/PagerRenderer.php index 64c4c40c531d..7db834833417 100644 --- a/system/Pager/PagerRenderer.php +++ b/system/Pager/PagerRenderer.php @@ -82,6 +82,27 @@ class PagerRenderer */ protected $pageSelector; + /** + * The number of items a page. + * + * @var int|null + */ + protected $perPage; + + /** + * The total items the current has started. + * + * @var int|null + */ + protected $perPageStart; + + /** + * The total items of the current page; + * + * @var int|null + */ + protected $perPageEnd; + /** * Constructor. */ @@ -98,6 +119,8 @@ public function __construct(array $details) $this->pageCount = $details['pageCount']; $this->segment = $details['segment'] ?? 0; $this->pageSelector = $details['pageSelector'] ?? 'page'; + $this->perPage = $details['perPage'] ?? null; + $this->updatePerPages(); } /** @@ -307,6 +330,28 @@ protected function updatePages(?int $count = null) $this->last = $this->current + $count <= $this->pageCount ? $this->current + $count : (int) $this->pageCount; } + /** + * Updates the start and end items per pages, which is + * the number of items displayed on the active page. + */ + protected function updatePerPages(): void + { + if ($this->total === null || $this->perPage === null) { + return; + } + + // When the page is the last, performs a different calculation. + if ($this->last === $this->current) { + $this->perPageStart = $this->perPage * ($this->current - 1) + 1; + $this->perPageEnd = $this->total; + + return; + } + + $this->perPageStart = $this->current === 1 ? 1 : ($this->perPage * $this->current) - $this->perPage + 1; + $this->perPageEnd = $this->perPage * $this->current; + } + /** * Checks to see if there is a "previous" page before our "first" page. */ @@ -430,4 +475,36 @@ public function getNextPageNumber(): ?int { return ($this->current === $this->pageCount) ? null : $this->current + 1; } + + /** + * Returns the total items of the page. + */ + public function getTotal(): ?int + { + return $this->total; + } + + /** + * Returns the number of items to be displayed on the page. + */ + public function getPerPage(): ?int + { + return $this->perPage; + } + + /** + * Returns the number of items the page starts with. + */ + public function getPerPageStart(): ?int + { + return $this->perPageStart; + } + + /** + * Returns the number of items the page ends with. + */ + public function getPerPageEnd(): ?int + { + return $this->perPageEnd; + } } diff --git a/tests/system/Pager/PagerRendererTest.php b/tests/system/Pager/PagerRendererTest.php index 8ac8683a4327..8d0095854374 100644 --- a/tests/system/Pager/PagerRendererTest.php +++ b/tests/system/Pager/PagerRendererTest.php @@ -15,6 +15,7 @@ use CodeIgniter\HTTP\URI; use CodeIgniter\Test\CIUnitTestCase; +use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\Attributes\Group; /** @@ -619,4 +620,55 @@ public function testGetNextPageNumberNull(): void $this->assertNull($pager->getNextPageNumber()); } + + #[DataProvider('providePageStartEnd')] + public function testPageStartEnd(array $details, int $pageStart, int $pageEnd): void + { + $pager = new PagerRenderer($details); + $pager->setSurroundCount(2); + + $this->assertSame($pager->getPerPageStart(), $pageStart); + $this->assertSame($pager->getPerPageEnd(), $pageEnd); + } + + public static function providePageStartEnd(): iterable + { + $uri = new URI('http://example.com/foo'); + + return [ + 'first page' => [ + 'details' => [ + 'uri' => $uri, + 'pageCount' => 3, + 'total' => 25, + 'currentPage' => 1, + 'perPage' => 10, + ], + 'pageStart' => 1, + 'pageEnd' => 10, + ], + 'second page' => [ + 'details' => [ + 'uri' => $uri, + 'pageCount' => 3, + 'total' => 25, + 'currentPage' => 2, + 'perPage' => 10, + ], + 'pageStart' => 11, + 'pageEnd' => 20, + ], + 'last page' => [ + 'details' => [ + 'uri' => $uri, + 'pageCount' => 3, + 'total' => 25, + 'currentPage' => 3, + 'perPage' => 10, + ], + 'pageStart' => 21, + 'pageEnd' => 25, + ], + ]; + } }