Completed
Push — master ( c30e79...8e747a )
by wen
13:09
created

Element::getTitle()   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
c 0
b 0
f 0
rs 10
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
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
        if (is_array($value)) {
97
            return implode(',', $value);
98
        }
99
100
        return $value;
101
    }
102
103
    /**
104
     * {@inheritdoc}
105
     */
106
    public function getModel()
107
    {
108
        return $this->model;
109
    }
110
111
    public function setModel(Model $model)
112
    {
113
        $this->model = $model;
114
115
        return $this;
116
    }
117
118
    /**
119
     * {@inheritdoc}
120
     */
121
    public function getValue()
122
    {
123
        return $this->getValueFromModel();
124
    }
125
126
    protected function getValueFromModel()
127
    {
128
        $model = $this->getModel();
129
        $value = $this->getDefaultValue();
130
        if (is_null($model) || !$model->exists) {
131
            return $value;
132
        }
133
        return $model->getAttribute($this->getName());
134
135
        /*$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...
136
        $count = count($relations);
137
138
        if ($count == 1) {
139
            return $model->getAttribute($this->getName());
140
        }
141
142
        foreach ($relations as $relation) {
143
            if ($model->{$relation} instanceof Model) {
144
                $model = $model->{$relation};
145
                continue;
146
            }
147
148
            return $model->getAttribute($relation);
149
        }*/
150
    }
151
152
    /**
153
     * @param mixed $value
154
     *
155
     * @return $this
156
     */
157
    public function setDefaultValue($value)
158
    {
159
        $this->defaultValue = $value;
160
161
        return $this;
162
    }
163
164
165
    protected function getDefaultValue()
166
    {
167
        return $this->defaultValue;
168
    }
169
170
    public function isDisabled()
171
    {
172
        return $this->disabled;
173
    }
174
175
    public function setDisabled()
176
    {
177
        $this->disabled = true;
178
179
        return $this;
180
    }
181
182
    public function toArray()
183
    {
184
        return [
185
            'key'      => $this->getName(),
186
            'title'    => $this->getTitle(),
187
            'type'     => $this->getType(),
188
            'disabled' => $this->isDisabled(),
189
        ];
190
    }
191
192
    public function jsonSerialize()
193
    {
194
        return $this->toArray();
195
    }
196
197
    public function toJson($options = 0)
198
    {
199
        return json_encode($this->jsonSerialize(), $options);
200
    }
201
202
    public function __toString()
203
    {
204
        return $this->toJson();
205
    }
206
}
207