Completed
Push — master ( d56b5f...d79d68 )
by WEBEWEB
04:01
created

DataTablesWrapper::getColumn()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
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\Wrapper;
13
14
use WBW\Bundle\JQuery\DatatablesBundle\Column\DataTablesColumn;
15
use WBW\Bundle\JQuery\DatatablesBundle\Mapping\DataTablesMapping;
16
use WBW\Bundle\JQuery\DatatablesBundle\Request\DataTablesRequest;
17
use WBW\Bundle\JQuery\DatatablesBundle\Response\DataTablesResponse;
18
use WBW\Library\Core\HTTP\HTTPMethodInterface;
19
20
/**
21
 * DataTables wrapper.
22
 *
23
 * @author webeweb <https://github.com/webeweb/>
24
 * @package WBW\Bundle\JQuery\DatatablesBundle\Wrapper
25
 * @final
26
 */
27
final class DataTablesWrapper implements HTTPMethodInterface {
28
29
    /**
30
     * Columns.
31
     *
32
     * @var DataTablesColumn[]
33
     */
34
    private $columns;
35
36
    /**
37
     * Mapping.
38
     *
39
     * @var DataTablesMapping
40
     */
41
    private $mapping;
42
43
    /**
44
     * Method.
45
     *
46
     * @var string
47
     */
48
    private $method;
49
50
    /**
51
     * Order.
52
     *
53
     * @var array
54
     */
55
    private $order;
56
57
    /**
58
     * Processing.
59
     *
60
     * @var boolean
61
     */
62
    private $processing;
63
64
    /**
65
     * Request.
66
     *
67
     * @var DataTablesRequest
68
     */
69
    private $request;
70
71
    /**
72
     * Response.
73
     *
74
     * @var DataTablesResponse
75
     */
76
    private $response;
77
78
    /**
79
     * Route.
80
     *
81
     * @var string
82
     */
83
    private $route;
84
85
    /**
86
     * Server side.
87
     *
88
     * @var boolean
89
     */
90
    private $serverSide;
91
92
    /**
93
     * Constructor.
94
     *
95
     * @param string $method The method.
96
     * @param string $route The route.
97
     * @param string $prefix The prefix.
98
     */
99
    public function __construct($method, $route, $prefix) {
100
        $this->mapping = new DataTablesMapping();
101
        $this->mapping->setPrefix($prefix);
102
103
        $this->setColumns([]);
104
        $this->setMethod($method);
105
        $this->setOrder([]);
106
        $this->setProcessing(true);
107
        $this->setRoute($route);
108
        $this->setServerSide(true);
109
    }
110
111
    /**
112
     * Add a column.
113
     *
114
     * @param DataTablesColumn $column The column.
115
     * @return DataTablesWrapper Returns the DataTables wrapper.
116
     */
117
    public function addColumn(DataTablesColumn $column) {
118
        $this->columns[$column->getName()] = $column;
119
        if (null === $column->getMapping()->getPrefix()) {
120
            $this->columns[$column->getName()]->getMapping()->setPrefix($this->mapping->getPrefix());
121
        }
122
        return $this;
123
    }
124
125
    /**
126
     * Get a column.
127
     *
128
     * @param string $name The column name.
129
     * @return DataTablesColumn Returns the DataTables column in case of success, null otherwise.
130
     */
131
    public function getColumn($name) {
132
        if (true === array_key_exists($name, $this->columns)) {
133
            return $this->columns[$name];
134
        }
135
        return null;
136
    }
137
138
    /**
139
     * Get the columns.
140
     *
141
     * @return DataTablesColumn[] Returns the columns.
142
     */
143
    public function getColumns() {
144
        return $this->columns;
145
    }
146
147
    /**
148
     * Get the mapping.
149
     *
150
     * @return DataTablesMapping The mapping.
151
     */
152
    public function getMapping() {
153
        return $this->mapping;
154
    }
155
156
    /**
157
     * Get the method.
158
     *
159
     * @return string Returns the method.
160
     */
161
    public function getMethod() {
162
        return $this->method;
163
    }
164
165
    /**
166
     * Get the order.
167
     *
168
     * @return array Returns the order.
169
     */
170
    public function getOrder() {
171
        return $this->order;
172
    }
173
174
    /**
175
     * Get the processing.
176
     *
177
     * @return boolean Returns the processing.
178
     */
179
    public function getProcessing() {
180
        return $this->processing;
181
    }
182
183
    /**
184
     * Get the request.
185
     *
186
     * @return DataTablesRequest The request.
187
     */
188
    public function getRequest() {
189
        return $this->request;
190
    }
191
192
    /**
193
     * Get the response.
194
     *
195
     * @return DataTablesResponse Returns the response.
196
     */
197
    public function getResponse() {
198
        return $this->response;
199
    }
200
201
    /**
202
     * Get the route.
203
     *
204
     * @return string Returns the route.
205
     */
206
    public function getRoute() {
207
        return $this->route;
208
    }
209
210
    /**
211
     * Get the server side.
212
     *
213
     * @return boolean Returns the server side.
214
     */
215
    public function getServerSide() {
216
        return $this->serverSide;
217
    }
218
219
    /**
220
     * Remove a column.
221
     *
222
     * @param DataTablesColumn $column The column.
223
     * @return DataTablesWrapper Returns the DataTables wrapper.
224
     */
225
    public function removeColumn(DataTablesColumn $column) {
226
        if (true === array_key_exists($column->getName(), $this->columns)) {
227
            $this->columns[$column->getName()]->getMapping()->setPrefix(null);
228
            unset($this->columns[$column->getName()]);
229
        }
230
        return $this;
231
    }
232
233
    /**
234
     * Set the columns.
235
     *
236
     * @param DataTablesColumn[] $columns The columns.
237
     * @return DataTablesWrapper Returns the DataTables wrapper.
238
     */
239
    private function setColumns(array $columns) {
240
        $this->columns = $columns;
241
        return $this;
242
    }
243
244
    /**
245
     * Set the method.
246
     *
247
     * @param string $method The method.
248
     * @return DataTablesWrapper Returns the DataTables wrapper.
249
     */
250
    public function setMethod($method) {
251
        switch ($method) {
252
            case self::METHOD_GET:
253
            case self::METHOD_POST:
254
                $this->method = $method;
255
                break;
256
            default:
257
                $this->method = self::METHOD_GET;
258
        }
259
        return $this;
260
    }
261
262
    /**
263
     * Set the order.
264
     *
265
     * @param array $order The order.
266
     * @return DataTablesWrapper Returns the DataTables wrapper.
267
     */
268
    public function setOrder(array $order) {
269
        $this->order = $order;
270
        return $this;
271
    }
272
273
    /**
274
     * Set the processing.
275
     *
276
     * @param boolean $processing The processing.
277
     * @return DataTablesWrapper Returns the DataTables wrapper.
278
     */
279
    public function setProcessing($processing) {
280
        switch (true) {
281
            case (false === $processing):
282
            case (true === $processing):
283
                $this->processing = $processing;
284
                break;
285
            default:
286
                $this->processing = true;
287
                break;
288
        }
289
        return $this;
290
    }
291
292
    /**
293
     * Set the route.
294
     *
295
     * @param string $route The route.
296
     * @return DataTablesWrapper Returns the DataTables wrapper.
297
     */
298
    public function setRoute($route) {
299
        $this->route = $route;
300
        return $this;
301
    }
302
303
    /**
304
     * Set the server side.
305
     *
306
     * @param boolean $serverSide The server side.
307
     * @return DataTablesWrapper Returns the DataTables wrapper.
308
     */
309
    public function setServerSide($serverSide) {
310
        switch (true) {
311
            case (false === $serverSide):
312
            case (true === $serverSide):
313
                $this->serverSide = $serverSide;
314
                break;
315
            default:
316
                $this->serverSide = true;
317
                break;
318
        }
319
        return $this;
320
    }
321
322
}
323