Completed
Push — master ( 6bb9b8...65628b )
by WEBEWEB
25:29
created

DataTablesWrapper::setMapping()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
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 WBW\Bundle\JQuery\DataTablesBundle\Provider\DataTablesProviderInterface;
15
use WBW\Library\Core\Network\HTTP\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 DataTablesWrapperInterface, HTTPInterface {
24
25
    /**
26
     * Columns.
27
     *
28
     * @var DataTablesColumnInterface[]
29
     */
30
    private $columns;
31
32
    /**
33
     * Mapping.
34
     *
35
     * @var DataTablesMappingInterface
36
     */
37
    private $mapping;
38
39
    /**
40
     * Method.
41
     *
42
     * @var string
43
     */
44
    private $method;
45
46
    /**
47
     * Options.
48
     *
49
     * @var DataTablesOptionsInterface
50
     */
51
    private $options;
52
53
    /**
54
     * Order.
55
     *
56
     * @var array
57
     */
58
    private $order;
59
60
    /**
61
     * Processing.
62
     *
63
     * @var bool
64
     */
65
    private $processing;
66
67
    /**
68
     * Provider.
69
     *
70
     * @var DataTablesProviderInterface
71
     */
72
    private $provider;
73
74
    /**
75
     * Request.
76
     *
77
     * @var DataTablesRequestInterface
78
     */
79
    private $request;
80
81
    /**
82
     * Response.
83
     *
84
     * @var DataTablesResponseInterface
85
     */
86
    private $response;
87
88
    /**
89
     * Server side.
90
     *
91
     * @var bool
92
     */
93
    private $serverSide;
94
95
    /**
96
     * URL.
97
     *
98
     * @var string
99
     */
100
    private $url;
101
102
    /**
103
     * Constructor.
104
     */
105
    public function __construct() {
106
        $this->setColumns([]);
107
        $this->setMapping(new DataTablesMapping());
108
        $this->setMethod(HTTPInterface::HTTP_METHOD_POST);
109
        $this->setOrder([]);
110
        $this->setProcessing(true);
111
        $this->setServerSide(true);
112
    }
113
114
    /**
115
     * {@inheritdoc}
116
     */
117
    public function addColumn(DataTablesColumnInterface $column) {
118
        if (null === $column->getMapping()->getPrefix()) {
119
            $column->getMapping()->setPrefix($this->mapping->getPrefix());
120
        }
121
        $this->columns[$column->getData()] = $column;
122
        return $this;
123
    }
124
125
    /**
126
     * {@inheritdoc}
127
     */
128
    public function getColumn($data) {
129
        if (true === array_key_exists($data, $this->columns)) {
130
            return $this->columns[$data];
131
        }
132
        return null;
133
    }
134
135
    /**
136
     * {@inheritdoc}
137
     */
138
    public function getColumns() {
139
        return $this->columns;
140
    }
141
142
    /**
143
     * {@inheritdoc}
144
     */
145
    public function getMapping() {
146
        return $this->mapping;
147
    }
148
149
    /**
150
     * {@inheritdoc}
151
     */
152
    public function getMethod() {
153
        return $this->method;
154
    }
155
156
    /**
157
     * {@inheritdoc}
158
     */
159
    public function getOptions() {
160
        return $this->options;
161
    }
162
163
    /**
164
     * {@inheritdoc}
165
     */
166
    public function getOrder() {
167
        return $this->order;
168
    }
169
170
    /**
171
     * {@inheritdoc}
172
     */
173
    public function getProcessing() {
174
        return $this->processing;
175
    }
176
177
    /**
178
     * {@inheritdoc}
179
     */
180
    public function getProvider() {
181
        return $this->provider;
182
    }
183
184
    /**
185
     * {@inheritdoc}
186
     */
187
    public function getRequest() {
188
        return $this->request;
189
    }
190
191
    /**
192
     * {@inheritdoc}
193
     */
194
    public function getResponse() {
195
        return $this->response;
196
    }
197
198
    /**
199
     * {@inheritdoc}
200
     */
201
    public function getServerSide() {
202
        return $this->serverSide;
203
    }
204
205
    /**
206
     * {@inheritdoc}
207
     */
208
    public function getUrl() {
209
        return $this->url;
210
    }
211
212
    /**
213
     * Remove a column.
214
     *
215
     * @param DataTablesColumnInterface $column The column.
216
     * @return DataTablesWrapperInterface Returns this wrapper.
217
     */
218
    public function removeColumn(DataTablesColumnInterface $column) {
219
        if (true === array_key_exists($column->getData(), $this->columns)) {
220
            $this->columns[$column->getData()]->getMapping()->setPrefix(null);
221
            unset($this->columns[$column->getData()]);
222
        }
223
        return $this;
224
    }
225
226
    /**
227
     * Set the columns.
228
     *
229
     * @param DataTablesColumnInterface[] $columns The columns.
230
     * @return DataTablesWrapperInterface Returns this wrapper.
231
     */
232
    private function setColumns(array $columns) {
233
        $this->columns = $columns;
234
        return $this;
235
    }
236
237
    /**
238
     * Set the method.
239
     *
240
     * @param string $method The method.
241
     * @return DataTablesWrapperInterface Returns this wrapper.
242
     */
243
    public function setMethod($method) {
244
        $this->method = (true === in_array($method, [self::HTTP_METHOD_GET, self::HTTP_METHOD_POST]) ? $method : self::HTTP_METHOD_POST);
245
        return $this;
246
    }
247
248
    /**
249
     * Set the mapping.
250
     *
251
     * @param DataTablesMappingInterface $mapping The mapping
252
     * @return DataTablesWrapperInterface Returns this wrapper.
253
     */
254
    public function setMapping(DataTablesMappingInterface $mapping) {
255
        $this->mapping = $mapping;
256
        return $this;
257
    }
258
259
    /**
260
     * Set the options.
261
     *
262
     * @param DataTablesOptionsInterface $options The options.
263
     * @return DataTablesWrapperInterface Returns this wrapper.
264
     */
265
    public function setOptions(DataTablesOptionsInterface $options) {
266
        $this->options = $options;
267
        return $this;
268
    }
269
270
    /**
271
     * Set the order.
272
     *
273
     * @param array $order The order.
274
     * @return DataTablesWrapperInterface Returns this wrapper.
275
     */
276
    public function setOrder(array $order) {
277
        $this->order = $order;
278
        return $this;
279
    }
280
281
    /**
282
     * Set the processing.
283
     *
284
     * @param bool $processing The processing.
285
     * @return DataTablesWrapperInterface Returns this wrapper.
286
     */
287
    public function setProcessing($processing) {
288
        $this->processing = (false === $processing ? false : true);
289
        return $this;
290
    }
291
292
    /**
293
     * Set the provider.
294
     *
295
     * @param DataTablesProviderInterface $provider The provider.
296
     * @return DataTablesWrapperInterface Returns this wrapper.
297
     */
298
    public function setProvider(DataTablesProviderInterface $provider) {
299
        $this->provider = $provider;
300
        return $this;
301
    }
302
303
    /**
304
     * {@inheritdoc}
305
     */
306
    public function setRequest(DataTablesRequestInterface $request) {
307
        $this->request = $request;
308
        return $this;
309
    }
310
311
    /**
312
     * {@inheritdoc}
313
     */
314
    public function setResponse(DataTablesResponseInterface $response) {
315
        $this->response = $response;
316
        return $this;
317
    }
318
319
    /**
320
     * Set the server side.
321
     *
322
     * @param bool $serverSide The server side.
323
     * @return DataTablesWrapperInterface Returns this wrapper.
324
     */
325
    public function setServerSide($serverSide) {
326
        $this->serverSide = (false === $serverSide ? false : true);
327
        return $this;
328
    }
329
330
    /**
331
     * Set the URL.
332
     *
333
     * @param string $url The UTL.
334
     * @return DataTablesWrapperInterface Returns this wrapper.
335
     */
336
    public function setUrl($url) {
337
        $this->url = $url;
338
        return $this;
339
    }
340
341
}
342