Skip to content

Commit 2bcf4ab

Browse files
committed
Add some methods to the page renderer to make easier to
get the first item, the last item and the total of items.
1 parent 4764fe3 commit 2bcf4ab

File tree

2 files changed

+129
-0
lines changed

2 files changed

+129
-0
lines changed

system/Pager/PagerRenderer.php

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,27 @@ class PagerRenderer
8282
*/
8383
protected $pageSelector;
8484

85+
/**
86+
* The number of items a page.
87+
*
88+
* @var int|null
89+
*/
90+
protected $perPage;
91+
92+
/**
93+
* The total items the current has started.
94+
*
95+
* @var int|null
96+
*/
97+
protected $perPageStart;
98+
99+
/**
100+
* The total items of the current page;
101+
*
102+
* @var int|null
103+
*/
104+
protected $perPageEnd;
105+
85106
/**
86107
* Constructor.
87108
*/
@@ -98,6 +119,8 @@ public function __construct(array $details)
98119
$this->pageCount = $details['pageCount'];
99120
$this->segment = $details['segment'] ?? 0;
100121
$this->pageSelector = $details['pageSelector'] ?? 'page';
122+
$this->perPage = $details['perPage'] ?? null;
123+
$this->updatePerPages();
101124
}
102125

103126
/**
@@ -307,6 +330,28 @@ protected function updatePages(?int $count = null)
307330
$this->last = $this->current + $count <= $this->pageCount ? $this->current + $count : (int) $this->pageCount;
308331
}
309332

333+
/**
334+
* Updates the start and end items per pages, which is
335+
* the number of items displayed on the active page.
336+
*/
337+
protected function updatePerPages(): void
338+
{
339+
if ($this->total === null || $this->perPage === null) {
340+
return;
341+
}
342+
343+
// When the page is the last, performs a different calculation.
344+
if ($this->last === $this->current) {
345+
$this->perPageStart = $this->perPage * ($this->current - 1) + 1;
346+
$this->perPageEnd = $this->total;
347+
348+
return;
349+
}
350+
351+
$this->perPageStart = $this->current === 1 ? 1 : ($this->perPage * $this->current) - $this->perPage + 1;
352+
$this->perPageEnd = $this->perPage * $this->current;
353+
}
354+
310355
/**
311356
* Checks to see if there is a "previous" page before our "first" page.
312357
*/
@@ -430,4 +475,36 @@ public function getNextPageNumber(): ?int
430475
{
431476
return ($this->current === $this->pageCount) ? null : $this->current + 1;
432477
}
478+
479+
/**
480+
* Returns the total items of the page.
481+
*/
482+
public function getTotal(): ?int
483+
{
484+
return $this->total;
485+
}
486+
487+
/**
488+
* Returns the number of items to be displayed on the page.
489+
*/
490+
public function getPerPage(): ?int
491+
{
492+
return $this->perPage;
493+
}
494+
495+
/**
496+
* Returns the number of items the page starts with.
497+
*/
498+
public function getPerPageStart(): ?int
499+
{
500+
return $this->perPageStart;
501+
}
502+
503+
/**
504+
* Returns the number of items the page ends with.
505+
*/
506+
public function getPerPageEnd(): ?int
507+
{
508+
return $this->perPageEnd;
509+
}
433510
}

tests/system/Pager/PagerRendererTest.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
use CodeIgniter\HTTP\URI;
1717
use CodeIgniter\Test\CIUnitTestCase;
18+
use PHPUnit\Framework\Attributes\DataProvider;
1819
use PHPUnit\Framework\Attributes\Group;
1920

2021
/**
@@ -619,4 +620,55 @@ public function testGetNextPageNumberNull(): void
619620

620621
$this->assertNull($pager->getNextPageNumber());
621622
}
623+
624+
#[DataProvider('providePageStartEnd')]
625+
public function testPageStartEnd(array $details, int $pageStart, int $pageEnd): void
626+
{
627+
$pager = new PagerRenderer($details);
628+
$pager->setSurroundCount(2);
629+
630+
$this->assertSame($pager->getPerPageStart(), $pageStart);
631+
$this->assertSame($pager->getPerPageEnd(), $pageEnd);
632+
}
633+
634+
public static function providePageStartEnd(): iterable
635+
{
636+
$uri = new URI('http://example.com/foo');
637+
638+
return [
639+
'first page' => [
640+
'details' => [
641+
'uri' => $uri,
642+
'pageCount' => 3,
643+
'total' => 25,
644+
'currentPage' => 1,
645+
'perPage' => 10,
646+
],
647+
'pageStart' => 1,
648+
'pageEnd' => 10,
649+
],
650+
'second page' => [
651+
'details' => [
652+
'uri' => $uri,
653+
'pageCount' => 3,
654+
'total' => 25,
655+
'currentPage' => 2,
656+
'perPage' => 10,
657+
],
658+
'pageStart' => 11,
659+
'pageEnd' => 20,
660+
],
661+
'last page' => [
662+
'details' => [
663+
'uri' => $uri,
664+
'pageCount' => 3,
665+
'total' => 25,
666+
'currentPage' => 3,
667+
'perPage' => 10,
668+
],
669+
'pageStart' => 21,
670+
'pageEnd' => 25,
671+
],
672+
];
673+
}
622674
}

0 commit comments

Comments
 (0)