Completed
Push — master ( 8bbe55...b9bf75 )
by WEBEWEB
01:35
created

DataTablesRequest::getQuery()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
c 0
b 0
f 0
rs 10
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\API;
13
14
use Symfony\Component\HttpFoundation\ParameterBag;
15
use Symfony\Component\HttpFoundation\Request;
16
use WBW\Library\Core\IO\HTTPInterface;
17
18
/**
19
 * DataTables request.
20
 *
21
 * @author webeweb <https://github.com/webeweb/>
22
 * @package WBW\Bundle\JQuery\DataTablesBundle\API
23
 */
24
class DataTablesRequest implements DataTablesRequestInterface, HTTPInterface {
25
26
    /**
27
     * Columns.
28
     *
29
     * @var DataTablesColumn[]
30
     */
31
    private $columns;
32
33
    /**
34
     * Draw.
35
     *
36
     * @var integer
37
     */
38
    private $draw;
39
40
    /**
41
     * Length.
42
     *
43
     * @var integer
44
     */
45
    private $length;
46
47
    /**
48
     * Order.
49
     *
50
     * @var DataTablesOrder[]
51
     */
52
    private $order;
53
54
    /**
55
     * Query.
56
     *
57
     * @var ParameterBag
58
     */
59
    private $query;
60
61
    /**
62
     * Request.
63
     *
64
     * @var ParameterBag
65
     */
66
    private $request;
67
68
    /**
69
     * Search.
70
     *
71
     * @var DataTablesSearch
72
     */
73
    private $search;
74
75
    /**
76
     * Start.
77
     *
78
     * @var integer
79
     */
80
    private $start;
81
82
    /**
83
     * Wrapper.
84
     *
85
     * @var DataTablesWrapper
86
     */
87
    private $wrapper;
88
89
    /**
90
     * Constructor.
91
     */
92
    protected function __construct() {
93
        $this->setColumns([]);
94
        $this->setDraw(0);
95
        $this->setLength(10);
96
        $this->setOrder([]);
97
        $this->setQuery(new ParameterBag());
98
        $this->setRequest(new ParameterBag());
99
        $this->setStart(0);
100
    }
101
102
    /**
103
     * DataTables parameters.
104
     *
105
     * @return array Returns the DataTables parameters.
106
     */
107
    public static function dtParameters() {
108
        return [
109
            self::DATATABLES_PARAMETER_COLUMNS,
110
            self::DATATABLES_PARAMETER_DRAW,
111
            self::DATATABLES_PARAMETER_LENGTH,
112
            self::DATATABLES_PARAMETER_ORDER,
113
            self::DATATABLES_PARAMETER_SEARCH,
114
            self::DATATABLES_PARAMETER_START,
115
        ];
116
    }
117
118
    /**
119
     * Get a column.
120
     *
121
     * @param string $data The column data.
122
     * @return array Returns the column in case of success, null otherwise.
123
     */
124
    public function getColumn($data) {
125
        foreach ($this->columns as $current) {
126
            if ($data === $current->getData()) {
127
                return $current;
128
            }
129
        }
130
        return null;
131
    }
132
133
    /**
134
     * Get the columns.
135
     *
136
     * @return DataTablesColumn[] Returns the columns.
137
     */
138
    public function getColumns() {
139
        return $this->columns;
140
    }
141
142
    /**
143
     * Get the draw.
144
     *
145
     * @return integer Returns the draw.
146
     */
147
    public function getDraw() {
148
        return $this->draw;
149
    }
150
151
    /**
152
     * Get the length.
153
     *
154
     * @return integer Returns the length.
155
     */
156
    public function getLength() {
157
        return $this->length;
158
    }
159
160
    /**
161
     * Get the order.
162
     *
163
     * @return DataTablesOrder[] Returns the order.
164
     */
165
    public function getOrder() {
166
        return $this->order;
167
    }
168
169
    /**
170
     * Get the query.
171
     *
172
     * @return ParameterBag Returns the query.
173
     */
174
    public function getQuery() {
175
        return $this->query;
176
    }
177
178
    /**
179
     * Get the request.
180
     *
181
     * @return ParameterBag Returns the request.
182
     */
183
    public function getRequest() {
184
        return $this->request;
185
    }
186
187
    /**
188
     * Get the search.
189
     *
190
     * @return DatTablesSearch Returns the search.
191
     */
192
    public function getSearch() {
193
        return $this->search;
194
    }
195
196
    /**
197
     * Get the start.
198
     *
199
     * @return integer Returns the start.
200
     */
201
    public function getStart() {
202
        return $this->start;
203
    }
204
205
    /**
206
     * Get the wrapper.
207
     *
208
     * @return DataTablesWrapper Returns the wrapper.
209
     */
210
    public function getWrapper() {
211
        return $this->wrapper;
212
    }
213
214
    /**
215
     * Parse a request.
216
     *
217
     * @param DataTablesWrapper $wrapper The wrapper.
218
     * @param Request $request The request.
219
     * @return DataTablesRequest Returns the DataTables request.
220
     */
221
    public static function parse(DataTablesWrapper $wrapper, Request $request) {
222
223
        // Initialize a DataTables request.
224
        $dtRequest = new DataTablesRequest();
225
226
        // Recopy the parameter bags.
227
        self::recopy($request->query, $dtRequest->getQuery());
228
        self::recopy($request->request, $dtRequest->getRequest());
229
230
        // Get the parameter bag.
231
        if (self::HTTP_METHOD_GET === $request->getMethod()) {
232
            $parameterBag = $request->query;
233
        } else {
234
            $parameterBag = $request->request;
235
        }
236
237
        // Get the request parameters.
238
        $columns = null !== $parameterBag->get(self::DATATABLES_PARAMETER_COLUMNS) ? $parameterBag->get(self::DATATABLES_PARAMETER_COLUMNS) : [];
239
        $orders  = null !== $parameterBag->get(self::DATATABLES_PARAMETER_ORDER) ? $parameterBag->get(self::DATATABLES_PARAMETER_ORDER) : [];
240
        $search  = null !== $parameterBag->get(self::DATATABLES_PARAMETER_SEARCH) ? $parameterBag->get(self::DATATABLES_PARAMETER_SEARCH) : [];
241
242
        // Set the DataTables request.
243
        $dtRequest->setColumns(DataTablesColumn::parse($columns, $wrapper));
244
        $dtRequest->setDraw($parameterBag->getInt(self::DATATABLES_PARAMETER_DRAW));
245
        $dtRequest->setLength($parameterBag->getInt(self::DATATABLES_PARAMETER_LENGTH));
246
        $dtRequest->setOrder(DataTablesOrder::parse($orders));
247
        $dtRequest->setSearch(DataTablesSearch::parse($search));
248
        $dtRequest->setStart($parameterBag->getInt(self::DATATABLES_PARAMETER_START));
249
        $dtRequest->setWrapper($wrapper);
250
251
        // Return the DataTables request.
252
        return $dtRequest;
253
    }
254
255
    /**
256
     * Recopy.
257
     *
258
     * @param ParameterBag $request The request.
259
     * @param ParameterBag $bag The bag.
260
     * @return void
261
     */
262
    protected static function recopy(ParameterBag $request, ParameterBag $bag) {
263
        foreach ($request->keys() as $current) {
264
            if (true === in_array($current, self::dtParameters())) {
265
                continue;
266
            }
267
            $bag->set($current, $request->get($current));
268
        }
269
    }
270
271
    /**
272
     * Set the columns.
273
     *
274
     * @param DataTablesColumn[] $columns The columns.
275
     * @return DataTablesRequest Returns this DataTables request.
276
     */
277
    protected function setColumns($columns) {
278
        $this->columns = $columns;
279
        return $this;
280
    }
281
282
    /**
283
     * Set the draw.
284
     *
285
     * @param integer $draw The draw.
286
     * @return DataTablesRequest Returns this DataTables request.
287
     */
288
    protected function setDraw($draw) {
289
        $this->draw = $draw;
290
        return $this;
291
    }
292
293
    /**
294
     * Set the length.
295
     *
296
     * @param integer $length The length.
297
     * @return DataTablesRequest Returns this DataTables request.
298
     */
299
    protected function setLength($length) {
300
        $this->length = $length;
301
        return $this;
302
    }
303
304
    /**
305
     * Set the order.
306
     *
307
     * @param DataTablesOrder[] $order The order.
308
     * @return DataTablesRequest Returns this DataTables request.
309
     */
310
    protected function setOrder($order) {
311
        $this->order = $order;
312
        return $this;
313
    }
314
315
    /**
316
     * Set the request.
317
     *
318
     * @param ParameterBag $query The query.
319
     * @return DataTablesRequest Returns this DataTables request.
320
     */
321
    protected function setQuery(ParameterBag $query) {
322
        $this->query = $query;
323
        return $this;
324
    }
325
326
    /**
327
     * Set the request.
328
     *
329
     * @param ParameterBag $request The request.
330
     * @return DataTablesRequest Returns this DataTables request.
331
     */
332
    protected function setRequest(ParameterBag $request) {
333
        $this->request = $request;
334
        return $this;
335
    }
336
337
    /**
338
     * Set the search.
339
     *
340
     * @param DataTablesSearch $search The search.
341
     * @return DataTablesRequest Returns this DataTables request.
342
     */
343
    protected function setSearch(DataTablesSearch $search) {
344
        $this->search = $search;
345
        return $this;
346
    }
347
348
    /**
349
     * Set the start.
350
     *
351
     * @param integer $start The start.
352
     * @return DataTablesRequest Returns this DataTables request.
353
     */
354
    protected function setStart($start) {
355
        $this->start = $start;
356
        return $this;
357
    }
358
359
    /**
360
     * Set the wrapper.
361
     *
362
     * @param DataTablesWrapper $wrapper The wrapper.
363
     * @return DataTablesRequest Returns this DataTables request.
364
     */
365
    protected function setWrapper(DataTablesWrapper $wrapper) {
366
        $this->wrapper = $wrapper;
367
        return $this;
368
    }
369
370
}
371