Completed
Push — master ( f0abe2...de1426 )
by Arjay
06:14 queued 12s
created

Column::hidden()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
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
9
/**
10
 * @property string data
11
 * @property string name
12
 * @property string orderable
13
 * @property string searchable
14
 * @property string printable
15
 * @property string exportable
16
 * @property string footer
17
 * @property array attributes
18
 * @see https://datatables.net/reference/option/#columns
19
 */
20
class Column extends Fluent
21
{
22
    /**
23
     * @param array $attributes
24
     */
25
    public function __construct($attributes = [])
26
    {
27
        $attributes['title']      = isset($attributes['title']) ? $attributes['title'] : self::titleFormat($attributes['data']);
28
        $attributes['orderable']  = isset($attributes['orderable']) ? $attributes['orderable'] : true;
29
        $attributes['searchable'] = isset($attributes['searchable']) ? $attributes['searchable'] : true;
30
        $attributes['exportable'] = isset($attributes['exportable']) ? $attributes['exportable'] : true;
31
        $attributes['printable']  = isset($attributes['printable']) ? $attributes['printable'] : true;
32
        $attributes['footer']     = isset($attributes['footer']) ? $attributes['footer'] : '';
33
        $attributes['attributes'] = isset($attributes['attributes']) ? $attributes['attributes'] : [];
34
35
        // Allow methods override attribute value
36
        foreach ($attributes as $attribute => $value) {
37
            $method = 'parse' . ucfirst(strtolower($attribute));
38
            if (! is_null($value) && method_exists($this, $method)) {
39
                $attributes[$attribute] = $this->$method($value);
40
            }
41
        }
42
43
        if (! isset($attributes['name']) && isset($attributes['data'])) {
44
            $attributes['name'] = $attributes['data'];
45
        }
46
47
        parent::__construct($attributes);
48
    }
49
50
    /**
51
     * Format string to title case.
52
     *
53
     * @param string $value
54
     * @return string
55
     */
56
    public static function titleFormat($value)
57
    {
58
        return Str::title(str_replace('_', ' ', $value));
59
    }
60
61
    /**
62
     * Create a computed column that is not searchable/orderable.
63
     *
64
     * @param string $data
65
     * @param string|null $title
66
     * @return Column
67
     */
68
    public static function computed($data, $title = null)
69
    {
70
        if (is_null($title)) {
71
            $title = self::titleFormat($data);
72
        }
73
74
        return static::make($data)->title($title)->orderable(false)->searchable(false);
75
    }
76
77
    /**
78
     * Set column searchable flag.
79
     *
80
     * @param bool $flag
81
     * @return $this
82
     * @see https://datatables.net/reference/option/columns.searchable
83
     */
84
    public function searchable(bool $flag = true)
85
    {
86
        $this->attributes['searchable'] = $flag;
87
88
        return $this;
89
    }
90
91
    /**
92
     * Set column orderable flag.
93
     *
94
     * @param bool $flag
95
     * @return $this
96
     * @see https://datatables.net/reference/option/columns.orderable
97
     */
98
    public function orderable(bool $flag = true)
99
    {
100
        $this->attributes['orderable'] = $flag;
101
102
        return $this;
103
    }
104
105
    /**
106
     * Set column title.
107
     *
108
     * @param string $value
109
     * @return $this
110
     * @see https://datatables.net/reference/option/columns.title
111
     */
112
    public function title($value)
113
    {
114
        $this->attributes['title'] = $value;
115
116
        return $this;
117
    }
118
119
    /**
120
     * Make a new column instance.
121
     *
122
     * @param string $data
123
     * @param string $name
124
     * @return Column
125
     */
126
    public static function make($data, $name = '')
127
    {
128
        $attr = [
129
            'data' => $data,
130
            'name' => $name ?: $data,
131
        ];
132
133
        return new static($attr);
134
    }
135
136
    /**
137
     * Create a checkbox column.
138
     *
139
     * @param string $title
140
     * @return Column
141
     */
142
    public static function checkbox($title = '')
143
    {
144
        return static::make('')
145
                     ->content('')
146
                     ->title($title)
147
                     ->className('select-checkbox')
148
                     ->orderable(false)
149
                     ->searchable(false);
150
    }
151
152
    /**
153
     * Set column class name.
154
     *
155
     * @param string $class
156
     * @return $this
157
     * @see https://datatables.net/reference/option/columns.className
158
     */
159
    public function className($class)
160
    {
161
        $this->attributes['className'] = $class;
162
163
        return $this;
164
    }
165
166
    /**
167
     * Set column default content.
168
     *
169
     * @param string $value
170
     * @return $this
171
     * @see https://datatables.net/reference/option/columns.defaultContent
172
     */
173
    public function content($value)
174
    {
175
        $this->attributes['defaultContent'] = $value;
176
177
        return $this;
178
    }
179
180
    /**
181
     * Set column visible flag.
182
     *
183
     * @param bool $flag
184
     * @return $this
185
     * @see https://datatables.net/reference/option/columns.visible
186
     */
187
    public function visible(bool $flag = true)
188
    {
189
        $this->attributes['visible'] = $flag;
190
191
        return $this;
192
    }
193
194
    /**
195
     * Set column hidden state.
196
     *
197
     * @return $this
198
     * @see https://datatables.net/reference/option/columns.visible
199
     */
200
    public function hidden()
201
    {
202
        return $this->visible(false);
203
    }
204
205
    /**
206
     * Append a class name to field.
207
     *
208
     * @param string $class
209
     * @return $this
210
     */
211
    public function addClass($class)
212
    {
213
        if (! isset($this->attributes['className'])) {
214
            $this->attributes['className'] = $class;
215
        } else {
216
            $this->attributes['className'] .= " $class";
217
        }
218
219
        return $this;
220
    }
221
222
    /**
223
     * Set column exportable flag.
224
     *
225
     * @param bool $flag
226
     * @return $this
227
     */
228
    public function exportable(bool $flag = true)
229
    {
230
        $this->attributes['exportable'] = $flag;
231
232
        return $this;
233
    }
234
235
    /**
236
     * Set column printable flag.
237
     *
238
     * @param bool $flag
239
     * @return $this
240
     */
241
    public function printable(bool $flag = true)
242
    {
243
        $this->attributes['printable'] = $flag;
244
245
        return $this;
246
    }
247
248
    /**
249
     * Set column width value.
250
     *
251
     * @param int|string $value
252
     * @return $this
253
     * @see https://datatables.net/reference/option/columns.width
254
     */
255
    public function width($value)
256
    {
257
        $this->attributes['width'] = $value;
258
259
        return $this;
260
    }
261
262
    /**
263
     * Set column data option value.
264
     *
265
     * @param string $value
266
     * @return $this
267
     * @see https://datatables.net/reference/option/columns.data
268
     */
269
    public function data($value)
270
    {
271
        $this->attributes['data'] = $value;
272
273
        return $this;
274
    }
275
276
    /**
277
     * Set column name option value.
278
     *
279
     * @param string $value
280
     * @return $this
281
     * @see https://datatables.net/reference/option/columns.name
282
     */
283
    public function name($value)
284
    {
285
        $this->attributes['name'] = $value;
286
287
        return $this;
288
    }
289
290
    /**
291
     * Set column edit field option value.
292
     *
293
     * @param string $value
294
     * @return $this
295
     * @see https://datatables.net/reference/option/columns.editField
296
     */
297
    public function editField($value)
298
    {
299
        $this->attributes['editField'] = $value;
300
301
        return $this;
302
    }
303
304
    /**
305
     * Set column orderData option value.
306
     *
307
     * @param mixed $value
308
     * @return $this
309
     * @see https://datatables.net/reference/option/columns.orderData
310
     */
311
    public function orderData($value)
312
    {
313
        $this->attributes['orderData'] = $value;
314
315
        return $this;
316
    }
317
318
    /**
319
     * Set column orderDataType option value.
320
     *
321
     * @param mixed $value
322
     * @return $this
323
     * @see https://datatables.net/reference/option/columns.orderDataType
324
     */
325
    public function orderDataType($value)
326
    {
327
        $this->attributes['orderDataType'] = $value;
328
329
        return $this;
330
    }
331
332
    /**
333
     * Set column orderSequence option value.
334
     *
335
     * @param mixed $value
336
     * @return $this
337
     * @see https://datatables.net/reference/option/columns.orderSequence
338
     */
339
    public function orderSequence($value)
340
    {
341
        $this->attributes['orderSequence'] = $value;
342
343
        return $this;
344
    }
345
346
    /**
347
     * Set column cellType option value.
348
     *
349
     * @param mixed $value
350
     * @return $this
351
     * @see https://datatables.net/reference/option/columns.cellType
352
     */
353
    public function cellType($value)
354
    {
355
        $this->attributes['cellType'] = $value;
356
357
        return $this;
358
    }
359
360
    /**
361
     * Set column type option value.
362
     *
363
     * @param mixed $value
364
     * @return $this
365
     * @see https://datatables.net/reference/option/columns.type
366
     */
367
    public function type($value)
368
    {
369
        $this->attributes['type'] = $value;
370
371
        return $this;
372
    }
373
374
    /**
375
     * Set column contentPadding option value.
376
     *
377
     * @param mixed $value
378
     * @return $this
379
     * @see https://datatables.net/reference/option/columns.contentPadding
380
     */
381
    public function contentPadding($value)
382
    {
383
        $this->attributes['contentPadding'] = $value;
384
385
        return $this;
386
    }
387
388
    /**
389
     * Set column createdCell option value.
390
     *
391
     * @param mixed $value
392
     * @return $this
393
     * @see https://datatables.net/reference/option/columns.createdCell
394
     */
395
    public function createdCell($value)
396
    {
397
        $this->attributes['createdCell'] = $value;
398
399
        return $this;
400
    }
401
402
    /**
403
     * Use the js renderer "$.fn.dataTable.render.".
404
     *
405
     * @param mixed $value
406
     * @param mixed ...$params
407
     * @return $this
408
     * @see https://datatables.net/reference/option/columns.render
409
     */
410
    public function renderJs($value, ...$params)
411
    {
412
        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...
413
            $value .= '(';
414
            foreach ($params as $param) {
415
                $value .= "'{$param}',";
416
            }
417
            $value = mb_substr($value, 0, -1);
418
            $value .= ')';
419
        }
420
421
        $renderer = '$.fn.dataTable.render.' . $value;
422
423
        return $this->render($renderer);
424
    }
425
426
    /**
427
     * Set column renderer.
428
     *
429
     * @param mixed $value
430
     * @return $this
431
     * @see https://datatables.net/reference/option/columns.render
432
     */
433
    public function render($value)
434
    {
435
        $this->attributes['render'] = $this->parseRender($value);
436
437
        return $this;
438
    }
439
440
    /**
441
     * Parse render attribute.
442
     *
443
     * @param mixed $value
444
     * @return string|null
445
     */
446
    public function parseRender($value)
447
    {
448
        /** @var \Illuminate\Contracts\View\Factory $view */
449
        $view       = app('view');
450
        $parameters = [];
451
452
        if (is_array($value)) {
453
            $parameters = Arr::except($value, 0);
454
            $value      = $value[0];
455
        }
456
457
        if (is_callable($value)) {
458
            return $value($parameters);
459
        } elseif ($this->isBuiltInRenderFunction($value)) {
460
            return $value;
461
        } elseif ($view->exists($value)) {
462
            return $view->make($value)->with($parameters)->render();
463
        }
464
465
        return $value ? $this->parseRenderAsString($value) : null;
466
    }
467
468
    /**
469
     * Check if given key & value is a valid datatables built-in renderer function.
470
     *
471
     * @param string $value
472
     * @return bool
473
     */
474
    private function isBuiltInRenderFunction($value)
475
    {
476
        if (empty($value)) {
477
            return false;
478
        }
479
480
        return Str::startsWith(trim($value), ['$.fn.dataTable.render']);
481
    }
482
483
    /**
484
     * Display render value as is.
485
     *
486
     * @param mixed $value
487
     * @return string
488
     */
489
    private function parseRenderAsString($value)
490
    {
491
        return "function(data,type,full,meta){return $value;}";
492
    }
493
494
    /**
495
     * Set column footer.
496
     *
497
     * @param mixed $value
498
     * @return $this
499
     */
500
    public function footer($value)
501
    {
502
        $this->attributes['footer'] = $value;
503
504
        return $this;
505
    }
506
507
    /**
508
     * @return array
509
     */
510
    public function toArray()
511
    {
512
        return Arr::except($this->attributes, [
513
            'printable',
514
            'exportable',
515
            'footer',
516
        ]);
517
    }
518
}
519