Completed
Push — master ( a8f6cd...a14bb3 )
by WEBEWEB
04:19
created

DataTablesColumn::setCellType()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 2
eloc 3
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\API;
13
14
use JsonSerializable;
15
use WBW\Library\Core\Utility\Argument\ArrayUtility;
16
17
/**
18
 * DataTables column.
19
 *
20
 * @author webeweb <https://github.com/webeweb/>
21
 * @package WBW\Bundle\JQuery\DatatablesBundle\API
22
 */
23
class DataTablesColumn implements JsonSerializable {
24
25
    /**
26
     * Cell type.
27
     *
28
     * @var string
29
     */
30
    private $cellType;
31
32
    /**
33
     * Class name.
34
     *
35
     * @var string
36
     */
37
    private $classname;
38
39
    /**
40
     * Content padding.
41
     *
42
     * @var string
43
     */
44
    private $contentPadding;
45
46
    /**
47
     * Data.
48
     *
49
     * @var integer|string
50
     */
51
    private $data;
52
53
    /**
54
     * Default content.
55
     *
56
     * @var string
57
     */
58
    private $defaultContent;
59
60
    /**
61
     * mapping.
62
     *
63
     * @var DataTablesMapping
64
     */
65
    private $mapping;
66
67
    /**
68
     * Name.
69
     *
70
     * @var string
71
     */
72
    private $name;
73
74
    /**
75
     * Order data.
76
     *
77
     * @var integer|array
78
     */
79
    private $orderData;
80
81
    /**
82
     * Order data type.
83
     *
84
     * @var string
85
     */
86
    private $orderDataType;
87
88
    /**
89
     * Order sequence.
90
     *
91
     * @var string
92
     */
93
    private $orderSequence;
94
95
    /**
96
     * Orderable.
97
     *
98
     * @var boolean
99
     */
100
    private $orderable;
101
102
    /**
103
     * Search.
104
     *
105
     * @var DataTablesSearch
106
     */
107
    private $search;
108
109
    /**
110
     * Searchable.
111
     *
112
     * @var boolean
113
     */
114
    private $searchable;
115
116
    /**
117
     * Type.
118
     *
119
     * @var string
120
     */
121
    private $type;
122
123
    /**
124
     * Visible.
125
     *
126
     * @var boolean
127
     */
128
    private $visible;
129
130
    /**
131
     * Width.
132
     *
133
     * @var string
134
     */
135
    private $width;
136
137
    /**
138
     * Constructor.
139
     */
140
    protected function __construct() {
141
        $this->setCellType("td");
142
        $this->setMapping(new DataTablesMapping());
143
        $this->setOrderable(true);
144
        $this->setSearchable(true);
145
        $this->setVisible(true);
146
    }
147
148
    /**
149
     * Get the cell type.
150
     *
151
     * @return string Returns the cell type.
152
     */
153
    public function getCellType() {
154
        return $this->cellType;
155
    }
156
157
    /**
158
     * Get the class name.
159
     *
160
     * @return string Returns the class name.
161
     */
162
    public function getClassname() {
163
        return $this->classname;
164
    }
165
166
    /**
167
     * Get the content padding.
168
     *
169
     * @return string Returns the content padding.
170
     */
171
    public function getContentPadding() {
172
        return $this->contentPadding;
173
    }
174
175
    /**
176
     * Get the data.
177
     *
178
     * @return integer|string Returns the data.
179
     */
180
    public function getData() {
181
        return $this->data;
182
    }
183
184
    /**
185
     * Get the default content
186
     *
187
     * @return string Returns the default content.
188
     */
189
    public function getDefaultContent() {
190
        return $this->defaultContent;
191
    }
192
193
    /**
194
     * Get the mapping.
195
     *
196
     * @return DataTablesMapping The mapping.
197
     */
198
    public function getMapping() {
199
        return $this->mapping;
200
    }
201
202
    /**
203
     * Get the name.
204
     *
205
     * @return string Returns the name.
206
     */
207
    public function getName() {
208
        return $this->name;
209
    }
210
211
    /**
212
     * Get the order data.
213
     *
214
     * @return integer|array Returns the order data.
215
     */
216
    public function getOrderData() {
217
        return $this->orderData;
218
    }
219
220
    /**
221
     * Get the order data type.
222
     *
223
     * @return string Returns the order data type.
224
     */
225
    public function getOrderDataType() {
226
        return $this->orderDataType;
227
    }
228
229
    /**
230
     * Get the order sequence.
231
     *
232
     * @return string Returns the order sequence.
233
     */
234
    public function getOrderSequence() {
235
        return $this->orderSequence;
236
    }
237
238
    /**
239
     * Get the orderable.
240
     *
241
     * @return boolean Returns the orderable.
242
     */
243
    public function getOrderable() {
244
        return $this->orderable;
245
    }
246
247
    /**
248
     * Get the search.
249
     *
250
     * @return DataTablesSearch Returns the search.
251
     */
252
    public function getSearch() {
253
        return $this->search;
254
    }
255
256
    /**
257
     * Get the searchable.
258
     *
259
     * @return boolean Returns the searchable.
260
     */
261
    public function getSearchable() {
262
        return $this->searchable;
263
    }
264
265
    /**
266
     * Get the type.
267
     *
268
     * @return string Returns the type.
269
     */
270
    public function getType() {
271
        return $this->type;
272
    }
273
274
    /**
275
     * Get the visible.
276
     *
277
     * @return boolean Returns the visible.
278
     */
279
    public function getVisible() {
280
        return $this->visible;
281
    }
282
283
    /**
284
     * Get the width.
285
     *
286
     * @return string Returns the width.
287
     */
288
    public function getWidth() {
289
        return $this->width;
290
    }
291
292
    /**
293
     * {@inheritdoc}
294
     */
295
    public function jsonSerialize() {
296
        return $this->toArray();
297
    }
298
299
    /**
300
     * Create a DataTables column instance.
301
     *
302
     * @param string $data The column data.
303
     * @param string $name The column name.
304
     * @param string $cellType The column cell type.
305
     * @return DataTablesColumn Returns a DataTables column.
306
     */
307
    public static function newInstance($data, $name, $cellType = "td") {
308
309
        // Initialize a DataTables column.
310
        $dtColumn = new DataTablesColumn();
311
        $dtColumn->setCellType($cellType);
312
        $dtColumn->setData($data);
313
        $dtColumn->setName($name);
314
        $dtColumn->mapping->setColumn($data);
315
316
        // Return the DataTables column.
317
        return $dtColumn;
318
    }
319
320
    /**
321
     * Parse a raw columns array.
322
     *
323
     * @param array $rawColumns The raw columns array.
324
     * @param DataTablesWrapper $wrapper The wrapper.
325
     * @return DataTablesColumn[] Returns the DataTables columns.
326
     */
327
    public static function parse(array $rawColumns, DataTablesWrapper $wrapper) {
328
329
        // Initialize the DataTables columns.
330
        $dtColumns = [];
331
332
        // Handle each column.
333
        foreach ($rawColumns as $current) {
334
335
            // Get the DataTables column.
336
            $dtColumn = $wrapper->getColumn($current["data"]);
337
338
            // Check the DataTables column.
339
            if (null === $dtColumn) {
340
                continue;
341
            }
342
            if ($current["name"] !== $dtColumn->getName()) {
343
                continue;
344
            }
345
            if (false === $dtColumn->getSearchable()) {
346
                $dtColumn->setSearch(DataTablesSearch::parse([])); // Set a defautl DataTables search.
347
                continue;
348
            }
349
350
            // Set the DataTables search.
351
            $dtColumn->setSearch(DataTablesSearch::parse($current["search"]));
352
353
            // Add the DataTables columns.
354
            $dtColumns[] = $dtColumn;
355
        }
356
357
        // Returns the DataTables columns.
358
        return $dtColumns;
359
    }
360
361
    /**
362
     * Set the cell type.
363
     *
364
     * @param string $cellType The cell type.
365
     * @return DataTablesColumn Returns this DataTables column.
366
     */
367
    public function setCellType($cellType) {
368
        $this->cellType = (true === in_array($cellType, ["td", "th"]) ? $cellType : "td");
369
        return $this;
370
    }
371
372
    /**
373
     * Set the class name.
374
     *
375
     * @param string $classname The class name.
376
     * @return DataTablesColumn Returns this DataTables column.
377
     */
378
    public function setClassname($classname) {
379
        $this->classname = $classname;
380
        return $this;
381
    }
382
383
    /**
384
     * Set the content padding.
385
     *
386
     * @param string $contentPadding The content padding.
387
     * @return DataTablesColumn Returns this DataTables column.
388
     */
389
    public function setContentPadding($contentPadding) {
390
        $this->contentPadding = $contentPadding;
391
        return $this;
392
    }
393
394
    /**
395
     * Set the data.
396
     *
397
     * @param integer|string $data The data.
398
     * @return DataTablesColumn Returns this DataTables column.
399
     */
400
    protected function setData($data) {
401
        $this->data = $data;
402
        return $this;
403
    }
404
405
    /**
406
     * Set the default content.
407
     *
408
     * @param string $defaultContent The default content.
409
     * @return DataTablesColumn Returns this DataTables column.
410
     */
411
    public function setDefaultContent($defaultContent) {
412
        $this->defaultContent = $defaultContent;
413
        return $this;
414
    }
415
416
    /**
417
     * Set the mapping.
418
     *
419
     * @param DataTablesMapping $mapping The mapping.
420
     * @return DataTablesColumn Returns this DataTables column.
421
     */
422
    protected function setMapping(DataTablesMapping $mapping) {
423
        $this->mapping = $mapping;
424
        return $this;
425
    }
426
427
    /**
428
     * Set the name.
429
     *
430
     * @param string $name The name.
431
     * @return DataTablesColumn Returns this DataTables column.
432
     */
433
    public function setName($name) {
434
        $this->name = $name;
435
        return $this;
436
    }
437
438
    /**
439
     * Set the order data.
440
     *
441
     * @param integer|array $orderData The order data.
442
     * @return DataTablesColumn Returns this DataTables column.
443
     */
444
    public function setOrderData($orderData) {
445
        $this->orderData = $orderData;
446
        return $this;
447
    }
448
449
    /**
450
     * Set the order data type.
451
     *
452
     * @param string $orderDataType The order data type.
453
     * @return DataTablesColumn Returns this DataTables column.
454
     */
455
    public function setOrderDataType($orderDataType) {
456
        $this->orderDataType = $orderDataType;
457
        return $this;
458
    }
459
460
    /**
461
     * Set the order sequence.
462
     *
463
     * @param string $orderSequence The order sequence.
464
     * @return DataTablesColumn Returns this DataTables column.
465
     */
466
    public function setOrderSequence($orderSequence) {
467
        $this->orderSequence = (true === in_array($orderSequence, ["asc", "desc"]) ? $orderSequence : null);
468
        return $this;
469
    }
470
471
    /**
472
     * Set the orderable.
473
     *
474
     * @param boolean $orderable The orderable.
475
     * @return DataTablesColumn Returns this DataTables column.
476
     */
477
    public function setOrderable($orderable) {
478
        $this->orderable = $orderable;
479
        return $this;
480
    }
481
482
    /**
483
     * Set the search.
484
     *
485
     * @param DataTablesSearch $search The search.
486
     * @return DataTablesColumn Returns this DataTables column.
487
     */
488
    protected function setSearch(DataTablesSearch $search) {
489
        $this->search = $search;
490
        return $this;
491
    }
492
493
    /**
494
     * Set the searchable.
495
     *
496
     * @param boolean $searchable The searchable.
497
     * @return DataTablesColumn Returns this DataTables column.
498
     */
499
    public function setSearchable($searchable) {
500
        $this->searchable = $searchable;
501
        return $this;
502
    }
503
504
    /**
505
     * Set the type.
506
     *
507
     * @param string $type The type.
508
     * @return DataTablesColumn Returns this DataTables column.
509
     */
510
    public function setType($type) {
511
        $this->type = (true === in_array($type, ["date", "num", "num-fmt", "html", "html-num", "string"]) ? $type : null);
512
        return $this;
513
    }
514
515
    /**
516
     * Set the visible.
517
     *
518
     * @param boolean $visible The visible.
519
     * @return DataTablesColumn Returns this DataTables column.
520
     */
521
    public function setVisible($visible) {
522
        $this->visible = $visible;
523
        return $this;
524
    }
525
526
    /**
527
     * Set the width.
528
     *
529
     * @param string $width The width.
530
     * @return DataTablesColumn Returns this DataTables column.
531
     */
532
    public function setWidth($width) {
533
        $this->width = $width;
534
        return $this;
535
    }
536
537
    /**
538
     * Convert into an array representing this instance.
539
     *
540
     * @return array Returns an array representing this instance.
541
     */
542
    public function toArray() {
543
544
        // Initialize the output.
545
        $output = [];
546
547
        ArrayUtility::set($output, "cellType", $this->cellType, [null]);
548
        ArrayUtility::set($output, "classname", $this->classname, [null]);
549
        ArrayUtility::set($output, "contentPadding", $this->contentPadding, [null]);
550
        ArrayUtility::set($output, "data", $this->data, [null]);
551
        ArrayUtility::set($output, "defaultContent", $this->defaultContent, [null]);
552
        ArrayUtility::set($output, "name", $this->name, [null]);
553
        ArrayUtility::set($output, "orderable", $this->orderable, [null, true]);
554
        ArrayUtility::set($output, "orderData", $this->orderData, [null]);
555
        ArrayUtility::set($output, "orderDataType", $this->orderDataType, [null]);
556
        ArrayUtility::set($output, "orderSequence", $this->orderSequence, [null]);
557
        ArrayUtility::set($output, "searchable", $this->searchable, [null, true]);
558
        ArrayUtility::set($output, "type", $this->type, [null]);
559
        ArrayUtility::set($output, "visible", $this->visible, [null, true]);
560
        ArrayUtility::set($output, "width", $this->width, [null]);
561
562
        // Return the output.
563
        return $output;
564
    }
565
566
}
567