|
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 JsonSerializable; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* DataTables response. |
|
18
|
|
|
* |
|
19
|
|
|
* @author webeweb <https://github.com/webeweb/> |
|
20
|
|
|
* @package WBW\Bundle\JQuery\DatatablesBundle\API |
|
21
|
|
|
*/ |
|
22
|
|
|
class DataTablesResponse implements DataTablesResponseInterface, JsonSerializable { |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Data. |
|
26
|
|
|
* |
|
27
|
|
|
* @var array |
|
28
|
|
|
*/ |
|
29
|
|
|
private $data; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Draw. |
|
33
|
|
|
* |
|
34
|
|
|
* @var integer |
|
35
|
|
|
*/ |
|
36
|
|
|
private $draw; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Error. |
|
40
|
|
|
* |
|
41
|
|
|
* @var string |
|
42
|
|
|
*/ |
|
43
|
|
|
private $error; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Records filtered. |
|
47
|
|
|
* |
|
48
|
|
|
* @var integer |
|
49
|
|
|
*/ |
|
50
|
|
|
private $recordsFiltered; |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Records total. |
|
54
|
|
|
* |
|
55
|
|
|
* @var integer |
|
56
|
|
|
*/ |
|
57
|
|
|
private $recordsTotal; |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Wrapper. |
|
61
|
|
|
* |
|
62
|
|
|
* @var DataTablesWrapper |
|
63
|
|
|
*/ |
|
64
|
|
|
private $wrapper; |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Constructor. |
|
68
|
|
|
* |
|
69
|
|
|
* @param DataTablesWrapper $wrapper The DataTables wrapper. |
|
70
|
|
|
* @param DataTablesRequest $request The DataTables request. |
|
71
|
|
|
*/ |
|
72
|
|
|
public function __construct(DataTablesWrapper $wrapper, DataTablesRequest $request) { |
|
73
|
|
|
$this->setData([]); |
|
74
|
|
|
$this->setDraw($request->getDraw()); |
|
75
|
|
|
$this->setRecordsFiltered(0); |
|
76
|
|
|
$this->setRecordsTotal(0); |
|
77
|
|
|
$this->setWrapper($wrapper); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Add a row. |
|
82
|
|
|
* |
|
83
|
|
|
* @return DataTablesResponse Returns the DataTables response. |
|
84
|
|
|
*/ |
|
85
|
|
|
public function addRow() { |
|
86
|
|
|
|
|
87
|
|
|
// Add a new row. |
|
88
|
|
|
$this->data[] = []; |
|
89
|
|
|
|
|
90
|
|
|
// Count the rows. |
|
91
|
|
|
$index = count($this->data); |
|
92
|
|
|
|
|
93
|
|
|
// Set each column name in the new row. |
|
94
|
|
|
foreach ($this->wrapper->getColumns() as $column) { |
|
95
|
|
|
$this->data[$index - 1][$column->getName()] = null; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
return $this; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Get the data. |
|
103
|
|
|
* |
|
104
|
|
|
* @return array Returns the data. |
|
105
|
|
|
*/ |
|
106
|
|
|
public function getData() { |
|
107
|
|
|
return $this->data; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* Get the draw |
|
112
|
|
|
* |
|
113
|
|
|
* @return integer Returns the draw. |
|
114
|
|
|
*/ |
|
115
|
|
|
public function getDraw() { |
|
116
|
|
|
return $this->draw; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* Get the error. |
|
121
|
|
|
* |
|
122
|
|
|
* @return string Returns the error. |
|
123
|
|
|
*/ |
|
124
|
|
|
public function getError() { |
|
125
|
|
|
return $this->error; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* Get the records filtered. |
|
130
|
|
|
* |
|
131
|
|
|
* @return integer The records filtered. |
|
132
|
|
|
*/ |
|
133
|
|
|
public function getRecordsFiltered() { |
|
134
|
|
|
return $this->recordsFiltered; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* Get the records total. |
|
139
|
|
|
* |
|
140
|
|
|
* @return integer Returns the records total. |
|
141
|
|
|
*/ |
|
142
|
|
|
public function getRecordsTotal() { |
|
143
|
|
|
return $this->recordsTotal; |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* Get the wrapper. |
|
148
|
|
|
* |
|
149
|
|
|
* @return DataTablesWrapper Returns the wrapper. |
|
150
|
|
|
*/ |
|
151
|
|
|
public function getWrapper() { |
|
152
|
|
|
return $this->wrapper; |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* {@inheritdoc} |
|
157
|
|
|
*/ |
|
158
|
|
|
public function jsonSerialize() { |
|
159
|
|
|
return $this->toArray(); |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
/** |
|
163
|
|
|
* Set the draw. |
|
164
|
|
|
* |
|
165
|
|
|
* @param integer $draw The draw. |
|
166
|
|
|
* @return DataTablesResponse Returns the DataTables response. |
|
167
|
|
|
*/ |
|
168
|
|
|
private function setDraw($draw) { |
|
169
|
|
|
$this->draw = $draw; |
|
170
|
|
|
return $this; |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
/** |
|
174
|
|
|
* Set the data. |
|
175
|
|
|
* |
|
176
|
|
|
* @param array $data The data. |
|
177
|
|
|
* @return DataTablesResponse Returns this DataTables response. |
|
178
|
|
|
*/ |
|
179
|
|
|
private function setData(array $data) { |
|
180
|
|
|
$this->data = $data; |
|
181
|
|
|
return $this; |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
/** |
|
185
|
|
|
* Set the error. |
|
186
|
|
|
* |
|
187
|
|
|
* @param string $error The error. |
|
188
|
|
|
* @return DataTablesResponse Returns this DataTables response. |
|
189
|
|
|
*/ |
|
190
|
|
|
public function setError($error) { |
|
191
|
|
|
$this->error = $error; |
|
192
|
|
|
return $this; |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
/** |
|
196
|
|
|
* Set the records filtered. |
|
197
|
|
|
* |
|
198
|
|
|
* @param integer $recordsFiltered The records filtered. |
|
199
|
|
|
* @return DataTablesResponse Returns this DataTables response. |
|
200
|
|
|
*/ |
|
201
|
|
|
public function setRecordsFiltered($recordsFiltered) { |
|
202
|
|
|
$this->recordsFiltered = $recordsFiltered; |
|
203
|
|
|
return $this; |
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
|
|
/** |
|
207
|
|
|
* Set the records total. |
|
208
|
|
|
* |
|
209
|
|
|
* @param integer $recordsTotal The records total. |
|
210
|
|
|
* @return DataTablesResponse Returns this DataTables response. |
|
211
|
|
|
*/ |
|
212
|
|
|
public function setRecordsTotal($recordsTotal) { |
|
213
|
|
|
$this->recordsTotal = $recordsTotal; |
|
214
|
|
|
return $this; |
|
215
|
|
|
} |
|
216
|
|
|
|
|
217
|
|
|
/** |
|
218
|
|
|
* Set a row value. |
|
219
|
|
|
* |
|
220
|
|
|
* @param string $name The column name. |
|
221
|
|
|
* @param string $value The column value. |
|
222
|
|
|
* @return DataTablesResponse Returns this DataTables response. |
|
223
|
|
|
*/ |
|
224
|
|
|
public function setRow($name, $value) { |
|
225
|
|
|
|
|
226
|
|
|
// Count the rows. |
|
227
|
|
|
$index = count($this->data); |
|
228
|
|
|
|
|
229
|
|
|
// Determines the available column names. |
|
230
|
|
|
$names = array_merge(array_keys($this->data[$index - 1]), [ |
|
231
|
|
|
self::DATATABLES_ROW_ATTR, |
|
232
|
|
|
self::DATATABLES_ROW_CLASS, |
|
233
|
|
|
self::DATATABLES_ROW_DATA, |
|
234
|
|
|
self::DATATABLES_ROW_ID, |
|
235
|
|
|
]); |
|
236
|
|
|
|
|
237
|
|
|
// Check the column name. |
|
238
|
|
|
if (true === in_array($name, $names)) { |
|
239
|
|
|
$this->data[$index - 1][$name] = $value; |
|
240
|
|
|
} |
|
241
|
|
|
|
|
242
|
|
|
return $this; |
|
243
|
|
|
} |
|
244
|
|
|
|
|
245
|
|
|
/** |
|
246
|
|
|
* Set the wrapper. |
|
247
|
|
|
* |
|
248
|
|
|
* @param DataTablesWrapper $wrapper The wrapper. |
|
249
|
|
|
* @return DataTablesResponse Returns this DataTables request. |
|
250
|
|
|
*/ |
|
251
|
|
|
private function setWrapper(DataTablesWrapper $wrapper) { |
|
252
|
|
|
$this->wrapper = $wrapper; |
|
253
|
|
|
return $this; |
|
254
|
|
|
} |
|
255
|
|
|
|
|
256
|
|
|
/** |
|
257
|
|
|
* Convert into an array representing this instance. |
|
258
|
|
|
* |
|
259
|
|
|
* @return array Returns an array representing this instance. |
|
260
|
|
|
*/ |
|
261
|
|
|
public function toArray() { |
|
262
|
|
|
|
|
263
|
|
|
// Initialize the output. |
|
264
|
|
|
$output = []; |
|
265
|
|
|
|
|
266
|
|
|
$output["data"] = $this->data; |
|
267
|
|
|
$output["draw"] = $this->draw; |
|
268
|
|
|
$output["recordsFiltered"] = $this->recordsFiltered; |
|
269
|
|
|
$output["recordsTotal"] = $this->recordsTotal; |
|
270
|
|
|
|
|
271
|
|
|
// Return the output. |
|
272
|
|
|
return $output; |
|
273
|
|
|
} |
|
274
|
|
|
|
|
275
|
|
|
} |
|
276
|
|
|
|