|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace LAG\AdminBundle\Admin\Request; |
|
4
|
|
|
|
|
5
|
|
|
use Exception; |
|
6
|
|
|
use LAG\AdminBundle\Action\Configuration\ActionConfiguration; |
|
7
|
|
|
use LAG\AdminBundle\Admin\AdminInterface; |
|
8
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
9
|
|
|
|
|
10
|
|
|
class LoadParameterExtractor |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* @var ActionConfiguration |
|
14
|
|
|
*/ |
|
15
|
|
|
private $configuration; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @var array |
|
19
|
|
|
*/ |
|
20
|
|
|
private $criteria = []; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var array |
|
24
|
|
|
*/ |
|
25
|
|
|
private $order = []; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var int |
|
29
|
|
|
*/ |
|
30
|
|
|
private $maxPerPage = 5000; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var int |
|
34
|
|
|
*/ |
|
35
|
|
|
private $page = 1; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* LoadParameterExtractor constructor. |
|
39
|
|
|
* |
|
40
|
|
|
* @param ActionConfiguration $configuration |
|
41
|
4 |
|
* @param array $filters |
|
42
|
|
|
* |
|
43
|
4 |
|
* @throws Exception |
|
44
|
4 |
|
*/ |
|
45
|
|
|
public function __construct(ActionConfiguration $configuration, array $filters = []) |
|
46
|
|
|
{ |
|
47
|
|
|
$this->configuration = $configuration; |
|
48
|
|
|
|
|
49
|
4 |
|
$configuredFilters = $this->configuration->getParameter('filters'); |
|
50
|
|
|
|
|
51
|
4 |
|
foreach ($filters as $filter => $value) { |
|
52
|
4 |
|
|
|
53
|
4 |
|
if (!key_exists($filter, $configuredFilters)) { |
|
54
|
4 |
|
throw new Exception('The filter "'.$filter.'" is not configured'); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
if (null !== $value) { |
|
58
|
|
|
if ('string' === $configuredFilters[$filter]['type']) { |
|
59
|
|
|
$value = '%'.$value.'%'; |
|
60
|
|
|
} |
|
61
|
4 |
|
$this->criteria[$filter] = $value; |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
4 |
|
} |
|
65
|
4 |
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @param Request $request |
|
68
|
4 |
|
*/ |
|
69
|
|
|
public function load(Request $request) |
|
70
|
|
|
{ |
|
71
|
|
|
$this->loadCriteria($request); |
|
72
|
|
|
$this->loadOrder($request); |
|
73
|
|
|
$this->loadPagination($request); |
|
74
|
|
|
} |
|
75
|
4 |
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Get the criteria values. |
|
78
|
|
|
* |
|
79
|
|
|
* @param Request $request |
|
80
|
4 |
|
*/ |
|
81
|
|
|
private function loadCriteria(Request $request) |
|
82
|
|
|
{ |
|
83
|
4 |
|
$criteriaConfiguration = $this |
|
84
|
4 |
|
->configuration |
|
85
|
|
|
->getParameter('criteria') |
|
86
|
|
|
; |
|
87
|
|
|
|
|
88
|
4 |
View Code Duplication |
foreach ($criteriaConfiguration as $criterion) { |
|
|
|
|
|
|
89
|
4 |
|
$value = $request->get($criterion); |
|
90
|
|
|
|
|
91
|
|
|
if (null !== $value) { |
|
92
|
|
|
$this->criteria[$criterion] = $value; |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* @param Request $request |
|
99
|
|
|
*/ |
|
100
|
|
|
private function loadOrder(Request $request) |
|
101
|
|
|
{ |
|
102
|
4 |
|
$sortable = $this |
|
103
|
|
|
->configuration |
|
104
|
|
|
->getParameter('sortable') |
|
105
|
4 |
|
; |
|
106
|
|
|
|
|
107
|
4 |
|
// if the Action is not sortable, we do not load order parameters |
|
108
|
|
|
if (true !== $sortable) { |
|
109
|
|
|
return; |
|
110
|
|
|
} |
|
111
|
|
|
$this->order = $this->configuration->getParameter('order'); |
|
|
|
|
|
|
112
|
4 |
|
$sort = $request->get('sort'); |
|
113
|
4 |
|
$order = $request->get('order', 'ASC'); |
|
114
|
|
|
|
|
115
|
|
|
if ($sort) { |
|
116
|
4 |
|
$this->order = [ |
|
117
|
|
|
$sort => $order, |
|
118
|
|
|
]; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
4 |
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
4 |
|
* Get the pagination values. |
|
125
|
|
|
* |
|
126
|
|
|
* @param Request $request |
|
127
|
|
|
*/ |
|
128
|
|
|
private function loadPagination(Request $request) |
|
129
|
4 |
|
{ |
|
130
|
|
|
// the default value is the configured one |
|
131
|
4 |
|
$this->maxPerPage = $this->configuration->getParameter('max_per_page'); |
|
132
|
|
|
|
|
133
|
|
|
if (false === $this->configuration->getParameter('pager')) { |
|
134
|
|
|
return; |
|
135
|
|
|
} |
|
136
|
|
|
// the pagination is required if the load strategy is multiple and the pagerfanta is configured |
|
137
|
4 |
|
$isPaginationRequired = |
|
138
|
|
|
AdminInterface::LOAD_STRATEGY_MULTIPLE === $this->configuration->getParameter('load_strategy') && |
|
139
|
4 |
|
'pagerfanta' === $this->configuration->getParameter('pager') |
|
140
|
|
|
; |
|
141
|
|
|
|
|
142
|
|
|
if ($isPaginationRequired) { |
|
143
|
|
|
// retrieve the page parameter value |
|
144
|
|
|
$this->page = (int)$request->get('page', 1); |
|
145
|
4 |
|
} |
|
146
|
|
|
|
|
147
|
4 |
|
if (null !== $request->get('maxPerPage')) { |
|
148
|
|
|
$this->maxPerPage = $request->get('maxPerPage'); |
|
149
|
|
|
} |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
4 |
|
* @return array |
|
154
|
|
|
*/ |
|
155
|
4 |
|
public function getCriteria() |
|
156
|
|
|
{ |
|
157
|
|
|
return $this->criteria; |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* @return array |
|
162
|
|
|
*/ |
|
163
|
|
|
public function getOrder() |
|
164
|
|
|
{ |
|
165
|
|
|
return $this->order; |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
/** |
|
169
|
|
|
* @return int |
|
170
|
|
|
*/ |
|
171
|
|
|
public function getMaxPerPage() |
|
172
|
|
|
{ |
|
173
|
|
|
return $this->maxPerPage; |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
/** |
|
177
|
|
|
* @return int |
|
178
|
|
|
*/ |
|
179
|
|
|
public function getPage() |
|
180
|
|
|
{ |
|
181
|
|
|
return $this->page; |
|
182
|
|
|
} |
|
183
|
|
|
} |
|
184
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.