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