Completed
Push — master ( a8f6cd...a14bb3 )
by WEBEWEB
04:19
created

DataTablesResponse::jsonSerialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
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
    protected function __construct() {
70
        $this->setData([]);
71
        $this->setDraw(0);
72
        $this->setRecordsFiltered(0);
73
        $this->setRecordsTotal(0);
74
    }
75
76
    /**
77
     * Add a row.
78
     *
79
     * @return DataTablesResponse Returns this DataTables response.
80
     */
81
    public function addRow() {
82
83
        // Add a new row.
84
        $this->data[] = [];
85
86
        // Count the rows.
87
        $index = count($this->data) - 1;
88
89
        // Set each column data in the new row.
90
        foreach ($this->wrapper->getColumns() as $dtColumn) {
91
            $this->data[$index][$dtColumn->getData()] = null;
92
        }
93
94
        return $this;
95
    }
96
97
    /**
98
     * Optional parameters.
99
     *
100
     * @return array Returns the optional parameters.
101
     */
102
    public static function dtRow() {
103
        return [
104
            self::DATATABLES_ROW_ATTR,
105
            self::DATATABLES_ROW_CLASS,
106
            self::DATATABLES_ROW_DATA,
107
            self::DATATABLES_ROW_ID,
108
        ];
109
    }
110
111
    /**
112
     * Get the data.
113
     *
114
     * @return array Returns the data.
115
     */
116
    public function getData() {
117
        return $this->data;
118
    }
119
120
    /**
121
     * Get the draw
122
     *
123
     * @return integer Returns the draw.
124
     */
125
    public function getDraw() {
126
        return $this->draw;
127
    }
128
129
    /**
130
     * Get the error.
131
     *
132
     * @return string Returns the error.
133
     */
134
    public function getError() {
135
        return $this->error;
136
    }
137
138
    /**
139
     * Get the records filtered.
140
     *
141
     * @return integer The records filtered.
142
     */
143
    public function getRecordsFiltered() {
144
        return $this->recordsFiltered;
145
    }
146
147
    /**
148
     * Get the records total.
149
     *
150
     * @return integer Returns the records total.
151
     */
152
    public function getRecordsTotal() {
153
        return $this->recordsTotal;
154
    }
155
156
    /**
157
     * Get the wrapper.
158
     *
159
     * @return DataTablesWrapper Returns the wrapper.
160
     */
161
    public function getWrapper() {
162
        return $this->wrapper;
163
    }
164
165
    /**
166
     * {@inheritdoc}
167
     */
168
    public function jsonSerialize() {
169
        return $this->toArray();
170
    }
171
172
    /**
173
     * Create a DataTables response instance.
174
     *
175
     * @param DataTablesWrapper $wrapper The wrapper.
176
     * @param DataTablesRequest $request The request.
177
     * @return DataTablesResponse Returns the DataTables response.
178
     */
179
    public static function parse(DataTablesWrapper $wrapper, DataTablesRequest $request) {
180
181
        // Initialize a DataTables response.
182
        $dtResponse = new DataTablesResponse();
183
        $dtResponse->setDraw($request->getDraw());
184
        $dtResponse->setWrapper($wrapper);
185
186
        // Return the DataTables response.
187
        return $dtResponse;
188
    }
189
190
    /**
191
     * Set the data.
192
     *
193
     * @param array $data The data.
194
     * @return DataTablesResponse Returns this DataTables response.
195
     */
196
    protected function setData(array $data) {
197
        $this->data = $data;
198
        return $this;
199
    }
200
201
    /**
202
     * Set the draw.
203
     *
204
     * @param integer $draw The draw.
205
     * @return DataTablesResponse Returns the DataTables response.
206
     */
207
    protected function setDraw($draw) {
208
        $this->draw = $draw;
209
        return $this;
210
    }
211
212
    /**
213
     * Set the error.
214
     *
215
     * @param string $error The error.
216
     * @return DataTablesResponse Returns this DataTables response.
217
     */
218
    public function setError($error) {
219
        $this->error = $error;
220
        return $this;
221
    }
222
223
    /**
224
     * Set the records filtered.
225
     *
226
     * @param integer $recordsFiltered The records filtered.
227
     * @return DataTablesResponse Returns this DataTables response.
228
     */
229
    public function setRecordsFiltered($recordsFiltered) {
230
        $this->recordsFiltered = $recordsFiltered;
231
        return $this;
232
    }
233
234
    /**
235
     * Set the records total.
236
     *
237
     * @param integer $recordsTotal The records total.
238
     * @return DataTablesResponse Returns this DataTables response.
239
     */
240
    public function setRecordsTotal($recordsTotal) {
241
        $this->recordsTotal = $recordsTotal;
242
        return $this;
243
    }
244
245
    /**
246
     * Set a row value.
247
     *
248
     * @param string $data The column data.
249
     * @param string $value The column value.
250
     * @return DataTablesResponse Returns this DataTables response.
251
     */
252
    public function setRow($data, $value) {
253
254
        // Count the rows.
255
        $index = count($this->data) - 1;
256
257
        // Check the column data.
258
        if ((true === in_array($data, self::dtRow()) && null !== $value) || (true === in_array($data, array_keys($this->data[$index])))) {
259
            $this->data[$index][$data] = $value;
260
        }
261
262
        // Returns the DataTables response.
263
        return $this;
264
    }
265
266
    /**
267
     * Set the wrapper.
268
     *
269
     * @param DataTablesWrapper $wrapper The wrapper.
270
     * @return DataTablesResponse Returns this DataTables response.
271
     */
272
    protected function setWrapper(DataTablesWrapper $wrapper) {
273
        $this->wrapper = $wrapper;
274
        return $this;
275
    }
276
277
    /**
278
     * Convert into an array representing this instance.
279
     *
280
     * @return array Returns an array representing this instance.
281
     */
282
    public function toArray() {
283
284
        // Initialize the output.
285
        $output = [];
286
287
        $output["data"]            = $this->data;
288
        $output["draw"]            = $this->draw;
289
        $output["recordsFiltered"] = $this->recordsFiltered;
290
        $output["recordsTotal"]    = $this->recordsTotal;
291
292
        // Return the output.
293
        return $output;
294
    }
295
296
}
297