Completed
Push — master ( 043997...8de9a3 )
by WEBEWEB
01:27
created

DataTablesRequest::getWrapper()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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