|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Bugloos\ResponderBundle\PaginatorHandler\Pagerfanta; |
|
4
|
|
|
|
|
5
|
|
|
use Bugloos\ResponderBundle\PaginatorHandler\Contract\PaginatorHandlerInterface; |
|
6
|
|
|
use Pagerfanta\Pagerfanta; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* @author Mojtaba Gheytasi <[email protected]> |
|
10
|
|
|
*/ |
|
11
|
|
|
class PagerfantaHandler implements PaginatorHandlerInterface |
|
12
|
|
|
{ |
|
13
|
|
|
private const FETCH_JOIN_COLLECTION = 'fetchJoinCollection'; |
|
14
|
|
|
|
|
15
|
|
|
private Pagerfanta $paginatorHandler; |
|
16
|
|
|
|
|
17
|
|
|
public function initializePaginatorHandler($query, int $limit = null, array $options = []): void |
|
18
|
|
|
{ |
|
19
|
|
|
$fetchJoinCollection = true; |
|
20
|
|
|
|
|
21
|
|
|
if ( |
|
22
|
|
|
array_key_exists(self::FETCH_JOIN_COLLECTION, $options) && |
|
23
|
|
|
$options['fetchJoinCollection'] === false |
|
24
|
|
|
) { |
|
25
|
|
|
$fetchJoinCollection = false; |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
$adapter = new CustomQueryAdapter($query, $fetchJoinCollection); |
|
29
|
|
|
|
|
30
|
|
|
if ($limit !== null) { |
|
31
|
|
|
$adapter->setLimit($limit); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
$this->paginatorHandler = new Pagerfanta($adapter); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
public function getCurrentPageResults(): iterable |
|
38
|
|
|
{ |
|
39
|
|
|
return $this->paginatorHandler->getCurrentPageResults(); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
public function totalPages(): int |
|
43
|
|
|
{ |
|
44
|
|
|
return $this->paginatorHandler->getNbPages(); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
public function totalItems(): int |
|
48
|
|
|
{ |
|
49
|
|
|
return $this->paginatorHandler->getNbResults(); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
public function count(): int |
|
53
|
|
|
{ |
|
54
|
|
|
return \count($this->paginatorHandler->getIterator()); |
|
|
|
|
|
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
public function numberOfItemsPerPage(): int |
|
58
|
|
|
{ |
|
59
|
|
|
return $this->paginatorHandler->getMaxPerPage(); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
public function setItemsPerPage(int $itemPerPage): void |
|
63
|
|
|
{ |
|
64
|
|
|
$this->paginatorHandler->setMaxPerPage($itemPerPage); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
public function setCurrentPage(int $page): void |
|
68
|
|
|
{ |
|
69
|
|
|
$this->paginatorHandler->setCurrentPage($page); |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
|