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