1 | <?php namespace Kris\LaravelFormBuilder; |
||
9 | class FormHelper |
||
10 | { |
||
11 | |||
12 | /** |
||
13 | * @var View |
||
14 | */ |
||
15 | protected $view; |
||
16 | |||
17 | /** |
||
18 | * @var TranslatorInterface |
||
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 | * @var array |
||
34 | */ |
||
35 | protected static $reservedFieldNames = [ |
||
36 | 'save' |
||
37 | ]; |
||
38 | |||
39 | /** |
||
40 | * All available field types |
||
41 | * |
||
42 | * @var array |
||
43 | */ |
||
44 | protected static $availableFieldTypes = [ |
||
45 | 'text' => 'InputType', |
||
46 | 'email' => 'InputType', |
||
47 | 'url' => 'InputType', |
||
48 | 'tel' => 'InputType', |
||
49 | 'search' => 'InputType', |
||
50 | 'password' => 'InputType', |
||
51 | 'hidden' => 'InputType', |
||
52 | 'number' => 'InputType', |
||
53 | 'date' => 'InputType', |
||
54 | 'file' => 'InputType', |
||
55 | 'image' => 'InputType', |
||
56 | 'color' => 'InputType', |
||
57 | 'datetime-local' => 'InputType', |
||
58 | 'month' => 'InputType', |
||
59 | 'range' => 'InputType', |
||
60 | 'time' => 'InputType', |
||
61 | 'week' => 'InputType', |
||
62 | 'select' => 'SelectType', |
||
63 | 'textarea' => 'TextareaType', |
||
64 | 'button' => 'ButtonType', |
||
65 | 'submit' => 'ButtonType', |
||
66 | 'reset' => 'ButtonType', |
||
67 | 'radio' => 'CheckableType', |
||
68 | 'checkbox' => 'CheckableType', |
||
69 | 'choice' => 'ChoiceType', |
||
70 | 'form' => 'ChildFormType', |
||
71 | 'entity' => 'EntityType', |
||
72 | 'collection' => 'CollectionType', |
||
73 | 'repeated' => 'RepeatedType', |
||
74 | 'static' => 'StaticType' |
||
75 | ]; |
||
76 | |||
77 | /** |
||
78 | * Custom types |
||
79 | * |
||
80 | * @var array |
||
81 | */ |
||
82 | private $customTypes = []; |
||
83 | |||
84 | /** |
||
85 | * @param View $view |
||
86 | * @param TranslatorInterface $translator |
||
87 | * @param array $config |
||
88 | */ |
||
89 | 102 | public function __construct(View $view, TranslatorInterface $translator, array $config = []) |
|
90 | { |
||
91 | 102 | $this->view = $view; |
|
92 | 102 | $this->translator = $translator; |
|
93 | 102 | $this->config = $config; |
|
94 | 102 | $this->loadCustomTypes(); |
|
95 | 102 | } |
|
96 | |||
97 | /** |
||
98 | * @param string $key |
||
99 | * @param string $default |
||
100 | * @return mixed |
||
101 | */ |
||
102 | 102 | public function getConfig($key, $default = null) |
|
103 | { |
||
104 | 102 | return array_get($this->config, $key, $default); |
|
105 | } |
||
106 | |||
107 | /** |
||
108 | * @return View |
||
109 | */ |
||
110 | 34 | public function getView() |
|
111 | { |
||
112 | 34 | return $this->view; |
|
113 | } |
||
114 | |||
115 | /** |
||
116 | * Merge options array |
||
117 | * |
||
118 | * @param array $first |
||
119 | * @param array $second |
||
120 | * @return array |
||
121 | */ |
||
122 | 102 | public function mergeOptions(array $first, array $second) |
|
123 | { |
||
124 | 102 | return array_replace_recursive($first, $second); |
|
125 | } |
||
126 | |||
127 | /** |
||
128 | * Get proper class for field type |
||
129 | * |
||
130 | * @param $type |
||
131 | * @return string |
||
132 | */ |
||
133 | 65 | public function getFieldType($type) |
|
134 | { |
||
135 | 65 | $types = array_keys(static::$availableFieldTypes); |
|
136 | |||
137 | 65 | if (!$type || trim($type) == '') { |
|
138 | 1 | throw new \InvalidArgumentException('Field type must be provided.'); |
|
139 | } |
||
140 | |||
141 | 64 | if (array_key_exists($type, $this->customTypes)) { |
|
142 | 2 | return $this->customTypes[$type]; |
|
143 | } |
||
144 | |||
145 | 62 | if (!in_array($type, $types)) { |
|
146 | 2 | throw new \InvalidArgumentException( |
|
147 | 2 | sprintf( |
|
148 | 2 | 'Unsupported field type [%s]. Available types are: %s', |
|
149 | 2 | $type, |
|
150 | 2 | join(', ', array_merge($types, array_keys($this->customTypes))) |
|
151 | 2 | ) |
|
152 | 2 | ); |
|
153 | } |
||
154 | |||
155 | 60 | $namespace = __NAMESPACE__.'\\Fields\\'; |
|
156 | |||
157 | 60 | return $namespace . static::$availableFieldTypes[$type]; |
|
158 | } |
||
159 | |||
160 | /** |
||
161 | * Convert array of attributes to html attributes |
||
162 | * |
||
163 | * @param $options |
||
164 | * @return string |
||
165 | */ |
||
166 | 81 | public function prepareAttributes($options) |
|
167 | { |
||
168 | 81 | if (!$options) { |
|
169 | 4 | return null; |
|
170 | } |
||
171 | |||
172 | 81 | $attributes = []; |
|
173 | |||
174 | 81 | foreach ($options as $name => $option) { |
|
175 | 81 | if ($option !== null) { |
|
176 | 81 | $name = is_numeric($name) ? $option : $name; |
|
177 | 81 | $attributes[] = $name.'="'.$option.'" '; |
|
178 | 81 | } |
|
179 | 81 | } |
|
180 | |||
181 | 81 | return join('', $attributes); |
|
182 | } |
||
183 | |||
184 | /** |
||
185 | * Add custom field |
||
186 | * |
||
187 | * @param $name |
||
188 | * @param $class |
||
189 | */ |
||
190 | 3 | public function addCustomField($name, $class) |
|
198 | |||
199 | /** |
||
200 | * Load custom field types from config file |
||
201 | */ |
||
202 | 102 | private function loadCustomTypes() |
|
203 | { |
||
212 | |||
213 | 5 | public function convertModelToArray($model) |
|
214 | { |
||
215 | 5 | if (!$model) { |
|
216 | 1 | return null; |
|
217 | } |
||
218 | |||
219 | 5 | if ($model instanceof Model) { |
|
220 | 1 | return $model->toArray(); |
|
221 | } |
||
222 | |||
223 | 5 | if ($model instanceof Collection) { |
|
224 | 2 | return $model->all(); |
|
225 | } |
||
226 | |||
227 | 5 | return $model; |
|
228 | } |
||
229 | |||
230 | /** |
||
231 | * Format the label to the proper format |
||
232 | * |
||
233 | * @param $name |
||
234 | * @return string |
||
235 | */ |
||
236 | 79 | public function formatLabel($name) |
|
252 | |||
253 | /** |
||
254 | * @param FormField[] $fields |
||
255 | * @return array |
||
256 | */ |
||
257 | 7 | public function mergeFieldsRules($fields) |
|
277 | |||
278 | /** |
||
279 | * @return array |
||
280 | */ |
||
281 | 1 | public function mergeAttributes($fields) |
|
282 | { |
||
283 | 1 | $attributes = []; |
|
284 | 1 | foreach ($fields as $field) { |
|
285 | 1 | $attributes = array_merge($attributes, $field->getAllAttributes()); |
|
286 | 1 | } |
|
287 | |||
288 | 1 | return $attributes; |
|
289 | } |
||
290 | |||
291 | /** |
||
292 | * @param string $string |
||
293 | * @return string |
||
294 | */ |
||
295 | 80 | public function transformToDotSyntax($string) |
|
299 | |||
300 | /** |
||
301 | * @return TranslatorInterface |
||
302 | */ |
||
303 | 3 | public function getTranslator() |
|
307 | |||
308 | /** |
||
309 | * Check if field name is valid and not reserved |
||
310 | * |
||
311 | * @throws \InvalidArgumentException |
||
312 | * @param string $name |
||
313 | * @param string $className |
||
314 | */ |
||
315 | 51 | public function checkFieldName($name, $className) |
|
332 | } |
||
333 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: