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\Response; |
13
|
|
|
|
14
|
|
|
use JsonSerializable; |
15
|
|
|
use WBW\Bundle\JQuery\DatatablesBundle\Request\DataTablesRequest; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* DataTables response. |
19
|
|
|
* |
20
|
|
|
* @author webeweb <https://github.com/webeweb/> |
21
|
|
|
* @package WBW\Bundle\JQuery\DatatablesBundle\Response |
22
|
|
|
* @final |
23
|
|
|
*/ |
24
|
|
|
final class DataTablesResponse implements JsonSerializable { |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Data. |
28
|
|
|
* |
29
|
|
|
* @var array |
30
|
|
|
*/ |
31
|
|
|
private $data; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Draw. |
35
|
|
|
* |
36
|
|
|
* @var integer |
37
|
|
|
*/ |
38
|
|
|
private $draw; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Error. |
42
|
|
|
* |
43
|
|
|
* @var string |
44
|
|
|
*/ |
45
|
|
|
private $error; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Records filtered. |
49
|
|
|
* |
50
|
|
|
* @var integer |
51
|
|
|
*/ |
52
|
|
|
private $recordsFiltered; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Records total. |
56
|
|
|
* |
57
|
|
|
* @var integer |
58
|
|
|
*/ |
59
|
|
|
private $recordsTotal; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Constructor. |
63
|
|
|
*/ |
64
|
|
|
private function __construct() { |
65
|
|
|
$this->setData([]); |
66
|
|
|
$this->setDraw(-1); |
67
|
|
|
$this->setRecordsFiltered(0); |
68
|
|
|
$this->setRecordsTotal(0); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Get the data. |
73
|
|
|
* |
74
|
|
|
* @return array Returns the data. |
75
|
|
|
*/ |
76
|
|
|
public function getData() { |
77
|
|
|
return $this->data; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Get the draw |
82
|
|
|
* |
83
|
|
|
* @return integer Returns the draw. |
84
|
|
|
*/ |
85
|
|
|
public function getDraw() { |
86
|
|
|
return $this->draw; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Get the error. |
91
|
|
|
* |
92
|
|
|
* @return string Returns the error. |
93
|
|
|
*/ |
94
|
|
|
public function getError() { |
95
|
|
|
return $this->error; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Get the records filtered. |
100
|
|
|
* |
101
|
|
|
* @return integer The records filtered. |
102
|
|
|
*/ |
103
|
|
|
public function getRecordsFiltered() { |
104
|
|
|
return $this->recordsFiltered; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Get the records total. |
109
|
|
|
* |
110
|
|
|
* @return integer Returns the records total. |
111
|
|
|
*/ |
112
|
|
|
public function getRecordsTotal() { |
113
|
|
|
return $this->recordsTotal; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* {@inheritdoc} |
118
|
|
|
*/ |
119
|
|
|
public function jsonSerialize() { |
120
|
|
|
return $this->toArray(); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Create a DataTables response. |
125
|
|
|
* |
126
|
|
|
* @param DataTablesRequest $dtRequest The DataTables request. |
127
|
|
|
* @return DataTablesResponse Returns a DataTables response. |
128
|
|
|
*/ |
129
|
|
|
public static function newInstance(DataTablesRequest $dtRequest) { |
130
|
|
|
|
131
|
|
|
// Initialize the DataTables response. |
132
|
|
|
$instance = new DataTablesResponse(); |
133
|
|
|
|
134
|
|
|
// Set the DataTables response. |
135
|
|
|
$instance->setDraw($dtRequest->getDraw()); |
136
|
|
|
|
137
|
|
|
// Return the DataTables response. |
138
|
|
|
return $instance; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Set the draw. |
143
|
|
|
* |
144
|
|
|
* @param integer $draw The draw. |
145
|
|
|
* @return DataTablesResponse Returns the DataTables response. |
146
|
|
|
*/ |
147
|
|
|
private function setDraw($draw) { |
148
|
|
|
$this->draw = $draw; |
149
|
|
|
return $this; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Set the data. |
154
|
|
|
* |
155
|
|
|
* @param array $data The data. |
156
|
|
|
* @return DataTablesResponse Returns the DataTables response. |
157
|
|
|
*/ |
158
|
|
|
private function setData(array $data) { |
159
|
|
|
$this->data = $data; |
160
|
|
|
return $this; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Set the error. |
165
|
|
|
* |
166
|
|
|
* @param string $error The error. |
167
|
|
|
* @return DataTablesResponse Returns the DataTables response. |
168
|
|
|
*/ |
169
|
|
|
public function setError($error) { |
170
|
|
|
$this->error = $error; |
171
|
|
|
return $this; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Set the records filtered. |
176
|
|
|
* |
177
|
|
|
* @param integer $recordsFiltered The records filtered. |
178
|
|
|
* @return DataTablesResponse Returns the DataTables response. |
179
|
|
|
*/ |
180
|
|
|
public function setRecordsFiltered($recordsFiltered) { |
181
|
|
|
$this->recordsFiltered = $recordsFiltered; |
182
|
|
|
return $this; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* Set the records total. |
187
|
|
|
* |
188
|
|
|
* @param integer $recordsTotal The records total. |
189
|
|
|
* @return DataTablesResponse Returns the DataTables response. |
190
|
|
|
*/ |
191
|
|
|
public function setRecordsTotal($recordsTotal) { |
192
|
|
|
$this->recordsTotal = $recordsTotal; |
193
|
|
|
return $this; |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* Convert into an array representing this instance. |
198
|
|
|
* |
199
|
|
|
* @return array Returns an array representing this instance. |
200
|
|
|
*/ |
201
|
|
|
public function toArray() { |
202
|
|
|
|
203
|
|
|
// Initialize the output. |
204
|
|
|
$output = []; |
205
|
|
|
|
206
|
|
|
$output["data"] = $this->data; |
207
|
|
|
$output["draw"] = $this->draw; |
208
|
|
|
$output["recordsFiltered"] = $this->recordsFiltered; |
209
|
|
|
$output["recordsTotal"] = $this->recordsTotal; |
210
|
|
|
|
211
|
|
|
// Return the output. |
212
|
|
|
return $output; |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
} |
216
|
|
|
|