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