Completed
Push — master ( dbc0ff...b4a470 )
by WEBEWEB
01:31
created

DataTablesWrapper::setProcessing()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

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