Completed
Push — master ( a7ed84...65cc57 )
by WEBEWEB
02:22
created

DataTablesResponse::setWrapper()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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