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
|
|
|
return $this->setModelAttribute( |
106
|
|
|
$this->getValueFromRequest() |
107
|
|
|
); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
public function finishSave() |
111
|
|
|
{ |
112
|
|
|
// |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @param $value |
117
|
|
|
* @return Model |
118
|
|
|
*/ |
119
|
|
|
protected function setModelAttribute($value) |
120
|
|
|
{ |
121
|
|
|
$model = $this->getModel(); |
122
|
|
|
return $model->setAttribute( |
123
|
|
|
$this->getName(), |
124
|
|
|
$this->prepareValue($value) |
125
|
|
|
); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @return array|string |
130
|
|
|
*/ |
131
|
|
|
protected function getValueFromRequest() |
132
|
|
|
{ |
133
|
|
|
return request()->input($this->getName()); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @return null|\Closure |
138
|
|
|
*/ |
139
|
|
|
public function getMutator() |
140
|
|
|
{ |
141
|
|
|
return $this->mutator; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* @param \Closure $mutator |
146
|
|
|
* @return $this |
147
|
|
|
*/ |
148
|
|
|
public function setMutator(\Closure $mutator) |
149
|
|
|
{ |
150
|
|
|
$this->mutator = $mutator; |
151
|
|
|
|
152
|
|
|
return $this; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
public function hasMutator() |
156
|
|
|
{ |
157
|
|
|
return is_callable($this->getMutator()); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* @param mixed $value |
162
|
|
|
* |
163
|
|
|
* @return mixed |
164
|
|
|
*/ |
165
|
|
|
protected function prepareValue($value) |
166
|
|
|
{ |
167
|
|
|
if ($this->hasMutator()) { |
168
|
|
|
return call_user_func($this->getMutator(), $value); |
169
|
|
|
} elseif ($value && $this->isElementOfDate() && $this->isDateCastable()) { |
170
|
|
|
$value = $this->castValueAsDateTime($value); |
171
|
|
|
if ($this instanceof Timestamp) { |
172
|
|
|
$value = $value->getTimestamp(); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
return $value; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
if (! is_null($value)) { |
179
|
|
|
if ($this->isJsonCastable()) { |
180
|
|
|
return $this->castValueAsJson($value); |
181
|
|
|
} elseif ($this->isCommaCastable()) { |
182
|
|
|
return $this->castValueAsCommaSeparated($value); |
183
|
|
|
} |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
return $value; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* {@inheritdoc} |
191
|
|
|
*/ |
192
|
|
|
public function getModel() |
193
|
|
|
{ |
194
|
|
|
return $this->model; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* @param \Illuminate\Database\Eloquent\Model $model |
199
|
|
|
* @return $this |
200
|
|
|
*/ |
201
|
|
|
public function setModel(Model $model) |
202
|
|
|
{ |
203
|
|
|
$this->model = $model; |
204
|
|
|
|
205
|
|
|
return $this; |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* {@inheritdoc} |
210
|
|
|
*/ |
211
|
|
|
public function getValue() |
212
|
|
|
{ |
213
|
|
|
$value = $this->getValueFromModel(); |
214
|
|
|
|
215
|
|
|
if (is_null($value)) { |
216
|
|
|
return $this->getDefaultValue(); |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
if ($this->isCastable()) { |
220
|
|
|
$value = $this->castValue($value); |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
return $value; |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
/** |
227
|
|
|
* @return mixed |
228
|
|
|
*/ |
229
|
|
|
protected function getValueFromModel() |
230
|
|
|
{ |
231
|
|
|
$model = $this->getModel(); |
232
|
|
|
if ($model && $model->exists) { |
233
|
|
|
return $model->getAttribute($this->getName()); |
234
|
|
|
} |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
/** |
238
|
|
|
* @param mixed $value |
239
|
|
|
* |
240
|
|
|
* @return $this |
241
|
|
|
*/ |
242
|
|
|
public function setDefaultValue($value) |
243
|
|
|
{ |
244
|
|
|
$this->defaultValue = $value; |
245
|
|
|
|
246
|
|
|
return $this; |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
/** |
250
|
|
|
* @return mixed |
251
|
|
|
*/ |
252
|
|
|
protected function getDefaultValue() |
253
|
|
|
{ |
254
|
|
|
return $this->defaultValue; |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
/** |
258
|
|
|
* @return array |
259
|
|
|
*/ |
260
|
|
|
public function toArray() |
261
|
|
|
{ |
262
|
|
|
return [ |
263
|
|
|
'name' => $this->getName(), |
264
|
|
|
'title' => $this->getTitle(), |
265
|
|
|
'type' => $this->getType(), |
266
|
|
|
]; |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
/** |
270
|
|
|
* @return array|mixed |
271
|
|
|
*/ |
272
|
|
|
public function jsonSerialize() |
273
|
|
|
{ |
274
|
|
|
return $this->toArray(); |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
/** |
278
|
|
|
* @param int $options |
279
|
|
|
* @return string |
280
|
|
|
*/ |
281
|
|
|
public function toJson($options = 0) |
282
|
|
|
{ |
283
|
|
|
return json_encode($this->jsonSerialize(), $options); |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
/** |
287
|
|
|
* @return string |
288
|
|
|
*/ |
289
|
|
|
public function __toString() |
290
|
|
|
{ |
291
|
|
|
return $this->toJson(); |
292
|
|
|
} |
293
|
|
|
} |
294
|
|
|
|