|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Halapi\Factory; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\Common\Persistence\ObjectManager; |
|
6
|
|
|
use Halapi\Representation\PaginatedRepresentation; |
|
7
|
|
|
use Pagerfanta\Adapter\AdapterInterface; |
|
8
|
|
|
use Pagerfanta\Pagerfanta; |
|
9
|
|
|
use Symfony\Component\HttpFoundation\RequestStack; |
|
10
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
|
11
|
|
|
use Symfony\Component\Routing\Generator\UrlGeneratorInterface; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Class PaginationFactory. |
|
15
|
|
|
* |
|
16
|
|
|
* @author Romain Richard |
|
17
|
|
|
*/ |
|
18
|
|
|
class PaginationFactory |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* @var ObjectManager |
|
22
|
|
|
*/ |
|
23
|
|
|
public $objectManager; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var UrlGeneratorInterface |
|
27
|
|
|
*/ |
|
28
|
|
|
public $urlGenerator; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var RequestStack |
|
32
|
|
|
*/ |
|
33
|
|
|
private $requestStack; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @var string |
|
37
|
|
|
*/ |
|
38
|
|
|
private $pagerStrategy; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* PaginationFactory constructor. |
|
42
|
|
|
* |
|
43
|
|
|
* @param UrlGeneratorInterface $urlGenerator |
|
44
|
|
|
* @param ObjectManager $objectManager |
|
45
|
|
|
* @param RequestStack $requestStack |
|
46
|
|
|
* @param string $pagerStrategy |
|
47
|
|
|
*/ |
|
48
|
|
|
public function __construct( |
|
49
|
|
|
UrlGeneratorInterface $urlGenerator, |
|
50
|
|
|
ObjectManager $objectManager, |
|
51
|
|
|
RequestStack $requestStack, |
|
52
|
|
|
$pagerStrategy = 'ORM' |
|
53
|
|
|
) { |
|
54
|
|
|
$this->urlGenerator = $urlGenerator; |
|
55
|
|
|
$this->objectManager = $objectManager; |
|
56
|
|
|
$this->requestStack = $requestStack; |
|
57
|
|
|
$this->setPagerStrategy($pagerStrategy); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Get a paginated representation of a collection of entities. |
|
62
|
|
|
* |
|
63
|
|
|
* @param string $className |
|
64
|
|
|
* |
|
65
|
|
|
* @return PaginatedRepresentation |
|
66
|
|
|
*/ |
|
67
|
|
|
public function getRepresentation($className) |
|
68
|
|
|
{ |
|
69
|
|
|
$shortName = (new \ReflectionClass($className))->getShortName(); |
|
70
|
|
|
$repository = $this->objectManager->getRepository($className); |
|
71
|
|
|
if (!is_callable([$repository, 'findAllSorted'])) { |
|
72
|
|
|
throw new \BadMethodCallException(sprintf( |
|
73
|
|
|
"Your repository for the object %s must implement the 'findAllSorted' method |
|
74
|
|
|
\n |
|
75
|
|
|
Prototype: findAllSorted(array \$sorting, array \$filterValues, array \$filerOperators)", |
|
76
|
|
|
$shortName |
|
77
|
|
|
)); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
list($page, $limit, $sorting, $filterValues, $filerOperators) = array_values($this->addPaginationParams()); |
|
81
|
|
|
$queryBuilder = $repository->findAllSorted($sorting, $filterValues, $filerOperators); |
|
|
|
|
|
|
82
|
|
|
|
|
83
|
|
|
$pagerAdapter = $this->getPagerAdapter($queryBuilder); |
|
84
|
|
|
$pager = new Pagerfanta($pagerAdapter); |
|
85
|
|
|
$pager->setMaxPerPage($limit); |
|
86
|
|
|
$pager->setCurrentPage($page); |
|
87
|
|
|
|
|
88
|
|
|
return new PaginatedRepresentation( |
|
89
|
|
|
$page, |
|
90
|
|
|
$limit, |
|
91
|
|
|
[ |
|
92
|
|
|
'self' => $this->getPaginatedRoute($shortName, $limit, $page, $sorting), |
|
93
|
|
|
'first' => $this->getPaginatedRoute($shortName, $limit, 1, $sorting), |
|
94
|
|
|
'next' => $this->getPaginatedRoute( |
|
95
|
|
|
$shortName, |
|
96
|
|
|
$limit, |
|
97
|
|
|
$page < $pager->getNbPages() ? $page + 1 : $pager->getNbPages(), |
|
98
|
|
|
$sorting |
|
99
|
|
|
), |
|
100
|
|
|
'last' => $this->getPaginatedRoute($shortName, $limit, $pager->getNbPages(), $sorting), |
|
101
|
|
|
], |
|
102
|
|
|
(array) $pager->getCurrentPageResults() |
|
103
|
|
|
); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* @param string $pagerStrategy |
|
108
|
|
|
*/ |
|
109
|
|
|
public function setPagerStrategy($pagerStrategy) |
|
110
|
|
|
{ |
|
111
|
|
|
if (!class_exists('Pagerfanta\Adapter\Doctrine'.$pagerStrategy.'Adapter')) { |
|
112
|
|
|
throw new \InvalidArgumentException(sprintf( |
|
113
|
|
|
'No adapter named %s found in %s namespace', |
|
114
|
|
|
'Doctrine'.$pagerStrategy.'Adapter', |
|
115
|
|
|
'Pagerfanta\Adapter' |
|
116
|
|
|
)); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
$this->pagerStrategy = $pagerStrategy; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* @param object $queryBuilder |
|
124
|
|
|
* |
|
125
|
|
|
* @return AdapterInterface |
|
126
|
|
|
*/ |
|
127
|
|
|
private function getPagerAdapter($queryBuilder) |
|
128
|
|
|
{ |
|
129
|
|
|
$adapterClassName = 'Pagerfanta\Adapter\Doctrine'.$this->pagerStrategy.'Adapter'; |
|
130
|
|
|
|
|
131
|
|
|
return new $adapterClassName($queryBuilder); |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* Get the pagination parameters, filtered. |
|
136
|
|
|
* |
|
137
|
|
|
* @return array |
|
138
|
|
|
*/ |
|
139
|
|
|
private function addPaginationParams() |
|
140
|
|
|
{ |
|
141
|
|
|
$resolver = new OptionsResolver(); |
|
142
|
|
|
|
|
143
|
|
|
$resolver->setDefaults(array( |
|
144
|
|
|
'page' => '1', |
|
145
|
|
|
'limit' => '20', |
|
146
|
|
|
'sorting' => [], |
|
147
|
|
|
'filtervalue' => [], |
|
148
|
|
|
'filteroperator' => [], |
|
149
|
|
|
)); |
|
150
|
|
|
|
|
151
|
|
|
$resolver->setAllowedTypes('page', ['NULL', 'string']); |
|
152
|
|
|
$resolver->setAllowedTypes('limit', ['NULL', 'string']); |
|
153
|
|
|
$resolver->setAllowedTypes('sorting', ['NULL', 'array']); |
|
154
|
|
|
$resolver->setAllowedTypes('filtervalue', ['NULL', 'array']); |
|
155
|
|
|
$resolver->setAllowedTypes('filteroperator', ['NULL', 'array']); |
|
156
|
|
|
|
|
157
|
|
|
$request = $this->requestStack->getMasterRequest(); |
|
158
|
|
|
|
|
159
|
|
|
return $resolver->resolve(array_filter([ |
|
160
|
|
|
'page' => $request->query->get('page'), |
|
161
|
|
|
'limit' => $request->query->get('limit'), |
|
162
|
|
|
'sorting' => $request->query->get('sorting'), |
|
163
|
|
|
'filtervalue' => $request->query->get('filtervalue'), |
|
164
|
|
|
'filteroperator' => $request->query->get('filteroperator'), |
|
165
|
|
|
])); |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
/** |
|
169
|
|
|
* Return the url of a resource based on the 'get_entity' route name convention. |
|
170
|
|
|
* |
|
171
|
|
|
* @param string $name |
|
172
|
|
|
* @param $limit |
|
173
|
|
|
* @param $page |
|
174
|
|
|
* @param $sorting |
|
175
|
|
|
* |
|
176
|
|
|
* @return string |
|
177
|
|
|
*/ |
|
178
|
|
|
private function getPaginatedRoute($name, $limit, $page, $sorting) |
|
179
|
|
|
{ |
|
180
|
|
|
return $this->urlGenerator->generate( |
|
181
|
|
|
'get_'.strtolower($name).'s', |
|
182
|
|
|
[ |
|
183
|
|
|
'sorting' => $sorting, |
|
184
|
|
|
'page' => $page, |
|
185
|
|
|
'limit' => $limit, |
|
186
|
|
|
] |
|
187
|
|
|
); |
|
188
|
|
|
} |
|
189
|
|
|
} |
|
190
|
|
|
|
This check marks calls to methods that do not seem to exist on an object.
This is most likely the result of a method being renamed without all references to it being renamed likewise.