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'; |
|
|
|
|
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
|
|
|
|
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.