Completed
Push — master ( c306a6...0eac4d )
by WEBEWEB
01:58
created

DataTablesResponse::toArray()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.8333
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\Normalizer\DataTablesNormalizer;
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
    use DataTablesWrapperTrait {
25
        setWrapper as public;
26
    }
27
28
    /**
29
     * Data.
30
     *
31
     * @var array
32
     */
33
    private $data;
34
35
    /**
36
     * Draw.
37
     *
38
     * @var int
39
     */
40
    private $draw;
41
42
    /**
43
     * Error.
44
     *
45
     * @var string
46
     */
47
    private $error;
48
49
    /**
50
     * Records filtered.
51
     *
52
     * @var int
53
     */
54
    private $recordsFiltered;
55
56
    /**
57
     * Records total.
58
     *
59
     * @var int
60
     */
61
    private $recordsTotal;
62
63
    /**
64
     * Constructor.
65
     */
66
    public function __construct() {
67
        $this->setData([]);
68
        $this->setDraw(0);
69
        $this->setRecordsFiltered(0);
70
        $this->setRecordsTotal(0);
71
    }
72
73
    /**
74
     * {@inheritdoc}
75
     */
76
    public function addRow() {
77
78
        // Count the rows.
79
        $index = $this->countRows();
80
81
        // Add a new row.
82
        $this->data[] = [];
83
84
        // Set each column data in the new row.
85
        foreach ($this->getWrapper()->getColumns() as $dtColumn) {
86
            $this->data[$index][$dtColumn->getData()] = null;
87
        }
88
89
        return $this;
90
    }
91
92
    /**
93
     * {@inheritdoc}
94
     */
95
    public function countRows() {
96
        return count($this->data);
97
    }
98
99
    /**
100
     * {@inheritdoc}
101
     */
102
    public function getData() {
103
        return $this->data;
104
    }
105
106
    /**
107
     * {@inheritdoc}
108
     */
109
    public function getDraw() {
110
        return $this->draw;
111
    }
112
113
    /**
114
     * {@inheritdoc}
115
     */
116
    public function getError() {
117
        return $this->error;
118
    }
119
120
    /**
121
     * {@inheritdoc}
122
     */
123
    public function getRecordsFiltered() {
124
        return $this->recordsFiltered;
125
    }
126
127
    /**
128
     * {@inheritdoc}
129
     */
130
    public function getRecordsTotal() {
131
        return $this->recordsTotal;
132
    }
133
134
    /**
135
     * {@inheritdoc}
136
     */
137
    public function jsonSerialize() {
138
        return DataTablesNormalizer::normalizeResponse($this);
139
    }
140
141
    /**
142
     * Set the data.
143
     *
144
     * @param array $data The data.
145
     * @return DataTablesResponseInterface Returns this response.
146
     */
147
    protected function setData(array $data) {
148
        $this->data = $data;
149
        return $this;
150
    }
151
152
    /**
153
     * Set the draw.
154
     *
155
     * @param int $draw The draw.
156
     * @return DataTablesResponseInterface Returns the response.
157
     */
158
    public function setDraw($draw) {
159
        $this->draw = $draw;
160
        return $this;
161
    }
162
163
    /**
164
     * {@inheritdoc}
165
     */
166
    public function setError($error) {
167
        $this->error = $error;
168
        return $this;
169
    }
170
171
    /**
172
     * {@inheritdoc}
173
     */
174
    public function setRecordsFiltered($recordsFiltered) {
175
        $this->recordsFiltered = $recordsFiltered;
176
        return $this;
177
    }
178
179
    /**
180
     * {@inheritdoc}
181
     */
182
    public function setRecordsTotal($recordsTotal) {
183
        $this->recordsTotal = $recordsTotal;
184
        return $this;
185
    }
186
187
    /**
188
     * {@inheritdoc}
189
     */
190
    public function setRow($data, $value) {
191
192
        $index = $this->countRows() - 1;
193
194
        if ((true === in_array($data, DataTablesEnumerator::enumRows()) && null !== $value) || (true === in_array($data, array_keys($this->data[$index])))) {
195
            $this->data[$index][$data] = $value;
196
        }
197
198
        return $this;
199
    }
200
}
201