1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Bugloos\ResponderBundle\Service; |
4
|
|
|
|
5
|
|
|
use Bugloos\ResponderBundle\PaginatorHandler\Contract\PaginatorHandlerInterface; |
6
|
|
|
use Symfony\Component\HttpFoundation\Request; |
7
|
|
|
use Doctrine\ORM\QueryBuilder; |
8
|
|
|
use Doctrine\ORM\Query; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* @author Mojtaba Gheytasi <[email protected]> |
12
|
|
|
*/ |
13
|
|
|
class Paginator |
14
|
|
|
{ |
15
|
|
|
private int $defaultMaxItemsPerPage; |
16
|
|
|
|
17
|
|
|
private int $defaultItemsPerPage; |
18
|
|
|
|
19
|
|
|
private string $pageKeyInRequest; |
20
|
|
|
|
21
|
|
|
private string $itemsPerPageKeyInRequest; |
22
|
|
|
|
23
|
|
|
private ?int $limit = null; |
24
|
|
|
|
25
|
|
|
private Request $request; |
26
|
|
|
|
27
|
|
|
private PaginatorHandlerInterface $paginatorHandler; |
28
|
|
|
|
29
|
|
|
private PaginatorResponseInterface $paginatorResponse; |
30
|
|
|
|
31
|
|
|
public function __construct( |
32
|
|
|
PaginatorHandlerInterface $paginatorHandler, |
33
|
|
|
PaginatorResponseInterface $paginatorResponse, |
34
|
|
|
int $defaultMaxItemsPerPage, |
35
|
|
|
int $defaultItemsPerPage, |
36
|
|
|
string $pageKeyInRequest, |
37
|
|
|
string $itemsPerPageKeyInRequest |
38
|
|
|
) { |
39
|
|
|
$this->paginatorHandler = $paginatorHandler; |
40
|
|
|
$this->paginatorResponse = $paginatorResponse; |
41
|
|
|
$this->defaultMaxItemsPerPage = $defaultMaxItemsPerPage; |
42
|
|
|
$this->defaultItemsPerPage = $defaultItemsPerPage; |
43
|
|
|
$this->pageKeyInRequest = $pageKeyInRequest; |
44
|
|
|
$this->itemsPerPageKeyInRequest = $itemsPerPageKeyInRequest; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function for(Request $request): self |
48
|
|
|
{ |
49
|
|
|
$this->request = $request; |
50
|
|
|
|
51
|
|
|
return $this; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function setDefaultItemPerPage(int $perPage): self |
55
|
|
|
{ |
56
|
|
|
$this->defaultItemsPerPage = $perPage; |
57
|
|
|
|
58
|
|
|
return $this; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function setLimit(int $limit): self |
62
|
|
|
{ |
63
|
|
|
$this->limit = $limit; |
64
|
|
|
|
65
|
|
|
return $this; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function paginate(object $query, array $options = []): iterable |
69
|
|
|
{ |
70
|
|
|
$this->paginatorHandler->initializePaginatorHandler($query, $this->limit, $options); |
71
|
|
|
|
72
|
|
|
$this->applyRequestOnPaginatorHandler(); |
73
|
|
|
|
74
|
|
|
return $this->paginatorResponse->getResult($this->paginatorHandler, $this); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function getRequestedPageNumber(): int |
78
|
|
|
{ |
79
|
|
|
return (int) $this->request->query->get($this->pageKeyInRequest, 1); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public function collection(): iterable |
83
|
|
|
{ |
84
|
|
|
return $this->getRequestedPageNumber() <= $this->paginatorHandler->totalPages() ? |
85
|
|
|
$this->paginatorHandler->getCurrentPageResults() : []; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
public function getRequestedItemPerPage(): int |
89
|
|
|
{ |
90
|
|
|
return (int) $this->request->query->get($this->itemsPerPageKeyInRequest, $this->defaultItemsPerPage); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
public function count(): int |
94
|
|
|
{ |
95
|
|
|
return $this->getRequestedPageNumber() <= $this->paginatorHandler->totalPages() ? |
96
|
|
|
$this->paginatorHandler->count() : 0; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
private function getNormalizedItemsPerPage(): int |
100
|
|
|
{ |
101
|
|
|
return $this->getRequestedItemPerPage() > $this->defaultMaxItemsPerPage ? |
102
|
|
|
$this->defaultMaxItemsPerPage : $this->getRequestedItemPerPage(); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
private function applyRequestOnPaginatorHandler(): void |
106
|
|
|
{ |
107
|
|
|
$this->paginatorHandler->setItemsPerPage($this->getNormalizedItemsPerPage()); |
108
|
|
|
|
109
|
|
|
if ($this->getRequestedPageNumber() <= $this->paginatorHandler->totalPages()) { |
110
|
|
|
$this->paginatorHandler->setCurrentPage($this->getRequestedPageNumber()); |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|