Completed
Push — master ( 20444a...763823 )
by WEBEWEB
02:46
created

DataTablesWrapper::getRoute()   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\API;
13
14
use Symfony\Component\HttpFoundation\Request;
15
use WBW\Library\Core\IO\HTTPInterface;
16
17
/**
18
 * DataTables wrapper.
19
 *
20
 * @author webeweb <https://github.com/webeweb/>
21
 * @package WBW\Bundle\JQuery\DataTablesBundle\API
22
 */
23
class DataTablesWrapper implements HTTPInterface {
24
25
    /**
26
     * Columns.
27
     *
28
     * @var DataTablesColumn[]
29
     */
30
    private $columns;
31
32
    /**
33
     * Mapping.
34
     *
35
     * @var DataTablesMapping
36
     */
37
    private $mapping;
38
39
    /**
40
     * Method.
41
     *
42
     * @var string
43
     */
44
    private $method;
45
46
    /**
47
     * Order.
48
     *
49
     * @var array
50
     */
51
    private $order;
52
53
    /**
54
     * Processing.
55
     *
56
     * @var boolean
57
     */
58
    private $processing;
59
60
    /**
61
     * Request.
62
     *
63
     * @var DataTablesRequest
64
     */
65
    private $request;
66
67
    /**
68
     * Response.
69
     *
70
     * @var DataTablesResponse
71
     */
72
    private $response;
73
74
    /**
75
     * Server side.
76
     *
77
     * @var boolean
78
     */
79
    private $serverSide;
80
81
    /**
82
     * URL.
83
     *
84
     * @var string
85
     */
86
    private $url;
87
88
    /**
89
     * Constructor.
90
     *
91
     * @param string $prefix The prefix.
92
     * @param string $method The method.
93
     * @param string $url The URL.
94
     */
95
    public function __construct($prefix, $method, $url) {
96
        $this->mapping = new DataTablesMapping();
97
        $this->mapping->setPrefix($prefix);
98
99
        $this->setColumns([]);
100
        $this->setMethod($method);
101
        $this->setOrder([]);
102
        $this->setProcessing(true);
103
        $this->setServerSide(true);
104
        $this->setUrl($url);
105
    }
106
107
    /**
108
     * Add a column.
109
     *
110
     * @param DataTablesColumn $column The column.
111
     * @return DataTablesWrapper Returns this DataTables wrapper.
112
     */
113
    public function addColumn(DataTablesColumn $column) {
114
        if (null === $column->getMapping()->getPrefix()) {
115
            $column->getMapping()->setPrefix($this->mapping->getPrefix());
116
        }
117
        $this->columns[$column->getData()] = $column;
118
        return $this;
119
    }
120
121
    /**
122
     * Get a column.
123
     *
124
     * @param string $data The column data.
125
     * @return DataTablesColumn Returns the column in case of success, null otherwise.
126
     */
127
    public function getColumn($data) {
128
        if (true === array_key_exists($data, $this->columns)) {
129
            return $this->columns[$data];
130
        }
131
        return null;
132
    }
133
134
    /**
135
     * Get the columns.
136
     *
137
     * @return DataTablesColumn[] Returns the columns.
138
     */
139
    public function getColumns() {
140
        return $this->columns;
141
    }
142
143
    /**
144
     * Get the mapping.
145
     *
146
     * @return DataTablesMapping The mapping.
147
     */
148
    public function getMapping() {
149
        return $this->mapping;
150
    }
151
152
    /**
153
     * Get the method.
154
     *
155
     * @return string Returns the method.
156
     */
157
    public function getMethod() {
158
        return $this->method;
159
    }
160
161
    /**
162
     * Get the order.
163
     *
164
     * @return array Returns the order.
165
     */
166
    public function getOrder() {
167
        return $this->order;
168
    }
169
170
    /**
171
     * Get the processing.
172
     *
173
     * @return boolean Returns the processing.
174
     */
175
    public function getProcessing() {
176
        return $this->processing;
177
    }
178
179
    /**
180
     * Get the request.
181
     *
182
     * @return DataTablesRequest The request.
183
     */
184
    public function getRequest() {
185
        return $this->request;
186
    }
187
188
    /**
189
     * Get the response.
190
     *
191
     * @return DataTablesResponse Returns the response.
192
     */
193
    public function getResponse() {
194
        return $this->response;
195
    }
196
197
    /**
198
     * Get the server side.
199
     *
200
     * @return boolean Returns the server side.
201
     */
202
    public function getServerSide() {
203
        return $this->serverSide;
204
    }
205
206
    /**
207
     * Get the URL.
208
     *
209
     * @return string Returns the URL.
210
     */
211
    public function getUrl() {
212
        return $this->url;
213
    }
214
215
    /**
216
     * Parse a request.
217
     *
218
     * @param Request $request The request.
219
     * @return DataTablesWrapper Returns this DataTables wrapper.
220
     */
221
    public function parse(Request $request) {
222
        $this->request  = DataTablesRequest::parse($this, $request);
223
        $this->response = DataTablesResponse::parse($this, $this->request);
224
        return $this;
225
    }
226
227
    /**
228
     * Remove a column.
229
     *
230
     * @param DataTablesColumn $column The column.
231
     * @return DataTablesWrapper Returns this DataTables wrapper.
232
     */
233
    public function removeColumn(DataTablesColumn $column) {
234
        if (true === array_key_exists($column->getData(), $this->columns)) {
235
            $this->columns[$column->getData()]->getMapping()->setPrefix(null);
236
            unset($this->columns[$column->getData()]);
237
        }
238
        return $this;
239
    }
240
241
    /**
242
     * Set the columns.
243
     *
244
     * @param DataTablesColumn[] $columns The columns.
245
     * @return DataTablesWrapper Returns this DataTables wrapper.
246
     */
247
    private function setColumns(array $columns) {
248
        $this->columns = $columns;
249
        return $this;
250
    }
251
252
    /**
253
     * Set the method.
254
     *
255
     * @param string $method The method.
256
     * @return DataTablesWrapper Returns this DataTables wrapper.
257
     */
258
    public function setMethod($method) {
259
        $this->method = (true === in_array($method, [self::HTTP_METHOD_GET, self::HTTP_METHOD_POST]) ? $method : self::HTTP_METHOD_GET);
260
        return $this;
261
    }
262
263
    /**
264
     * Set the order.
265
     *
266
     * @param array $order The order.
267
     * @return DataTablesWrapper Returns this DataTables wrapper.
268
     */
269
    public function setOrder(array $order) {
270
        $this->order = $order;
271
        return $this;
272
    }
273
274
    /**
275
     * Set the processing.
276
     *
277
     * @param boolean $processing The processing.
278
     * @return DataTablesWrapper Returns this DataTables wrapper.
279
     */
280
    public function setProcessing($processing) {
281
        $this->processing = (false === $processing ? false : true);
282
        return $this;
283
    }
284
285
    /**
286
     * Set the server side.
287
     *
288
     * @param boolean $serverSide The server side.
289
     * @return DataTablesWrapper Returns this DataTables wrapper.
290
     */
291
    public function setServerSide($serverSide) {
292
        $this->serverSide = (false === $serverSide ? false : true);
293
        return $this;
294
    }
295
296
    /**
297
     * Set the URL.
298
     *
299
     * @param string $url The UTL.
300
     * @return DataTablesWrapper Returns this DataTables wrapper.
301
     */
302
    public function setUrl($url) {
303
        $this->url = $url;
304
        return $this;
305
    }
306
307
}
308