|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Sco\Admin\Form\Elements; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Model; |
|
6
|
|
|
use Sco\Admin\Contracts\Form\Elements\ElementInterface; |
|
7
|
|
|
use Sco\Admin\Form\Elements\Concerns\HasCast; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* All Element abstract class |
|
11
|
|
|
* |
|
12
|
|
|
* @package Sco\Admin\Form\Elements |
|
13
|
|
|
*/ |
|
14
|
|
|
abstract class Element implements ElementInterface |
|
15
|
|
|
{ |
|
16
|
|
|
use HasCast; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @var string |
|
20
|
|
|
*/ |
|
21
|
|
|
protected $type; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @var string |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $name; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var string |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $title; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @var mixed |
|
35
|
|
|
*/ |
|
36
|
|
|
protected $defaultValue; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @var \Illuminate\Database\Eloquent\Model |
|
40
|
|
|
*/ |
|
41
|
|
|
protected $model; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @var \Closure |
|
45
|
|
|
*/ |
|
46
|
|
|
protected $mutator; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Element constructor. |
|
50
|
|
|
* |
|
51
|
|
|
* @param string $name |
|
52
|
|
|
* @param string $title |
|
53
|
|
|
*/ |
|
54
|
|
|
public function __construct(string $name, string $title) |
|
55
|
|
|
{ |
|
56
|
|
|
$this->setName($name)->setTitle($title); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @return string |
|
61
|
|
|
*/ |
|
62
|
|
|
public function getType() |
|
63
|
|
|
{ |
|
64
|
|
|
return $this->type; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @return string |
|
69
|
|
|
*/ |
|
70
|
|
|
public function getName() |
|
71
|
|
|
{ |
|
72
|
|
|
return $this->name; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* {@inheritdoc} |
|
77
|
|
|
*/ |
|
78
|
|
|
public function setName(string $value) |
|
79
|
|
|
{ |
|
80
|
|
|
$this->name = $value; |
|
81
|
|
|
|
|
82
|
|
|
return $this; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* {@inheritdoc} |
|
87
|
|
|
*/ |
|
88
|
|
|
public function getTitle() |
|
89
|
|
|
{ |
|
90
|
|
|
return $this->title; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* {@inheritdoc} |
|
95
|
|
|
*/ |
|
96
|
|
|
public function setTitle(string $value) |
|
97
|
|
|
{ |
|
98
|
|
|
$this->title = $value; |
|
99
|
|
|
|
|
100
|
|
|
return $this; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
public function save() |
|
104
|
|
|
{ |
|
105
|
|
|
$this->setModelAttribute( |
|
106
|
|
|
$this->getValueFromRequest() |
|
107
|
|
|
); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
public function finishSave() |
|
111
|
|
|
{ |
|
112
|
|
|
// |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* @param $value |
|
117
|
|
|
*/ |
|
118
|
|
|
protected function setModelAttribute($value) |
|
119
|
|
|
{ |
|
120
|
|
|
$model = $this->getModel(); |
|
121
|
|
|
$model->setAttribute( |
|
122
|
|
|
$this->getName(), |
|
123
|
|
|
$this->prepareValue($value) |
|
124
|
|
|
); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* @return array|string |
|
129
|
|
|
*/ |
|
130
|
|
|
protected function getValueFromRequest() |
|
131
|
|
|
{ |
|
132
|
|
|
return request()->input($this->getName()); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* @return null|\Closure |
|
137
|
|
|
*/ |
|
138
|
|
|
public function getMutator() |
|
139
|
|
|
{ |
|
140
|
|
|
return $this->mutator; |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* @param \Closure $mutator |
|
145
|
|
|
* @return $this |
|
146
|
|
|
*/ |
|
147
|
|
|
public function setMutator(\Closure $mutator) |
|
148
|
|
|
{ |
|
149
|
|
|
$this->mutator = $mutator; |
|
150
|
|
|
|
|
151
|
|
|
return $this; |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
public function hasMutator() |
|
155
|
|
|
{ |
|
156
|
|
|
return is_callable($this->getMutator()); |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
/** |
|
160
|
|
|
* @param mixed $value |
|
161
|
|
|
* |
|
162
|
|
|
* @return mixed |
|
163
|
|
|
*/ |
|
164
|
|
|
protected function prepareValue($value) |
|
165
|
|
|
{ |
|
166
|
|
|
if ($this->isJsonCastable() && ! is_null($value)) { |
|
167
|
|
|
$value = $this->castValueAsJson($value); |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
if ($this->isCommaCastable() && ! is_null($value)) { |
|
171
|
|
|
$value = $this->castValueAsCommaSeparated($value); |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
if ($this->hasMutator()) { |
|
175
|
|
|
$value = call_user_func($this->getMutator(), $value); |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
return $value; |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
/** |
|
182
|
|
|
* {@inheritdoc} |
|
183
|
|
|
*/ |
|
184
|
|
|
public function getModel() |
|
185
|
|
|
{ |
|
186
|
|
|
return $this->model; |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
/** |
|
190
|
|
|
* @param \Illuminate\Database\Eloquent\Model $model |
|
191
|
|
|
* @return $this |
|
192
|
|
|
*/ |
|
193
|
|
|
public function setModel(Model $model) |
|
194
|
|
|
{ |
|
195
|
|
|
$this->model = $model; |
|
196
|
|
|
|
|
197
|
|
|
return $this; |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
/** |
|
201
|
|
|
* {@inheritdoc} |
|
202
|
|
|
*/ |
|
203
|
|
|
public function getValue() |
|
204
|
|
|
{ |
|
205
|
|
|
$value = $this->getValueFromModel(); |
|
206
|
|
|
|
|
207
|
|
|
if ($this->isCastable()) { |
|
208
|
|
|
$value = $this->castValue($value); |
|
209
|
|
|
} |
|
210
|
|
|
|
|
211
|
|
|
return $value; |
|
212
|
|
|
} |
|
213
|
|
|
|
|
214
|
|
|
/** |
|
215
|
|
|
* @return mixed |
|
216
|
|
|
*/ |
|
217
|
|
|
protected function getValueFromModel() |
|
218
|
|
|
{ |
|
219
|
|
|
$model = $this->getModel(); |
|
220
|
|
|
$value = $this->getDefaultValue(); |
|
221
|
|
|
if (is_null($model) || ! $model->exists) { |
|
222
|
|
|
return $value; |
|
223
|
|
|
} |
|
224
|
|
|
|
|
225
|
|
|
return $model->getAttribute($this->getName()); |
|
226
|
|
|
} |
|
227
|
|
|
|
|
228
|
|
|
/** |
|
229
|
|
|
* @param mixed $value |
|
230
|
|
|
* |
|
231
|
|
|
* @return $this |
|
232
|
|
|
*/ |
|
233
|
|
|
public function setDefaultValue($value) |
|
234
|
|
|
{ |
|
235
|
|
|
$this->defaultValue = $value; |
|
236
|
|
|
|
|
237
|
|
|
return $this; |
|
238
|
|
|
} |
|
239
|
|
|
|
|
240
|
|
|
/** |
|
241
|
|
|
* @return mixed |
|
242
|
|
|
*/ |
|
243
|
|
|
protected function getDefaultValue() |
|
244
|
|
|
{ |
|
245
|
|
|
return $this->defaultValue; |
|
246
|
|
|
} |
|
247
|
|
|
|
|
248
|
|
|
/** |
|
249
|
|
|
* @return array |
|
250
|
|
|
*/ |
|
251
|
|
|
public function toArray() |
|
252
|
|
|
{ |
|
253
|
|
|
return [ |
|
254
|
|
|
'name' => $this->getName(), |
|
255
|
|
|
'title' => $this->getTitle(), |
|
256
|
|
|
'type' => $this->getType(), |
|
257
|
|
|
]; |
|
258
|
|
|
} |
|
259
|
|
|
|
|
260
|
|
|
/** |
|
261
|
|
|
* @return array|mixed |
|
262
|
|
|
*/ |
|
263
|
|
|
public function jsonSerialize() |
|
264
|
|
|
{ |
|
265
|
|
|
return $this->toArray(); |
|
266
|
|
|
} |
|
267
|
|
|
|
|
268
|
|
|
/** |
|
269
|
|
|
* @param int $options |
|
270
|
|
|
* @return string |
|
271
|
|
|
*/ |
|
272
|
|
|
public function toJson($options = 0) |
|
273
|
|
|
{ |
|
274
|
|
|
return json_encode($this->jsonSerialize(), $options); |
|
275
|
|
|
} |
|
276
|
|
|
|
|
277
|
|
|
/** |
|
278
|
|
|
* @return string |
|
279
|
|
|
*/ |
|
280
|
|
|
public function __toString() |
|
281
|
|
|
{ |
|
282
|
|
|
return $this->toJson(); |
|
283
|
|
|
} |
|
284
|
|
|
} |
|
285
|
|
|
|