|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Anax\HTMLForm; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Factory to create form elements. |
|
7
|
|
|
*/ |
|
8
|
|
|
class FormElementFactory |
|
9
|
|
|
{ |
|
10
|
|
|
/** |
|
11
|
|
|
* Create a formelement from an array, factory returns the correct |
|
12
|
|
|
* instance. |
|
13
|
|
|
* |
|
14
|
|
|
* @param string $name name of the element. |
|
15
|
|
|
* @param array $attributes to use when creating the element. |
|
16
|
|
|
* |
|
17
|
|
|
* @return the instance of the form element. |
|
18
|
|
|
*/ |
|
19
|
|
|
public static function create($name, $attributes) |
|
20
|
|
|
{ |
|
21
|
|
|
// Not supported is type=image, <button>, list, output, select-optgroup |
|
22
|
|
|
$types = [ |
|
23
|
|
|
|
|
24
|
|
|
// Standard HTML 4.01 |
|
25
|
|
|
'text' => '\Anax\HTMLForm\FormElementText', |
|
26
|
|
|
'file' => '\Anax\HTMLForm\FormElementFile', |
|
27
|
|
|
'password' => '\Anax\HTMLForm\FormElementPassword', |
|
28
|
|
|
'hidden' => '\Anax\HTMLForm\FormElementHidden', |
|
29
|
|
|
'textarea' => '\Anax\HTMLForm\FormElementTextarea', |
|
30
|
|
|
'radio' => '\Anax\HTMLForm\FormElementRadio', |
|
31
|
|
|
'checkbox' => '\Anax\HTMLForm\FormElementCheckbox', |
|
32
|
|
|
'select' => '\Anax\HTMLForm\FormElementSelect', |
|
33
|
|
|
'select-multiple' => '\Anax\HTMLForm\FormElementSelectMultiple', |
|
34
|
|
|
'submit' => '\Anax\HTMLForm\FormElementSubmit', |
|
35
|
|
|
'reset' => '\Anax\HTMLForm\FormElementReset', |
|
36
|
|
|
'button' => '\Anax\HTMLForm\FormElementButton', |
|
37
|
|
|
|
|
38
|
|
|
// HTML5 |
|
39
|
|
|
'color' => '\Anax\HTMLForm\FormElementColor', |
|
40
|
|
|
'date' => '\Anax\HTMLForm\FormElementDate', |
|
41
|
|
|
'number' => '\Anax\HTMLForm\FormElementNumber', |
|
42
|
|
|
'range' => '\Anax\HTMLForm\FormElementRange', |
|
43
|
|
|
'tel' => '\Anax\HTMLForm\FormElementTel', |
|
44
|
|
|
'email' => '\Anax\HTMLForm\FormElementEmail', |
|
45
|
|
|
'url' => '\Anax\HTMLForm\FormElementUrl', |
|
46
|
|
|
'search' => '\Anax\HTMLForm\FormElementSearch', |
|
47
|
|
|
'file-multiple' => '\Anax\HTMLForm\FormElementFileMultiple', |
|
48
|
|
|
'datetime' => '\Anax\HTMLForm\FormElementDatetime', |
|
49
|
|
|
'datetime-local' => '\Anax\HTMLForm\FormElementDatetimeLocal', |
|
50
|
|
|
'month' => '\Anax\HTMLForm\FormElementMonth', |
|
51
|
|
|
'time' => '\Anax\HTMLForm\FormElementTime', |
|
52
|
|
|
'week' => '\Anax\HTMLForm\FormElementWeek', |
|
53
|
|
|
|
|
54
|
|
|
// Custom |
|
55
|
|
|
'search-widget' => '\Anax\HTMLForm\FormElementSearchWidget', |
|
56
|
|
|
'checkbox-multiple' => '\Anax\HTMLForm\FormElementCheckboxMultiple', |
|
57
|
|
|
// Address |
|
58
|
|
|
]; |
|
59
|
|
|
|
|
60
|
|
|
// $attributes['type'] must contain a valid type creating an object |
|
61
|
|
|
// to succeed. |
|
62
|
|
|
$type = isset($attributes['type']) ? $attributes['type'] : null; |
|
63
|
|
|
if (!($type && isset($types[$type]))) { |
|
64
|
|
|
throw new Exception("Form element does not exists and can not be created: $name - $type"); |
|
65
|
|
|
} |
|
66
|
|
|
return new $types[$type]($name, $attributes); |
|
67
|
|
|
} |
|
68
|
|
|
} |
|
69
|
|
|
|