1 | <?php |
||
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->setName($name)->setTitle($title); |
||
31 | } |
||
32 | |||
33 | public function getType() |
||
34 | { |
||
35 | return $this->type; |
||
36 | } |
||
37 | |||
38 | public function getName() |
||
39 | { |
||
40 | return $this->name; |
||
41 | } |
||
42 | |||
43 | public function setName($value) |
||
44 | { |
||
45 | $this->name = $value; |
||
46 | |||
47 | return $this; |
||
48 | } |
||
49 | |||
50 | public function getTitle() |
||
51 | { |
||
52 | return $this->title; |
||
53 | } |
||
54 | |||
55 | public function setTitle($value) |
||
56 | { |
||
57 | $this->title = $value; |
||
58 | |||
59 | return $this; |
||
60 | } |
||
61 | |||
62 | public function save() |
||
63 | { |
||
64 | $this->setModelAttribute( |
||
65 | $this->getValueFromRequest() |
||
66 | ); |
||
67 | } |
||
68 | |||
69 | public function finishSave() |
||
70 | { |
||
71 | // |
||
72 | } |
||
73 | |||
74 | protected function setModelAttribute($value) |
||
75 | { |
||
76 | $model = $this->getModel(); |
||
77 | $model->setAttribute( |
||
78 | $this->getName(), |
||
79 | $this->prepareValue($value) |
||
80 | ); |
||
81 | } |
||
82 | |||
83 | protected function getValueFromRequest() |
||
84 | { |
||
85 | return request()->input($this->getName()); |
||
86 | } |
||
87 | |||
88 | /** |
||
89 | * @param mixed $value |
||
90 | * |
||
91 | * @return mixed |
||
92 | */ |
||
93 | protected function prepareValue($value) |
||
94 | { |
||
95 | if (is_array($value)) { |
||
96 | return implode(',', $value); |
||
97 | } |
||
98 | |||
99 | return $value; |
||
100 | } |
||
101 | |||
102 | /** |
||
103 | * {@inheritdoc} |
||
104 | */ |
||
105 | public function getModel() |
||
106 | { |
||
107 | return $this->model; |
||
108 | } |
||
109 | |||
110 | public function setModel(Model $model) |
||
111 | { |
||
112 | $this->model = $model; |
||
113 | |||
114 | return $this; |
||
115 | } |
||
116 | |||
117 | /** |
||
118 | * {@inheritdoc} |
||
119 | */ |
||
120 | public function getValue() |
||
121 | { |
||
122 | return $this->getValueFromModel(); |
||
123 | } |
||
124 | |||
125 | protected function getValueFromModel() |
||
126 | { |
||
127 | $model = $this->getModel(); |
||
128 | $value = $this->getDefaultValue(); |
||
129 | if (is_null($model) || !$model->exists) { |
||
130 | return $value; |
||
131 | } |
||
132 | return $model->getAttribute($this->getName()); |
||
133 | } |
||
134 | |||
135 | /** |
||
136 | * @param mixed $value |
||
137 | * |
||
138 | * @return $this |
||
139 | */ |
||
140 | public function setDefaultValue($value) |
||
141 | { |
||
142 | $this->defaultValue = $value; |
||
143 | |||
144 | return $this; |
||
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 | 'name' => $this->getName(), |
||
169 | 'title' => $this->getTitle(), |
||
170 | 'type' => $this->getType(), |
||
171 | 'disabled' => $this->isDisabled(), |
||
172 | ]; |
||
173 | } |
||
174 | |||
175 | public function jsonSerialize() |
||
179 | |||
180 | public function toJson($options = 0) |
||
184 | |||
185 | public function __toString() |
||
189 | } |
||
190 |