Completed
Push — master ( 85c58c...2c5e88 )
by wen
10:27
created

Column::enableSortable()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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