Completed
Push — master ( f05a87...6b2b65 )
by wen
14:04
created

Element::getValueFromModel()   B

Complexity

Conditions 3
Paths 2

Size

Total Lines 25
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 25
c 0
b 0
f 0
rs 8.8571
cc 3
eloc 6
nc 2
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
abstract class Element implements ElementInterface
9
{
10
    protected $type;
11
12
    protected $name;
13
14
    protected $title;
15
16
    protected $disabled = false;
17
18
    /**
19
     * @var mixed
20
     */
21
    protected $defaultValue;
22
23
    /**
24
     * @var \Illuminate\Database\Eloquent\Model
25
     */
26
    protected $model;
27
28
    public function __construct($name, $title)
29
    {
30
        $this->name  = $name;
31
        $this->title = $title;
32
    }
33
34
    public function getType()
35
    {
36
        return $this->type;
37
    }
38
39
    public function getName()
40
    {
41
        return $this->name;
42
    }
43
44
    public function setName($value)
45
    {
46
        $this->name = $value;
47
48
        return $this;
49
    }
50
51
    public function getTitle()
52
    {
53
        return $this->title;
54
    }
55
56
    public function setTitle($value)
57
    {
58
        $this->title = $value;
59
60
        return $this;
61
    }
62
63
    public function save()
64
    {
65
        $this->setModelAttribute(
66
            $this->getValueFromRequest()
67
        );
68
    }
69
70
    public function finishSave()
71
    {
72
        //
73
    }
74
75
    protected function setModelAttribute($value)
76
    {
77
        $model = $this->getModel();
78
        $model->setAttribute(
79
            $this->getName(),
80
            $this->prepareValue($value)
81
        );
82
    }
83
84
    protected function getValueFromRequest()
85
    {
86
        return request()->input($this->getName());
87
    }
88
89
    /**
90
     * @param mixed $value
91
     *
92
     * @return mixed
93
     */
94
    protected function prepareValue($value)
95
    {
96
        return $value;
97
    }
98
99
    /**
100
     * {@inheritdoc}
101
     */
102
    public function getModel()
103
    {
104
        return $this->model;
105
    }
106
107
    public function setModel(Model $model)
108
    {
109
        $this->model = $model;
110
111
        return $this;
112
    }
113
114
    /**
115
     * {@inheritdoc}
116
     */
117
    public function getValue()
118
    {
119
        return $this->getValueFromModel();
120
    }
121
122
    protected function getValueFromModel()
123
    {
124
        $model = $this->getModel();
125
        $value = $this->getDefaultValue();
126
        if (is_null($model) || !$model->exists) {
127
            return $value;
128
        }
129
        return $model->getAttribute($this->getName());
130
131
        /*$relations = explode('.', $this->getName(), 2);
0 ignored issues
show
Unused Code Comprehensibility introduced by
60% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
132
        $count = count($relations);
133
134
        if ($count == 1) {
135
            return $model->getAttribute($this->getName());
136
        }
137
138
        foreach ($relations as $relation) {
139
            if ($model->{$relation} instanceof Model) {
140
                $model = $model->{$relation};
141
                continue;
142
            }
143
144
            return $model->getAttribute($relation);
145
        }*/
146
    }
147
148
    protected function getDefaultValue()
149
    {
150
        return $this->defaultValue;
151
    }
152
153
    public function isDisabled()
154
    {
155
        return $this->disabled;
156
    }
157
158
    public function setDisabled()
159
    {
160
        $this->disabled = true;
161
162
        return $this;
163
    }
164
165
    public function toArray()
166
    {
167
        return [
168
            'key'      => $this->getName(),
169
            'title'    => $this->getTitle(),
170
            'type'     => $this->getType(),
171
            'disabled' => $this->isDisabled(),
172
        ];
173
    }
174
175
    public function jsonSerialize()
176
    {
177
        return $this->toArray();
178
    }
179
180
    public function toJson($options = 0)
181
    {
182
        return json_encode($this->jsonSerialize(), $options);
183
    }
184
185
    public function __toString()
186
    {
187
        return $this->toJson();
188
    }
189
}
190