Completed
Push — master ( aef7f8...916348 )
by WEBEWEB
01:35
created

DataTablesResponse::addRow()   A

Complexity

Conditions 2
Paths 2

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 2
nc 2
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
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->countRows();
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 countRows(): int {
95
        return count($this->data);
96
    }
97
98
    /**
99
     * {@inheritDoc}
100
     */
101
    public function getData(): array {
102
        return $this->data;
103
    }
104
105
    /**
106
     * {@inheritDoc}
107
     */
108
    public function getDraw(): int {
109
        return $this->draw;
110
    }
111
112
    /**
113
     * {@inheritDoc}
114
     */
115
    public function getError(): ?string {
116
        return $this->error;
117
    }
118
119
    /**
120
     * {@inheritDoc}
121
     */
122
    public function getRecordsFiltered(): int {
123
        return $this->recordsFiltered;
124
    }
125
126
    /**
127
     * {@inheritDoc}
128
     */
129
    public function getRecordsTotal(): int {
130
        return $this->recordsTotal;
131
    }
132
133
    /**
134
     * {@inheritDoc}
135
     */
136
    public function jsonSerialize(): array {
137
        return DataTablesNormalizer::normalizeResponse($this);
138
    }
139
140
    /**
141
     * Set the data.
142
     *
143
     * @param array $data The data.
144
     * @return DataTablesResponseInterface Returns this response.
145
     */
146
    protected function setData(array $data): DataTablesResponseInterface {
147
        $this->data = $data;
148
        return $this;
149
    }
150
151
    /**
152
     * Set the draw.
153
     *
154
     * @param int $draw The draw.
155
     * @return DataTablesResponseInterface Returns the response.
156
     */
157
    public function setDraw(int $draw): DataTablesResponseInterface {
158
        $this->draw = $draw;
159
        return $this;
160
    }
161
162
    /**
163
     * {@inheritDoc}
164
     */
165
    public function setError(?string $error): DataTablesResponseInterface {
166
        $this->error = $error;
167
        return $this;
168
    }
169
170
    /**
171
     * {@inheritDoc}
172
     */
173
    public function setRecordsFiltered(int $recordsFiltered): DataTablesResponseInterface {
174
        $this->recordsFiltered = $recordsFiltered;
175
        return $this;
176
    }
177
178
    /**
179
     * {@inheritDoc}
180
     */
181
    public function setRecordsTotal(int $recordsTotal): DataTablesResponseInterface {
182
        $this->recordsTotal = $recordsTotal;
183
        return $this;
184
    }
185
186
    /**
187
     * {@inheritDoc}
188
     */
189
    public function setRow(string $data, $value): DataTablesResponseInterface {
190
191
        $index = $this->countRows() - 1;
192
193
        if ((true === in_array($data, DataTablesEnumerator::enumRows()) && null !== $value) || (true === in_array($data, array_keys($this->data[$index])))) {
194
            $this->data[$index][$data] = $value;
195
        }
196
197
        return $this;
198
    }
199
}
200