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