Completed
Push — master ( acb427...0d7e58 )
by Arjay
08:04
created

Column::__construct()   D

Complexity

Conditions 11
Paths 384

Size

Total Lines 23
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 23
rs 4.0344
c 0
b 0
f 0
cc 11
eloc 14
nc 384
nop 1

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
        if (! isset($attributes['name']) && isset($attributes['data'])) {
41
            $attributes['name'] = $attributes['data'];
42
        }
43
44
        parent::__construct($attributes);
45
    }
46
47
    /**
48
     * Parse render attribute.
49
     *
50
     * @param mixed $value
51
     * @return string|null
52
     */
53
    public function parseRender($value)
54
    {
55
        /** @var \Illuminate\Contracts\View\Factory $view */
56
        $view       = app('view');
57
        $parameters = [];
58
59
        if (is_array($value)) {
60
            $parameters = array_except($value, 0);
61
            $value      = $value[0];
62
        }
63
64
        if (is_callable($value)) {
65
            return $value($parameters);
66
        } elseif ($view->exists($value)) {
67
            return $view->make($value)->with($parameters)->render();
68
        }
69
70
        return $value ? $this->parseRenderAsString($value) : null;
71
    }
72
73
    /**
74
     * @return array
75
     */
76
    public function toArray()
77
    {
78
        return array_except($this->attributes, ['printable', 'exportable', 'footer']);
79
    }
80
81
    /**
82
     * Display render value as is.
83
     *
84
     * @param mixed $value
85
     * @return string
86
     */
87
    private function parseRenderAsString($value)
88
    {
89
        return "function(data,type,full,meta){return $value;}";
90
    }
91
}
92