Completed
Push — master ( c43b9a...3eca47 )
by WEBEWEB
01:29
created

DataTablesRequest   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 220
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 21
c 0
b 0
f 0
lcom 1
cbo 2
dl 0
loc 220
rs 10

15 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getColumn() 0 9 3
A getColumns() 0 3 1
A getDraw() 0 3 1
A getLength() 0 3 1
A getOrder() 0 3 1
A getSearch() 0 3 1
A getStart() 0 3 1
B newInstance() 0 23 5
A setColumns() 0 4 1
A setDraw() 0 4 1
A setLength() 0 4 1
A setOrder() 0 4 1
A setSearch() 0 4 1
A setStart() 0 4 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\Request;
13
14
use Symfony\Component\HttpFoundation\Request;
15
16
/**
17
 * DataTables request.
18
 *
19
 * @author webeweb <https://github.com/webeweb/>
20
 * @package WBW\Bundle\JQuery\DatatablesBundle\API\Request
21
 * @final
22
 */
23
final class DataTablesRequest {
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
     * Constructor.
69
     */
70
    private function __construct() {
71
        $this->setColumns([]);
72
        $this->setOrder([]);
73
        $this->setSearch([]);
74
    }
75
76
    /**
77
     * Get a column.
78
     *
79
     * @param string $name The column name.
80
     * @return array Returns the column in case of success, null otherwise.
81
     */
82
    public function getColumn($name) {
83
        foreach ($this->columns as $column) {
84
            if ($name !== $column["name"]) {
85
                continue;
86
            }
87
            return $column;
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 ("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