|
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 ?int $limit = null; |
|
20
|
|
|
|
|
21
|
|
|
private Request $request; |
|
22
|
|
|
|
|
23
|
|
|
private $customResultDecorator; |
|
24
|
|
|
|
|
25
|
|
|
private PaginatorHandlerInterface $paginatorHandler; |
|
26
|
|
|
|
|
27
|
|
|
public function __construct( |
|
28
|
|
|
PaginatorHandlerInterface $paginatorHandler, |
|
29
|
|
|
int $defaultMaxItemsPerPage, |
|
30
|
|
|
int $defaultItemsPerPage |
|
31
|
|
|
) { |
|
32
|
|
|
$this->paginatorHandler = $paginatorHandler; |
|
33
|
|
|
$this->defaultMaxItemsPerPage = $defaultMaxItemsPerPage; |
|
34
|
|
|
$this->defaultItemsPerPage = $defaultItemsPerPage; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
public function for(Request $request): self |
|
38
|
|
|
{ |
|
39
|
|
|
$this->request = $request; |
|
40
|
|
|
|
|
41
|
|
|
return $this; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
public function setDefaultItemPerPage(int $perPage): self |
|
45
|
|
|
{ |
|
46
|
|
|
$this->defaultItemsPerPage = $perPage; |
|
47
|
|
|
|
|
48
|
|
|
return $this; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
public function setLimit(int $limit): self |
|
52
|
|
|
{ |
|
53
|
|
|
$this->limit = $limit; |
|
54
|
|
|
|
|
55
|
|
|
return $this; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
public function setCustomResultDecorator(callable $callable): self |
|
59
|
|
|
{ |
|
60
|
|
|
$this->customResultDecorator = $callable; |
|
61
|
|
|
|
|
62
|
|
|
return $this; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @param Query|QueryBuilder $query |
|
67
|
|
|
* @param array $options |
|
68
|
|
|
* |
|
69
|
|
|
* @return array |
|
70
|
|
|
*/ |
|
71
|
|
|
public function paginate($query, array $options = []): array |
|
72
|
|
|
{ |
|
73
|
|
|
$this->paginatorHandler->initializePaginatorHandler($query, $this->limit, $options); |
|
74
|
|
|
|
|
75
|
|
|
$this->applyRequestOnPaginatorHandler(); |
|
76
|
|
|
|
|
77
|
|
|
if ($this->hasCustomResultDecorator()) { |
|
78
|
|
|
return call_user_func($this->customResultDecorator, $this->paginatorHandler); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
return $this->getResult(); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
private function hasCustomResultDecorator(): bool |
|
85
|
|
|
{ |
|
86
|
|
|
return is_callable($this->customResultDecorator); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
private function getNormalizedItemsPerPage(): int |
|
90
|
|
|
{ |
|
91
|
|
|
return $this->getRequestedItemPerPage() > $this->defaultMaxItemsPerPage ? |
|
92
|
|
|
$this->defaultMaxItemsPerPage : $this->getRequestedItemPerPage(); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
private function applyRequestOnPaginatorHandler(): void |
|
96
|
|
|
{ |
|
97
|
|
|
$this->paginatorHandler->setItemsPerPage($this->getNormalizedItemsPerPage()); |
|
98
|
|
|
|
|
99
|
|
|
if ($this->getRequestedPageNumber() <= $this->paginatorHandler->totalPages()) { |
|
100
|
|
|
$this->paginatorHandler->setCurrentPage($this->getRequestedPageNumber()); |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
private function getRequestedPageNumber(): int |
|
105
|
|
|
{ |
|
106
|
|
|
return (int) $this->request->query->get('page', 1); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
private function collection() |
|
110
|
|
|
{ |
|
111
|
|
|
return $this->getRequestedPageNumber() <= $this->paginatorHandler->totalPages() ? |
|
112
|
|
|
$this->paginatorHandler->getCurrentPageResults() : []; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
private function getRequestedItemPerPage(): int |
|
116
|
|
|
{ |
|
117
|
|
|
return (int) $this->request->query->get('itemsPerPage', $this->defaultItemsPerPage); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
private function count(): int |
|
121
|
|
|
{ |
|
122
|
|
|
return $this->getRequestedPageNumber() <= $this->paginatorHandler->totalPages() ? |
|
123
|
|
|
$this->paginatorHandler->count() : 0; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
private function getResult(): array |
|
127
|
|
|
{ |
|
128
|
|
|
return [ |
|
129
|
|
|
'pagination' => [ |
|
130
|
|
|
'totalPages' => $this->paginatorHandler->totalPages(), |
|
131
|
|
|
'totalItems' => $this->paginatorHandler->totalItems(), |
|
132
|
|
|
'count' => $this->count(), |
|
133
|
|
|
'itemsPerPage' => $this->paginatorHandler->numberOfItemsPerPage(), |
|
134
|
|
|
'page' => $this->getRequestedPageNumber(), |
|
135
|
|
|
], |
|
136
|
|
|
'data' => $this->collection(), |
|
137
|
|
|
]; |
|
138
|
|
|
} |
|
139
|
|
|
} |
|
140
|
|
|
|