Completed
Push — master ( 0b41f4...3868fa )
by WEBEWEB
01:43
created

DataTablesResponse::setRow()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 20
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 20
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 10
nc 2
nop 2
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(DataTablesWrapper $wrapper, DataTablesRequest $request) {
0 ignored issues
show
Unused Code introduced by
The parameter $wrapper is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

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