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