SHOW:
|
|
- or go back to the newest paste.
1 | <?php | |
2 | ||
3 | function _($x) { | |
4 | return htmlspecialchars($x); | |
5 | } | |
6 | ||
7 | function filter($x) { | |
8 | $r = array(); | |
9 | foreach ($x as $v) { | |
10 | if ($v) array_push($r, $v); | |
11 | } | |
12 | return $r; | |
13 | } | |
14 | ||
15 | class Input { | |
16 | ||
17 | // The type of the input-field is stored in the attribute-slot. | |
18 | private $typeName; | |
19 | ||
20 | // This is an array of elements that will be rendered when the input-field | |
21 | // type requires sub-elements, such as the `select` input-field. The values | |
22 | // inserted here depend on the input-field type. They will be ignored when | |
23 | // the input-field type does not require sub-elements. | |
24 | private $subElements; | |
25 | ||
26 | // Attributes of the input-field are saved in this array. | |
27 | private $attributes; | |
28 | ||
29 | // This array contains all css-classes for the input-field and will be | |
30 | // treated special from the $attributes slot. The class-value generated | |
31 | // here will overwrite attributes set in the $attributes array. | |
32 | private $classes; | |
33 | ||
34 | public function __construct($typeName, $id=null, $class=null) { | |
35 | $this->typeName = $typeName; | |
36 | $this->subElements = array(); | |
37 | $this->attributes = array(); | |
38 | $this->classes = array(); | |
39 | ||
40 | if ($id !== null) | |
41 | $this->setAttribute('id', $id); | |
42 | if ($class !== null) | |
43 | $this->setClasses(explode(' ', $class)); | |
44 | } | |
45 | ||
46 | public function setClasses($classes) { | |
47 | - | foreach ($args as $className) { |
47 | + | |
48 | - | $contained = array_search($className, $this->classes); |
48 | + | |
49 | - | if ($contained === false) { |
49 | + | |
50 | - | array_push($this->classes, $className); |
50 | + | |
51 | } | |
52 | ||
53 | public function addClass(/* ... */) { | |
54 | $args = func_get_args(); | |
55 | $this->addClassA($args); | |
56 | } | |
57 | ||
58 | public function addClassA($array) { | |
59 | foreach ($array as $className) { | |
60 | // Class-names with spaces in it will be added seperately, again. | |
61 | $multipleNames = filter(explode(' ', $className)); | |
62 | if (count($multipleNames) > 1) { | |
63 | $this->addClassA($multipleNames); | |
64 | } | |
65 | elseif ($multipleNames) { | |
66 | $className = $multipleNames[0]; | |
67 | ||
68 | $contained = array_search($className, $this->classes); | |
69 | if ($contained === false) { | |
70 | array_push($this->classes, $className); | |
71 | } | |
72 | } | |
73 | } | |
74 | } | |
75 | ||
76 | public function removeClass($className) { | |
77 | $index = array_search($className, $this->classes); | |
78 | if ($index !== false) { | |
79 | unset($this->classes[$index]); | |
80 | } | |
81 | } | |
82 | ||
83 | public function setAttribute($name, $value) { | |
84 | if ($name == 'class' or $name == 'type') { | |
85 | - | $string .= '>'; |
85 | + | |
86 | } | |
87 | $this->attributes[$name] = $value; | |
88 | } | |
89 | - | $string .= '</input>'; |
89 | + | |
90 | // removeAttribute() | |
91 | // hasAttribute() | |
92 | ||
93 | public function render() { | |
94 | $string = '<input type="' . _($this->typeName) . '" '; | |
95 | ||
96 | - | $i = new Input('text', 'myInput', 'foo bar'); |
96 | + | |
97 | - | $i->addClass('foo', 'baar', 'spam', 'eggs'); |
97 | + | |
98 | - | print $i->render(); |
98 | + | |
99 | $string .= 'class="' . $classNames . '" '; | |
100 | } | |
101 | ||
102 | // Render the rest of the attributes. | |
103 | foreach ($this->attributes as $key => $value) { | |
104 | $string .= $key . '="' . _($value) . '" '; | |
105 | } | |
106 | ||
107 | // Special treatment for `select`, etc.. | |
108 | ||
109 | $string .= '/>'; | |
110 | ||
111 | return $string; | |
112 | } | |
113 | ||
114 | } | |
115 | ?> | |
116 | <!DOCTYPE html> | |
117 | <html> | |
118 | <body> | |
119 | <?php | |
120 | $i = new Input('text', 'myInput', 'foo bar'); | |
121 | $i->addClass('foo', 'baar', 'spam FU', 'eggs'); | |
122 | print $i->render(); | |
123 | ?> | |
124 | </body> | |
125 | </html> |