Completed
Push — master ( 1a8d3e...7d5060 )
by Arjay
01:43 queued 12s
created

Column::name()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace Yajra\DataTables\Html;
4
5
use Illuminate\Support\Arr;
6
use Illuminate\Support\Str;
7
use Illuminate\Support\Fluent;
8
use Yajra\DataTables\Html\Options\Plugins\SearchPanes;
9
10
/**
11
 * @property string data
12
 * @property string name
13
 * @property string orderable
14
 * @property string searchable
15
 * @property string printable
16
 * @property string exportable
17
 * @property string footer
18
 * @property array attributes
19
 * @see https://datatables.net/reference/option/#columns
20
 */
21
class Column extends Fluent
22
{
23
    use SearchPanes;
24
25
    /**
26
     * @param array $attributes
27
     */
28
    public function __construct($attributes = [])
29
    {
30
        $attributes['title']      = isset($attributes['title']) ? $attributes['title'] : self::titleFormat($attributes['data']);
31
        $attributes['orderable']  = isset($attributes['orderable']) ? $attributes['orderable'] : true;
32
        $attributes['searchable'] = isset($attributes['searchable']) ? $attributes['searchable'] : true;
33
        $attributes['exportable'] = isset($attributes['exportable']) ? $attributes['exportable'] : true;
34
        $attributes['printable']  = isset($attributes['printable']) ? $attributes['printable'] : true;
35
        $attributes['footer']     = isset($attributes['footer']) ? $attributes['footer'] : '';
36
        $attributes['attributes'] = isset($attributes['attributes']) ? $attributes['attributes'] : [];
37
38
        // Allow methods override attribute value
39
        foreach ($attributes as $attribute => $value) {
40
            $method = 'parse' . ucfirst(strtolower($attribute));
41
            if (! is_null($value) && method_exists($this, $method)) {
42
                $attributes[$attribute] = $this->$method($value);
43
            }
44
        }
45
46
        if (! isset($attributes['name']) && isset($attributes['data'])) {
47
            $attributes['name'] = $attributes['data'];
48
        }
49
50
        parent::__construct($attributes);
51
    }
52
53
    /**
54
     * Format string to title case.
55
     *
56
     * @param string $value
57
     * @return string
58
     */
59
    public static function titleFormat($value)
60
    {
61
        return Str::title(str_replace('_', ' ', $value));
62
    }
63
64
    /**
65
     * Create a computed column that is not searchable/orderable.
66
     *
67
     * @param string $data
68
     * @param string|null $title
69
     * @return Column
70
     */
71
    public static function computed($data, $title = null)
72
    {
73
        if (is_null($title)) {
74
            $title = self::titleFormat($data);
75
        }
76
77
        return static::make($data)->title($title)->orderable(false)->searchable(false);
78
    }
79
80
    /**
81
     * Set column searchable flag.
82
     *
83
     * @param bool $flag
84
     * @return $this
85
     * @see https://datatables.net/reference/option/columns.searchable
86
     */
87
    public function searchable(bool $flag = true)
88
    {
89
        $this->attributes['searchable'] = $flag;
90
91
        return $this;
92
    }
93
94
    /**
95
     * Set column orderable flag.
96
     *
97
     * @param bool $flag
98
     * @return $this
99
     * @see https://datatables.net/reference/option/columns.orderable
100
     */
101
    public function orderable(bool $flag = true)
102
    {
103
        $this->attributes['orderable'] = $flag;
104
105
        return $this;
106
    }
107
108
     /**
109
     * Set column responsive priority.
110
     *
111
     * @param int|string $value
112
     * @return $this
113
     * @see https://datatables.net/reference/option/columns.responsivePriority
114
     */
115
    public function responsivePriority($value)
116
    {
117
        $this->attributes['responsivePriority'] = $value;
118
119
        return $this;
120
    }
121
122
    /**
123
     * Set column title.
124
     *
125
     * @param string $value
126
     * @return $this
127
     * @see https://datatables.net/reference/option/columns.title
128
     */
129
    public function title($value)
130
    {
131
        $this->attributes['title'] = $value;
132
133
        return $this;
134
    }
135
136
    /**
137
     * Make a new column instance.
138
     *
139
     * @param string $data
140
     * @param string $name
141
     * @return Column
142
     */
143
    public static function make($data, $name = '')
144
    {
145
        $attr = [
146
            'data' => $data,
147
            'name' => $name ?: $data,
148
        ];
149
150
        return new static($attr);
151
    }
152
153
    /**
154
     * Make a new formatted column instance.
155
     *
156
     * @param string $name
157
     * @return Column
158
     */
159
    public static function formatted($name)
160
    {
161
        $attr = [
162
            'data'  => $name . '_formatted',
163
            'name'  => $name,
164
            'title' => self::titleFormat($name),
165
        ];
166
167
        return new static($attr);
168
    }
169
170
    /**
171
     * Create a checkbox column.
172
     *
173
     * @param string $title
174
     * @return Column
175
     */
176
    public static function checkbox($title = '')
177
    {
178
        return static::make('')
179
                     ->content('')
180
                     ->title($title)
181
                     ->className('select-checkbox')
182
                     ->orderable(false)
183
                     ->searchable(false);
184
    }
185
186
    /**
187
     * Set column class name.
188
     *
189
     * @param string $class
190
     * @return $this
191
     * @see https://datatables.net/reference/option/columns.className
192
     */
193
    public function className($class)
194
    {
195
        $this->attributes['className'] = $class;
196
197
        return $this;
198
    }
199
200
    /**
201
     * Set column default content.
202
     *
203
     * @param string $value
204
     * @return $this
205
     * @see https://datatables.net/reference/option/columns.defaultContent
206
     */
207
    public function content($value)
208
    {
209
        $this->attributes['defaultContent'] = $value;
210
211
        return $this;
212
    }
213
214
    /**
215
     * Set column visible flag.
216
     *
217
     * @param bool $flag
218
     * @return $this
219
     * @see https://datatables.net/reference/option/columns.visible
220
     */
221
    public function visible(bool $flag = true)
222
    {
223
        $this->attributes['visible'] = $flag;
224
225
        return $this;
226
    }
227
228
    /**
229
     * Set column hidden state.
230
     *
231
     * @return $this
232
     * @see https://datatables.net/reference/option/columns.visible
233
     */
234
    public function hidden()
235
    {
236
        return $this->visible(false);
237
    }
238
239
    /**
240
     * Append a class name to field.
241
     *
242
     * @param string $class
243
     * @return $this
244
     */
245 View Code Duplication
    public function addClass($class)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
246
    {
247
        if (! isset($this->attributes['className'])) {
248
            $this->attributes['className'] = $class;
249
        } else {
250
            $this->attributes['className'] .= " $class";
251
        }
252
253
        return $this;
254
    }
255
256
    /**
257
     * Set column exportable flag.
258
     *
259
     * @param bool $flag
260
     * @return $this
261
     */
262
    public function exportable(bool $flag = true)
263
    {
264
        $this->attributes['exportable'] = $flag;
265
266
        return $this;
267
    }
268
269
    /**
270
     * Set column printable flag.
271
     *
272
     * @param bool $flag
273
     * @return $this
274
     */
275
    public function printable(bool $flag = true)
276
    {
277
        $this->attributes['printable'] = $flag;
278
279
        return $this;
280
    }
281
282
    /**
283
     * Set column width value.
284
     *
285
     * @param int|string $value
286
     * @return $this
287
     * @see https://datatables.net/reference/option/columns.width
288
     */
289
    public function width($value)
290
    {
291
        $this->attributes['width'] = $value;
292
293
        return $this;
294
    }
295
296
    /**
297
     * Set column data option value.
298
     *
299
     * @param string $value
300
     * @return $this
301
     * @see https://datatables.net/reference/option/columns.data
302
     */
303
    public function data($value)
304
    {
305
        $this->attributes['data'] = $value;
306
307
        return $this;
308
    }
309
310
    /**
311
     * Set column name option value.
312
     *
313
     * @param string $value
314
     * @return $this
315
     * @see https://datatables.net/reference/option/columns.name
316
     */
317
    public function name($value)
318
    {
319
        $this->attributes['name'] = $value;
320
321
        return $this;
322
    }
323
324
    /**
325
     * Set column edit field option value.
326
     *
327
     * @param string $value
328
     * @return $this
329
     * @see https://datatables.net/reference/option/columns.editField
330
     */
331
    public function editField($value)
332
    {
333
        $this->attributes['editField'] = $value;
334
335
        return $this;
336
    }
337
338
    /**
339
     * Set column orderData option value.
340
     *
341
     * @param mixed $value
342
     * @return $this
343
     * @see https://datatables.net/reference/option/columns.orderData
344
     */
345
    public function orderData($value)
346
    {
347
        $this->attributes['orderData'] = $value;
348
349
        return $this;
350
    }
351
352
    /**
353
     * Set column orderDataType option value.
354
     *
355
     * @param mixed $value
356
     * @return $this
357
     * @see https://datatables.net/reference/option/columns.orderDataType
358
     */
359
    public function orderDataType($value)
360
    {
361
        $this->attributes['orderDataType'] = $value;
362
363
        return $this;
364
    }
365
366
    /**
367
     * Set column orderSequence option value.
368
     *
369
     * @param mixed $value
370
     * @return $this
371
     * @see https://datatables.net/reference/option/columns.orderSequence
372
     */
373
    public function orderSequence($value)
374
    {
375
        $this->attributes['orderSequence'] = $value;
376
377
        return $this;
378
    }
379
380
    /**
381
     * Set column cellType option value.
382
     *
383
     * @param mixed $value
384
     * @return $this
385
     * @see https://datatables.net/reference/option/columns.cellType
386
     */
387
    public function cellType($value)
388
    {
389
        $this->attributes['cellType'] = $value;
390
391
        return $this;
392
    }
393
394
    /**
395
     * Set column type option value.
396
     *
397
     * @param mixed $value
398
     * @return $this
399
     * @see https://datatables.net/reference/option/columns.type
400
     */
401
    public function type($value)
402
    {
403
        $this->attributes['type'] = $value;
404
405
        return $this;
406
    }
407
408
    /**
409
     * Set column contentPadding option value.
410
     *
411
     * @param mixed $value
412
     * @return $this
413
     * @see https://datatables.net/reference/option/columns.contentPadding
414
     */
415
    public function contentPadding($value)
416
    {
417
        $this->attributes['contentPadding'] = $value;
418
419
        return $this;
420
    }
421
422
    /**
423
     * Set column createdCell option value.
424
     *
425
     * @param mixed $value
426
     * @return $this
427
     * @see https://datatables.net/reference/option/columns.createdCell
428
     */
429
    public function createdCell($value)
430
    {
431
        $this->attributes['createdCell'] = $value;
432
433
        return $this;
434
    }
435
436
    /**
437
     * Use the js renderer "$.fn.dataTable.render.".
438
     *
439
     * @param mixed $value
440
     * @param mixed ...$params
441
     * @return $this
442
     * @see https://datatables.net/reference/option/columns.render
443
     */
444
    public function renderJs($value, ...$params)
445
    {
446
        if ($params) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $params of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
447
            $value .= '(';
448
            foreach ($params as $param) {
449
                $value .= "'{$param}',";
450
            }
451
            $value = mb_substr($value, 0, -1);
452
            $value .= ')';
453
        }
454
455
        $renderer = '$.fn.dataTable.render.' . $value;
456
457
        return $this->render($renderer);
458
    }
459
460
    /**
461
     * Set column renderer.
462
     *
463
     * @param mixed $value
464
     * @return $this
465
     * @see https://datatables.net/reference/option/columns.render
466
     */
467
    public function render($value)
468
    {
469
        $this->attributes['render'] = $this->parseRender($value);
470
471
        return $this;
472
    }
473
474
    /**
475
     * Set column renderer with give raw value.
476
     *
477
     * @param mixed $value
478
     * @return $this
479
     * @see https://datatables.net/reference/option/columns.render
480
     */
481
    public function renderRaw($value)
482
    {
483
        $this->attributes['render'] = $value;
484
485
        return $this;
486
    }
487
488
    /**
489
     * Parse render attribute.
490
     *
491
     * @param mixed $value
492
     * @return string|null
493
     */
494
    public function parseRender($value)
495
    {
496
        /** @var \Illuminate\Contracts\View\Factory $view */
497
        $view       = app('view');
498
        $parameters = [];
499
500
        if (is_array($value)) {
501
            $parameters = Arr::except($value, 0);
502
            $value      = $value[0];
503
        }
504
505
        if (is_callable($value)) {
506
            return $value($parameters);
507
        } elseif ($this->isBuiltInRenderFunction($value)) {
508
            return $value;
509
        } elseif ($view->exists($value)) {
510
            return $view->make($value)->with($parameters)->render();
511
        }
512
513
        return $value ? $this->parseRenderAsString($value) : null;
514
    }
515
516
    /**
517
     * Check if given key & value is a valid datatables built-in renderer function.
518
     *
519
     * @param string $value
520
     * @return bool
521
     */
522
    private function isBuiltInRenderFunction($value)
523
    {
524
        if (empty($value)) {
525
            return false;
526
        }
527
528
        return Str::startsWith(trim($value), ['$.fn.dataTable.render', '[']);
529
    }
530
531
    /**
532
     * Display render value as is.
533
     *
534
     * @param mixed $value
535
     * @return string
536
     */
537
    private function parseRenderAsString($value)
538
    {
539
        return "function(data,type,full,meta){return $value;}";
540
    }
541
542
    /**
543
     * Set column footer.
544
     *
545
     * @param mixed $value
546
     * @return $this
547
     */
548
    public function footer($value)
549
    {
550
        $this->attributes['footer'] = $value;
551
552
        return $this;
553
    }
554
555
    /**
556
     * Set custom html title instead defult label.
557
     *
558
     * @param mixed $value
559
     * @return $this
560
     */
561
    public function titleAttr($value)
562
    {
563
        $this->attributes['titleAttr'] = $value;
564
565
        return $this;
566
    }
567
568
    /**
569
     * @return array
570
     */
571
    public function toArray()
572
    {
573
        return Arr::except($this->attributes, [
574
            'printable',
575
            'exportable',
576
            'footer',
577
        ]);
578
    }
579
}
580