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