Completed
Push — master ( 1504b8...22bc8c )
by Arjay
02:35
created

Column::parseRender()   B

Complexity

Conditions 5
Paths 8

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

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