|
- <?php
-
- declare(strict_types=1);
-
- namespace Core;
-
- class Pagination
- {
- public readonly int $totalPages;
- public readonly int $offset;
-
- public function __construct(
- public readonly int $total,
- public readonly int $page,
- public readonly int $perPage
- ) {
- $this->totalPages = $perPage > 0 ? (int) ceil($total / $perPage) : 1;
- $this->offset = ($page - 1) * $perPage;
- }
-
- public function hasPages(): bool
- {
- return $this->totalPages > 1;
- }
-
- public function previousPage(): ?int
- {
- return $this->page > 1 ? $this->page - 1 : null;
- }
-
- public function nextPage(): ?int
- {
- return $this->page < $this->totalPages ? $this->page + 1 : null;
- }
-
- /** @return list<int> */
- public function pageRange(): array
- {
- $start = max(1, $this->page - 2);
- $end = min($this->totalPages, $this->page + 2);
-
- return range($start, $end);
- }
- }
|