Completed
Push — master ( cc2b3d...e0d8bb )
by WEBEWEB
01:34
created

DataTablesColumn::parse()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 33

Duplication

Lines 0
Ratio 0 %

Importance

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