Completed
Push — master ( d9d5ce...5f84b6 )
by WEBEWEB
06:30
created

DataTablesWrapper::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 9
nc 1
nop 3
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
     * Server side.
88
     *
89
     * @var boolean
90
     */
91
    private $serverSide;
92
93
    /**
94
     * Constructor.
95
     *
96
     * @param string $method The method.
97
     * @param string $route The route.
98
     * @param string $prefix The prefix.
99
     */
100
    public function __construct($method, $route, $prefix) {
101
        $this->mapping = new DataTablesMapping();
102
        $this->mapping->setPrefix($prefix);
103
104
        $this->setColumns([]);
105
        $this->setMethod($method);
106
        $this->setOrder([]);
107
        $this->setProcessing(true);
108
        $this->setRoute($route);
109
        $this->setServerSide(true);
110
    }
111
112
    /**
113
     * Add a column.
114
     *
115
     * @param DataTablesColumn $column The column.
116
     * @return DataTablesWrapper Returns the DataTables wrapper.
117
     */
118
    public function addColumn(DataTablesColumn $column) {
119
        $this->columns[$column->getName()] = $column;
120
        if (null === $column->getMapping()->getPrefix()) {
121
            $this->columns[$column->getName()]->getMapping()->setPrefix($this->mapping->getPrefix());
122
        }
123
        return $this;
124
    }
125
126
    /**
127
     * Get a column.
128
     *
129
     * @param string $name The column name.
130
     * @return DataTablesColumn Returns the DataTables column in case of success, null otherwise.
131
     */
132
    public function getColumn($name) {
133
        if (true === array_key_exists($name, $this->columns)) {
134
            return $this->columns[$name];
135
        }
136
        return null;
137
    }
138
139
    /**
140
     * Get the columns.
141
     *
142
     * @return DataTablesColumn[] Returns the columns.
143
     */
144
    public function getColumns() {
145
        return $this->columns;
146
    }
147
148
    /**
149
     * Get the mapping.
150
     *
151
     * @return DataTablesMapping The mapping.
152
     */
153
    public function getMapping() {
154
        return $this->mapping;
155
    }
156
157
    /**
158
     * Get the method.
159
     *
160
     * @return string Returns the method.
161
     */
162
    public function getMethod() {
163
        return $this->method;
164
    }
165
166
    /**
167
     * Get the order.
168
     *
169
     * @return array Returns the order.
170
     */
171
    public function getOrder() {
172
        return $this->order;
173
    }
174
175
    /**
176
     * Get the processing.
177
     *
178
     * @return boolean Returns the processing.
179
     */
180
    public function getProcessing() {
181
        return $this->processing;
182
    }
183
184
    /**
185
     * Get the request.
186
     *
187
     * @return DataTablesRequest The request.
188
     */
189
    public function getRequest() {
190
        return $this->request;
191
    }
192
193
    /**
194
     * Get the response.
195
     *
196
     * @return DataTablesResponse Returns the response.
197
     */
198
    public function getResponse() {
199
        return $this->response;
200
    }
201
202
    /**
203
     * Get the route.
204
     *
205
     * @return string Returns the route.
206
     */
207
    public function getRoute() {
208
        return $this->route;
209
    }
210
211
    /**
212
     * Get the server side.
213
     *
214
     * @return boolean Returns the server side.
215
     */
216
    public function getServerSide() {
217
        return $this->serverSide;
218
    }
219
220
    /**
221
     * Parse a request.
222
     *
223
     * @param Request $request The request.
224
     */
225
    public function parse(Request $request) {
226
        $this->request  = new DataTablesRequest($request);
227
        $this->response = new DataTablesResponse($this->request);
228
    }
229
230
    /**
231
     * Remove a column.
232
     *
233
     * @param DataTablesColumn $column The column.
234
     * @return DataTablesWrapper Returns the DataTables wrapper.
235
     */
236
    public function removeColumn(DataTablesColumn $column) {
237
        if (true === array_key_exists($column->getName(), $this->columns)) {
238
            $this->columns[$column->getName()]->getMapping()->setPrefix(null);
239
            unset($this->columns[$column->getName()]);
240
        }
241
        return $this;
242
    }
243
244
    /**
245
     * Set the columns.
246
     *
247
     * @param DataTablesColumn[] $columns The columns.
248
     * @return DataTablesWrapper Returns the DataTables wrapper.
249
     */
250
    private function setColumns(array $columns) {
251
        $this->columns = $columns;
252
        return $this;
253
    }
254
255
    /**
256
     * Set the method.
257
     *
258
     * @param string $method The method.
259
     * @return DataTablesWrapper Returns the DataTables wrapper.
260
     */
261
    public function setMethod($method) {
262
        switch ($method) {
263
            case self::METHOD_GET:
264
            case self::METHOD_POST:
265
                $this->method = $method;
266
                break;
267
            default:
268
                $this->method = self::METHOD_GET;
269
        }
270
        return $this;
271
    }
272
273
    /**
274
     * Set the order.
275
     *
276
     * @param array $order The order.
277
     * @return DataTablesWrapper Returns the DataTables wrapper.
278
     */
279
    public function setOrder(array $order) {
280
        $this->order = $order;
281
        return $this;
282
    }
283
284
    /**
285
     * Set the processing.
286
     *
287
     * @param boolean $processing The processing.
288
     * @return DataTablesWrapper Returns the DataTables wrapper.
289
     */
290
    public function setProcessing($processing) {
291
        switch (true) {
292
            case (false === $processing):
293
            case (true === $processing):
294
                $this->processing = $processing;
295
                break;
296
            default:
297
                $this->processing = true;
298
                break;
299
        }
300
        return $this;
301
    }
302
303
    /**
304
     * Set the route.
305
     *
306
     * @param string $route The route.
307
     * @return DataTablesWrapper Returns the DataTables wrapper.
308
     */
309
    public function setRoute($route) {
310
        $this->route = $route;
311
        return $this;
312
    }
313
314
    /**
315
     * Set the server side.
316
     *
317
     * @param boolean $serverSide The server side.
318
     * @return DataTablesWrapper Returns the DataTables wrapper.
319
     */
320
    public function setServerSide($serverSide) {
321
        switch (true) {
322
            case (false === $serverSide):
323
            case (true === $serverSide):
324
                $this->serverSide = $serverSide;
325
                break;
326
            default:
327
                $this->serverSide = true;
328
                break;
329
        }
330
        return $this;
331
    }
332
333
}
334