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 array $config |
80
|
|
|
*/ |
81
|
82 |
|
public function __construct(View $view, array $config = [], Translator $translator) |
82
|
|
|
{ |
83
|
82 |
|
$this->view = $view; |
84
|
82 |
|
$this->translator = $translator; |
85
|
82 |
|
$this->config = $config; |
86
|
82 |
|
$this->loadCustomTypes(); |
87
|
82 |
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @param string $key |
91
|
|
|
* @param string $default |
92
|
|
|
* @return mixed |
93
|
|
|
*/ |
94
|
82 |
|
public function getConfig($key, $default = null) |
95
|
|
|
{ |
96
|
82 |
|
return array_get($this->config, $key, $default); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @return View |
101
|
|
|
*/ |
102
|
27 |
|
public function getView() |
103
|
|
|
{ |
104
|
27 |
|
return $this->view; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Merge options array |
109
|
|
|
* |
110
|
|
|
* @param array $first |
111
|
|
|
* @param array $second |
112
|
|
|
* @return array |
113
|
|
|
*/ |
114
|
82 |
|
public function mergeOptions(array $first, array $second) |
115
|
|
|
{ |
116
|
82 |
|
return array_replace_recursive($first, $second); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Get proper class for field type |
121
|
|
|
* |
122
|
|
|
* @param $type |
123
|
|
|
* @return string |
124
|
|
|
*/ |
125
|
54 |
|
public function getFieldType($type) |
126
|
|
|
{ |
127
|
54 |
|
$types = array_keys(static::$availableFieldTypes); |
128
|
|
|
|
129
|
54 |
|
if (!$type || trim($type) == '') { |
130
|
1 |
|
throw new \InvalidArgumentException('Field type must be provided.'); |
131
|
|
|
} |
132
|
|
|
|
133
|
53 |
|
if (array_key_exists($type, $this->customTypes)) { |
134
|
2 |
|
return $this->customTypes[$type]; |
135
|
|
|
} |
136
|
|
|
|
137
|
51 |
|
if (!in_array($type, $types)) { |
138
|
2 |
|
throw new \InvalidArgumentException( |
139
|
|
|
sprintf( |
140
|
2 |
|
'Unsupported field type [%s]. Available types are: %s', |
141
|
|
|
$type, |
142
|
2 |
|
join(', ', array_merge($types, array_keys($this->customTypes))) |
143
|
|
|
) |
144
|
|
|
); |
145
|
|
|
} |
146
|
|
|
|
147
|
49 |
|
$namespace = __NAMESPACE__.'\\Fields\\'; |
148
|
|
|
|
149
|
49 |
|
return $namespace . static::$availableFieldTypes[$type]; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Convert array of attributes to html attributes |
154
|
|
|
* |
155
|
|
|
* @param $options |
156
|
|
|
* @return string |
157
|
|
|
*/ |
158
|
63 |
|
public function prepareAttributes($options) |
159
|
|
|
{ |
160
|
63 |
|
if (!$options) { |
161
|
4 |
|
return null; |
162
|
|
|
} |
163
|
|
|
|
164
|
63 |
|
$attributes = []; |
165
|
|
|
|
166
|
63 |
|
foreach ($options as $name => $option) { |
167
|
63 |
|
if ($option !== null) { |
168
|
63 |
|
$name = is_numeric($name) ? $option : $name; |
169
|
63 |
|
$attributes[] = $name.'="'.$option.'" '; |
170
|
|
|
} |
171
|
|
|
} |
172
|
|
|
|
173
|
63 |
|
return join('', $attributes); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* Add custom field |
178
|
|
|
* |
179
|
|
|
* @param $name |
180
|
|
|
* @param $class |
181
|
|
|
*/ |
182
|
3 |
|
public function addCustomField($name, $class) |
183
|
|
|
{ |
184
|
3 |
|
if (!array_key_exists($name, $this->customTypes)) { |
185
|
3 |
|
return $this->customTypes[$name] = $class; |
186
|
|
|
} |
187
|
|
|
|
188
|
1 |
|
throw new \InvalidArgumentException('Custom field ['.$name.'] already exists on this form object.'); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* Load custom field types from config file |
193
|
|
|
*/ |
194
|
82 |
|
private function loadCustomTypes() |
195
|
|
|
{ |
196
|
82 |
|
$customFields = (array) $this->getConfig('custom_fields'); |
197
|
|
|
|
198
|
82 |
|
if (!empty($customFields)) { |
199
|
1 |
|
foreach ($customFields as $fieldName => $fieldClass) { |
200
|
1 |
|
$this->addCustomField($fieldName, $fieldClass); |
201
|
|
|
} |
202
|
|
|
} |
203
|
82 |
|
} |
204
|
|
|
|
205
|
5 |
|
public function convertModelToArray($model) |
206
|
|
|
{ |
207
|
5 |
|
if (!$model) { |
208
|
1 |
|
return null; |
209
|
|
|
} |
210
|
|
|
|
211
|
5 |
|
if ($model instanceof Model) { |
212
|
1 |
|
return $model->toArray(); |
213
|
|
|
} |
214
|
|
|
|
215
|
5 |
|
if ($model instanceof Collection) { |
216
|
2 |
|
return $model->all(); |
217
|
|
|
} |
218
|
|
|
|
219
|
5 |
|
return $model; |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* Format the label to the proper format |
224
|
|
|
* |
225
|
|
|
* @param $name |
226
|
|
|
* @return string |
227
|
|
|
*/ |
228
|
63 |
|
public function formatLabel($name) |
229
|
|
|
{ |
230
|
63 |
|
if (!$name) { |
231
|
1 |
|
return null; |
232
|
|
|
} |
233
|
|
|
|
234
|
63 |
|
if ($this->translator->has($name)) { |
235
|
|
|
return $this->translator->get($name); |
236
|
|
|
} |
237
|
|
|
|
238
|
63 |
|
return ucfirst(str_replace('_', ' ', $name)); |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
/** |
242
|
|
|
* @param FormField[] $fields |
243
|
|
|
* @return array |
244
|
|
|
*/ |
245
|
3 |
|
public function mergeFieldsRules($fields) |
246
|
|
|
{ |
247
|
3 |
|
$rules = []; |
248
|
3 |
|
$attributes = []; |
249
|
|
|
|
250
|
3 |
|
foreach ($fields as $field) { |
251
|
3 |
|
if ($fieldRules = $field->getValidationRules()) { |
252
|
3 |
|
$rules = array_merge($rules, $fieldRules['rules']); |
253
|
3 |
|
$attributes = array_merge($attributes, $fieldRules['attributes']); |
254
|
|
|
} |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
return [ |
258
|
3 |
|
'rules' => $rules, |
259
|
3 |
|
'attributes' => $attributes |
260
|
|
|
]; |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
/** |
264
|
|
|
* @param string $string |
265
|
|
|
* @return string |
266
|
|
|
*/ |
267
|
62 |
|
public function transformToDotSyntax($string) |
268
|
|
|
{ |
269
|
62 |
|
return str_replace(['.', '[]', '[', ']'], ['_', '', '.', ''], $string); |
270
|
|
|
} |
271
|
|
|
} |
272
|
|
|
|