Completed
Push — master ( 043997...8de9a3 )
by WEBEWEB
01:27
created

DataTablesWrapper::getRouteArguments()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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