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\ParameterBag; |
15
|
|
|
use Symfony\Component\HttpFoundation\Request; |
16
|
|
|
use WBW\Bundle\JQuery\DataTablesBundle\Helper\DataTablesRequestHelper; |
17
|
|
|
use WBW\Library\Core\Network\HTTP\HTTPInterface; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* DataTables request. |
21
|
|
|
* |
22
|
|
|
* @author webeweb <https://github.com/webeweb/> |
23
|
|
|
* @package WBW\Bundle\JQuery\DataTablesBundle\API |
24
|
|
|
*/ |
25
|
|
|
class DataTablesRequest implements DataTablesRequestInterface, HTTPInterface { |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Columns. |
29
|
|
|
* |
30
|
|
|
* @var DataTablesColumn[] |
31
|
|
|
*/ |
32
|
|
|
private $columns; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Draw. |
36
|
|
|
* |
37
|
|
|
* @var int |
38
|
|
|
*/ |
39
|
|
|
private $draw; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Length. |
43
|
|
|
* |
44
|
|
|
* @var int |
45
|
|
|
*/ |
46
|
|
|
private $length; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Order. |
50
|
|
|
* |
51
|
|
|
* @var DataTablesOrderInterface[] |
52
|
|
|
*/ |
53
|
|
|
private $order; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Query. |
57
|
|
|
* |
58
|
|
|
* @var ParameterBag |
59
|
|
|
*/ |
60
|
|
|
private $query; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Request. |
64
|
|
|
* |
65
|
|
|
* @var ParameterBag |
66
|
|
|
*/ |
67
|
|
|
private $request; |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Search. |
71
|
|
|
* |
72
|
|
|
* @var DataTablesSearchInterface |
73
|
|
|
*/ |
74
|
|
|
private $search; |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Start. |
78
|
|
|
* |
79
|
|
|
* @var int |
80
|
|
|
*/ |
81
|
|
|
private $start; |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Wrapper. |
85
|
|
|
* |
86
|
|
|
* @var DataTablesWrapper |
87
|
|
|
*/ |
88
|
|
|
private $wrapper; |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Constructor. |
92
|
|
|
*/ |
93
|
|
|
protected function __construct() { |
94
|
|
|
$this->setColumns([]); |
95
|
|
|
$this->setDraw(0); |
96
|
|
|
$this->setLength(10); |
97
|
|
|
$this->setOrder([]); |
98
|
|
|
$this->setQuery(new ParameterBag()); |
99
|
|
|
$this->setRequest(new ParameterBag()); |
100
|
|
|
$this->setStart(0); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* {@inheritdoc} |
105
|
|
|
*/ |
106
|
|
|
public function getColumn($data) { |
107
|
|
|
foreach ($this->columns as $current) { |
108
|
|
|
if ($data === $current->getData()) { |
109
|
|
|
return $current; |
|
|
|
|
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
return null; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* {@inheritdoc} |
117
|
|
|
*/ |
118
|
|
|
public function getColumns() { |
119
|
|
|
return $this->columns; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* {@inheritdoc} |
124
|
|
|
*/ |
125
|
|
|
public function getDraw() { |
126
|
|
|
return $this->draw; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* {@inheritdoc} |
131
|
|
|
*/ |
132
|
|
|
public function getLength() { |
133
|
|
|
return $this->length; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* {@inheritdoc} |
138
|
|
|
*/ |
139
|
|
|
public function getOrder() { |
140
|
|
|
return $this->order; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* {@inheritdoc} |
145
|
|
|
*/ |
146
|
|
|
public function getQuery() { |
147
|
|
|
return $this->query; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* {@inheritdoc} |
152
|
|
|
*/ |
153
|
|
|
public function getRequest() { |
154
|
|
|
return $this->request; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* {@inheritdoc} |
159
|
|
|
*/ |
160
|
|
|
public function getSearch() { |
161
|
|
|
return $this->search; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* {@inheritdoc} |
166
|
|
|
*/ |
167
|
|
|
public function getStart() { |
168
|
|
|
return $this->start; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* {@inheritdoc} |
173
|
|
|
*/ |
174
|
|
|
public function getWrapper() { |
175
|
|
|
return $this->wrapper; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* Parse a request. |
180
|
|
|
* |
181
|
|
|
* @param DataTablesWrapper $wrapper The wrapper. |
182
|
|
|
* @param Request $request The request. |
183
|
|
|
* @return DataTablesRequestInterface Returns the request. |
184
|
|
|
*/ |
185
|
|
|
public static function parse(DataTablesWrapper $wrapper, Request $request) { |
186
|
|
|
|
187
|
|
|
// Initialize a request. |
188
|
|
|
$dtRequest = new DataTablesRequest(); |
189
|
|
|
|
190
|
|
|
// Recopy the parameter bags. |
191
|
|
|
static::recopy($request->query, $dtRequest->getQuery()); |
192
|
|
|
static::recopy($request->request, $dtRequest->getRequest()); |
193
|
|
|
|
194
|
|
|
// Get the parameter bag. |
195
|
|
|
if (self::HTTP_METHOD_GET === $request->getMethod()) { |
196
|
|
|
$parameterBag = $request->query; |
197
|
|
|
} else { |
198
|
|
|
$parameterBag = $request->request; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
// Get the request parameters. |
202
|
|
|
$columns = null !== $parameterBag->get(self::DATATABLES_PARAMETER_COLUMNS) ? $parameterBag->get(self::DATATABLES_PARAMETER_COLUMNS) : []; |
203
|
|
|
$orders = null !== $parameterBag->get(self::DATATABLES_PARAMETER_ORDER) ? $parameterBag->get(self::DATATABLES_PARAMETER_ORDER) : []; |
204
|
|
|
$search = null !== $parameterBag->get(self::DATATABLES_PARAMETER_SEARCH) ? $parameterBag->get(self::DATATABLES_PARAMETER_SEARCH) : []; |
205
|
|
|
|
206
|
|
|
// Set the request. |
207
|
|
|
$dtRequest->setColumns(DataTablesColumn::parse($columns, $wrapper)); |
208
|
|
|
$dtRequest->setDraw($parameterBag->getInt(self::DATATABLES_PARAMETER_DRAW)); |
209
|
|
|
$dtRequest->setLength($parameterBag->getInt(self::DATATABLES_PARAMETER_LENGTH)); |
210
|
|
|
$dtRequest->setOrder(DataTablesOrder::parse($orders)); |
211
|
|
|
$dtRequest->setSearch(DataTablesSearch::parse($search)); |
212
|
|
|
$dtRequest->setStart($parameterBag->getInt(self::DATATABLES_PARAMETER_START)); |
213
|
|
|
$dtRequest->setWrapper($wrapper); |
214
|
|
|
|
215
|
|
|
// Return the request. |
216
|
|
|
return $dtRequest; |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* Recopy. |
221
|
|
|
* |
222
|
|
|
* @param ParameterBag $request The request. |
223
|
|
|
* @param ParameterBag $bag The bag. |
224
|
|
|
* @return void |
225
|
|
|
*/ |
226
|
|
|
protected static function recopy(ParameterBag $request, ParameterBag $bag) { |
227
|
|
|
foreach ($request->keys() as $current) { |
228
|
|
|
if (true === in_array($current, DataTablesRequestHelper::dtParameters())) { |
229
|
|
|
continue; |
230
|
|
|
} |
231
|
|
|
$bag->set($current, $request->get($current)); |
232
|
|
|
} |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
/** |
236
|
|
|
* Set the columns. |
237
|
|
|
* |
238
|
|
|
* @param DataTablesColumn[] $columns The columns. |
239
|
|
|
* @return DataTablesRequestInterface Returns this request. |
240
|
|
|
*/ |
241
|
|
|
protected function setColumns(array $columns) { |
242
|
|
|
$this->columns = $columns; |
243
|
|
|
return $this; |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
/** |
247
|
|
|
* Set the draw. |
248
|
|
|
* |
249
|
|
|
* @param int $draw The draw. |
250
|
|
|
* @return DataTablesRequestInterface Returns this request. |
251
|
|
|
*/ |
252
|
|
|
protected function setDraw($draw) { |
253
|
|
|
$this->draw = $draw; |
254
|
|
|
return $this; |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
/** |
258
|
|
|
* Set the length. |
259
|
|
|
* |
260
|
|
|
* @param int $length The length. |
261
|
|
|
* @return DataTablesRequestInterface Returns this request. |
262
|
|
|
*/ |
263
|
|
|
protected function setLength($length) { |
264
|
|
|
$this->length = $length; |
265
|
|
|
return $this; |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
/** |
269
|
|
|
* Set the order. |
270
|
|
|
* |
271
|
|
|
* @param DataTablesOrderInterface[] $order The order. |
272
|
|
|
* @return DataTablesRequestInterface Returns this request. |
273
|
|
|
*/ |
274
|
|
|
protected function setOrder(array $order) { |
275
|
|
|
$this->order = $order; |
276
|
|
|
return $this; |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
/** |
280
|
|
|
* Set the request. |
281
|
|
|
* |
282
|
|
|
* @param ParameterBag $query The query. |
283
|
|
|
* @return DataTablesRequestInterface Returns this request. |
284
|
|
|
*/ |
285
|
|
|
protected function setQuery(ParameterBag $query) { |
286
|
|
|
$this->query = $query; |
287
|
|
|
return $this; |
288
|
|
|
} |
289
|
|
|
|
290
|
|
|
/** |
291
|
|
|
* Set the request. |
292
|
|
|
* |
293
|
|
|
* @param ParameterBag $request The request. |
294
|
|
|
* @return DataTablesRequestInterface Returns this request. |
295
|
|
|
*/ |
296
|
|
|
protected function setRequest(ParameterBag $request) { |
297
|
|
|
$this->request = $request; |
298
|
|
|
return $this; |
299
|
|
|
} |
300
|
|
|
|
301
|
|
|
/** |
302
|
|
|
* Set the search. |
303
|
|
|
* |
304
|
|
|
* @param DataTablesSearchInterface $search The search. |
305
|
|
|
* @return DataTablesRequestInterface Returns this request. |
306
|
|
|
*/ |
307
|
|
|
protected function setSearch(DataTablesSearchInterface $search) { |
308
|
|
|
$this->search = $search; |
309
|
|
|
return $this; |
310
|
|
|
} |
311
|
|
|
|
312
|
|
|
/** |
313
|
|
|
* Set the start. |
314
|
|
|
* |
315
|
|
|
* @param int $start The start. |
316
|
|
|
* @return DataTablesRequestInterface Returns this request. |
317
|
|
|
*/ |
318
|
|
|
protected function setStart($start) { |
319
|
|
|
$this->start = $start; |
320
|
|
|
return $this; |
321
|
|
|
} |
322
|
|
|
|
323
|
|
|
/** |
324
|
|
|
* Set the wrapper. |
325
|
|
|
* |
326
|
|
|
* @param DataTablesWrapper $wrapper The wrapper. |
327
|
|
|
* @return DataTablesRequestInterface Returns this request. |
328
|
|
|
*/ |
329
|
|
|
protected function setWrapper(DataTablesWrapper $wrapper) { |
330
|
|
|
$this->wrapper = $wrapper; |
331
|
|
|
return $this; |
332
|
|
|
} |
333
|
|
|
|
334
|
|
|
} |
335
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.