Skip to content

Commit

Permalink
Add some methods to the page renderer to make easier to
Browse files Browse the repository at this point in the history
get the first item, the last item and the total of items.
  • Loading branch information
murilohpucci committed Jan 4, 2025
1 parent 4764fe3 commit 4147d9f
Show file tree
Hide file tree
Showing 2 changed files with 129 additions and 0 deletions.
77 changes: 77 additions & 0 deletions system/Pager/PagerRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand All @@ -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();
}

/**
Expand Down Expand Up @@ -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.
*/
Expand Down Expand Up @@ -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;
}
}
52 changes: 52 additions & 0 deletions tests/system/Pager/PagerRendererTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

use CodeIgniter\HTTP\URI;
use CodeIgniter\Test\CIUnitTestCase;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Group;

/**
Expand Down Expand Up @@ -619,4 +620,55 @@ public function testGetNextPageNumberNull(): void

$this->assertNull($pager->getNextPageNumber());
}

#[DataProvider('providePageStartEnd')]

Check failure on line 624 in tests/system/Pager/PagerRendererTest.php

View workflow job for this annotation

GitHub Actions / PHP Static Analysis

Method CodeIgniter\Pager\PagerRendererTest::testPageStartEnd() has parameter $details with no value type specified in iterable type array.
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

Check failure on line 634 in tests/system/Pager/PagerRendererTest.php

View workflow job for this annotation

GitHub Actions / PHP Static Analysis

Method CodeIgniter\Pager\PagerRendererTest::providePageStartEnd() return type has no value type specified in iterable type 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,
],
];
}
}

0 comments on commit 4147d9f

Please sign in to comment.