Completed
Push — master ( 7e02e6...ea0e93 )
by wen
03:01
created

Column::getValueFromObject()   B

Complexity

Conditions 5
Paths 6

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 18
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 10
nc 6
nop 2
1
<?php
2
3
4
namespace Sco\Admin\View\Columns;
5
6
use Illuminate\Database\Eloquent\Model;
7
use Illuminate\Support\Collection;
8
use JsonSerializable;
9
use Illuminate\Contracts\Support\Arrayable;
10
use Illuminate\Contracts\Support\Jsonable;
11
use Sco\Admin\Contracts\ColumnInterface;
12
13
abstract class Column implements ColumnInterface, Arrayable, Jsonable, JsonSerializable
14
{
15
    protected $name;
16
17
    protected $label;
18
19
    protected $width = 0;
20
21
    protected $minWidth = 80;
22
23
    protected $sortable = false;
24
25
    protected $fixed = false;
26
27
    protected $model;
28
29
    protected $template = '<span>{{value}}</span>';
30
31
    public function __construct($name, $label)
32
    {
33
        $this->name  = $name;
34
        $this->label = $label;
35
    }
36
37
    public function getName()
38
    {
39
        return $this->name;
40
    }
41
42
    public function setWidth($width)
43
    {
44
        $this->width = $width;
45
46
        return $this;
47
    }
48
49
    public function setMinWidth($width)
50
    {
51
        $this->minWidth = $width;
52
53
        return $this;
54
    }
55
56
    public function isSortable()
57
    {
58
        $this->sortable = true;
59
60
        return $this;
61
    }
62
63
    public function isCustomSortable()
64
    {
65
        $this->sortable = 'custom';
0 ignored issues
show
Documentation Bug introduced by
The property $sortable was declared of type boolean, but 'custom' is of type string. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
66
        // TODO
67
        // register sort route
68
69
        return $this;
70
    }
71
72
    public function isFixed()
73
    {
74
        $this->fixed = true;
75
76
        return $this;
77
    }
78
79
    public function getModel()
80
    {
81
        return $this->model;
82
    }
83
84
    public function setModel(Model $model)
85
    {
86
        $this->model = $model;
87
        return $this;
88
    }
89
90
    public function getTemplate()
91
    {
92
        return $this->template;
93
    }
94
95
    public function setTemplate($template)
96
    {
97
        $this->template = $template;
98
    }
99
100
    public function toArray()
101
    {
102
        return [
103
            'name'     => $this->getName(),
104
            'label'    => $this->label,
105
            'width'    => $this->width,
106
            'fixed'    => $this->fixed,
107
            'minWidth' => $this->minWidth,
108
            'sortable' => $this->sortable,
109
            'template' => $this->getTemplate(),
110
        ];
111
    }
112
113
    public function getModelValue()
114
    {
115
        return $this->getValueFromObject($this->getModel(), $this->getName());
116
    }
117
118
    protected function getValueFromObject($instance, $name)
119
    {
120
121
        $parts = explode('.', $name);
122
        $part = array_shift($parts);
123
124
        if ($instance instanceof Collection) {
125
            $instance = $instance->pluck($part);
126
        } elseif (!is_null($instance)) {
127
            $instance = $instance->getAttribute($part);
128
        }
129
130
        if (!empty($parts) && !is_null($instance)) {
131
            return $this->getValueFromObject($instance, implode('.', $parts));
132
        }
133
134
        return $instance;
135
    }
136
137
    public function jsonSerialize()
138
    {
139
        return $this->toArray();
140
    }
141
142
    public function toJson($options = 0)
143
    {
144
        return json_encode($this->jsonSerialize(), $options);
145
    }
146
147
    public function __toString()
148
    {
149
        return $this->toJson();
150
    }
151
}
152