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