|
1
|
|
|
<?php namespace Kris\LaravelFormBuilder; |
|
2
|
|
|
|
|
3
|
|
|
use Illuminate\Support\Collection; |
|
4
|
|
|
use Illuminate\Translation\Translator; |
|
5
|
|
|
use Illuminate\Database\Eloquent\Model; |
|
6
|
|
|
use Kris\LaravelFormBuilder\Fields\FormField; |
|
7
|
|
|
use Illuminate\Contracts\View\Factory as View; |
|
8
|
|
|
|
|
9
|
|
|
class FormHelper |
|
10
|
|
|
{ |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* @var View |
|
14
|
|
|
*/ |
|
15
|
|
|
protected $view; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @var Translator |
|
19
|
|
|
*/ |
|
20
|
|
|
protected $translator; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var array |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $config; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var FormBuilder |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $formBuilder; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* All available field types |
|
34
|
|
|
* |
|
35
|
|
|
* @var array |
|
36
|
|
|
*/ |
|
37
|
|
|
protected static $availableFieldTypes = [ |
|
38
|
|
|
'text' => 'InputType', |
|
39
|
|
|
'email' => 'InputType', |
|
40
|
|
|
'url' => 'InputType', |
|
41
|
|
|
'tel' => 'InputType', |
|
42
|
|
|
'search' => 'InputType', |
|
43
|
|
|
'password' => 'InputType', |
|
44
|
|
|
'hidden' => 'InputType', |
|
45
|
|
|
'number' => 'InputType', |
|
46
|
|
|
'date' => 'InputType', |
|
47
|
|
|
'file' => 'InputType', |
|
48
|
|
|
'image' => 'InputType', |
|
49
|
|
|
'color' => 'InputType', |
|
50
|
|
|
'datetime-local' => 'InputType', |
|
51
|
|
|
'month' => 'InputType', |
|
52
|
|
|
'range' => 'InputType', |
|
53
|
|
|
'time' => 'InputType', |
|
54
|
|
|
'week' => 'InputType', |
|
55
|
|
|
'select' => 'SelectType', |
|
56
|
|
|
'textarea' => 'TextareaType', |
|
57
|
|
|
'button' => 'ButtonType', |
|
58
|
|
|
'submit' => 'ButtonType', |
|
59
|
|
|
'reset' => 'ButtonType', |
|
60
|
|
|
'radio' => 'CheckableType', |
|
61
|
|
|
'checkbox' => 'CheckableType', |
|
62
|
|
|
'choice' => 'ChoiceType', |
|
63
|
|
|
'form' => 'ChildFormType', |
|
64
|
|
|
'entity' => 'EntityType', |
|
65
|
|
|
'collection' => 'CollectionType', |
|
66
|
|
|
'repeated' => 'RepeatedType', |
|
67
|
|
|
'static' => 'StaticType' |
|
68
|
|
|
]; |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Custom types |
|
72
|
|
|
* |
|
73
|
|
|
* @var array |
|
74
|
|
|
*/ |
|
75
|
|
|
private $customTypes = []; |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @param View $view |
|
79
|
|
|
* @param Translator $translator |
|
80
|
|
|
* @param array $config |
|
81
|
|
|
*/ |
|
82
|
89 |
|
public function __construct(View $view, Translator $translator, array $config = []) |
|
83
|
|
|
{ |
|
84
|
89 |
|
$this->view = $view; |
|
85
|
89 |
|
$this->translator = $translator; |
|
86
|
89 |
|
$this->config = $config; |
|
87
|
89 |
|
$this->loadCustomTypes(); |
|
88
|
89 |
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* @param string $key |
|
92
|
|
|
* @param string $default |
|
93
|
|
|
* @return mixed |
|
94
|
|
|
*/ |
|
95
|
89 |
|
public function getConfig($key, $default = null) |
|
96
|
|
|
{ |
|
97
|
89 |
|
return array_get($this->config, $key, $default); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* @return View |
|
102
|
|
|
*/ |
|
103
|
30 |
|
public function getView() |
|
104
|
|
|
{ |
|
105
|
30 |
|
return $this->view; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Merge options array |
|
110
|
|
|
* |
|
111
|
|
|
* @param array $first |
|
112
|
|
|
* @param array $second |
|
113
|
|
|
* @return array |
|
114
|
|
|
*/ |
|
115
|
89 |
|
public function mergeOptions(array $first, array $second) |
|
116
|
|
|
{ |
|
117
|
89 |
|
return array_replace_recursive($first, $second); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* Get proper class for field type |
|
122
|
|
|
* |
|
123
|
|
|
* @param $type |
|
124
|
|
|
* @return string |
|
125
|
|
|
*/ |
|
126
|
59 |
|
public function getFieldType($type) |
|
127
|
|
|
{ |
|
128
|
59 |
|
$types = array_keys(static::$availableFieldTypes); |
|
129
|
|
|
|
|
130
|
59 |
|
if (!$type || trim($type) == '') { |
|
131
|
1 |
|
throw new \InvalidArgumentException('Field type must be provided.'); |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
58 |
|
if (array_key_exists($type, $this->customTypes)) { |
|
135
|
2 |
|
return $this->customTypes[$type]; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
56 |
|
if (!in_array($type, $types)) { |
|
139
|
2 |
|
throw new \InvalidArgumentException( |
|
140
|
2 |
|
sprintf( |
|
141
|
2 |
|
'Unsupported field type [%s]. Available types are: %s', |
|
142
|
2 |
|
$type, |
|
143
|
2 |
|
join(', ', array_merge($types, array_keys($this->customTypes))) |
|
144
|
2 |
|
) |
|
145
|
2 |
|
); |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
54 |
|
$namespace = __NAMESPACE__.'\\Fields\\'; |
|
149
|
|
|
|
|
150
|
54 |
|
return $namespace . static::$availableFieldTypes[$type]; |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* Convert array of attributes to html attributes |
|
155
|
|
|
* |
|
156
|
|
|
* @param $options |
|
157
|
|
|
* @return string |
|
158
|
|
|
*/ |
|
159
|
71 |
|
public function prepareAttributes($options) |
|
160
|
|
|
{ |
|
161
|
71 |
|
if (!$options) { |
|
162
|
4 |
|
return null; |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
71 |
|
$attributes = []; |
|
166
|
|
|
|
|
167
|
71 |
|
foreach ($options as $name => $option) { |
|
168
|
71 |
|
if ($option !== null) { |
|
169
|
71 |
|
$name = is_numeric($name) ? $option : $name; |
|
170
|
71 |
|
$attributes[] = $name.'="'.$option.'" '; |
|
171
|
71 |
|
} |
|
172
|
71 |
|
} |
|
173
|
|
|
|
|
174
|
71 |
|
return join('', $attributes); |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
/** |
|
178
|
|
|
* Add custom field |
|
179
|
|
|
* |
|
180
|
|
|
* @param $name |
|
181
|
|
|
* @param $class |
|
182
|
|
|
*/ |
|
183
|
3 |
|
public function addCustomField($name, $class) |
|
184
|
|
|
{ |
|
185
|
3 |
|
if (!array_key_exists($name, $this->customTypes)) { |
|
186
|
3 |
|
return $this->customTypes[$name] = $class; |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
1 |
|
throw new \InvalidArgumentException('Custom field ['.$name.'] already exists on this form object.'); |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
/** |
|
193
|
|
|
* Load custom field types from config file |
|
194
|
|
|
*/ |
|
195
|
89 |
|
private function loadCustomTypes() |
|
196
|
|
|
{ |
|
197
|
89 |
|
$customFields = (array) $this->getConfig('custom_fields'); |
|
198
|
|
|
|
|
199
|
89 |
|
if (!empty($customFields)) { |
|
200
|
1 |
|
foreach ($customFields as $fieldName => $fieldClass) { |
|
201
|
1 |
|
$this->addCustomField($fieldName, $fieldClass); |
|
202
|
1 |
|
} |
|
203
|
1 |
|
} |
|
204
|
89 |
|
} |
|
205
|
|
|
|
|
206
|
5 |
|
public function convertModelToArray($model) |
|
207
|
|
|
{ |
|
208
|
5 |
|
if (!$model) { |
|
209
|
1 |
|
return null; |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
5 |
|
if ($model instanceof Model) { |
|
213
|
1 |
|
return $model->toArray(); |
|
214
|
|
|
} |
|
215
|
|
|
|
|
216
|
5 |
|
if ($model instanceof Collection) { |
|
217
|
2 |
|
return $model->all(); |
|
218
|
|
|
} |
|
219
|
|
|
|
|
220
|
5 |
|
return $model; |
|
221
|
|
|
} |
|
222
|
|
|
|
|
223
|
|
|
/** |
|
224
|
|
|
* Format the label to the proper format |
|
225
|
|
|
* |
|
226
|
|
|
* @param $name |
|
227
|
|
|
* @return string |
|
228
|
|
|
*/ |
|
229
|
69 |
|
public function formatLabel($name) |
|
230
|
|
|
{ |
|
231
|
69 |
|
if (!$name) { |
|
232
|
1 |
|
return null; |
|
233
|
|
|
} |
|
234
|
|
|
|
|
235
|
69 |
|
if ($this->translator->has($name)) { |
|
236
|
1 |
|
$translatedName = $this->translator->get($name); |
|
237
|
|
|
|
|
238
|
1 |
|
if (is_string($translatedName)) { |
|
239
|
1 |
|
return $translatedName; |
|
240
|
|
|
} |
|
241
|
|
|
} |
|
242
|
|
|
|
|
243
|
68 |
|
return ucfirst(str_replace('_', ' ', $name)); |
|
244
|
|
|
} |
|
245
|
|
|
|
|
246
|
|
|
/** |
|
247
|
|
|
* @param FormField[] $fields |
|
248
|
|
|
* @return array |
|
249
|
|
|
*/ |
|
250
|
3 |
|
public function mergeFieldsRules($fields) |
|
251
|
|
|
{ |
|
252
|
3 |
|
$rules = []; |
|
253
|
3 |
|
$attributes = []; |
|
254
|
3 |
|
$messages = []; |
|
255
|
|
|
|
|
256
|
3 |
|
foreach ($fields as $field) { |
|
257
|
3 |
|
if ($fieldRules = $field->getValidationRules()) { |
|
258
|
3 |
|
$rules = array_merge($rules, $fieldRules['rules']); |
|
259
|
3 |
|
$attributes = array_merge($attributes, $fieldRules['attributes']); |
|
260
|
3 |
|
$messages = array_merge($messages, $fieldRules['error_messages']); |
|
261
|
3 |
|
} |
|
262
|
3 |
|
} |
|
263
|
|
|
|
|
264
|
|
|
return [ |
|
265
|
3 |
|
'rules' => $rules, |
|
266
|
3 |
|
'attributes' => $attributes, |
|
267
|
|
|
'error_messages' => $messages |
|
268
|
3 |
|
]; |
|
269
|
|
|
} |
|
270
|
|
|
|
|
271
|
|
|
/** |
|
272
|
|
|
* @param string $string |
|
273
|
|
|
* @return string |
|
274
|
|
|
*/ |
|
275
|
70 |
|
public function transformToDotSyntax($string) |
|
276
|
|
|
{ |
|
277
|
70 |
|
return str_replace(['.', '[]', '[', ']'], ['_', '', '.', ''], $string); |
|
278
|
|
|
} |
|
279
|
|
|
|
|
280
|
|
|
/** |
|
281
|
|
|
* @return Translator |
|
282
|
|
|
*/ |
|
283
|
3 |
|
public function getTranslator() |
|
284
|
|
|
{ |
|
285
|
3 |
|
return $this->translator; |
|
286
|
|
|
} |
|
287
|
|
|
} |
|
288
|
|
|
|