Completed
Push — master ( a7ed84...65cc57 )
by WEBEWEB
02:22
created

DataTablesColumn::getContentPadding()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
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 JsonSerializable;
15
use WBW\Library\Core\Argument\ArrayHelper;
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 DataTablesColumnInterface, 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
     * Title.
118
     *
119
     * @var string
120
     */
121
    private $title;
122
123
    /**
124
     * Type.
125
     *
126
     * @var string
127
     */
128
    private $type;
129
130
    /**
131
     * Visible.
132
     *
133
     * @var boolean
134
     */
135
    private $visible;
136
137
    /**
138
     * Width.
139
     *
140
     * @var string
141
     */
142
    private $width;
143
144
    /**
145
     * Constructor.
146
     */
147
    protected function __construct() {
148
        $this->setCellType(self::DATATABLES_CELL_TYPE_TD);
149
        $this->setMapping(new DataTablesMapping());
150
        $this->setOrderable(true);
151
        $this->setSearchable(true);
152
        $this->setVisible(true);
153
    }
154
155
    /**
156
     * DataTables cell types.
157
     *
158
     * @return array Returns the cell types.
159
     */
160
    public static function dtCellTypes() {
161
        return [
162
            self::DATATABLES_CELL_TYPE_TD,
163
            self::DATATABLES_CELL_TYPE_TH,
164
        ];
165
    }
166
167
    /**
168
     * DataTables order sequences.
169
     *
170
     * @return array Returns the order sequences.
171
     */
172
    public static function dtOrderSequences() {
173
        return [
174
            self::DATATABLES_ORDER_SEQUENCE_ASC,
175
            self::DATATABLES_ORDER_SEQUENCE_DESC,
176
        ];
177
    }
178
179
    /**
180
     * DataTables types.
181
     *
182
     * @return array Returns the types.
183
     */
184
    public static function dtTypes() {
185
        return [
186
            self::DATATABLES_TYPE_DATE,
187
            self::DATATABLES_TYPE_HTML,
188
            self::DATATABLES_TYPE_HTML_NUM,
189
            self::DATATABLES_TYPE_NUM,
190
            self::DATATABLES_TYPE_NUM_FMT,
191
            self::DATATABLES_TYPE_STRING,
192
        ];
193
    }
194
195
    /**
196
     * Get the cell type.
197
     *
198
     * @return string Returns the cell type.
199
     */
200
    public function getCellType() {
201
        return $this->cellType;
202
    }
203
204
    /**
205
     * Get the class name.
206
     *
207
     * @return string Returns the class name.
208
     */
209
    public function getClassname() {
210
        return $this->classname;
211
    }
212
213
    /**
214
     * Get the content padding.
215
     *
216
     * @return string Returns the content padding.
217
     */
218
    public function getContentPadding() {
219
        return $this->contentPadding;
220
    }
221
222
    /**
223
     * Get the data.
224
     *
225
     * @return integer|string Returns the data.
226
     */
227
    public function getData() {
228
        return $this->data;
229
    }
230
231
    /**
232
     * Get the default content
233
     *
234
     * @return string Returns the default content.
235
     */
236
    public function getDefaultContent() {
237
        return $this->defaultContent;
238
    }
239
240
    /**
241
     * Get the mapping.
242
     *
243
     * @return DataTablesMapping The mapping.
244
     */
245
    public function getMapping() {
246
        return $this->mapping;
247
    }
248
249
    /**
250
     * Get the name.
251
     *
252
     * @return string Returns the name.
253
     */
254
    public function getName() {
255
        return $this->name;
256
    }
257
258
    /**
259
     * Get the order data.
260
     *
261
     * @return integer|array Returns the order data.
262
     */
263
    public function getOrderData() {
264
        return $this->orderData;
265
    }
266
267
    /**
268
     * Get the order data type.
269
     *
270
     * @return string Returns the order data type.
271
     */
272
    public function getOrderDataType() {
273
        return $this->orderDataType;
274
    }
275
276
    /**
277
     * Get the order sequence.
278
     *
279
     * @return string Returns the order sequence.
280
     */
281
    public function getOrderSequence() {
282
        return $this->orderSequence;
283
    }
284
285
    /**
286
     * Get the orderable.
287
     *
288
     * @return boolean Returns the orderable.
289
     */
290
    public function getOrderable() {
291
        return $this->orderable;
292
    }
293
294
    /**
295
     * Get the search.
296
     *
297
     * @return DataTablesSearch Returns the search.
298
     */
299
    public function getSearch() {
300
        return $this->search;
301
    }
302
303
    /**
304
     * Get the searchable.
305
     *
306
     * @return boolean Returns the searchable.
307
     */
308
    public function getSearchable() {
309
        return $this->searchable;
310
    }
311
312
    /**
313
     * Get the title.
314
     *
315
     * @return string Returns the title.
316
     */
317
    public function getTitle() {
318
        return $this->title;
319
    }
320
321
    /**
322
     * Get the type.
323
     *
324
     * @return string Returns the type.
325
     */
326
    public function getType() {
327
        return $this->type;
328
    }
329
330
    /**
331
     * Get the visible.
332
     *
333
     * @return boolean Returns the visible.
334
     */
335
    public function getVisible() {
336
        return $this->visible;
337
    }
338
339
    /**
340
     * Get the width.
341
     *
342
     * @return string Returns the width.
343
     */
344
    public function getWidth() {
345
        return $this->width;
346
    }
347
348
    /**
349
     * {@inheritdoc}
350
     */
351
    public function jsonSerialize() {
352
        return $this->toArray();
353
    }
354
355
    /**
356
     * Create a column instance.
357
     *
358
     * @param string $data The column data.
359
     * @param string $name The column name.
360
     * @param string $cellType The column cell type.
361
     * @return DataTablesColumn Returns a column.
362
     */
363
    public static function newInstance($data, $name, $cellType = self::DATATABLES_CELL_TYPE_TD) {
364
365
        // Initialize a column.
366
        $dtColumn = new DataTablesColumn();
367
        $dtColumn->setCellType($cellType);
368
        $dtColumn->setData($data);
369
        $dtColumn->setName($name);
370
        $dtColumn->setTitle($name);
371
        $dtColumn->mapping->setColumn($data);
372
373
        // Return the column.
374
        return $dtColumn;
375
    }
376
377
    /**
378
     * Parse a raw columns array.
379
     *
380
     * @param array $rawColumns The raw columns array.
381
     * @param DataTablesWrapper $wrapper The wrapper.
382
     * @return DataTablesColumn[] Returns the columns.
383
     */
384
    public static function parse(array $rawColumns, DataTablesWrapper $wrapper) {
385
386
        // Initialize the columns.
387
        $dtColumns = [];
388
389
        // Handle each column.
390
        foreach ($rawColumns as $current) {
391
392
            // Get the column.
393
            $dtColumn = $wrapper->getColumn($current[self::DATATABLES_PARAMETER_DATA]);
394
395
            // Check the column.
396
            if (null === $dtColumn) {
397
                continue;
398
            }
399
            if ($current[self::DATATABLES_PARAMETER_NAME] !== $dtColumn->getName()) {
400
                continue;
401
            }
402
            if (false === $dtColumn->getSearchable()) {
403
                $dtColumn->setSearch(DataTablesSearch::parse([])); // Set a default search.
404
                continue;
405
            }
406
407
            // Set the search.
408
            $dtColumn->setSearch(DataTablesSearch::parse($current[self::DATATABLES_PARAMETER_SEARCH]));
409
410
            // Add the column.
411
            $dtColumns[] = $dtColumn;
412
        }
413
414
        // Returns the columns.
415
        return $dtColumns;
416
    }
417
418
    /**
419
     * Set the cell type.
420
     *
421
     * @param string $cellType The cell type.
422
     * @return DataTablesColumn Returns this column.
423
     */
424
    public function setCellType($cellType) {
425
        if (false === in_array($cellType, self::dtCellTypes())) {
426
            $cellType = self::DATATABLES_CELL_TYPE_TD;
427
        }
428
        $this->cellType = $cellType;
429
        return $this;
430
    }
431
432
    /**
433
     * Set the class name.
434
     *
435
     * @param string $classname The class name.
436
     * @return DataTablesColumn Returns this column.
437
     */
438
    public function setClassname($classname) {
439
        $this->classname = $classname;
440
        return $this;
441
    }
442
443
    /**
444
     * Set the content padding.
445
     *
446
     * @param string $contentPadding The content padding.
447
     * @return DataTablesColumn Returns this column.
448
     */
449
    public function setContentPadding($contentPadding) {
450
        $this->contentPadding = $contentPadding;
451
        return $this;
452
    }
453
454
    /**
455
     * Set the data.
456
     *
457
     * @param integer|string $data The data.
458
     * @return DataTablesColumn Returns this column.
459
     */
460
    protected function setData($data) {
461
        $this->data = $data;
462
        return $this;
463
    }
464
465
    /**
466
     * Set the default content.
467
     *
468
     * @param string $defaultContent The default content.
469
     * @return DataTablesColumn Returns this column.
470
     */
471
    public function setDefaultContent($defaultContent) {
472
        $this->defaultContent = $defaultContent;
473
        return $this;
474
    }
475
476
    /**
477
     * Set the mapping.
478
     *
479
     * @param DataTablesMapping $mapping The mapping.
480
     * @return DataTablesColumn Returns this column.
481
     */
482
    protected function setMapping(DataTablesMapping $mapping) {
483
        $this->mapping = $mapping;
484
        return $this;
485
    }
486
487
    /**
488
     * Set the name.
489
     *
490
     * @param string $name The name.
491
     * @return DataTablesColumn Returns this column.
492
     */
493
    public function setName($name) {
494
        $this->name = $name;
495
        return $this;
496
    }
497
498
    /**
499
     * Set the order data.
500
     *
501
     * @param integer|array $orderData The order data.
502
     * @return DataTablesColumn Returns this column.
503
     */
504
    public function setOrderData($orderData) {
505
        $this->orderData = $orderData;
506
        return $this;
507
    }
508
509
    /**
510
     * Set the order data type.
511
     *
512
     * @param string $orderDataType The order data type.
513
     * @return DataTablesColumn Returns this column.
514
     */
515
    public function setOrderDataType($orderDataType) {
516
        $this->orderDataType = $orderDataType;
517
        return $this;
518
    }
519
520
    /**
521
     * Set the order sequence.
522
     *
523
     * @param string $orderSequence The order sequence.
524
     * @return DataTablesColumn Returns this column.
525
     */
526
    public function setOrderSequence($orderSequence) {
527
        if (false === in_array($orderSequence, self::dtOrderSequences())) {
528
            $orderSequence = null;
529
        }
530
        $this->orderSequence = $orderSequence;
531
        return $this;
532
    }
533
534
    /**
535
     * Set the orderable.
536
     *
537
     * @param boolean $orderable The orderable.
538
     * @return DataTablesColumn Returns this column.
539
     */
540
    public function setOrderable($orderable) {
541
        $this->orderable = $orderable;
542
        return $this;
543
    }
544
545
    /**
546
     * Set the search.
547
     *
548
     * @param DataTablesSearch $search The search.
549
     * @return DataTablesColumn Returns this column.
550
     */
551
    protected function setSearch(DataTablesSearch $search) {
552
        $this->search = $search;
553
        return $this;
554
    }
555
556
    /**
557
     * Set the searchable.
558
     *
559
     * @param boolean $searchable The searchable.
560
     * @return DataTablesColumn Returns this column.
561
     */
562
    public function setSearchable($searchable) {
563
        $this->searchable = $searchable;
564
        return $this;
565
    }
566
567
    /**
568
     * Set the title.
569
     *
570
     * @param string $title The title.
571
     * @return DataTablesColumn Returns this column.
572
     */
573
    public function setTitle($title) {
574
        $this->title = $title;
575
        return $this;
576
    }
577
578
    /**
579
     * Set the type.
580
     *
581
     * @param string $type The type.
582
     * @return DataTablesColumn Returns this column.
583
     */
584
    public function setType($type) {
585
        if (false === in_array($type, self::dtTypes())) {
586
            $type = null;
587
        }
588
        $this->type = $type;
589
        return $this;
590
    }
591
592
    /**
593
     * Set the visible.
594
     *
595
     * @param boolean $visible The visible.
596
     * @return DataTablesColumn Returns this column.
597
     */
598
    public function setVisible($visible) {
599
        $this->visible = $visible;
600
        return $this;
601
    }
602
603
    /**
604
     * Set the width.
605
     *
606
     * @param string $width The width.
607
     * @return DataTablesColumn Returns this column.
608
     */
609
    public function setWidth($width) {
610
        $this->width = $width;
611
        return $this;
612
    }
613
614
    /**
615
     * Convert into an array representing this instance.
616
     *
617
     * @return array Returns an array representing this instance.
618
     */
619
    public function toArray() {
620
621
        // Initialize the output.
622
        $output = [];
623
624
        ArrayHelper::set($output, "cellType", $this->cellType, [null]);
625
        ArrayHelper::set($output, "classname", $this->classname, [null]);
626
        ArrayHelper::set($output, "contentPadding", $this->contentPadding, [null]);
627
        ArrayHelper::set($output, "data", $this->data, [null]);
628
        ArrayHelper::set($output, "defaultContent", $this->defaultContent, [null]);
629
        ArrayHelper::set($output, "name", $this->name, [null]);
630
        ArrayHelper::set($output, "orderable", $this->orderable, [null, true]);
631
        ArrayHelper::set($output, "orderData", $this->orderData, [null]);
632
        ArrayHelper::set($output, "orderDataType", $this->orderDataType, [null]);
633
        ArrayHelper::set($output, "orderSequence", $this->orderSequence, [null]);
634
        ArrayHelper::set($output, "searchable", $this->searchable, [null, true]);
635
        ArrayHelper::set($output, "type", $this->type, [null]);
636
        ArrayHelper::set($output, "visible", $this->visible, [null, true]);
637
        ArrayHelper::set($output, "width", $this->width, [null]);
638
639
        // Return the output.
640
        return $output;
641
    }
642
643
}
644