Completed
Push — master ( 0cf65c...1773ee )
by wen
02:56
created

Element::getValueFromRequest()   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 1
1
<?php
2
3
4
namespace Sco\Admin\Form\Elements;
5
6
use Illuminate\Database\Eloquent\Model;
7
use Illuminate\Http\Request;
8
use JsonSerializable;
9
use Illuminate\Contracts\Support\Arrayable;
10
use Illuminate\Contracts\Support\Jsonable;
11
use Sco\Admin\Contracts\Form\Elements\ElementInterface;
12
use Sco\Admin\Contracts\Validable;
13
use Sco\Admin\Contracts\WithModel;
14
15
abstract class Element implements
16
    ElementInterface,
17
    WithModel,
18
    Arrayable,
19
    Jsonable,
20
    JsonSerializable,
21
    Validable
22
{
23
    protected $type;
24
25
    protected $name;
26
    protected $title;
27
28
    /**
29
     * @var mixed
30
     */
31
    protected $defaultValue;
32
33
    /**
34
     * @var \Illuminate\Database\Eloquent\Model
35
     */
36
    protected $model;
37
38
    protected $validationRules = [];
39
40
    protected $validationMessages = [];
41
42
    public function __construct($name, $title)
43
    {
44
        $this->name  = $name;
45
        $this->title = $title;
46
    }
47
48
    public function getName()
49
    {
50
        return $this->name;
51
    }
52
53
    public function setName($value)
54
    {
55
        $this->name = $value;
56
57
        return $this;
58
    }
59
60
    public function getTitle()
61
    {
62
        return $this->title;
63
    }
64
65
    public function setTitle($value)
66
    {
67
        $this->title = $value;
68
69
        return $this;
70
    }
71
72
    public function save(Request $request)
73
    {
74
        $this->setModelAttribute(
75
            $this->getValueFromRequest($request)
76
        );
77
    }
78
79
    protected function setModelAttribute($value)
80
    {
81
        $model = $this->getModel();
82
        $model->setAttribute($this->getName(), $value);
83
    }
84
85
    protected function getValueFromRequest(Request $request)
86
    {
87
        return $request->input($this->getName());
88
    }
89
90
    public function getModel()
91
    {
92
        return $this->model;
93
    }
94
95
    public function setModel(Model $model)
96
    {
97
        $this->model = $model;
98
99
        return $this;
100
    }
101
102
    public function getValue()
103
    {
104
        return $this->getModelValue();
105
    }
106
107
    protected function getModelValue()
108
    {
109
        $model = $this->getModel();
110
        $value = $this->getDefaultValue();
111
        if (is_null($model) || !$model->exists) {
112
            return $value;
113
        }
114
115
        $relations = explode('.', $this->getName(), 2);
116
        $count = count($relations);
117
118
        if ($count == 1) {
119
            return $model->getAttribute($this->getName());
120
        }
121
122
        foreach ($relations as $relation) {
123
            if ($model->{$relation} instanceof Model) {
124
                $model = $model->{$relation};
125
                continue;
126
            }
127
128
            return $model->getAttribute($relation);
129
        }
130
    }
131
132
    protected function getDefaultValue()
133
    {
134
        return $this->defaultValue;
135
    }
136
137
    public function toArray()
138
    {
139
        return [
140
            'key'   => $this->name,
141
            'title' => $this->title,
142
            'type'  => $this->type,
143
        ];
144
    }
145
146
    public function addValidationRule($rule, $message = null)
147
    {
148
        $this->validationRules[] = $rule;
149
150
        if (is_null($message)) {
151
            return $this;
152
        }
153
154
        return $this->addValidationMessage($rule, $message);
155
    }
156
157
    public function addValidationMessage($rule, $message)
158
    {
159
        if (($pos = strpos($rule, ':')) !== false) {
160
            $rule = substr($rule, 0, $pos);
161
        }
162
163
        $this->validationMessages[$rule] = $message;
164
165
        return $this;
166
    }
167
168
    public function getValidationMessages()
169
    {
170
        return $this->validationMessages;
171
    }
172
173
    public function getValidationRules()
174
    {
175
        return $this->validationRules;
176
    }
177
178
    public function getValidationTitles()
179
    {
180
        return [$this->getName() => $this->getTitle()];
181
    }
182
183
    public function jsonSerialize()
184
    {
185
        return $this->toArray();
186
    }
187
188
    public function toJson($options = 0)
189
    {
190
        return json_encode($this->jsonSerialize(), $options);
191
    }
192
193
    public function __toString()
194
    {
195
        return $this->toJson();
196
    }
197
}
198