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