1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Psi\Component\Grid\View; |
6
|
|
|
|
7
|
|
|
use Psi\Component\Grid\GridContext; |
8
|
|
|
|
9
|
|
|
class Paginator |
10
|
|
|
{ |
11
|
|
|
private $options; |
12
|
|
|
private $numberOfRecords; |
13
|
|
|
private $numberOfRecordsOnPage; |
14
|
|
|
|
15
|
|
|
public function __construct(GridContext $options, int $numberOfRecordsOnPage, int $numberOfRecords = null) |
16
|
|
|
{ |
17
|
|
|
$this->options = $options; |
18
|
|
|
$this->numberOfRecords = $numberOfRecords; |
19
|
|
|
$this->numberOfRecordsOnPage = $numberOfRecordsOnPage; |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
public function getPageSize(): int |
23
|
|
|
{ |
24
|
|
|
return $this->options->getPageSize(); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
public function getCurrentPage(): int |
28
|
|
|
{ |
29
|
|
|
return $this->options->getCurrentPage(); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public function getNumberOfRecords() |
33
|
|
|
{ |
34
|
|
|
return $this->numberOfRecords; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function getNumberOfPages(): int |
38
|
|
|
{ |
39
|
|
|
if (null === $this->numberOfRecords) { |
40
|
|
|
throw new \RuntimeException( |
41
|
|
|
'Cannot determine the last page when the total number of ' . |
42
|
|
|
'records has not been provided.' |
43
|
|
|
); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
return (int) ceil($this->getNumberOfRecords() / $this->getPageSize()); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function isLastPage(): bool |
50
|
|
|
{ |
51
|
|
|
if (null === $this->numberOfRecords) { |
52
|
|
|
return $this->numberOfRecordsOnPage < $this->getPageSize(); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
return $this->getCurrentPage() == $this->getNumberOfPages(); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function getUrlParametersForPage($page = null) |
59
|
|
|
{ |
60
|
|
|
return array_merge($this->options->getUrlParameters(), [ |
61
|
|
|
'page' => $page ?: $this->options->getCurrentPage(), |
62
|
|
|
]); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|