1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the jquery-datatables-bundle package. |
5
|
|
|
* |
6
|
|
|
* (c) 2018 WEBEWEB |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace WBW\Bundle\JQuery\DataTablesBundle\Factory; |
13
|
|
|
|
14
|
|
|
use Symfony\Component\HttpFoundation\ParameterBag; |
15
|
|
|
use Symfony\Component\HttpFoundation\Request; |
16
|
|
|
use Symfony\Component\Security\Core\User\UserInterface; |
17
|
|
|
use WBW\Bundle\JQuery\DataTablesBundle\Api\DataTablesColumnInterface; |
18
|
|
|
use WBW\Bundle\JQuery\DataTablesBundle\Api\DataTablesOptionsInterface; |
19
|
|
|
use WBW\Bundle\JQuery\DataTablesBundle\Api\DataTablesOrderInterface; |
20
|
|
|
use WBW\Bundle\JQuery\DataTablesBundle\Api\DataTablesRequestInterface; |
21
|
|
|
use WBW\Bundle\JQuery\DataTablesBundle\Api\DataTablesResponseInterface; |
22
|
|
|
use WBW\Bundle\JQuery\DataTablesBundle\Api\DataTablesSearchInterface; |
23
|
|
|
use WBW\Bundle\JQuery\DataTablesBundle\Api\DataTablesWrapperInterface; |
24
|
|
|
use WBW\Bundle\JQuery\DataTablesBundle\Model\DataTablesColumn; |
25
|
|
|
use WBW\Bundle\JQuery\DataTablesBundle\Model\DataTablesEnumerator; |
26
|
|
|
use WBW\Bundle\JQuery\DataTablesBundle\Model\DataTablesOptions; |
27
|
|
|
use WBW\Bundle\JQuery\DataTablesBundle\Model\DataTablesOrder; |
28
|
|
|
use WBW\Bundle\JQuery\DataTablesBundle\Model\DataTablesRequest; |
29
|
|
|
use WBW\Bundle\JQuery\DataTablesBundle\Model\DataTablesResponse; |
30
|
|
|
use WBW\Bundle\JQuery\DataTablesBundle\Model\DataTablesSearch; |
31
|
|
|
use WBW\Bundle\JQuery\DataTablesBundle\Model\DataTablesWrapper; |
32
|
|
|
use WBW\Bundle\JQuery\DataTablesBundle\Provider\DataTablesProviderInterface; |
33
|
|
|
use WBW\Library\Types\Helper\ArrayHelper; |
34
|
|
|
use WBW\Library\Types\Helper\BooleanHelper; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* DataTables factory. |
38
|
|
|
* |
39
|
|
|
* @author webeweb <https://github.com/webeweb> |
40
|
|
|
* @package WBW\Bundle\JQuery\DataTablesBundle\Factory |
41
|
|
|
*/ |
42
|
|
|
class DataTablesFactory { |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Copy a parameter bag. |
46
|
|
|
* |
47
|
|
|
* @param ParameterBag $src The source. |
48
|
|
|
* @param ParameterBag $dst The destination. |
49
|
|
|
* @return void |
50
|
|
|
*/ |
51
|
|
|
protected static function copyParameterBag(ParameterBag $src, ParameterBag $dst): void { |
52
|
|
|
|
53
|
|
|
foreach ($src->keys() as $current) { |
54
|
|
|
|
55
|
|
|
if (true === in_array($current, DataTablesEnumerator::enumParameters())) { |
56
|
|
|
continue; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
$dst->set($current, ArrayHelper::get($src->all(), $current)); |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Determine if a raw column is valid. |
65
|
|
|
* |
66
|
|
|
* @param array<string,mixed> $rawColumn The raw column. |
67
|
|
|
* @return bool Returns true in case of success, false otherwise. |
68
|
|
|
*/ |
69
|
|
|
protected static function isValidRawColumn(array $rawColumn): bool { |
70
|
|
|
|
71
|
|
|
if (false === array_key_exists(DataTablesColumnInterface::DATATABLES_PARAMETER_DATA, $rawColumn)) { |
72
|
|
|
return false; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
if (false === array_key_exists(DataTablesColumnInterface::DATATABLES_PARAMETER_NAME, $rawColumn)) { |
76
|
|
|
return false; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
return true; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Determine if a raw order is valid. |
84
|
|
|
* |
85
|
|
|
* @param array<string,string> $rawOrder The raw order. |
86
|
|
|
* @return bool Returns true in case of success, false otherwise. |
87
|
|
|
*/ |
88
|
|
|
protected static function isValidRawOrder(array $rawOrder): bool { |
89
|
|
|
|
90
|
|
|
if (false === array_key_exists(DataTablesOrderInterface::DATATABLES_PARAMETER_COLUMN, $rawOrder)) { |
91
|
|
|
return false; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
if (false === array_key_exists(DataTablesOrderInterface::DATATABLES_PARAMETER_DIR, $rawOrder)) { |
95
|
|
|
return false; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
return true; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Determine if a raw search is valid. |
103
|
|
|
* |
104
|
|
|
* @param array<string,string> $rawSearch The raw search. |
105
|
|
|
* @return bool Returns true in case of success, false otherwise. |
106
|
|
|
*/ |
107
|
|
|
protected static function isValidRawSearch(array $rawSearch): bool { |
108
|
|
|
|
109
|
|
|
if (false === array_key_exists(DataTablesSearchInterface::DATATABLES_PARAMETER_REGEX, $rawSearch)) { |
110
|
|
|
return false; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
if (false === array_key_exists(DataTablesSearchInterface::DATATABLES_PARAMETER_VALUE, $rawSearch)) { |
114
|
|
|
return false; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
return true; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Create a column. |
122
|
|
|
* |
123
|
|
|
* @param string $data The column data. |
124
|
|
|
* @param string $name The column name. |
125
|
|
|
* @param string $cellType The column cell type. |
126
|
|
|
* @return DataTablesColumnInterface Returns the column. |
127
|
|
|
*/ |
128
|
|
|
public static function newColumn(string $data, string $name, string $cellType = DataTablesColumnInterface::DATATABLES_CELL_TYPE_TD): DataTablesColumnInterface { |
129
|
|
|
|
130
|
|
|
$dtColumn = new DataTablesColumn(); |
131
|
|
|
$dtColumn->getMapping()->setColumn($data); |
132
|
|
|
$dtColumn->setCellType($cellType); |
133
|
|
|
$dtColumn->setData($data); |
134
|
|
|
$dtColumn->setName($name); |
135
|
|
|
$dtColumn->setTitle($name); |
136
|
|
|
|
137
|
|
|
return $dtColumn; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Create an options. |
142
|
|
|
* |
143
|
|
|
* @return DataTablesOptionsInterface Returns the options. |
144
|
|
|
*/ |
145
|
|
|
public static function newOptions(): DataTablesOptionsInterface { |
146
|
|
|
return new DataTablesOptions(); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Create a response. |
151
|
|
|
* |
152
|
|
|
* @param DataTablesWrapperInterface $wrapper The wrapper. |
153
|
|
|
* @return DataTablesResponseInterface Returns the response. |
154
|
|
|
*/ |
155
|
|
|
protected static function newResponse(DataTablesWrapperInterface $wrapper): DataTablesResponseInterface { |
156
|
|
|
|
157
|
|
|
$dtResponse = new DataTablesResponse(); |
158
|
|
|
$dtResponse->setDraw($wrapper->getRequest()->getDraw()); |
159
|
|
|
$dtResponse->setWrapper($wrapper); |
160
|
|
|
|
161
|
|
|
return $dtResponse; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Create a wrapper. |
166
|
|
|
* |
167
|
|
|
* @param string $url The URL. |
168
|
|
|
* @param DataTablesProviderInterface $provider The provider. |
169
|
|
|
* @param UserInterface|null $user The user. |
170
|
|
|
* @return DataTablesWrapperInterface Returns the wrapper. |
171
|
|
|
*/ |
172
|
|
|
public static function newWrapper(string $url, DataTablesProviderInterface $provider, UserInterface $user = null): DataTablesWrapperInterface { |
173
|
|
|
|
174
|
|
|
$dtWrapper = new DataTablesWrapper(); |
175
|
|
|
$dtWrapper->getMapping()->setPrefix($provider->getPrefix()); |
176
|
|
|
$dtWrapper->setMethod($provider->getMethod()); |
177
|
|
|
$dtWrapper->setProvider($provider); |
178
|
|
|
$dtWrapper->setUser($user); |
179
|
|
|
$dtWrapper->setUrl($url); |
180
|
|
|
|
181
|
|
|
return $dtWrapper; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* Parse a raw column. |
186
|
|
|
* |
187
|
|
|
* @param array<string,mixed> $rawColumn The raw column. |
188
|
|
|
* @param DataTablesWrapperInterface $wrapper The wrapper. |
189
|
|
|
* @return DataTablesColumnInterface|null Returns the column. |
190
|
|
|
*/ |
191
|
|
|
protected static function parseColumn(array $rawColumn, DataTablesWrapperInterface $wrapper): ?DataTablesColumnInterface { |
192
|
|
|
|
193
|
|
|
if (false === DataTablesFactory::isValidRawColumn($rawColumn)) { |
194
|
|
|
return null; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
$dtColumn = $wrapper->getColumn($rawColumn[DataTablesColumnInterface::DATATABLES_PARAMETER_DATA]); |
198
|
|
|
if (null === $dtColumn) { |
199
|
|
|
return null; |
200
|
|
|
} |
201
|
|
|
if ($dtColumn->getName() !== $rawColumn[DataTablesColumnInterface::DATATABLES_PARAMETER_NAME]) { |
202
|
|
|
return null; |
203
|
|
|
} |
204
|
|
|
if (false === $dtColumn->getSearchable()) { |
205
|
|
|
$dtColumn->setSearch(DataTablesFactory::parseSearch([])); // Set a default search. |
206
|
|
|
return $dtColumn; |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
$dtColumn->setSearch(DataTablesFactory::parseSearch($rawColumn[DataTablesColumnInterface::DATATABLES_PARAMETER_SEARCH])); |
210
|
|
|
|
211
|
|
|
return $dtColumn; |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* Parse the raw columns. |
216
|
|
|
* |
217
|
|
|
* @param array<string,mixed>[] $rawColumns The raw columns. |
218
|
|
|
* @param DataTablesWrapperInterface $wrapper The wrapper. |
219
|
|
|
* @return DataTablesColumnInterface[] Returns the columns. |
220
|
|
|
*/ |
221
|
|
|
protected static function parseColumns(array $rawColumns, DataTablesWrapperInterface $wrapper): array { |
222
|
|
|
|
223
|
|
|
$dtColumns = []; |
224
|
|
|
|
225
|
|
|
foreach ($rawColumns as $current) { |
226
|
|
|
|
227
|
|
|
$dtColumn = DataTablesFactory::parseColumn($current, $wrapper); |
228
|
|
|
if (null === $dtColumn) { |
229
|
|
|
continue; |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
$dtColumns[] = $dtColumn; |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
return $dtColumns; |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* Parse a raw order. |
240
|
|
|
* |
241
|
|
|
* @param array<string,string> $rawOrder The raw order. |
242
|
|
|
* @return DataTablesOrderInterface Returns the order. |
243
|
|
|
*/ |
244
|
|
|
protected static function parseOrder(array $rawOrder): DataTablesOrderInterface { |
245
|
|
|
|
246
|
|
|
$dtOrder = new DataTablesOrder(); |
247
|
|
|
|
248
|
|
|
if (false === DataTablesFactory::isValidRawOrder($rawOrder)) { |
249
|
|
|
return $dtOrder; |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
$dtOrder->setColumn(intval($rawOrder[DataTablesOrderInterface::DATATABLES_PARAMETER_COLUMN])); |
253
|
|
|
$dtOrder->setDir($rawOrder[DataTablesOrderInterface::DATATABLES_PARAMETER_DIR]); |
254
|
|
|
|
255
|
|
|
return $dtOrder; |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
/** |
259
|
|
|
* Parse the raw orders. |
260
|
|
|
* |
261
|
|
|
* @param array<string,string>[] $rawOrders The raw orders. |
262
|
|
|
* @return DataTablesOrderInterface[] Returns the orders. |
263
|
|
|
*/ |
264
|
|
|
protected static function parseOrders(array $rawOrders): array { |
265
|
|
|
|
266
|
|
|
$dtOrders = []; |
267
|
|
|
|
268
|
|
|
foreach ($rawOrders as $current) { |
269
|
|
|
$dtOrders[] = DataTablesFactory::parseOrder($current); |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
return $dtOrders; |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
/** |
276
|
|
|
* Parse a request. |
277
|
|
|
* |
278
|
|
|
* @param DataTablesWrapperInterface $wrapper The wrapper. |
279
|
|
|
* @param Request $request The request. |
280
|
|
|
* @return DataTablesRequestInterface Returns the request. |
281
|
|
|
*/ |
282
|
|
|
protected static function parseRequest(DataTablesWrapperInterface $wrapper, Request $request): DataTablesRequestInterface { |
283
|
|
|
|
284
|
|
|
$dtRequest = new DataTablesRequest(); |
285
|
|
|
|
286
|
|
|
DataTablesFactory::copyParameterBag($request->query, $dtRequest->getQuery()); |
287
|
|
|
DataTablesFactory::copyParameterBag($request->request, $dtRequest->getRequest()); |
288
|
|
|
|
289
|
|
|
if ("GET" === $request->getMethod()) { |
290
|
|
|
$parameterBag = $request->query; |
291
|
|
|
} else { |
292
|
|
|
$parameterBag = $request->request; |
293
|
|
|
} |
294
|
|
|
|
295
|
|
|
// Get the request parameters. |
296
|
|
|
$columns = ArrayHelper::get($parameterBag->all(), DataTablesRequestInterface::DATATABLES_PARAMETER_COLUMNS, []); |
297
|
|
|
$orders = ArrayHelper::get($parameterBag->all(), DataTablesRequestInterface::DATATABLES_PARAMETER_ORDER, []); |
298
|
|
|
$search = ArrayHelper::get($parameterBag->all(), DataTablesRequestInterface::DATATABLES_PARAMETER_SEARCH, []); |
299
|
|
|
|
300
|
|
|
// Set the request. |
301
|
|
|
$dtRequest->setColumns(DataTablesFactory::parseColumns($columns, $wrapper)); |
302
|
|
|
$dtRequest->setDraw($parameterBag->getInt(DataTablesRequestInterface::DATATABLES_PARAMETER_DRAW)); |
303
|
|
|
$dtRequest->setLength($parameterBag->getInt(DataTablesRequestInterface::DATATABLES_PARAMETER_LENGTH)); |
304
|
|
|
$dtRequest->setOrder(DataTablesFactory::parseOrders($orders)); |
305
|
|
|
$dtRequest->setSearch(DataTablesFactory::parseSearch($search)); |
306
|
|
|
$dtRequest->setStart($parameterBag->getInt(DataTablesRequestInterface::DATATABLES_PARAMETER_START)); |
307
|
|
|
$dtRequest->setWrapper($wrapper); |
308
|
|
|
|
309
|
|
|
return $dtRequest; |
310
|
|
|
} |
311
|
|
|
|
312
|
|
|
/** |
313
|
|
|
* Parse a raw search. |
314
|
|
|
* |
315
|
|
|
* @param array<string,string> $rawSearch The raw search. |
316
|
|
|
* @return DataTablesSearchInterface Returns the search. |
317
|
|
|
*/ |
318
|
|
|
protected static function parseSearch(array $rawSearch): DataTablesSearchInterface { |
319
|
|
|
|
320
|
|
|
$dtSearch = new DataTablesSearch(); |
321
|
|
|
|
322
|
|
|
if (false === DataTablesFactory::isValidRawSearch($rawSearch)) { |
323
|
|
|
return $dtSearch; |
324
|
|
|
} |
325
|
|
|
|
326
|
|
|
$dtSearch->setRegex(BooleanHelper::parseString($rawSearch[DataTablesSearchInterface::DATATABLES_PARAMETER_REGEX])); |
327
|
|
|
$dtSearch->setValue($rawSearch[DataTablesSearchInterface::DATATABLES_PARAMETER_VALUE]); |
328
|
|
|
|
329
|
|
|
return $dtSearch; |
330
|
|
|
} |
331
|
|
|
|
332
|
|
|
/** |
333
|
|
|
* Parse a request. |
334
|
|
|
* |
335
|
|
|
* @param DataTablesWrapperInterface $wrapper The wrapper. |
336
|
|
|
* @param Request $request The request. |
337
|
|
|
* @return void |
338
|
|
|
*/ |
339
|
|
|
public static function parseWrapper(DataTablesWrapperInterface $wrapper, Request $request): void { |
340
|
|
|
$wrapper->setRequest(DataTablesFactory::parseRequest($wrapper, $request)); |
341
|
|
|
$wrapper->setResponse(DataTablesFactory::newResponse($wrapper)); |
342
|
|
|
} |
343
|
|
|
} |
344
|
|
|
|