1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Sco\Admin\Form; |
4
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Model; |
6
|
|
|
use InvalidArgumentException; |
7
|
|
|
use Sco\Admin\Contracts\Form\Elements\ElementInterface; |
8
|
|
|
use Sco\Admin\Contracts\Form\FormInterface; |
9
|
|
|
use Sco\Admin\Contracts\Validatable; |
10
|
|
|
use Validator; |
11
|
|
|
|
12
|
|
|
class Form implements |
13
|
|
|
FormInterface, |
14
|
|
|
Validatable |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var \Sco\Admin\Form\ElementsCollection |
18
|
|
|
*/ |
19
|
|
|
protected $elements; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var \Illuminate\Database\Eloquent\Model |
23
|
|
|
*/ |
24
|
|
|
protected $model; |
25
|
|
|
|
26
|
|
|
public function __construct(array $elements = []) |
27
|
|
|
{ |
28
|
|
|
$this->setElements($elements); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* {@inheritdoc} |
33
|
|
|
*/ |
34
|
|
|
public function getElements() |
35
|
|
|
{ |
36
|
|
|
return $this->elements; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* {@inheritdoc} |
41
|
|
|
*/ |
42
|
|
|
public function getElement($name) |
43
|
|
|
{ |
44
|
|
|
$key = $this->getElements()->search(function (ElementInterface $item) use ($name |
45
|
|
|
) { |
46
|
|
|
return $item->getName() == $name; |
47
|
|
|
}); |
48
|
|
|
if ($key === false) { |
49
|
|
|
throw new InvalidArgumentException('Not found element'); |
50
|
|
|
} |
51
|
|
|
return $this->getElements()->get($key); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* {@inheritdoc} |
56
|
|
|
*/ |
57
|
|
|
public function setElements(array $elements) |
58
|
|
|
{ |
59
|
|
|
$this->elements = new ElementsCollection($elements); |
60
|
|
|
|
61
|
|
|
return $this; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* {@inheritdoc} |
66
|
|
|
*/ |
67
|
|
|
public function addElement(ElementInterface $element) |
68
|
|
|
{ |
69
|
|
|
$this->elements->push($element); |
70
|
|
|
|
71
|
|
|
return $this; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* {@inheritdoc} |
76
|
|
|
*/ |
77
|
|
|
public function getModel() |
78
|
|
|
{ |
79
|
|
|
return $this->model; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* {@inheritdoc} |
84
|
|
|
*/ |
85
|
|
|
public function setModel(Model $model) |
86
|
|
|
{ |
87
|
|
|
$this->model = $model; |
88
|
|
|
|
89
|
|
|
$this->setElementModel($model); |
90
|
|
|
|
91
|
|
|
return $this; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* {@inheritdoc} |
96
|
|
|
*/ |
97
|
|
|
public function setElementModel(Model $model) |
98
|
|
|
{ |
99
|
|
|
$this->elements->each(function (ElementInterface $element) use ($model) { |
100
|
|
|
//if ($element instanceof WithModel) { |
|
|
|
|
101
|
|
|
$element->setModel($model); |
102
|
|
|
//} |
103
|
|
|
}); |
104
|
|
|
|
105
|
|
|
return $this; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* {@inheritdoc} |
110
|
|
|
*/ |
111
|
|
|
public function validate() |
112
|
|
|
{ |
113
|
|
|
Validator::validate( |
114
|
|
|
request()->all(), |
115
|
|
|
$this->getValidationRules(), |
116
|
|
|
$this->getValidationMessages(), |
117
|
|
|
$this->getValidationTitles() |
118
|
|
|
); |
119
|
|
|
|
120
|
|
|
return $this; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
public function getValidationRules() |
124
|
|
|
{ |
125
|
|
|
return $this->getElementsValidationRules(); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
public function getValidationMessages() |
129
|
|
|
{ |
130
|
|
|
return $this->getElementsValidationMessages(); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
public function getValidationTitles() |
134
|
|
|
{ |
135
|
|
|
return $this->getElementsValidationTitles(); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
protected function getElementsValidationRules() |
139
|
|
|
{ |
140
|
|
|
$rules = []; |
141
|
|
|
$this->elements->each(function (ElementInterface $element) use (&$rules) { |
142
|
|
|
if ($element instanceof Validatable) { |
143
|
|
|
$rules += $element->getValidationRules(); |
144
|
|
|
} |
145
|
|
|
}); |
146
|
|
|
return $rules; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
protected function getElementsValidationMessages() |
150
|
|
|
{ |
151
|
|
|
$messages = []; |
152
|
|
|
$this->elements->each(function (ElementInterface $element) use (&$messages) { |
153
|
|
|
if ($element instanceof Validatable) { |
154
|
|
|
$messages += $element->getValidationMessages(); |
155
|
|
|
} |
156
|
|
|
}); |
157
|
|
|
return $messages; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
protected function getElementsValidationTitles() |
161
|
|
|
{ |
162
|
|
|
$titles = []; |
163
|
|
|
$this->elements->each(function (ElementInterface $element) use (&$titles) { |
164
|
|
|
if ($element instanceof Validatable) { |
165
|
|
|
$titles += $element->getValidationTitles(); |
166
|
|
|
} |
167
|
|
|
}); |
168
|
|
|
return $titles; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
protected function saveElements() |
172
|
|
|
{ |
173
|
|
|
$this->getElements()->each(function (ElementInterface $element) { |
174
|
|
|
$element->save(); |
175
|
|
|
}); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
protected function finishSaveElements() |
179
|
|
|
{ |
180
|
|
|
$this->getElements()->each(function (ElementInterface $element) { |
181
|
|
|
$element->finishSave(); |
182
|
|
|
}); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
protected function finishSave() |
186
|
|
|
{ |
187
|
|
|
$this->finishSaveElements(); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
public function save() |
191
|
|
|
{ |
192
|
|
|
$this->saveElements(); |
193
|
|
|
|
194
|
|
|
$model = $this->getModel(); |
195
|
|
|
$saved = $model->save(); |
196
|
|
|
|
197
|
|
|
if ($saved) { |
198
|
|
|
$this->finishSave(); |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
return true; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* {@inheritdoc} |
206
|
|
|
*/ |
207
|
|
|
public function getValues() |
208
|
|
|
{ |
209
|
|
|
return $this->elements->mapWithKeys(function (ElementInterface $element) { |
210
|
|
|
return [ |
211
|
|
|
$element->getName() => $element->getValue(), |
212
|
|
|
]; |
213
|
|
|
}); |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
public function toArray() |
217
|
|
|
{ |
218
|
|
|
return [ |
219
|
|
|
'elements' => $this->getElements(), |
220
|
|
|
'values' => $this->getValues(), |
221
|
|
|
]; |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
public function jsonSerialize() |
225
|
|
|
{ |
226
|
|
|
return $this->toArray(); |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
public function toJson($options = 0) |
230
|
|
|
{ |
231
|
|
|
return json_encode($this->jsonSerialize(), $options); |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
public function __toString() |
235
|
|
|
{ |
236
|
|
|
return $this->toJson(); |
237
|
|
|
} |
238
|
|
|
} |
239
|
|
|
|
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.