Completed
Push — master ( cc2b3d...e0d8bb )
by WEBEWEB
01:34
created

DataTablesRequest::parse()   A

Complexity

Conditions 5
Paths 16

Size

Total Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 29
rs 9.1448
c 0
b 0
f 0
cc 5
nc 16
nop 2
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\Library\Core\IO\HTTPInterface;
16
17
/**
18
 * DataTables request.
19
 *
20
 * @author webeweb <https://github.com/webeweb/>
21
 * @package WBW\Bundle\JQuery\DataTablesBundle\API
22
 */
23
class DataTablesRequest implements HTTPInterface {
24
25
    /**
26
     * Columns.
27
     *
28
     * @var DataTablesColumn[]
29
     */
30
    private $columns;
31
32
    /**
33
     * Draw.
34
     *
35
     * @var integer
36
     */
37
    private $draw;
38
39
    /**
40
     * Length.
41
     *
42
     * @var integer
43
     */
44
    private $length;
45
46
    /**
47
     * Order.
48
     *
49
     * @var DataTablesOrder[]
50
     */
51
    private $order;
52
53
    /**
54
     * Search.
55
     *
56
     * @var DataTablesSearch
57
     */
58
    private $search;
59
60
    /**
61
     * Start.
62
     *
63
     * @var integer
64
     */
65
    private $start;
66
67
    /**
68
     * Wrapper.
69
     *
70
     * @var DataTablesWrapper
71
     */
72
    private $wrapper;
73
74
    /**
75
     * Constructor.
76
     */
77
    protected function __construct() {
78
        $this->setColumns([]);
79
        $this->setDraw(0);
80
        $this->setLength(10);
81
        $this->setOrder([]);
82
        $this->setStart(0);
83
    }
84
85
    /**
86
     * Get a column.
87
     *
88
     * @param string $data The column data.
89
     * @return array Returns the column in case of success, null otherwise.
90
     */
91
    public function getColumn($data) {
92
        foreach ($this->columns as $current) {
93
            if ($data === $current->getData()) {
94
                return $current;
95
            }
96
        }
97
        return null;
98
    }
99
100
    /**
101
     * Get the columns.
102
     *
103
     * @return DataTablesColumn[] Returns the columns.
104
     */
105
    public function getColumns() {
106
        return $this->columns;
107
    }
108
109
    /**
110
     * Get the draw.
111
     *
112
     * @return integer Returns the draw.
113
     */
114
    public function getDraw() {
115
        return $this->draw;
116
    }
117
118
    /**
119
     * Get the length.
120
     *
121
     * @return integer Returns the length.
122
     */
123
    public function getLength() {
124
        return $this->length;
125
    }
126
127
    /**
128
     * Get the order.
129
     *
130
     * @return DataTablesOrder[] Returns the order.
131
     */
132
    public function getOrder() {
133
        return $this->order;
134
    }
135
136
    /**
137
     * Get the search.
138
     *
139
     * @return DatTablesSearch Returns the search.
140
     */
141
    public function getSearch() {
142
        return $this->search;
143
    }
144
145
    /**
146
     * Get the start.
147
     *
148
     * @return integer Returns the start.
149
     */
150
    public function getStart() {
151
        return $this->start;
152
    }
153
154
    /**
155
     * Get the wrapper.
156
     *
157
     * @return DataTablesWrapper Returns the wrapper.
158
     */
159
    public function getWrapper() {
160
        return $this->wrapper;
161
    }
162
163
    /**
164
     * Parse a request.
165
     *
166
     * @param DataTablesWrapper $wrapper The wrapper.
167
     * @param Request $request The request.
168
     * @return DataTablesRequest Returns the DataTables request.
169
     */
170
    public static function parse(DataTablesWrapper $wrapper, Request $request) {
171
172
        // Initialize a DataTables request.
173
        $dtRequest = new DataTablesRequest();
174
175
        // Get the parameter bag.
176
        if (self::HTTP_METHOD_GET === $request->getMethod()) {
177
            $parameterBag = $request->query;
178
        } else {
179
            $parameterBag = $request->request;
180
        }
181
182
        // Get the request parameters.
183
        $columns = null !== $parameterBag->get("columns") ? $parameterBag->get("columns") : [];
184
        $orders  = null !== $parameterBag->get("order") ? $parameterBag->get("order") : [];
185
        $search  = null !== $parameterBag->get("search") ? $parameterBag->get("search") : [];
186
187
        // Set the DataTables request.
188
        $dtRequest->setColumns(DataTablesColumn::parse($columns, $wrapper));
189
        $dtRequest->setDraw(intval($parameterBag->get("draw")));
190
        $dtRequest->setLength(intval($parameterBag->get("length")));
191
        $dtRequest->setOrder(DataTablesOrder::parse($orders));
192
        $dtRequest->setSearch(DataTablesSearch::parse($search));
193
        $dtRequest->setStart(intval($parameterBag->get("start")));
194
        $dtRequest->setWrapper($wrapper);
195
196
        // Return the DataTables request.
197
        return $dtRequest;
198
    }
199
200
    /**
201
     * Set the columns.
202
     *
203
     * @param DataTablesColumn[] $columns The columns.
204
     * @return DataTablesRequest Returns this DataTables request.
205
     */
206
    protected function setColumns($columns) {
207
        $this->columns = $columns;
208
        return $this;
209
    }
210
211
    /**
212
     * Set the draw.
213
     *
214
     * @param integer $draw The draw.
215
     * @return DataTablesRequest Returns this DataTables request.
216
     */
217
    protected function setDraw($draw) {
218
        $this->draw = $draw;
219
        return $this;
220
    }
221
222
    /**
223
     * Set the length.
224
     *
225
     * @param integer $length The length.
226
     * @return DataTablesRequest Returns this DataTables request.
227
     */
228
    protected function setLength($length) {
229
        $this->length = $length;
230
        return $this;
231
    }
232
233
    /**
234
     * Set the order.
235
     *
236
     * @param DataTablesOrder[] $order The order.
237
     * @return DataTablesRequest Returns this DataTables request.
238
     */
239
    protected function setOrder($order) {
240
        $this->order = $order;
241
        return $this;
242
    }
243
244
    /**
245
     * Set the search.
246
     *
247
     * @param DataTablesSearch $search The search.
248
     * @return DataTablesRequest Returns this DataTables request.
249
     */
250
    protected function setSearch(DataTablesSearch $search) {
251
        $this->search = $search;
252
        return $this;
253
    }
254
255
    /**
256
     * Set the start.
257
     *
258
     * @param integer $start The start.
259
     * @return DataTablesRequest Returns this DataTables request.
260
     */
261
    protected function setStart($start) {
262
        $this->start = $start;
263
        return $this;
264
    }
265
266
    /**
267
     * Set the wrapper.
268
     *
269
     * @param DataTablesWrapper $wrapper The wrapper.
270
     * @return DataTablesRequest Returns this DataTables request.
271
     */
272
    protected function setWrapper(DataTablesWrapper $wrapper) {
273
        $this->wrapper = $wrapper;
274
        return $this;
275
    }
276
277
}
278