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\Request; |
13
|
|
|
|
14
|
|
|
use Symfony\Component\HttpFoundation\Request; |
15
|
|
|
use WBW\Library\Core\HTTP\HTTPMethodInterface; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* DataTables request. |
19
|
|
|
* |
20
|
|
|
* @author webeweb <https://github.com/webeweb/> |
21
|
|
|
* @package WBW\Bundle\JQuery\DatatablesBundle\Request |
22
|
|
|
* @final |
23
|
|
|
*/ |
24
|
|
|
final class DataTablesRequest implements HTTPMethodInterface { |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Columns. |
28
|
|
|
* |
29
|
|
|
* @var array |
30
|
|
|
*/ |
31
|
|
|
private $columns; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Draw. |
35
|
|
|
* |
36
|
|
|
* @var integer |
37
|
|
|
*/ |
38
|
|
|
private $draw; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Length. |
42
|
|
|
* |
43
|
|
|
* @var integer |
44
|
|
|
*/ |
45
|
|
|
private $length; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Order. |
49
|
|
|
* |
50
|
|
|
* @var array |
51
|
|
|
*/ |
52
|
|
|
private $order; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Search. |
56
|
|
|
* |
57
|
|
|
* @var array |
58
|
|
|
*/ |
59
|
|
|
private $search; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Start. |
63
|
|
|
* |
64
|
|
|
* @var integer |
65
|
|
|
*/ |
66
|
|
|
private $start; |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Constructor. |
70
|
|
|
* |
71
|
|
|
* @param Request $request The request. |
72
|
|
|
*/ |
73
|
|
|
public function __construct(Request $request) { |
74
|
|
|
|
75
|
|
|
// Set the attributes. |
76
|
|
|
$this->setColumns([]); |
77
|
|
|
$this->setOrder([]); |
78
|
|
|
$this->setSearch([]); |
79
|
|
|
|
80
|
|
|
// Parse the request. |
81
|
|
|
$this->parse($request); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Get a column. |
86
|
|
|
* |
87
|
|
|
* @param string $name The column name. |
88
|
|
|
* @return array Returns the column in case of success, null otherwise. |
89
|
|
|
*/ |
90
|
|
|
public function getColumn($name) { |
91
|
|
|
foreach ($this->columns as $current) { |
92
|
|
|
if ($name === $current["name"]) { |
93
|
|
|
return $current; |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
return null; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Get the columns. |
101
|
|
|
* |
102
|
|
|
* @return array Returns the columns. |
103
|
|
|
*/ |
104
|
|
|
public function getColumns() { |
105
|
|
|
return $this->columns; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Get the draw. |
110
|
|
|
* |
111
|
|
|
* @return integer Returns the draw. |
112
|
|
|
*/ |
113
|
|
|
public function getDraw() { |
114
|
|
|
return $this->draw; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Get the length. |
119
|
|
|
* |
120
|
|
|
* @return integer Returns the length. |
121
|
|
|
*/ |
122
|
|
|
public function getLength() { |
123
|
|
|
return $this->length; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Get the order. |
128
|
|
|
* |
129
|
|
|
* @return array Returns the order. |
130
|
|
|
*/ |
131
|
|
|
public function getOrder() { |
132
|
|
|
return $this->order; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Get the search. |
137
|
|
|
* |
138
|
|
|
* @return array Returns the search. |
139
|
|
|
*/ |
140
|
|
|
public function getSearch() { |
141
|
|
|
return $this->search; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Get the start. |
146
|
|
|
* |
147
|
|
|
* @return integer Returns the start. |
148
|
|
|
*/ |
149
|
|
|
public function getStart() { |
150
|
|
|
return $this->start; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Parse a request. |
155
|
|
|
* |
156
|
|
|
* @param Request $request The request. |
157
|
|
|
* @return void |
158
|
|
|
*/ |
159
|
|
|
private function parse(Request $request) { |
160
|
|
|
|
161
|
|
|
// Get the parameter bag. |
162
|
|
|
if (self::METHOD_GET === $request->getMethod()) { |
163
|
|
|
$parameterBag = $request->query; |
164
|
|
|
} else { |
165
|
|
|
$parameterBag = $request->request; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
// Set the DataTables request. |
169
|
|
|
$this->setColumns(null !== $parameterBag->get("columns") ? $parameterBag->get("columns") : []); |
170
|
|
|
$this->setDraw(intval($parameterBag->get("draw"))); |
171
|
|
|
$this->setLength(intval($parameterBag->get("length"))); |
172
|
|
|
$this->setOrder(null !== $parameterBag->get("order") ? $parameterBag->get("order") : []); |
173
|
|
|
$this->setSearch(null !== $parameterBag->get("search") ? $parameterBag->get("search") : []); |
174
|
|
|
$this->setStart(intval($parameterBag->get("start"))); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* Set the columns. |
179
|
|
|
* |
180
|
|
|
* @param array $columns The columns. |
181
|
|
|
* @return DataTablesRequest Returns the DataTables request. |
182
|
|
|
*/ |
183
|
|
|
private function setColumns(array $columns) { |
184
|
|
|
$this->columns = $columns; |
185
|
|
|
return $this; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* Set the draw. |
190
|
|
|
* |
191
|
|
|
* @param integer $draw The draw. |
192
|
|
|
* @return DataTablesRequest Returns the DataTables request. |
193
|
|
|
*/ |
194
|
|
|
public function setDraw($draw) { |
195
|
|
|
$this->draw = $draw; |
196
|
|
|
return $this; |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* Set the length. |
201
|
|
|
* |
202
|
|
|
* @param integer $length The length. |
203
|
|
|
* @return DataTablesRequest Returns the DataTables request. |
204
|
|
|
*/ |
205
|
|
|
private function setLength($length) { |
206
|
|
|
$this->length = $length; |
207
|
|
|
return $this; |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* Set the order. |
212
|
|
|
* |
213
|
|
|
* @param array $order The order. |
214
|
|
|
* @return DataTablesRequest Returns the DataTables request. |
215
|
|
|
*/ |
216
|
|
|
private function setOrder(array $order) { |
217
|
|
|
$this->order = $order; |
218
|
|
|
return $this; |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* Set the search. |
223
|
|
|
* |
224
|
|
|
* @param array $search The search. |
225
|
|
|
* @return DataTablesRequest Returns the DataTables request. |
226
|
|
|
*/ |
227
|
|
|
private function setSearch(array $search) { |
228
|
|
|
$this->search = $search; |
229
|
|
|
return $this; |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* Set the start. |
234
|
|
|
* |
235
|
|
|
* @param integer $start The start. |
236
|
|
|
* @return DataTablesRequest Returns the DataTables request. |
237
|
|
|
*/ |
238
|
|
|
private function setStart($start) { |
239
|
|
|
$this->start = $start; |
240
|
|
|
return $this; |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
} |
244
|
|
|
|