Completed
Push — master ( dbc0ff...b4a470 )
by WEBEWEB
01:31
created

DataTablesRequest::parse()   B

Complexity

Conditions 5
Paths 2

Size

Total Lines 20
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 20
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 12
nc 2
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\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 array
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 array
50
     */
51
    private $order;
52
53
    /**
54
     * Search.
55
     *
56
     * @var array
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
     * @param DataTableswrapper The wrapper.
78
     * @param Request The request.
79
     */
80
    public function __construct(DataTablesWrapper $wrapper, Request $request) {
81
        $this->setColumns([]);
82
        $this->setOrder([]);
83
        $this->setSearch([]);
84
        $this->setWrapper($wrapper);
85
        $this->parse($request);
86
    }
87
88
    /**
89
     * Get a column.
90
     *
91
     * @param string $name The column name.
92
     * @return array Returns the column in case of success, null otherwise.
93
     */
94
    public function getColumn($name) {
95
        foreach ($this->columns as $current) {
96
            if ($name === $current["name"]) {
97
                return $current;
98
            }
99
        }
100
        return null;
101
    }
102
103
    /**
104
     * Get the columns.
105
     *
106
     * @return array Returns the columns.
107
     */
108
    public function getColumns() {
109
        return $this->columns;
110
    }
111
112
    /**
113
     * Get the draw.
114
     *
115
     * @return integer Returns the draw.
116
     */
117
    public function getDraw() {
118
        return $this->draw;
119
    }
120
121
    /**
122
     * Get the length.
123
     *
124
     * @return integer Returns the length.
125
     */
126
    public function getLength() {
127
        return $this->length;
128
    }
129
130
    /**
131
     * Get the order.
132
     *
133
     * @return array Returns the order.
134
     */
135
    public function getOrder() {
136
        return $this->order;
137
    }
138
139
    /**
140
     * Get the search.
141
     *
142
     * @return array Returns the search.
143
     */
144
    public function getSearch() {
145
        return $this->search;
146
    }
147
148
    /**
149
     * Get the start.
150
     *
151
     * @return integer Returns the start.
152
     */
153
    public function getStart() {
154
        return $this->start;
155
    }
156
157
    /**
158
     * Get the wrapper.
159
     *
160
     * @return DataTablesWrapper Returns the wrapper.
161
     */
162
    public function getWrapper() {
163
        return $this->wrapper;
164
    }
165
166
    /**
167
     * Parse a request.
168
     *
169
     * @param Request $request The request.
170
     * @return DataTablesRequest Returns the DataTables request.
171
     */
172
    private function parse(Request $request) {
173
174
        // Get the parameter bag.
175
        if (self::HTTP_METHOD_GET === $request->getMethod()) {
176
            $parameterBag = $request->query;
177
        } else {
178
            $parameterBag = $request->request;
179
        }
180
181
        // Set the DataTables request.
182
        $this->setColumns(null !== $parameterBag->get("columns") ? $parameterBag->get("columns") : []);
183
        $this->setDraw(intval($parameterBag->get("draw")));
184
        $this->setLength(intval($parameterBag->get("length")));
185
        $this->setOrder(null !== $parameterBag->get("order") ? $parameterBag->get("order") : []);
186
        $this->setSearch(null !== $parameterBag->get("search") ? $parameterBag->get("search") : []);
187
        $this->setStart(intval($parameterBag->get("start")));
188
189
        // Return the DataTables request.
190
        return $this;
191
    }
192
193
    /**
194
     * Set the columns.
195
     *
196
     * @param array $columns The columns.
197
     * @return DataTablesRequest Returns the DataTables request.
198
     */
199
    private function setColumns(array $columns) {
200
        $this->columns = $columns;
201
        return $this;
202
    }
203
204
    /**
205
     * Set the draw.
206
     *
207
     * @param integer $draw The draw.
208
     * @return DataTablesRequest Returns the DataTables request.
209
     */
210
    public function setDraw($draw) {
211
        $this->draw = $draw;
212
        return $this;
213
    }
214
215
    /**
216
     * Set the length.
217
     *
218
     * @param integer $length The length.
219
     * @return DataTablesRequest Returns the DataTables request.
220
     */
221
    private function setLength($length) {
222
        $this->length = $length;
223
        return $this;
224
    }
225
226
    /**
227
     * Set the order.
228
     *
229
     * @param array $order The order.
230
     * @return DataTablesRequest Returns the DataTables request.
231
     */
232
    private function setOrder(array $order) {
233
        $this->order = $order;
234
        return $this;
235
    }
236
237
    /**
238
     * Set the search.
239
     *
240
     * @param array $search The search.
241
     * @return DataTablesRequest Returns the DataTables request.
242
     */
243
    private function setSearch(array $search) {
244
        $this->search = $search;
245
        return $this;
246
    }
247
248
    /**
249
     * Set the start.
250
     *
251
     * @param integer $start The start.
252
     * @return DataTablesRequest Returns the DataTables request.
253
     */
254
    private function setStart($start) {
255
        $this->start = $start;
256
        return $this;
257
    }
258
259
    /**
260
     * Set the wrapper.
261
     *
262
     * @param DataTablesWrapper $wrapper The wrapper.
263
     * @return DataTablesRequest Returns the DataTables request.
264
     */
265
    private function setWrapper(DataTablesWrapper $wrapper) {
266
        $this->wrapper = $wrapper;
267
        return $this;
268
    }
269
270
}
271