Completed
Push — master ( 35a1b3...6b82fc )
by WEBEWEB
01:38
created

DataTablesWrapper::getName()   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
     * Name.
48
     *
49
     * @var string
50
     */
51
    private $name;
52
53
    /**
54
     * Order.
55
     *
56
     * @var array
57
     */
58
    private $order;
59
60
    /**
61
     * Processing.
62
     *
63
     * @var boolean
64
     */
65
    private $processing;
66
67
    /**
68
     * Request.
69
     *
70
     * @var DataTablesRequest
71
     */
72
    private $request;
73
74
    /**
75
     * Response.
76
     *
77
     * @var DataTablesResponse
78
     */
79
    private $response;
80
81
    /**
82
     * Server side.
83
     *
84
     * @var boolean
85
     */
86
    private $serverSide;
87
88
    /**
89
     * URL.
90
     *
91
     * @var string
92
     */
93
    private $url;
94
95
    /**
96
     * Constructor.
97
     *
98
     * @param string $method The method.
99
     * @param string $url The URL.
100
     * @param string $name The name.
101
     */
102
    public function __construct($method, $url, $name) {
103
        $this->mapping = new DataTablesMapping();
104
105
        $this->setColumns([]);
106
        $this->setMethod($method);
107
        $this->setName($name);
108
        $this->setOrder([]);
109
        $this->setProcessing(true);
110
        $this->setServerSide(true);
111
        $this->setUrl($url);
112
    }
113
114
    /**
115
     * Add a column.
116
     *
117
     * @param DataTablesColumn $column The column.
118
     * @return DataTablesWrapper Returns this DataTables wrapper.
119
     */
120
    public function addColumn(DataTablesColumn $column) {
121
        if (null === $column->getMapping()->getPrefix()) {
122
            $column->getMapping()->setPrefix($this->mapping->getPrefix());
123
        }
124
        $this->columns[$column->getData()] = $column;
125
        return $this;
126
    }
127
128
    /**
129
     * Get a column.
130
     *
131
     * @param string $data The column data.
132
     * @return DataTablesColumn Returns the column in case of success, null otherwise.
133
     */
134
    public function getColumn($data) {
135
        if (true === array_key_exists($data, $this->columns)) {
136
            return $this->columns[$data];
137
        }
138
        return null;
139
    }
140
141
    /**
142
     * Get the columns.
143
     *
144
     * @return DataTablesColumn[] Returns the columns.
145
     */
146
    public function getColumns() {
147
        return $this->columns;
148
    }
149
150
    /**
151
     * Get the mapping.
152
     *
153
     * @return DataTablesMapping Returns the mapping.
154
     */
155
    public function getMapping() {
156
        return $this->mapping;
157
    }
158
159
    /**
160
     * Get the method.
161
     *
162
     * @return string Returns the method.
163
     */
164
    public function getMethod() {
165
        return $this->method;
166
    }
167
168
    /**
169
     * Get the name.
170
     *
171
     * @return string Returns the name.
172
     */
173
    public function getName() {
174
        return $this->name;
175
    }
176
177
    /**
178
     * Get the order.
179
     *
180
     * @return array Returns the order.
181
     */
182
    public function getOrder() {
183
        return $this->order;
184
    }
185
186
    /**
187
     * Get the processing.
188
     *
189
     * @return boolean Returns the processing.
190
     */
191
    public function getProcessing() {
192
        return $this->processing;
193
    }
194
195
    /**
196
     * Get the request.
197
     *
198
     * @return DataTablesRequest The request.
199
     */
200
    public function getRequest() {
201
        return $this->request;
202
    }
203
204
    /**
205
     * Get the response.
206
     *
207
     * @return DataTablesResponse Returns the response.
208
     */
209
    public function getResponse() {
210
        return $this->response;
211
    }
212
213
    /**
214
     * Get the server side.
215
     *
216
     * @return boolean Returns the server side.
217
     */
218
    public function getServerSide() {
219
        return $this->serverSide;
220
    }
221
222
    /**
223
     * Get the URL.
224
     *
225
     * @return string Returns the URL.
226
     */
227
    public function getUrl() {
228
        return $this->url;
229
    }
230
231
    /**
232
     * Parse a request.
233
     *
234
     * @param Request $request The request.
235
     * @return DataTablesWrapper Returns this DataTables wrapper.
236
     */
237
    public function parse(Request $request) {
238
        $this->request  = DataTablesRequest::parse($this, $request);
239
        $this->response = DataTablesResponse::parse($this, $this->request);
240
        return $this;
241
    }
242
243
    /**
244
     * Remove a column.
245
     *
246
     * @param DataTablesColumn $column The column.
247
     * @return DataTablesWrapper Returns this DataTables wrapper.
248
     */
249
    public function removeColumn(DataTablesColumn $column) {
250
        if (true === array_key_exists($column->getData(), $this->columns)) {
251
            $this->columns[$column->getData()]->getMapping()->setPrefix(null);
252
            unset($this->columns[$column->getData()]);
253
        }
254
        return $this;
255
    }
256
257
    /**
258
     * Set the columns.
259
     *
260
     * @param DataTablesColumn[] $columns The columns.
261
     * @return DataTablesWrapper Returns this DataTables wrapper.
262
     */
263
    private function setColumns(array $columns) {
264
        $this->columns = $columns;
265
        return $this;
266
    }
267
268
    /**
269
     * Set the method.
270
     *
271
     * @param string $method The method.
272
     * @return DataTablesWrapper Returns this DataTables wrapper.
273
     */
274
    public function setMethod($method) {
275
        $this->method = (true === in_array($method, [self::HTTP_METHOD_GET, self::HTTP_METHOD_POST]) ? $method : self::HTTP_METHOD_GET);
276
        return $this;
277
    }
278
279
    /**
280
     * Set the name.
281
     *
282
     * @param string $name The name.
283
     * @return DataTablesWrapper Returns this DataTables wrapper.
284
     */
285
    public function setName($name) {
286
        $this->name = $name;
287
        return $this;
288
    }
289
290
    /**
291
     * Set the order.
292
     *
293
     * @param array $order The order.
294
     * @return DataTablesWrapper Returns this DataTables wrapper.
295
     */
296
    public function setOrder(array $order) {
297
        $this->order = $order;
298
        return $this;
299
    }
300
301
    /**
302
     * Set the processing.
303
     *
304
     * @param boolean $processing The processing.
305
     * @return DataTablesWrapper Returns this DataTables wrapper.
306
     */
307
    public function setProcessing($processing) {
308
        $this->processing = (false === $processing ? false : true);
309
        return $this;
310
    }
311
312
    /**
313
     * Set the server side.
314
     *
315
     * @param boolean $serverSide The server side.
316
     * @return DataTablesWrapper Returns this DataTables wrapper.
317
     */
318
    public function setServerSide($serverSide) {
319
        $this->serverSide = (false === $serverSide ? false : true);
320
        return $this;
321
    }
322
323
    /**
324
     * Set the URL.
325
     *
326
     * @param string $url The UTL.
327
     * @return DataTablesWrapper Returns this DataTables wrapper.
328
     */
329
    public function setUrl($url) {
330
        $this->url = $url;
331
        return $this;
332
    }
333
334
}
335