Completed
Push — master ( 2c39c7...3c2c2f )
by Arjay
01:14
created

Column   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 16
lcom 1
cbo 3
dl 0
loc 70
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 19 9
B parseRender() 0 19 5
A parseRenderAsString() 0 4 1
A toArray() 0 4 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
        $attributes['footer']     = isset($attributes['footer']) ? $attributes['footer'] : '';
26
        $attributes['attributes'] = isset($attributes['attributes']) ? $attributes['attributes'] : [];
27
28
        // Allow methods override attribute value
29
        foreach ($attributes as $attribute => $value) {
30
            $method = 'parse' . ucfirst(strtolower($attribute));
31
            if (method_exists($this, $method)) {
32
                $attributes[$attribute] = $this->$method($value);
33
            }
34
        }
35
36
        parent::__construct($attributes);
37
    }
38
39
    /**
40
     * Parse render attribute.
41
     *
42
     * @param mixed $value
43
     * @return string|null
44
     */
45
    public function parseRender($value)
46
    {
47
        /** @var \Illuminate\Contracts\View\Factory $view */
48
        $view       = app('view');
49
        $parameters = [];
50
51
        if (is_array($value)) {
52
            $parameters = array_except($value, 0);
53
            $value      = $value[0];
54
        }
55
56
        if (is_callable($value)) {
57
            return $value($parameters);
58
        } elseif ($view->exists($value)) {
59
            return $view->make($value)->with($parameters)->render();
60
        }
61
62
        return $value ? $this->parseRenderAsString($value) : null;
63
    }
64
65
    /**
66
     * Display render value as is.
67
     *
68
     * @param mixed $value
69
     * @return string
70
     */
71
    private function parseRenderAsString($value)
72
    {
73
        return "function(data,type,full,meta){return $value;}";
74
    }
75
76
    /**
77
     * @return array
78
     */
79
    public function toArray()
80
    {
81
        return array_except($this->attributes, ['printable', 'exportable', 'footer']);
82
    }
83
}
84