Completed
Push — master ( 8b59f7...62ee86 )
by Arjay
01:32
created

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