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\API; |
13
|
|
|
|
14
|
|
|
use Symfony\Component\HttpFoundation\Request; |
15
|
|
|
use WBW\Bundle\JQuery\DataTablesBundle\Provider\DataTablesProviderInterface; |
16
|
|
|
use WBW\Library\Core\Network\HTTP\HTTPInterface; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* DataTables wrapper. |
20
|
|
|
* |
21
|
|
|
* @author webeweb <https://github.com/webeweb/> |
22
|
|
|
* @package WBW\Bundle\JQuery\DataTablesBundle\API |
23
|
|
|
*/ |
24
|
|
|
class DataTablesWrapper implements HTTPInterface { |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Columns. |
28
|
|
|
* |
29
|
|
|
* @var DataTablesColumn[] |
30
|
|
|
*/ |
31
|
|
|
private $columns; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Mapping. |
35
|
|
|
* |
36
|
|
|
* @var DataTablesMappingInterface |
37
|
|
|
*/ |
38
|
|
|
private $mapping; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Method. |
42
|
|
|
* |
43
|
|
|
* @var string |
44
|
|
|
*/ |
45
|
|
|
private $method; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Name. |
49
|
|
|
* |
50
|
|
|
* @var string |
51
|
|
|
*/ |
52
|
|
|
private $name; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Options. |
56
|
|
|
* |
57
|
|
|
* @var DataTablesOptionsInterface |
58
|
|
|
*/ |
59
|
|
|
private $options; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Order. |
63
|
|
|
* |
64
|
|
|
* @var array |
65
|
|
|
*/ |
66
|
|
|
private $order; |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Processing. |
70
|
|
|
* |
71
|
|
|
* @var bool |
72
|
|
|
*/ |
73
|
|
|
private $processing; |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Provider. |
77
|
|
|
* |
78
|
|
|
* @var DataTablesProviderInterface |
79
|
|
|
*/ |
80
|
|
|
private $provider; |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Request. |
84
|
|
|
* |
85
|
|
|
* @var DataTablesRequestInterface |
86
|
|
|
*/ |
87
|
|
|
private $request; |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Response. |
91
|
|
|
* |
92
|
|
|
* @var DataTablesResponseInterface |
93
|
|
|
*/ |
94
|
|
|
private $response; |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Server side. |
98
|
|
|
* |
99
|
|
|
* @var bool |
100
|
|
|
*/ |
101
|
|
|
private $serverSide; |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* URL. |
105
|
|
|
* |
106
|
|
|
* @var string |
107
|
|
|
*/ |
108
|
|
|
private $url; |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Constructor. |
112
|
|
|
* |
113
|
|
|
* @param string $method The method. |
114
|
|
|
* @param string $url The URL. |
115
|
|
|
* @param string $name The name. |
116
|
|
|
*/ |
117
|
|
|
public function __construct($method, $url, $name) { |
118
|
|
|
$this->setMapping(new DataTablesMapping()); |
119
|
|
|
|
120
|
|
|
$this->setColumns([]); |
121
|
|
|
$this->setMethod($method); |
122
|
|
|
$this->setName($name); |
123
|
|
|
$this->setOrder([]); |
124
|
|
|
$this->setProcessing(true); |
125
|
|
|
$this->setServerSide(true); |
126
|
|
|
$this->setUrl($url); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Add a column. |
131
|
|
|
* |
132
|
|
|
* @param DataTablesColumn $column The column. |
133
|
|
|
* @return DataTablesWrapper Returns this wrapper. |
134
|
|
|
*/ |
135
|
|
|
public function addColumn(DataTablesColumn $column) { |
136
|
|
|
if (null === $column->getMapping()->getPrefix()) { |
137
|
|
|
$column->getMapping()->setPrefix($this->mapping->getPrefix()); |
138
|
|
|
} |
139
|
|
|
$this->columns[$column->getData()] = $column; |
140
|
|
|
return $this; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Get a column. |
145
|
|
|
* |
146
|
|
|
* @param string $data The column data. |
147
|
|
|
* @return DataTablesColumn Returns the column in case of success, null otherwise. |
148
|
|
|
*/ |
149
|
|
|
public function getColumn($data) { |
150
|
|
|
if (true === array_key_exists($data, $this->columns)) { |
151
|
|
|
return $this->columns[$data]; |
152
|
|
|
} |
153
|
|
|
return null; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Get the columns. |
158
|
|
|
* |
159
|
|
|
* @return DataTablesColumn[] Returns the columns. |
160
|
|
|
*/ |
161
|
|
|
public function getColumns() { |
162
|
|
|
return $this->columns; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Get the mapping. |
167
|
|
|
* |
168
|
|
|
* @return DataTablesMappingInterface Returns the mapping. |
169
|
|
|
*/ |
170
|
|
|
public function getMapping() { |
171
|
|
|
return $this->mapping; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Get the method. |
176
|
|
|
* |
177
|
|
|
* @return string Returns the method. |
178
|
|
|
*/ |
179
|
|
|
public function getMethod() { |
180
|
|
|
return $this->method; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* Get the name. |
185
|
|
|
* |
186
|
|
|
* @return string Returns the name. |
187
|
|
|
*/ |
188
|
|
|
public function getName() { |
189
|
|
|
return $this->name; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* Get the options. |
194
|
|
|
* |
195
|
|
|
* @return DataTablesOptionsInterface Returns the options. |
196
|
|
|
*/ |
197
|
|
|
public function getOptions() { |
198
|
|
|
return $this->options; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* Get the order. |
203
|
|
|
* |
204
|
|
|
* @return array Returns the order. |
205
|
|
|
*/ |
206
|
|
|
public function getOrder() { |
207
|
|
|
return $this->order; |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* Get the processing. |
212
|
|
|
* |
213
|
|
|
* @return bool Returns the processing. |
214
|
|
|
*/ |
215
|
|
|
public function getProcessing() { |
216
|
|
|
return $this->processing; |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* Get the provider. |
221
|
|
|
* |
222
|
|
|
* @return DataTablesProviderInterface Returns the provider. |
223
|
|
|
*/ |
224
|
|
|
public function getProvider() { |
225
|
|
|
return $this->provider; |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
/** |
229
|
|
|
* Get the request. |
230
|
|
|
* |
231
|
|
|
* @return DataTablesRequestInterface The request. |
232
|
|
|
*/ |
233
|
|
|
public function getRequest() { |
234
|
|
|
return $this->request; |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
/** |
238
|
|
|
* Get the response. |
239
|
|
|
* |
240
|
|
|
* @return DataTablesResponseInterface Returns the response. |
241
|
|
|
*/ |
242
|
|
|
public function getResponse() { |
243
|
|
|
return $this->response; |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
/** |
247
|
|
|
* Get the server side. |
248
|
|
|
* |
249
|
|
|
* @return bool Returns the server side. |
250
|
|
|
*/ |
251
|
|
|
public function getServerSide() { |
252
|
|
|
return $this->serverSide; |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
/** |
256
|
|
|
* Get the URL. |
257
|
|
|
* |
258
|
|
|
* @return string Returns the URL. |
259
|
|
|
*/ |
260
|
|
|
public function getUrl() { |
261
|
|
|
return $this->url; |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
/** |
265
|
|
|
* Parse a request. |
266
|
|
|
* |
267
|
|
|
* @param Request $request The request. |
268
|
|
|
* @return DataTablesWrapper Returns this wrapper. |
269
|
|
|
*/ |
270
|
|
|
public function parse(Request $request) { |
271
|
|
|
$this->request = DataTablesRequest::parse($this, $request); |
272
|
|
|
$this->response = DataTablesResponse::parse($this, $this->request); |
273
|
|
|
return $this; |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
/** |
277
|
|
|
* Remove a column. |
278
|
|
|
* |
279
|
|
|
* @param DataTablesColumn $column The column. |
280
|
|
|
* @return DataTablesWrapper Returns this wrapper. |
281
|
|
|
*/ |
282
|
|
|
public function removeColumn(DataTablesColumn $column) { |
283
|
|
|
if (true === array_key_exists($column->getData(), $this->columns)) { |
284
|
|
|
$this->columns[$column->getData()]->getMapping()->setPrefix(null); |
285
|
|
|
unset($this->columns[$column->getData()]); |
286
|
|
|
} |
287
|
|
|
return $this; |
288
|
|
|
} |
289
|
|
|
|
290
|
|
|
/** |
291
|
|
|
* Set the columns. |
292
|
|
|
* |
293
|
|
|
* @param DataTablesColumn[] $columns The columns. |
294
|
|
|
* @return DataTablesWrapper Returns this wrapper. |
295
|
|
|
*/ |
296
|
|
|
private function setColumns(array $columns) { |
297
|
|
|
$this->columns = $columns; |
298
|
|
|
return $this; |
299
|
|
|
} |
300
|
|
|
|
301
|
|
|
/** |
302
|
|
|
* Set the method. |
303
|
|
|
* |
304
|
|
|
* @param string $method The method. |
305
|
|
|
* @return DataTablesWrapper Returns this wrapper. |
306
|
|
|
*/ |
307
|
|
|
public function setMethod($method) { |
308
|
|
|
$this->method = (true === in_array($method, [self::HTTP_METHOD_GET, self::HTTP_METHOD_POST]) ? $method : self::HTTP_METHOD_POST); |
309
|
|
|
return $this; |
310
|
|
|
} |
311
|
|
|
|
312
|
|
|
/** |
313
|
|
|
* Set the mapping. |
314
|
|
|
* |
315
|
|
|
* @param DataTablesMappingInterface $mapping The mapping |
316
|
|
|
* @return DataTablesWrapper Returns this wrapper. |
317
|
|
|
*/ |
318
|
|
|
protected function setMapping(DataTablesMappingInterface $mapping) { |
319
|
|
|
$this->mapping = $mapping; |
320
|
|
|
return $this; |
321
|
|
|
} |
322
|
|
|
|
323
|
|
|
/** |
324
|
|
|
* Set the name. |
325
|
|
|
* |
326
|
|
|
* @param string $name The name. |
327
|
|
|
* @return DataTablesWrapper Returns this wrapper. |
328
|
|
|
*/ |
329
|
|
|
public function setName($name) { |
330
|
|
|
$this->name = $name; |
331
|
|
|
return $this; |
332
|
|
|
} |
333
|
|
|
|
334
|
|
|
/** |
335
|
|
|
* Set the options. |
336
|
|
|
* |
337
|
|
|
* @param DataTablesOptionsInterface $options The options. |
338
|
|
|
* @return DataTablesWrapper Returns this wrapper. |
339
|
|
|
*/ |
340
|
|
|
public function setOptions(DataTablesOptionsInterface $options) { |
341
|
|
|
$this->options = $options; |
342
|
|
|
return $this; |
343
|
|
|
} |
344
|
|
|
|
345
|
|
|
/** |
346
|
|
|
* Set the order. |
347
|
|
|
* |
348
|
|
|
* @param array $order The order. |
349
|
|
|
* @return DataTablesWrapper Returns this wrapper. |
350
|
|
|
*/ |
351
|
|
|
public function setOrder(array $order) { |
352
|
|
|
$this->order = $order; |
353
|
|
|
return $this; |
354
|
|
|
} |
355
|
|
|
|
356
|
|
|
/** |
357
|
|
|
* Set the processing. |
358
|
|
|
* |
359
|
|
|
* @param bool $processing The processing. |
360
|
|
|
* @return DataTablesWrapper Returns this wrapper. |
361
|
|
|
*/ |
362
|
|
|
public function setProcessing($processing) { |
363
|
|
|
$this->processing = (false === $processing ? false : true); |
364
|
|
|
return $this; |
365
|
|
|
} |
366
|
|
|
|
367
|
|
|
/** |
368
|
|
|
* Set the provider. |
369
|
|
|
* |
370
|
|
|
* @param DataTablesProviderInterface $provider The provider. |
371
|
|
|
* @return DataTablesWrapper Returns this wrapper. |
372
|
|
|
*/ |
373
|
|
|
public function setProvider(DataTablesProviderInterface $provider) { |
374
|
|
|
$this->provider = $provider; |
375
|
|
|
return $this; |
376
|
|
|
} |
377
|
|
|
|
378
|
|
|
/** |
379
|
|
|
* Set the server side. |
380
|
|
|
* |
381
|
|
|
* @param bool $serverSide The server side. |
382
|
|
|
* @return DataTablesWrapper Returns this wrapper. |
383
|
|
|
*/ |
384
|
|
|
public function setServerSide($serverSide) { |
385
|
|
|
$this->serverSide = (false === $serverSide ? false : true); |
386
|
|
|
return $this; |
387
|
|
|
} |
388
|
|
|
|
389
|
|
|
/** |
390
|
|
|
* Set the URL. |
391
|
|
|
* |
392
|
|
|
* @param string $url The UTL. |
393
|
|
|
* @return DataTablesWrapper Returns this wrapper. |
394
|
|
|
*/ |
395
|
|
|
public function setUrl($url) { |
396
|
|
|
$this->url = $url; |
397
|
|
|
return $this; |
398
|
|
|
} |
399
|
|
|
|
400
|
|
|
} |
401
|
|
|
|