Completed
Push — master ( 50be05...1c3c2e )
by Arjay
01:18
created

Column::visible()   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\Str;
6
use Illuminate\Support\Fluent;
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/ for possible columns option
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'] : Str::title(
27
            str_replace('_', ' ', $attributes['data'])
28
        );
29
30
        $attributes['orderable']  = isset($attributes['orderable']) ? $attributes['orderable'] : true;
31
        $attributes['searchable'] = isset($attributes['searchable']) ? $attributes['searchable'] : true;
32
        $attributes['exportable'] = isset($attributes['exportable']) ? $attributes['exportable'] : true;
33
        $attributes['printable']  = isset($attributes['printable']) ? $attributes['printable'] : true;
34
        $attributes['footer']     = isset($attributes['footer']) ? $attributes['footer'] : '';
35
        $attributes['attributes'] = isset($attributes['attributes']) ? $attributes['attributes'] : [];
36
37
        // Allow methods override attribute value
38
        foreach ($attributes as $attribute => $value) {
39
            $method = 'parse' . ucfirst(strtolower($attribute));
40
            if (method_exists($this, $method)) {
41
                $attributes[$attribute] = $this->$method($value);
42
            }
43
        }
44
45
        if (! isset($attributes['name']) && isset($attributes['data'])) {
46
            $attributes['name'] = $attributes['data'];
47
        }
48
49
        parent::__construct($attributes);
50
    }
51
52
    /**
53
     * Create a computed column that is not searchable/orderable.
54
     *
55
     * @param string $data
56
     * @param string $title
57
     * @return Column
58
     */
59
    public static function computed($data, $title = '')
60
    {
61
        return static::make($data)->title($title)->orderable(false)->searchable(false);
62
    }
63
64
    /**
65
     * Set column searchable flag.
66
     *
67
     * @param bool $flag
68
     * @return $this
69
     */
70
    public function searchable(bool $flag = true)
71
    {
72
        $this->attributes['searchable'] = $flag;
73
74
        return $this;
75
    }
76
77
    /**
78
     * Set column orderable flag.
79
     *
80
     * @param bool $flag
81
     * @return $this
82
     */
83
    public function orderable(bool $flag = true)
84
    {
85
        $this->attributes['orderable'] = $flag;
86
87
        return $this;
88
    }
89
90
    /**
91
     * Set column visible flag.
92
     *
93
     * @param bool $flag
94
     * @return $this
95
     */
96
    public function visible(bool $flag = true)
97
    {
98
        $this->attributes['visible'] = $flag;
99
100
        return $this;
101
    }
102
103
    /**
104
     * Make a new column instance.
105
     *
106
     * @param string $data
107
     * @param string $name
108
     * @return Column
109
     */
110
    public static function make($data, $name = '')
111
    {
112
        $attr = [
113
            'data' => $data,
114
            'name' => $name ?: $data,
115
        ];
116
117
        return new static($attr);
118
    }
119
120
    /**
121
     * Create a checkbox column.
122
     *
123
     * @param string $title
124
     * @return Column
125
     */
126
    public static function checkbox($title = '')
127
    {
128
        return static::make('')
129
                     ->content('')
130
                    ->title($title)
131
                     ->className('select-checkbox')
132
                     ->orderable(false)
133
                     ->searchable(false);
134
    }
135
136
    /**
137
     * Set column class name.
138
     *
139
     * @param string $class
140
     * @return $this
141
     */
142
    public function className($class)
143
    {
144
        $this->attributes['className'] = $class;
145
146
        return $this;
147
    }
148
149
    /**
150
     * Append a class name to field.
151
     *
152
     * @param string $class
153
     * @return $this
154
     */
155
    public function addClass($class)
156
    {
157
        if (! isset($this->attributes['className'])) {
158
            $this->attributes['className'] = $class;
159
        } else {
160
            $this->attributes['className'] .= " $class";
161
        }
162
163
        return $this;
164
    }
165
166
    /**
167
     * Set column default content.
168
     *
169
     * @param string $value
170
     * @return $this
171
     */
172
    public function content($value)
173
    {
174
        $this->attributes['defaultContent'] = $value;
175
176
        return $this;
177
    }
178
179
    /**
180
     * Set column exportable flag.
181
     *
182
     * @param bool $flag
183
     * @return $this
184
     */
185
    public function exportable(bool $flag = true)
186
    {
187
        $this->attributes['exportable'] = $flag;
188
189
        return $this;
190
    }
191
192
    /**
193
     * Set column printable flag.
194
     *
195
     * @param bool $flag
196
     * @return $this
197
     */
198
    public function printable(bool $flag = true)
199
    {
200
        $this->attributes['printable'] = $flag;
201
202
        return $this;
203
    }
204
205
    /**
206
     * Set column width value.
207
     *
208
     * @param int|string $value
209
     * @return $this
210
     */
211
    public function width($value)
212
    {
213
        $this->attributes['width'] = $value;
214
215
        return $this;
216
    }
217
218
    /**
219
     * Set column title.
220
     *
221
     * @param string $value
222
     * @return $this
223
     */
224
    public function title($value)
225
    {
226
        $this->attributes['title'] = $value;
227
228
        return $this;
229
    }
230
231
    /**
232
     * Set column name.
233
     *
234
     * @param string $value
235
     * @return $this
236
     */
237
    public function name($value)
238
    {
239
        $this->attributes['name'] = $value;
240
241
        return $this;
242
    }
243
244
    /**
245
     * Set column renderer.
246
     *
247
     * @param mixed $value
248
     * @return $this
249
     */
250
    public function render($value)
251
    {
252
        $this->attributes['render'] = $value;
253
254
        return $this;
255
    }
256
257
    /**
258
     * Parse render attribute.
259
     *
260
     * @param mixed $value
261
     * @return string|null
262
     */
263
    public function parseRender($value)
264
    {
265
        /** @var \Illuminate\Contracts\View\Factory $view */
266
        $view       = app('view');
267
        $parameters = [];
268
269
        if (is_array($value)) {
270
            $parameters = array_except($value, 0);
271
            $value      = $value[0];
272
        }
273
274
        if (is_callable($value)) {
275
            return $value($parameters);
276
        } elseif ($this->isBuiltInRenderFunction($value)) {
277
            return $value;
278
        } elseif ($view->exists($value)) {
279
            return $view->make($value)->with($parameters)->render();
280
        }
281
282
        return $value ? $this->parseRenderAsString($value) : null;
283
    }
284
285
    /**
286
     * Check if given key & value is a valid datatables built-in renderer function.
287
     *
288
     * @param string $value
289
     * @return bool
290
     */
291
    private function isBuiltInRenderFunction($value)
292
    {
293
        if (empty($value)) {
294
            return false;
295
        }
296
297
        return Str::startsWith(trim($value), ['$.fn.dataTable.render']);
298
    }
299
300
    /**
301
     * Display render value as is.
302
     *
303
     * @param mixed $value
304
     * @return string
305
     */
306
    private function parseRenderAsString($value)
307
    {
308
        return "function(data,type,full,meta){return $value;}";
309
    }
310
311
    /**
312
     * @return array
313
     */
314
    public function toArray()
315
    {
316
        return array_except($this->attributes, ['printable', 'exportable', 'footer']);
317
    }
318
}
319