Completed
Push — master ( 17853b...f690dd )
by WEBEWEB
02:04
created

DataTablesWrapper::setResponse()   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 Symfony\Component\HttpFoundation\Request;
15
use WBW\Bundle\JQuery\DataTablesBundle\Factory\DataTablesFactory;
16
use WBW\Bundle\JQuery\DataTablesBundle\Provider\DataTablesProviderInterface;
17
use WBW\Library\Core\Network\HTTP\HTTPInterface;
18
19
/**
20
 * DataTables wrapper.
21
 *
22
 * @author webeweb <https://github.com/webeweb/>
23
 * @package WBW\Bundle\JQuery\DataTablesBundle\API
24
 */
25
class DataTablesWrapper implements DataTablesWrapperInterface, HTTPInterface {
26
27
    /**
28
     * Columns.
29
     *
30
     * @var DataTablesColumn[]
31
     */
32
    private $columns;
33
34
    /**
35
     * Mapping.
36
     *
37
     * @var DataTablesMappingInterface
38
     */
39
    private $mapping;
40
41
    /**
42
     * Method.
43
     *
44
     * @var string
45
     */
46
    private $method;
47
48
    /**
49
     * Name.
50
     *
51
     * @var string
52
     */
53
    private $name;
54
55
    /**
56
     * Options.
57
     *
58
     * @var DataTablesOptionsInterface
59
     */
60
    private $options;
61
62
    /**
63
     * Order.
64
     *
65
     * @var array
66
     */
67
    private $order;
68
69
    /**
70
     * Processing.
71
     *
72
     * @var bool
73
     */
74
    private $processing;
75
76
    /**
77
     * Provider.
78
     *
79
     * @var DataTablesProviderInterface
80
     */
81
    private $provider;
82
83
    /**
84
     * Request.
85
     *
86
     * @var DataTablesRequestInterface
87
     */
88
    private $request;
89
90
    /**
91
     * Response.
92
     *
93
     * @var DataTablesResponseInterface
94
     */
95
    private $response;
96
97
    /**
98
     * Server side.
99
     *
100
     * @var bool
101
     */
102
    private $serverSide;
103
104
    /**
105
     * URL.
106
     *
107
     * @var string
108
     */
109
    private $url;
110
111
    /**
112
     * Constructor.
113
     *
114
     * @param string $method The method.
115
     * @param string $url The URL.
116
     * @param string $name The name.
117
     */
118
    public function __construct($method, $url, $name) {
119
        $this->setColumns([]);
120
        $this->setMethod($method);
121
        $this->setMapping(new DataTablesMapping());
122
        $this->setName($name);
123
        $this->setOrder([]);
124
        $this->setProcessing(true);
125
        $this->setServerSide(true);
126
        $this->setUrl($url);
127
    }
128
129
    /**
130
     * Add a column.
131
     *
132
     * @param DataTablesColumn $column The column.
133
     * @return DataTablesWrapper Returns this wrapper.
134
     */
135
    public function addColumn(DataTablesColumn $column) {
136
        if (null === $column->getMapping()->getPrefix()) {
137
            $column->getMapping()->setPrefix($this->mapping->getPrefix());
138
        }
139
        $this->columns[$column->getData()] = $column;
140
        return $this;
141
    }
142
143
    /**
144
     * Get a column.
145
     *
146
     * @param string $data The column data.
147
     * @return DataTablesColumn Returns the column in case of success, null otherwise.
148
     */
149
    public function getColumn($data) {
150
        if (true === array_key_exists($data, $this->columns)) {
151
            return $this->columns[$data];
152
        }
153
        return null;
154
    }
155
156
    /**
157
     * Get the columns.
158
     *
159
     * @return DataTablesColumn[] Returns the columns.
160
     */
161
    public function getColumns() {
162
        return $this->columns;
163
    }
164
165
    /**
166
     * Get the mapping.
167
     *
168
     * @return DataTablesMappingInterface Returns the mapping.
169
     */
170
    public function getMapping() {
171
        return $this->mapping;
172
    }
173
174
    /**
175
     * Get the method.
176
     *
177
     * @return string Returns the method.
178
     */
179
    public function getMethod() {
180
        return $this->method;
181
    }
182
183
    /**
184
     * Get the name.
185
     *
186
     * @return string Returns the name.
187
     */
188
    public function getName() {
189
        return $this->name;
190
    }
191
192
    /**
193
     * Get the options.
194
     *
195
     * @return DataTablesOptionsInterface Returns the options.
196
     */
197
    public function getOptions() {
198
        return $this->options;
199
    }
200
201
    /**
202
     * Get the order.
203
     *
204
     * @return array Returns the order.
205
     */
206
    public function getOrder() {
207
        return $this->order;
208
    }
209
210
    /**
211
     * Get the processing.
212
     *
213
     * @return bool Returns the processing.
214
     */
215
    public function getProcessing() {
216
        return $this->processing;
217
    }
218
219
    /**
220
     * Get the provider.
221
     *
222
     * @return DataTablesProviderInterface Returns the provider.
223
     */
224
    public function getProvider() {
225
        return $this->provider;
226
    }
227
228
    /**
229
     * Get the request.
230
     *
231
     * @return DataTablesRequestInterface The request.
232
     */
233
    public function getRequest() {
234
        return $this->request;
235
    }
236
237
    /**
238
     * Get the response.
239
     *
240
     * @return DataTablesResponseInterface Returns the response.
241
     */
242
    public function getResponse() {
243
        return $this->response;
244
    }
245
246
    /**
247
     * Get the server side.
248
     *
249
     * @return bool Returns the server side.
250
     */
251
    public function getServerSide() {
252
        return $this->serverSide;
253
    }
254
255
    /**
256
     * Get the URL.
257
     *
258
     * @return string Returns the URL.
259
     */
260
    public function getUrl() {
261
        return $this->url;
262
    }
263
264
    /**
265
     * Parse a request.
266
     *
267
     * @param Request $request The request.
268
     * @return DataTablesWrapper Returns this wrapper.
269
     */
270
    public function parse(Request $request) {
271
        $this->request  = DataTablesFactory::parseRequest($this, $request);
272
        $this->response = DataTablesResponse::parse($this, $this->request);
0 ignored issues
show
Bug introduced by
The method parse() does not seem to exist on object<WBW\Bundle\JQuery...API\DataTablesResponse>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
273
        return $this;
274
    }
275
276
    /**
277
     * Remove a column.
278
     *
279
     * @param DataTablesColumn $column The column.
280
     * @return DataTablesWrapper Returns this wrapper.
281
     */
282
    public function removeColumn(DataTablesColumn $column) {
283
        if (true === array_key_exists($column->getData(), $this->columns)) {
284
            $this->columns[$column->getData()]->getMapping()->setPrefix(null);
285
            unset($this->columns[$column->getData()]);
286
        }
287
        return $this;
288
    }
289
290
    /**
291
     * Set the columns.
292
     *
293
     * @param DataTablesColumn[] $columns The columns.
294
     * @return DataTablesWrapper Returns this wrapper.
295
     */
296
    private function setColumns(array $columns) {
297
        $this->columns = $columns;
298
        return $this;
299
    }
300
301
    /**
302
     * Set the method.
303
     *
304
     * @param string $method The method.
305
     * @return DataTablesWrapper Returns this wrapper.
306
     */
307
    public function setMethod($method) {
308
        $this->method = (true === in_array($method, [self::HTTP_METHOD_GET, self::HTTP_METHOD_POST]) ? $method : self::HTTP_METHOD_POST);
309
        return $this;
310
    }
311
312
    /**
313
     * Set the mapping.
314
     *
315
     * @param DataTablesMappingInterface $mapping The mapping
316
     * @return DataTablesWrapper Returns this wrapper.
317
     */
318
    public function setMapping(DataTablesMappingInterface $mapping) {
319
        $this->mapping = $mapping;
320
        return $this;
321
    }
322
323
    /**
324
     * Set the name.
325
     *
326
     * @param string $name The name.
327
     * @return DataTablesWrapper Returns this wrapper.
328
     */
329
    public function setName($name) {
330
        $this->name = $name;
331
        return $this;
332
    }
333
334
    /**
335
     * Set the options.
336
     *
337
     * @param DataTablesOptionsInterface $options The options.
338
     * @return DataTablesWrapper Returns this wrapper.
339
     */
340
    public function setOptions(DataTablesOptionsInterface $options) {
341
        $this->options = $options;
342
        return $this;
343
    }
344
345
    /**
346
     * Set the order.
347
     *
348
     * @param array $order The order.
349
     * @return DataTablesWrapper Returns this wrapper.
350
     */
351
    public function setOrder(array $order) {
352
        $this->order = $order;
353
        return $this;
354
    }
355
356
    /**
357
     * Set the processing.
358
     *
359
     * @param bool $processing The processing.
360
     * @return DataTablesWrapper Returns this wrapper.
361
     */
362
    public function setProcessing($processing) {
363
        $this->processing = (false === $processing ? false : true);
364
        return $this;
365
    }
366
367
    /**
368
     * Set the provider.
369
     *
370
     * @param DataTablesProviderInterface $provider The provider.
371
     * @return DataTablesWrapper Returns this wrapper.
372
     */
373
    public function setProvider(DataTablesProviderInterface $provider) {
374
        $this->provider = $provider;
375
        return $this;
376
    }
377
378
    /**
379
     * Set the request.
380
     *
381
     * @param DataTablesRequestInterface $request The request.
382
     * @return DataTablesWrapperInterface Returns this wrapper.
383
     */
384
    public function setRequest(DataTablesRequestInterface $request) {
385
        $this->request = $request;
386
        return $this;
387
    }
388
389
    /**
390
     * Set the response.
391
     *
392
     * @param DataTablesResponseInterface $response The response.
393
     * @return DataTablesWrapperInterface Returns this wrapper.
394
     */
395
    public function setResponse(DataTablesResponseInterface $response) {
396
        $this->response = $response;
397
        return $this;
398
    }
399
400
    /**
401
     * Set the server side.
402
     *
403
     * @param bool $serverSide The server side.
404
     * @return DataTablesWrapper Returns this wrapper.
405
     */
406
    public function setServerSide($serverSide) {
407
        $this->serverSide = (false === $serverSide ? false : true);
408
        return $this;
409
    }
410
411
    /**
412
     * Set the URL.
413
     *
414
     * @param string $url The UTL.
415
     * @return DataTablesWrapper Returns this wrapper.
416
     */
417
    public function setUrl($url) {
418
        $this->url = $url;
419
        return $this;
420
    }
421
422
}
423