Completed
Push — master ( 875706...2db17d )
by Arjay
06:52
created

Column::parseRenderAsString()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace Yajra\DataTables\Html;
4
5
use Illuminate\Support\Fluent;
6
7
/**
8
 * @property string data
9
 * @property string name
10
 * @property string orderable
11
 * @property string searchable
12
 * @property string printable
13
 * @property string exportable
14
 * @property string footer
15
 * @property array  attributes
16
 * @see     https://datatables.net/reference/option/ for possible columns option
17
 */
18
class Column extends Fluent
19
{
20
    /**
21
     * @param array $attributes
22
     */
23
    public function __construct($attributes = [])
24
    {
25
        $attributes['orderable']  = isset($attributes['orderable']) ? $attributes['orderable'] : true;
26
        $attributes['searchable'] = isset($attributes['searchable']) ? $attributes['searchable'] : true;
27
        $attributes['exportable'] = isset($attributes['exportable']) ? $attributes['exportable'] : true;
28
        $attributes['printable']  = isset($attributes['printable']) ? $attributes['printable'] : true;
29
        $attributes['footer']     = isset($attributes['footer']) ? $attributes['footer'] : '';
30
        $attributes['attributes'] = isset($attributes['attributes']) ? $attributes['attributes'] : [];
31
32
        // Allow methods override attribute value
33
        foreach ($attributes as $attribute => $value) {
34
            $method = 'parse' . ucfirst(strtolower($attribute));
35
            if (method_exists($this, $method)) {
36
                $attributes[$attribute] = $this->$method($value);
37
            }
38
        }
39
40
        parent::__construct($attributes);
41
    }
42
43
    /**
44
     * Parse render attribute.
45
     *
46
     * @param mixed $value
47
     * @return string|null
48
     */
49
    public function parseRender($value)
50
    {
51
        /** @var \Illuminate\Contracts\View\Factory $view */
52
        $view       = app('view');
53
        $parameters = [];
54
55
        if (is_array($value)) {
56
            $parameters = array_except($value, 0);
57
            $value      = $value[0];
58
        }
59
60
        if (is_callable($value)) {
61
            return $value($parameters);
62
        } elseif ($view->exists($value)) {
63
            return $view->make($value)->with($parameters)->render();
64
        }
65
66
        return $value ? $this->parseRenderAsString($value) : null;
67
    }
68
69
    /**
70
     * Display render value as is.
71
     *
72
     * @param mixed $value
73
     * @return string
74
     */
75
    private function parseRenderAsString($value)
76
    {
77
        return "function(data,type,full,meta){return $value;}";
78
    }
79
80
    /**
81
     * @return array
82
     */
83
    public function toArray()
84
    {
85
        return array_except($this->attributes, ['printable', 'exportable', 'footer']);
86
    }
87
}
88