Completed
Push — master ( e78d8a...96e91a )
by WEBEWEB
01:20
created

DataTablesRequest::newInstance()   B

Complexity

Conditions 5
Paths 2

Size

Total Lines 23
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

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