Completed
Push — master ( 3eca47...f1e1aa )
by WEBEWEB
01:33
created

DataTablesWrapper::removeColumn()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
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\API\Wrapper;
13
14
use WBW\Bundle\JQuery\DatatablesBundle\API\Column\DataTablesColumn;
15
use WBW\Bundle\JQuery\DatatablesBundle\API\Mapping\DataTablesMapping;
16
use WBW\Bundle\JQuery\DatatablesBundle\API\Request\DataTablesRequest;
17
use WBW\Bundle\JQuery\DatatablesBundle\API\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\API\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
        $this->columns[$column->getName()]->getMapping()->setPrefix($this->mapping->getPrefix());
120
        return $this;
121
    }
122
123
    /**
124
     * Get the columns.
125
     *
126
     * @return DataTablesColumn[] Returns the columns.
127
     */
128
    public function getColumns() {
129
        return $this->columns;
130
    }
131
132
    /**
133
     * Get the mapping.
134
     *
135
     * @return DataTablesMapping The mapping.
136
     */
137
    public function getMapping() {
138
        return $this->mapping;
139
    }
140
141
    /**
142
     * Get the method.
143
     *
144
     * @return string Returns the method.
145
     */
146
    public function getMethod() {
147
        return $this->method;
148
    }
149
150
    /**
151
     * Get the order.
152
     *
153
     * @return array Returns the order.
154
     */
155
    public function getOrder() {
156
        return $this->order;
157
    }
158
159
    /**
160
     * Get the processing.
161
     *
162
     * @return boolean Returns the processing.
163
     */
164
    public function getProcessing() {
165
        return $this->processing;
166
    }
167
168
    /**
169
     * Get the request.
170
     *
171
     * @return DataTablesRequest The request.
172
     */
173
    public function getRequest() {
174
        return $this->request;
175
    }
176
177
    /**
178
     * Get the response.
179
     *
180
     * @return DataTablesResponse Returns the response.
181
     */
182
    public function getResponse() {
183
        return $this->response;
184
    }
185
186
    /**
187
     * Get the route.
188
     *
189
     * @return string Returns the reoute.
190
     */
191
    public function getRoute() {
192
        return $this->route;
193
    }
194
195
    /**
196
     * Get the server side.
197
     *
198
     * @return boolean Returns the server side.
199
     */
200
    public function getServerSide() {
201
        return $this->serverSide;
202
    }
203
204
    /**
205
     * Remove a column.
206
     *
207
     * @param DataTablesColumn $column The column.
208
     * @return DataTablesWrapper Returns the DataTables wrapper.
209
     */
210
    public function removeColumn(DataTablesColumn $column) {
211
        if (true === array_key_exists($column->getName(), $this->columns)) {
212
            $this->columns[$column->getName()]->getMapping()->setPrefix(null);
213
            unset($this->columns[$column->getName()]);
214
        }
215
        return $this;
216
    }
217
218
    /**
219
     * Set the columns.
220
     *
221
     * @param DataTablesColumn[] $columns The columns.
222
     * @return DataTablesWrapper Returns the DataTables wrapper.
223
     */
224
    private function setColumns(array $columns) {
225
        $this->columns = $columns;
226
        return $this;
227
    }
228
229
    /**
230
     * Set the method.
231
     *
232
     * @param string $method The method.
233
     * @return DataTablesWrapper Returns the DataTables wrapper.
234
     */
235
    public function setMethod($method) {
236
        switch ($method) {
237
            case self::METHOD_GET:
238
            case self::METHOD_POST:
239
                $this->method = $method;
240
                break;
241
            default:
242
                $this->method = self::METHOD_GET;
243
        }
244
        return $this;
245
    }
246
247
    /**
248
     * Set the order.
249
     *
250
     * @param array $order The order.
251
     * @return DataTablesWrapper Returns the DataTables wrapper.
252
     */
253
    public function setOrder(array $order) {
254
        $this->order = $order;
255
        return $this;
256
    }
257
258
    /**
259
     * Set the processing.
260
     *
261
     * @param boolean $processing The processing.
262
     * @return DataTablesWrapper Returns the DataTables wrapper.
263
     */
264
    public function setProcessing($processing) {
265
        switch ($processing) {
266
            case false :
0 ignored issues
show
Coding Style introduced by
There must be no space before the colon in a CASE statement

As per the PSR-2 coding standard, there must not be a space in front of the colon in case statements.

switch ($selector) {
    case "A": //right
        doSomething();
        break;
    case "B" : //wrong
        doSomethingElse();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
267
            case true :
0 ignored issues
show
Coding Style introduced by
There must be no space before the colon in a CASE statement

As per the PSR-2 coding standard, there must not be a space in front of the colon in case statements.

switch ($selector) {
    case "A": //right
        doSomething();
        break;
    case "B" : //wrong
        doSomethingElse();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
268
                $this->processing = $processing;
269
                break;
270
            default:
271
                $this->processing = true;
272
                break;
273
        }
274
        return $this;
275
    }
276
277
    /**
278
     * Set the route.
279
     *
280
     * @param string $route The route.
281
     * @return DataTablesWrapper Returns the DataTables wrapper.
282
     */
283
    public function setRoute($route) {
284
        $this->route = $route;
285
        return $this;
286
    }
287
288
    /**
289
     * Set the server side.
290
     *
291
     * @param boolean $serverSide The server side.
292
     * @return DataTablesWrapper Returns the DataTables wrapper.
293
     */
294
    public function setServerSide($serverSide) {
295
        switch ($serverSide) {
296
            case false :
0 ignored issues
show
Coding Style introduced by
There must be no space before the colon in a CASE statement

As per the PSR-2 coding standard, there must not be a space in front of the colon in case statements.

switch ($selector) {
    case "A": //right
        doSomething();
        break;
    case "B" : //wrong
        doSomethingElse();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
297
            case true :
0 ignored issues
show
Coding Style introduced by
There must be no space before the colon in a CASE statement

As per the PSR-2 coding standard, there must not be a space in front of the colon in case statements.

switch ($selector) {
    case "A": //right
        doSomething();
        break;
    case "B" : //wrong
        doSomethingElse();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
298
                $this->serverSide = $serverSide;
299
                break;
300
            default:
301
                $this->serverSide = true;
302
                break;
303
        }
304
        return $this;
305
    }
306
307
}
308