Completed
Push — master ( aef7f8...916348 )
by WEBEWEB
01:35
created

DataTablesWrapper::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

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