Completed
Push — master ( 3eb599...90cc4e )
by WEBEWEB
01:39
created

DataTablesResponse::getWrapper()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
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 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
     * @var DataTablesWrapperTrait
61
     */
62
    use DataTablesWrapperTrait;
63
64
    /**
65
     * Constructor.
66
     */
67
    public function __construct() {
68
        $this->setData([]);
69
        $this->setDraw(0);
70
        $this->setRecordsFiltered(0);
71
        $this->setRecordsTotal(0);
72
    }
73
74
    /**
75
     * {@inheritdoc}
76
     */
77
    public function addRow() {
78
79
        // Count the rows.
80
        $index = $this->countRows();
81
82
        // Add a new row.
83
        $this->data[] = [];
84
85
        // Set each column data in the new row.
86
        foreach ($this->getWrapper()->getColumns() as $dtColumn) {
87
            $this->data[$index][$dtColumn->getData()] = null;
88
        }
89
90
        return $this;
91
    }
92
93
    /**
94
     * {@inheritdoc}
95
     */
96
    public function countRows() {
97
        return count($this->data);
98
    }
99
100
    /**
101
     * {@inheritdoc}
102
     */
103
    public function getData() {
104
        return $this->data;
105
    }
106
107
    /**
108
     * {@inheritdoc}
109
     */
110
    public function getDraw() {
111
        return $this->draw;
112
    }
113
114
    /**
115
     * {@inheritdoc}
116
     */
117
    public function getError() {
118
        return $this->error;
119
    }
120
121
    /**
122
     * {@inheritdoc}
123
     */
124
    public function getRecordsFiltered() {
125
        return $this->recordsFiltered;
126
    }
127
128
    /**
129
     * {@inheritdoc}
130
     */
131
    public function getRecordsTotal() {
132
        return $this->recordsTotal;
133
    }
134
135
    /**
136
     * {@inheritdoc}
137
     */
138
    public function jsonSerialize() {
139
        return $this->toArray();
140
    }
141
142
    /**
143
     * Set the data.
144
     *
145
     * @param array $data The data.
146
     * @return DataTablesResponseInterface Returns this response.
147
     */
148
    protected function setData(array $data) {
149
        $this->data = $data;
150
        return $this;
151
    }
152
153
    /**
154
     * Set the draw.
155
     *
156
     * @param int $draw The draw.
157
     * @return DataTablesResponseInterface Returns the response.
158
     */
159
    public function setDraw($draw) {
160
        $this->draw = $draw;
161
        return $this;
162
    }
163
164
    /**
165
     * {@inheritdoc}
166
     */
167
    public function setError($error) {
168
        $this->error = $error;
169
        return $this;
170
    }
171
172
    /**
173
     * {@inheritdoc}
174
     */
175
    public function setRecordsFiltered($recordsFiltered) {
176
        $this->recordsFiltered = $recordsFiltered;
177
        return $this;
178
    }
179
180
    /**
181
     * {@inheritdoc}
182
     */
183
    public function setRecordsTotal($recordsTotal) {
184
        $this->recordsTotal = $recordsTotal;
185
        return $this;
186
    }
187
188
    /**
189
     * {@inheritdoc}
190
     */
191
    public function setRow($data, $value) {
192
193
        // Count the rows.
194
        $index = $this->countRows() - 1;
195
196
        // Check the column data.
197
        if ((true === in_array($data, DataTablesResponseHelper::dtRows()) && null !== $value) || (true === in_array($data, array_keys($this->data[$index])))) {
198
            $this->data[$index][$data] = $value;
199
        }
200
201
        // Returns the response.
202
        return $this;
203
    }
204
205
    /**
206
     * Convert into an array representing this instance.
207
     *
208
     * @return array Returns an array representing this instance.
209
     */
210
    public function toArray() {
211
212
        // Initialize the output.
213
        $output = [];
214
215
        $output["data"]            = $this->data;
216
        $output["draw"]            = $this->draw;
217
        $output["recordsFiltered"] = $this->recordsFiltered;
218
        $output["recordsTotal"]    = $this->recordsTotal;
219
220
        // Return the output.
221
        return $output;
222
    }
223
224
}
225