1 | <?php |
||
8 | * @author Philip Washington Sorst <[email protected]> |
||
9 | */ |
||
10 | class Pagination |
||
11 | { |
||
12 | private int $currentPage; |
||
|
|||
13 | |||
14 | private int $perPage; |
||
15 | |||
16 | private int $total; |
||
17 | |||
18 | public function __construct(int $currentPage, int $perPage, int $total) |
||
19 | { |
||
20 | if ($currentPage < 1) { |
||
21 | throw new InvalidArgumentException('CurrentPage must be greater than 0'); |
||
22 | } |
||
23 | |||
24 | if ($perPage < 1) { |
||
25 | throw new InvalidArgumentException('PerPage mustbe greater than 0'); |
||
26 | } |
||
27 | |||
28 | if ($total < 0) { |
||
29 | throw new InvalidArgumentException('Total must be greater equals 0'); |
||
30 | } |
||
31 | |||
32 | $this->perPage = $perPage; |
||
33 | $this->currentPage = $currentPage; |
||
34 | $this->total = $total; |
||
35 | } |
||
36 | |||
37 | public function getCurrentPage(): int |
||
38 | { |
||
39 | return $this->currentPage; |
||
40 | } |
||
41 | |||
42 | public function getTotal(): int |
||
43 | { |
||
44 | return $this->total; |
||
45 | } |
||
46 | |||
47 | public function getTotalPages(): int |
||
48 | { |
||
49 | if ($this->total == 0) { |
||
50 | return 0; |
||
51 | } |
||
52 | |||
53 | return (int)(($this->total - 1) / $this->perPage + 1); |
||
54 | } |
||
55 | |||
56 | public function getPerPage(): int |
||
57 | { |
||
58 | return $this->perPage; |
||
59 | } |
||
60 | } |
||
61 |