Completed
Push — master ( a91069...e573fc )
by WEBEWEB
01:35
created

DataTablesResponse::setData()   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 WBW\Bundle\JQuery\DataTablesBundle\Helper\DataTablesResponseHelper;
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 {
23
24
    /**
25
     * Data.
26
     *
27
     * @var array
28
     */
29
    private $data;
30
31
    /**
32
     * Draw.
33
     *
34
     * @var int
35
     */
36
    private $draw;
37
38
    /**
39
     * Error.
40
     *
41
     * @var string
42
     */
43
    private $error;
44
45
    /**
46
     * Records filtered.
47
     *
48
     * @var int
49
     */
50
    private $recordsFiltered;
51
52
    /**
53
     * Records total.
54
     *
55
     * @var int
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
     * {@inheritdoc}
78
     */
79
    public function addRow() {
80
81
        // Count the rows.
82
        $index = $this->countRows();
83
84
        // Add a new row.
85
        $this->data[] = [];
86
87
        // Set each column data in the new row.
88
        foreach ($this->wrapper->getColumns() as $dtColumn) {
89
            $this->data[$index][$dtColumn->getData()] = null;
90
        }
91
92
        return $this;
93
    }
94
95
    /**
96
     * {@inheritdoc}
97
     */
98
    public function countRows() {
99
        return count($this->data);
100
    }
101
102
    /**
103
     * {@inheritdoc}
104
     */
105
    public function getData() {
106
        return $this->data;
107
    }
108
109
    /**
110
     * {@inheritdoc}
111
     */
112
    public function getDraw() {
113
        return $this->draw;
114
    }
115
116
    /**
117
     * {@inheritdoc}
118
     */
119
    public function getError() {
120
        return $this->error;
121
    }
122
123
    /**
124
     * {@inheritdoc}
125
     */
126
    public function getRecordsFiltered() {
127
        return $this->recordsFiltered;
128
    }
129
130
    /**
131
     * {@inheritdoc}
132
     */
133
    public function getRecordsTotal() {
134
        return $this->recordsTotal;
135
    }
136
137
    /**
138
     * {@inheritdoc}
139
     */
140
    public function getWrapper() {
141
        return $this->wrapper;
142
    }
143
144
    /**
145
     * {@inheritdoc}
146
     */
147
    public function jsonSerialize() {
148
        return $this->toArray();
149
    }
150
151
    /**
152
     * Create a response instance.
153
     *
154
     * @param DataTablesWrapper $wrapper The wrapper.
155
     * @param DataTablesRequestInterface $request The request.
156
     * @return DataTablesResponseInterface Returns the response.
157
     */
158
    public static function parse(DataTablesWrapper $wrapper, DataTablesRequestInterface $request) {
159
160
        // Initialize a response.
161
        $dtResponse = new DataTablesResponse();
162
        $dtResponse->setDraw($request->getDraw());
163
        $dtResponse->setWrapper($wrapper);
164
165
        // Return the response.
166
        return $dtResponse;
167
    }
168
169
    /**
170
     * Set the data.
171
     *
172
     * @param array $data The data.
173
     * @return DataTablesResponseInterface Returns this response.
174
     */
175
    protected function setData(array $data) {
176
        $this->data = $data;
177
        return $this;
178
    }
179
180
    /**
181
     * Set the draw.
182
     *
183
     * @param int $draw The draw.
184
     * @return DataTablesResponseInterface Returns the response.
185
     */
186
    protected function setDraw($draw) {
187
        $this->draw = $draw;
188
        return $this;
189
    }
190
191
    /**
192
     * {@inheritdoc}
193
     */
194
    public function setError($error) {
195
        $this->error = $error;
196
        return $this;
197
    }
198
199
    /**
200
     * {@inheritdoc}
201
     */
202
    public function setRecordsFiltered($recordsFiltered) {
203
        $this->recordsFiltered = $recordsFiltered;
204
        return $this;
205
    }
206
207
    /**
208
     * {@inheritdoc}
209
     */
210
    public function setRecordsTotal($recordsTotal) {
211
        $this->recordsTotal = $recordsTotal;
212
        return $this;
213
    }
214
215
    /**
216
     * {@inheritdoc}
217
     */
218
    public function setRow($data, $value) {
219
220
        // Count the rows.
221
        $index = $this->countRows() - 1;
222
223
        // Check the column data.
224
        if ((true === in_array($data, DataTablesResponseHelper::dtRows()) && null !== $value) || (true === in_array($data, array_keys($this->data[$index])))) {
225
            $this->data[$index][$data] = $value;
226
        }
227
228
        // Returns the response.
229
        return $this;
230
    }
231
232
    /**
233
     * Set the wrapper.
234
     *
235
     * @param DataTablesWrapper $wrapper The wrapper.
236
     * @return DataTablesResponseInterface Returns this response.
237
     */
238
    protected function setWrapper(DataTablesWrapper $wrapper) {
239
        $this->wrapper = $wrapper;
240
        return $this;
241
    }
242
243
    /**
244
     * Convert into an array representing this instance.
245
     *
246
     * @return array Returns an array representing this instance.
247
     */
248
    public function toArray() {
249
250
        // Initialize the output.
251
        $output = [];
252
253
        $output["data"]            = $this->data;
254
        $output["draw"]            = $this->draw;
255
        $output["recordsFiltered"] = $this->recordsFiltered;
256
        $output["recordsTotal"]    = $this->recordsTotal;
257
258
        // Return the output.
259
        return $output;
260
    }
261
262
}
263