Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
7 / 7 |
|
100.00% |
5 / 5 |
CRAP | |
100.00% |
1 / 1 |
| Pagination | |
100.00% |
7 / 7 |
|
100.00% |
5 / 5 |
6 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getTotalPages | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
| hasNextPage | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| hasPreviousPage | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getOffset | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | // ╔════════════════════════════════════════════════════════════╗ |
| 4 | // ║ MIT Licence (#Expat) - https://opensource.org/licenses/MIT ║ |
| 5 | // ║ Copyright 2026 Frederic Poeydomenge <dyno@phexium.com> ║ |
| 6 | // ╚════════════════════════════════════════════════════════════╝ |
| 7 | |
| 8 | declare(strict_types=1); |
| 9 | |
| 10 | namespace Phexium\Domain; |
| 11 | |
| 12 | final readonly class Pagination |
| 13 | { |
| 14 | public function __construct( |
| 15 | public int $totalCount, |
| 16 | public int $page, |
| 17 | public int $perPage, |
| 18 | ) {} |
| 19 | |
| 20 | public function getTotalPages(): int |
| 21 | { |
| 22 | if ($this->perPage <= 0) { |
| 23 | return 0; |
| 24 | } |
| 25 | |
| 26 | return (int) ceil($this->totalCount / $this->perPage); |
| 27 | } |
| 28 | |
| 29 | public function hasNextPage(): bool |
| 30 | { |
| 31 | return $this->page < $this->getTotalPages(); |
| 32 | } |
| 33 | |
| 34 | public function hasPreviousPage(): bool |
| 35 | { |
| 36 | return $this->page > 1; |
| 37 | } |
| 38 | |
| 39 | public function getOffset(): int |
| 40 | { |
| 41 | return max(0, ($this->page - 1) * $this->perPage); |
| 42 | } |
| 43 | } |