Completed
Push — master ( fe0acb...db8d28 )
by wen
11:03
created

Column   A

Complexity

Total Complexity 30

Size/Duplication

Total Lines 247
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 247
c 0
b 0
f 0
wmc 30
lcom 1
cbo 1
rs 10

25 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getName() 0 4 1
A setName() 0 5 1
A getLabel() 0 4 1
A setLabel() 0 5 1
A getWidth() 0 4 1
A setWidth() 0 5 1
A getMinWidth() 0 4 1
A setMinWidth() 0 5 1
A sortable() 0 6 1
A customSortable() 0 8 1
A enableFixed() 0 6 1
A getModel() 0 4 1
A setModel() 0 6 1
A getTemplate() 0 4 1
A setTemplate() 0 5 1
A toArray() 0 12 1
A setDefaultValue() 0 6 1
A getDefaultValue() 0 4 1
A getValue() 0 8 2
A getModelValue() 0 4 1
B getValueFromObject() 0 17 5
A jsonSerialize() 0 4 1
A toJson() 0 4 1
A __toString() 0 4 1
1
<?php
2
3
4
namespace Sco\Admin\View\Columns;
5
6
use Illuminate\Database\Eloquent\Model;
7
use Illuminate\Support\Collection;
8
use Sco\Admin\Contracts\View\ColumnInterface;
9
10
abstract class Column implements ColumnInterface
11
{
12
    protected $name;
13
14
    protected $label;
15
16
    protected $width = 0;
17
18
    protected $minWidth = 80;
19
20
    protected $sortable = false;
21
22
    protected $fixed = false;
23
24
    /**
25
     * @var Model
26
     */
27
    protected $model;
28
29
    protected $defaultValue = '';
30
31
    protected $template = '<span>{{value}}</span>';
32
33
    public function __construct($name, $label)
34
    {
35
        $this->setName($name)->setLabel($label);
36
    }
37
38
    /**
39
     * @return string
40
     */
41
    public function getName()
42
    {
43
        return $this->name;
44
    }
45
46
    /**
47
     * @param string $name
48
     *
49
     * @return Column
50
     */
51
    public function setName($name)
52
    {
53
        $this->name = $name;
54
        return $this;
55
    }
56
57
    /**
58
     * @return string
59
     */
60
    public function getLabel()
61
    {
62
        return $this->label;
63
    }
64
65
    /**
66
     * @param string $label
67
     *
68
     * @return Column
69
     */
70
    public function setLabel(string $label)
71
    {
72
        $this->label = $label;
73
        return $this;
74
    }
75
76
    /**
77
     * @return int
78
     */
79
    public function getWidth()
80
    {
81
        return $this->width;
82
    }
83
84
    /**
85
     * @param int $width
86
     *
87
     * @return Column
88
     */
89
    public function setWidth(int $width)
90
    {
91
        $this->width = $width;
92
        return $this;
93
    }
94
95
    /**
96
     * @return int
97
     */
98
    public function getMinWidth()
99
    {
100
        return $this->minWidth;
101
    }
102
103
    /**
104
     * @param int $minWidth
105
     *
106
     * @return Column
107
     */
108
    public function setMinWidth(int $minWidth)
109
    {
110
        $this->minWidth = $minWidth;
111
        return $this;
112
    }
113
114
    public function sortable()
115
    {
116
        $this->sortable = true;
117
118
        return $this;
119
    }
120
121
    public function customSortable()
122
    {
123
        $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...
124
        // TODO
125
        // register sort route
126
127
        return $this;
128
    }
129
130
    public function enableFixed()
131
    {
132
        $this->fixed = true;
133
134
        return $this;
135
    }
136
137
    public function getModel()
138
    {
139
        return $this->model;
140
    }
141
142
    public function setModel(Model $model)
143
    {
144
        $this->model = $model;
145
146
        return $this;
147
    }
148
149
    /**
150
     * @return string
151
     */
152
    public function getTemplate()
153
    {
154
        return $this->template;
155
    }
156
157
    /**
158
     * @param string $template
159
     *
160
     * @return Column
161
     */
162
    public function setTemplate(string $template)
163
    {
164
        $this->template = $template;
165
        return $this;
166
    }
167
168
    /**
169
     * The column options
170
     *
171
     * @return array
172
     */
173
    public function toArray()
174
    {
175
        return [
176
            'name'     => $this->getName(),
177
            'label'    => $this->getLabel(),
178
            'width'    => $this->getWidth(),
179
            'fixed'    => $this->fixed,
180
            'minWidth' => $this->getMinWidth(),
181
            'sortable' => $this->sortable,
182
            'template' => $this->getTemplate(),
183
        ];
184
    }
185
186
    /**
187
     * Set the column default value
188
     *
189
     * @param mixed $value
190
     *
191
     * @return $this
192
     */
193
    public function setDefaultValue($value)
194
    {
195
        $this->defaultValue = $value;
196
197
        return $this;
198
    }
199
200
    public function getDefaultValue()
201
    {
202
        return $this->defaultValue;
203
    }
204
205
    /**
206
     * Get the column value
207
     *
208
     * @return string|static
209
     */
210
    public function getValue()
211
    {
212
        $value = $this->getModelValue();
213
        if (is_null($value)) {
214
            $value = $this->getDefaultValue();
215
        }
216
        return $value;
217
    }
218
219
    protected function getModelValue()
220
    {
221
        return $this->getValueFromObject($this->getModel(), $this->getName());
222
    }
223
224
    protected function getValueFromObject($instance, $name)
225
    {
226
        $parts = explode('.', $name);
227
        $part = array_shift($parts);
228
229
        if ($instance instanceof Collection) {
230
            $instance = $instance->pluck($part);
231
        } elseif (!is_null($instance)) {
232
            $instance = $instance->getAttribute($part);
233
        }
234
235
        if (!empty($parts) && !is_null($instance)) {
236
            return $this->getValueFromObject($instance, implode('.', $parts));
237
        }
238
239
        return $instance;
240
    }
241
242
    public function jsonSerialize()
243
    {
244
        return $this->toArray();
245
    }
246
247
    public function toJson($options = 0)
248
    {
249
        return json_encode($this->jsonSerialize(), $options);
250
    }
251
252
    public function __toString()
253
    {
254
        return $this->toJson();
255
    }
256
}
257