Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

45 řádky
967B

  1. <?php
  2. declare(strict_types=1);
  3. namespace Core;
  4. class Pagination
  5. {
  6. public readonly int $totalPages;
  7. public readonly int $offset;
  8. public function __construct(
  9. public readonly int $total,
  10. public readonly int $page,
  11. public readonly int $perPage
  12. ) {
  13. $this->totalPages = $perPage > 0 ? (int) ceil($total / $perPage) : 1;
  14. $this->offset = ($page - 1) * $perPage;
  15. }
  16. public function hasPages(): bool
  17. {
  18. return $this->totalPages > 1;
  19. }
  20. public function previousPage(): ?int
  21. {
  22. return $this->page > 1 ? $this->page - 1 : null;
  23. }
  24. public function nextPage(): ?int
  25. {
  26. return $this->page < $this->totalPages ? $this->page + 1 : null;
  27. }
  28. /** @return list<int> */
  29. public function pageRange(): array
  30. {
  31. $start = max(1, $this->page - 2);
  32. $end = min($this->totalPages, $this->page + 2);
  33. return range($start, $end);
  34. }
  35. }

Powered by TurnKey Linux.