1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the Explicit Architecture POC, |
7
|
|
|
* which is created on top of the Symfony Demo application. |
8
|
|
|
* |
9
|
|
|
* (c) Herberto Graça <[email protected]> |
10
|
|
|
* |
11
|
|
|
* For the full copyright and license information, please view the LICENSE |
12
|
|
|
* file that was distributed with this source code. |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace Acme\App\Presentation\Web\Infrastructure\Paginator\Pagerfanta; |
16
|
|
|
|
17
|
|
|
use Acme\App\Presentation\Web\Core\Port\Paginator\PaginatorInterface; |
18
|
|
|
use ArrayIterator; |
19
|
|
|
use IteratorAggregate; |
20
|
|
|
use Pagerfanta\Adapter\ArrayAdapter; |
21
|
|
|
use Pagerfanta\Pagerfanta; |
22
|
|
|
|
23
|
|
|
final class PagerfantaPaginator implements PaginatorInterface, PagerfantaInterface, IteratorAggregate |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @var Pagerfanta |
27
|
|
|
*/ |
28
|
|
|
private $pagerfanta; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var array |
32
|
|
|
*/ |
33
|
|
|
private $data; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var int |
37
|
|
|
*/ |
38
|
|
|
private $page; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var int |
42
|
|
|
*/ |
43
|
|
|
private $maxItemsPerPage; |
44
|
|
|
|
45
|
|
|
public function __construct( |
46
|
|
|
array $data, |
47
|
|
|
int $page = self::DEFAULT_PAGE, |
48
|
|
|
int $maxItemsPerPage = self::DEFAULT_MAX_ITEMS_PER_PAGE |
49
|
|
|
) { |
50
|
|
|
$this->data = $data; |
51
|
|
|
$this->page = $page; |
52
|
|
|
$this->maxItemsPerPage = $maxItemsPerPage; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function getIterator(): ArrayIterator |
56
|
|
|
{ |
57
|
|
|
$this->paginate($this->data); |
58
|
|
|
|
59
|
|
|
return $this->pagerfanta->getIterator(); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function count(): int |
63
|
|
|
{ |
64
|
|
|
return $this->getIterator()->count(); |
|
|
|
|
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function totalCount(): int |
68
|
|
|
{ |
69
|
|
|
$this->paginate($this->data); |
70
|
|
|
|
71
|
|
|
return $this->pagerfanta->count(); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function haveToPaginate(): bool |
75
|
|
|
{ |
76
|
|
|
$this->paginate($this->data); |
77
|
|
|
|
78
|
|
|
return $this->pagerfanta->haveToPaginate(); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public function getCurrentPage(): int |
82
|
|
|
{ |
83
|
|
|
$this->paginate($this->data); |
84
|
|
|
|
85
|
|
|
return $this->pagerfanta->getCurrentPage(); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
public function getNbPages(): int |
89
|
|
|
{ |
90
|
|
|
$this->paginate($this->data); |
91
|
|
|
|
92
|
|
|
return $this->pagerfanta->getNbPages(); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
public function hasPreviousPage(): bool |
96
|
|
|
{ |
97
|
|
|
$this->paginate($this->data); |
98
|
|
|
|
99
|
|
|
return $this->pagerfanta->hasPreviousPage(); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
public function hasNextPage(): bool |
103
|
|
|
{ |
104
|
|
|
$this->paginate($this->data); |
105
|
|
|
|
106
|
|
|
return $this->pagerfanta->hasNextPage(); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
public function getNextPage(): int |
110
|
|
|
{ |
111
|
|
|
$this->paginate($this->data); |
112
|
|
|
|
113
|
|
|
return $this->pagerfanta->getNextPage(); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
public function getPreviousPage(): int |
117
|
|
|
{ |
118
|
|
|
$this->paginate($this->data); |
119
|
|
|
|
120
|
|
|
return $this->pagerfanta->getPreviousPage(); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
public function setCurrentPage(int $page): void |
124
|
|
|
{ |
125
|
|
|
$this->paginate($this->data); |
126
|
|
|
|
127
|
|
|
$this->pagerfanta->setCurrentPage($page); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
private function paginate(array $data): void |
131
|
|
|
{ |
132
|
|
|
if (!$this->pagerfanta) { |
133
|
|
|
$this->pagerfanta = new Pagerfanta(new ArrayAdapter($data)); |
134
|
|
|
$this->pagerfanta->setMaxPerPage($this->calculateMaxPerPageForArray($data)); |
135
|
|
|
$this->pagerfanta->setCurrentPage($this->page); |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
private function calculateMaxPerPageForArray(array $result): int |
140
|
|
|
{ |
141
|
|
|
if (\count($result) === 0) { |
142
|
|
|
return $this->maxItemsPerPage; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
return min([\count($result), $this->maxItemsPerPage]); |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: