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->name = $name; |
36
|
|
|
$this->label = $label; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function getName() |
40
|
|
|
{ |
41
|
|
|
return $this->name; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function setWidth($width) |
45
|
|
|
{ |
46
|
|
|
$this->width = $width; |
47
|
|
|
|
48
|
|
|
return $this; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function setMinWidth($width) |
52
|
|
|
{ |
53
|
|
|
$this->minWidth = $width; |
54
|
|
|
|
55
|
|
|
return $this; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function isSortable() |
59
|
|
|
{ |
60
|
|
|
$this->sortable = true; |
61
|
|
|
|
62
|
|
|
return $this; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function isCustomSortable() |
66
|
|
|
{ |
67
|
|
|
$this->sortable = 'custom'; |
|
|
|
|
68
|
|
|
// TODO |
69
|
|
|
// register sort route |
70
|
|
|
|
71
|
|
|
return $this; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function isFixed() |
75
|
|
|
{ |
76
|
|
|
$this->fixed = true; |
77
|
|
|
|
78
|
|
|
return $this; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public function getModel() |
82
|
|
|
{ |
83
|
|
|
return $this->model; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public function setModel(Model $model) |
87
|
|
|
{ |
88
|
|
|
$this->model = $model; |
89
|
|
|
|
90
|
|
|
return $this; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
public function getTemplate() |
94
|
|
|
{ |
95
|
|
|
return $this->template; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
public function setTemplate($template) |
99
|
|
|
{ |
100
|
|
|
$this->template = $template; |
101
|
|
|
|
102
|
|
|
return $this; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
public function toArray() |
106
|
|
|
{ |
107
|
|
|
return [ |
108
|
|
|
'name' => $this->getName(), |
109
|
|
|
'label' => $this->label, |
110
|
|
|
'width' => $this->width, |
111
|
|
|
'fixed' => $this->fixed, |
112
|
|
|
'minWidth' => $this->minWidth, |
113
|
|
|
'sortable' => $this->sortable, |
114
|
|
|
'template' => $this->getTemplate(), |
115
|
|
|
]; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
public function setDefaultValue($value) |
119
|
|
|
{ |
120
|
|
|
$this->defaultValue = $value; |
121
|
|
|
|
122
|
|
|
return $this; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
public function getDefaultValue() |
126
|
|
|
{ |
127
|
|
|
return $this->defaultValue; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
public function getValue() |
131
|
|
|
{ |
132
|
|
|
$value = $this->getModelValue(); |
133
|
|
|
if (is_null($value)) { |
134
|
|
|
$value = $this->getDefaultValue(); |
135
|
|
|
} |
136
|
|
|
return $value; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
protected function getModelValue() |
140
|
|
|
{ |
141
|
|
|
return $this->getValueFromObject($this->getModel(), $this->getName()); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
protected function getValueFromObject($instance, $name) |
145
|
|
|
{ |
146
|
|
|
$parts = explode('.', $name); |
147
|
|
|
$part = array_shift($parts); |
148
|
|
|
|
149
|
|
|
if ($instance instanceof Collection) { |
150
|
|
|
$instance = $instance->pluck($part); |
151
|
|
|
} elseif (!is_null($instance)) { |
152
|
|
|
$instance = $instance->getAttribute($part); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
if (!empty($parts) && !is_null($instance)) { |
156
|
|
|
return $this->getValueFromObject($instance, implode('.', $parts)); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
return $instance; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
public function jsonSerialize() |
163
|
|
|
{ |
164
|
|
|
return $this->toArray(); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
public function toJson($options = 0) |
168
|
|
|
{ |
169
|
|
|
return json_encode($this->jsonSerialize(), $options); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
public function __toString() |
173
|
|
|
{ |
174
|
|
|
return $this->toJson(); |
175
|
|
|
} |
176
|
|
|
} |
177
|
|
|
|
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.