DataTablesResponse::getDraw()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 2
c 0
b 0
f 0
rs 10
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\Model;
13
14
use WBW\Bundle\JQuery\DataTablesBundle\Api\DataTablesResponseInterface;
15
use WBW\Bundle\JQuery\DataTablesBundle\Normalizer\DataTablesNormalizer;
16
17
/**
18
 * DataTables response.
19
 *
20
 * @author webeweb <https://github.com/webeweb>
21
 * @package WBW\Bundle\JQuery\DataTablesBundle\Model
22
 */
23
class DataTablesResponse implements DataTablesResponseInterface {
24
25
    use DataTablesWrapperTrait {
26
        setWrapper as public;
27
    }
28
29
    /**
30
     * Data.
31
     *
32
     * @var array<string,mixed>[]
33
     */
34
    private $data;
35
36
    /**
37
     * Draw.
38
     *
39
     * @var int
40
     */
41
    private $draw;
42
43
    /**
44
     * Error.
45
     *
46
     * @var string|null
47
     */
48
    private $error;
49
50
    /**
51
     * Records filtered.
52
     *
53
     * @var int
54
     */
55
    private $recordsFiltered;
56
57
    /**
58
     * Records total.
59
     *
60
     * @var int
61
     */
62
    private $recordsTotal;
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(): DataTablesResponseInterface {
78
79
        $index = $this->rowsCount();
80
81
        $this->data[] = [];
82
83
        // Set each column data in the new row.
84
        foreach ($this->getWrapper()->getColumns() as $dtColumn) {
85
            $this->data[$index][$dtColumn->getData()] = null;
86
        }
87
88
        return $this;
89
    }
90
91
    /**
92
     * {@inheritDoc}
93
     */
94
    public function getData(): array {
95
        return $this->data;
96
    }
97
98
    /**
99
     * {@inheritDoc}
100
     */
101
    public function getDraw(): int {
102
        return $this->draw;
103
    }
104
105
    /**
106
     * {@inheritDoc}
107
     */
108
    public function getError(): ?string {
109
        return $this->error;
110
    }
111
112
    /**
113
     * {@inheritDoc}
114
     */
115
    public function getRecordsFiltered(): int {
116
        return $this->recordsFiltered;
117
    }
118
119
    /**
120
     * {@inheritDoc}
121
     */
122
    public function getRecordsTotal(): int {
123
        return $this->recordsTotal;
124
    }
125
126
    /**
127
     * {@inheritDoc}
128
     * @return array<string,mixed> Returns this serialized instance.
129
     */
130
    public function jsonSerialize(): array {
131
        return DataTablesNormalizer::normalizeResponse($this);
132
    }
133
134
    /**
135
     * {@inheritDoc}
136
     */
137
    public function rowsCount(): int {
138
        return count($this->data);
139
    }
140
141
    /**
142
     * Set the data.
143
     *
144
     * @param array<string,mixed>[] $data The data.
145
     * @return DataTablesResponseInterface Returns this response.
146
     */
147
    protected function setData(array $data): DataTablesResponseInterface {
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(int $draw): DataTablesResponseInterface {
159
        $this->draw = $draw;
160
        return $this;
161
    }
162
163
    /**
164
     * {@inheritDoc}
165
     */
166
    public function setError(?string $error): DataTablesResponseInterface {
167
        $this->error = $error;
168
        return $this;
169
    }
170
171
    /**
172
     * {@inheritDoc}
173
     */
174
    public function setRecordsFiltered(int $recordsFiltered): DataTablesResponseInterface {
175
        $this->recordsFiltered = $recordsFiltered;
176
        return $this;
177
    }
178
179
    /**
180
     * {@inheritDoc}
181
     */
182
    public function setRecordsTotal(int $recordsTotal): DataTablesResponseInterface {
183
        $this->recordsTotal = $recordsTotal;
184
        return $this;
185
    }
186
187
    /**
188
     * {@inheritDoc}
189
     */
190
    public function setRow(string $data, $value): DataTablesResponseInterface {
191
192
        $index = $this->rowsCount() - 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