/
Paginator.php
63 lines (46 loc) · 1.19 KB
/
Paginator.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
<?php
namespace Centum\Paginator;
use Centum\Interfaces\Paginator\DataInterface;
use Centum\Interfaces\Paginator\PageInterface;
use Centum\Interfaces\Paginator\PaginatorInterface;
class Paginator implements PaginatorInterface
{
/**
* @param positive-int $itemsPerPage
*/
public function __construct(
protected readonly DataInterface $data,
protected readonly int $itemsPerPage,
protected readonly string $urlPrefix
) {
}
public function getData(): DataInterface
{
return $this->data;
}
public function getItemsPerPage(): int
{
return $this->itemsPerPage;
}
public function getUrlPrefix(): string
{
return $this->urlPrefix;
}
public function getTotalItems(): int
{
return $this->data->getTotal();
}
public function getTotalPages(): int
{
$totalItems = $this->getTotalItems();
if ($totalItems === 0) {
return 1;
}
/** @var positive-int */
return (int) ceil($totalItems / $this->itemsPerPage);
}
public function getPage(int $pageNumber): PageInterface
{
return new Page($this, $pageNumber);
}
}