Completed
Push — master ( c139be...7e212d )
by wen
14:16
created

Element::getMutator()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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