formulier-klasse
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
<?php
/**
* HTML Interface for drawing HTML elements
* @version 0.9
* @author Kees Schepers <[email protected]>
*/
interface htmlInterface {
/**
* Returns HTML content
* @return string html
*/
public function getHTML();
}
/**
* Exceptions which will be thrown for this libary.
* @author Kees Schepers <[email protected]>
*/
class Form_Exception extends Exception {}
/**
* A HTML element object which is used for generating parts of the form
* @version 0.9
* @author Kees Schepers <[email protected]>
*/
abstract class Form_Html_Element implements htmlInterface {
/**
* The tagname of the current element drawing
*
* @var string
*/
protected $tagName;
/**
* Indicates the HTMLtag is selfclosing or not
*
* @var unknown_type
*/
protected $selfClosing;
/**
* An numeric array with HTML properties which are allowed
*
* @var array
*/
protected $allowedProperties;
/**
* A set of properties, key is the name, value is the value
*
* @var array
*/
private $properties;
public function __construct() {
$this->properties = array();
$this->allowedProperties = array();
$this->selfClosing = false;
$this->tagName = null;
}
/**
* Set a desired property of this HTML element
*
* @param string $propertyName
* @param string $propertyValue
* @return boolean
*/
public function __set($propertyName,$propertyValue) {
if(in_array($propertyName,$this->allowedProperties)) {
$this->properties[$propertyName] = $propertyValue;
return true;
} else {
throw new Form_Exception('Property ('.$propertyName.') is not known.');
}
}
/**
* Get a desired property of the this HTML element
*
* @param string $propertyName
* @return string
*/
public function __get($propertyName) {
switch ($propertyName) {
case 'text' :
return $this->text;
default :
if(in_array($propertyName,$this->allowedProperties)) {
return $this->properties[$propertyName];
} else {
throw new Form_Exception('Property ('.$propertyName.') is not known.');
}
}
}
/**
* Returns all of the properties
*
* @return array
*/
protected function getProperties() {
return $this->properties;
}
/**
* Draw the property, and draw innerHTML of parent properties of neccasary
*
* @param string $innerHTML HTML contents
* @return string HTML contents
*/
public function getHTML($innerHTML=false) {
if(is_null($this->tagName)) throw new Form_Exception('Tagname of Form_Html_Element not set yet.');
$str = '';
$str = '<'.$this->tagName.' ';
foreach ($this->getProperties() as $name=>$property) {
$str .= $name.'="'.$property.'" ';
}
if($this->selfClosing) {
$str .= '/>';
} else {
$str .= '>'.(!$innerHTML ? '' : $innerHTML).'</'.$this->tagName.'>';
}
return $str;
}
}
/**
* Class the generate forms
*
* @version 0.9
* @author Kees Schepers <[email protected]>
*
*/
class Form extends Form_Html_Element {
protected $fields;
public function __construct() {
parent::__construct();
$this->fields = array();
$this->allowedProperties[] = 'method';
$this->allowedProperties[] = 'action';
$this->allowedProperties[] = 'enctype';
$this->allowedProperties[] = 'id';
$this->allowedProperties[] = 'class';
$this->tagName = 'form';
$this->selfClosing = false;
}
public function addField($label,Form_Field $field) {
$this->fields[$field->name] = array('label' => $label, 'field' => $field);
}
public function getHTML() {
$innerHTML = '';
foreach ($this->fields as $fieldElements) {
$innerHTML .= '<div>';
if($fieldElements['label'] instanceof Form_Label ) {
$innerHTML .= $fieldElements['label']->getHTML();
}
$innerHTML .= $fieldElements['field']->getHTML();
$innerHTML .= '</div>';
}
return parent::getHTML($innerHTML);
}
}
/**
* Abstract class for a form field. A form field can either be a input field or a textarea
*
* @author Kees Schepers <[email protected]>
*/
abstract class Form_Field extends Form_Html_Element {}
/**
* The abstract class for generating the labels for a form field. The children tell the class
* wether it's a div or something else.
*
* @author Kees Schepers <[email protected]>
*/
abstract class Form_Label extends Form_Html_Element {
const SAFE_LABEL = true;
protected $text;
public function __construct($text) {
parent::__construct();
$this->text = self::SAFE_LABEL ? htmlentities($text) : $text;
}
/**
* The inner text can be protected against HTML code
*
* @param string $propertyName
* @param string $propertyValue
* @return boolean
*/
public function __set($propertyName,$propertyValue) {
switch ($propertyName) {
case 'text':
$this->text = self::SAFE_LABEL ? htmlentities($propertyValue) : $propertyValue;
return true;
default:
return parent::__set($propertyName,$propertyValue);
}
}
/**
* Text is special property, returns property value
*
* @param string $propertyName
* @return string
*/
public function __get($propertyName) {
switch ($propertyName) {
case 'text' :
return $this->text;
default :
return parent::__get($propertyName);
}
}
}
/**
* Input elements
*
* @author Kees Schepers <[email protected]>
*/
final class Form_Field_Input extends Form_Field {
public function __construct($type,$name) {
parent::__construct();
$this->allowedProperties[] = 'class';
$this->allowedProperties[] = 'name';
$this->allowedProperties[] = 'type';
$this->allowedProperties[] = 'value';
$this->selfClosing = true;
$this->tagName = 'input';
$this->type = $type;
$this->name = $name;
}
}
/**
* Class for drawing textarea's
*
* @author Kees Schepers <[email protected]>
*/
final class Form_Field_TextArea extends Form_Field {
const SAFE_LABEL = true;
protected $text;
/**
*
* @param string $text innertext of the element
* @param string $name html property name
* @param integer $cols number of editable columns
* @param integer $rows number of editable rows
*/
public function __construct($text,$name,$cols,$rows) {
parent::__construct();
$this->allowedProperties[] = 'name';
$this->allowedProperties[] = 'id';
$this->allowedProperties[] = 'class';
$this->allowedProperties[] = 'cols';
$this->allowedProperties[] = 'rows';
$this->text = self::SAFE_LABEL ? htmlentities($text) : $text;
$this->tagName = 'textarea';
$this->selfClosing = false;
$this->name = $name;
$this->cols = $cols;
$this->rows = $rows;
}
/**
*
* @param string $propertyName
* @param string $propertyValue
* @return boolean
*/
public function __set($propertyName,$propertyValue) {
switch ($propertyName) {
case 'text':
$this->text = self::SAFE_LABEL ? htmlentities($propertyValue) : $propertyValue;
return true;
default:
return parent::__set($propertyName,$propertyValue);
}
}
/**
*
* @param string $propertyName
* @return string
*/
public function __get($propertyName) {
switch ($propertyName) {
case 'text' :
return $this->text;
default :
return parent::__get($propertyName);
}
}
/**
*
* @return string
*/
public function getHTML() {
return parent::getHTML($this->text);
}
}
/**
* A field label with a DIV before it.
* @author Kees Schepers <[email protected]>
*/
final class Form_Label_Div extends Form_Label {
/**
*
* @param string $text innertext of the element
*/
public function __construct($text) {
parent::__construct($text);
$this->text = $text;
$this->tagName = 'div';
$this->selfClosing = false;
}
/**
*
* @return string HTML contents
*/
public function getHTML() {
return parent::getHTML($this->text);
}
}
/**
* Voorbeeld
*/
$form = new Form();
$form->addField(new Form_Label_Div('Gebruikersnaam'),new Form_Field_Input('text','username'));
$form->addField(new Form_Label_Div('E-mail'),new Form_Field_Input('text','e-mail'));
$form->addField(null,new Form_Field_TextArea('Test','comments',40,10));
echo $form->getHTML();
?>
/**
* HTML Interface for drawing HTML elements
* @version 0.9
* @author Kees Schepers <[email protected]>
*/
interface htmlInterface {
/**
* Returns HTML content
* @return string html
*/
public function getHTML();
}
/**
* Exceptions which will be thrown for this libary.
* @author Kees Schepers <[email protected]>
*/
class Form_Exception extends Exception {}
/**
* A HTML element object which is used for generating parts of the form
* @version 0.9
* @author Kees Schepers <[email protected]>
*/
abstract class Form_Html_Element implements htmlInterface {
/**
* The tagname of the current element drawing
*
* @var string
*/
protected $tagName;
/**
* Indicates the HTMLtag is selfclosing or not
*
* @var unknown_type
*/
protected $selfClosing;
/**
* An numeric array with HTML properties which are allowed
*
* @var array
*/
protected $allowedProperties;
/**
* A set of properties, key is the name, value is the value
*
* @var array
*/
private $properties;
public function __construct() {
$this->properties = array();
$this->allowedProperties = array();
$this->selfClosing = false;
$this->tagName = null;
}
/**
* Set a desired property of this HTML element
*
* @param string $propertyName
* @param string $propertyValue
* @return boolean
*/
public function __set($propertyName,$propertyValue) {
if(in_array($propertyName,$this->allowedProperties)) {
$this->properties[$propertyName] = $propertyValue;
return true;
} else {
throw new Form_Exception('Property ('.$propertyName.') is not known.');
}
}
/**
* Get a desired property of the this HTML element
*
* @param string $propertyName
* @return string
*/
public function __get($propertyName) {
switch ($propertyName) {
case 'text' :
return $this->text;
default :
if(in_array($propertyName,$this->allowedProperties)) {
return $this->properties[$propertyName];
} else {
throw new Form_Exception('Property ('.$propertyName.') is not known.');
}
}
}
/**
* Returns all of the properties
*
* @return array
*/
protected function getProperties() {
return $this->properties;
}
/**
* Draw the property, and draw innerHTML of parent properties of neccasary
*
* @param string $innerHTML HTML contents
* @return string HTML contents
*/
public function getHTML($innerHTML=false) {
if(is_null($this->tagName)) throw new Form_Exception('Tagname of Form_Html_Element not set yet.');
$str = '';
$str = '<'.$this->tagName.' ';
foreach ($this->getProperties() as $name=>$property) {
$str .= $name.'="'.$property.'" ';
}
if($this->selfClosing) {
$str .= '/>';
} else {
$str .= '>'.(!$innerHTML ? '' : $innerHTML).'</'.$this->tagName.'>';
}
return $str;
}
}
/**
* Class the generate forms
*
* @version 0.9
* @author Kees Schepers <[email protected]>
*
*/
class Form extends Form_Html_Element {
protected $fields;
public function __construct() {
parent::__construct();
$this->fields = array();
$this->allowedProperties[] = 'method';
$this->allowedProperties[] = 'action';
$this->allowedProperties[] = 'enctype';
$this->allowedProperties[] = 'id';
$this->allowedProperties[] = 'class';
$this->tagName = 'form';
$this->selfClosing = false;
}
public function addField($label,Form_Field $field) {
$this->fields[$field->name] = array('label' => $label, 'field' => $field);
}
public function getHTML() {
$innerHTML = '';
foreach ($this->fields as $fieldElements) {
$innerHTML .= '<div>';
if($fieldElements['label'] instanceof Form_Label ) {
$innerHTML .= $fieldElements['label']->getHTML();
}
$innerHTML .= $fieldElements['field']->getHTML();
$innerHTML .= '</div>';
}
return parent::getHTML($innerHTML);
}
}
/**
* Abstract class for a form field. A form field can either be a input field or a textarea
*
* @author Kees Schepers <[email protected]>
*/
abstract class Form_Field extends Form_Html_Element {}
/**
* The abstract class for generating the labels for a form field. The children tell the class
* wether it's a div or something else.
*
* @author Kees Schepers <[email protected]>
*/
abstract class Form_Label extends Form_Html_Element {
const SAFE_LABEL = true;
protected $text;
public function __construct($text) {
parent::__construct();
$this->text = self::SAFE_LABEL ? htmlentities($text) : $text;
}
/**
* The inner text can be protected against HTML code
*
* @param string $propertyName
* @param string $propertyValue
* @return boolean
*/
public function __set($propertyName,$propertyValue) {
switch ($propertyName) {
case 'text':
$this->text = self::SAFE_LABEL ? htmlentities($propertyValue) : $propertyValue;
return true;
default:
return parent::__set($propertyName,$propertyValue);
}
}
/**
* Text is special property, returns property value
*
* @param string $propertyName
* @return string
*/
public function __get($propertyName) {
switch ($propertyName) {
case 'text' :
return $this->text;
default :
return parent::__get($propertyName);
}
}
}
/**
* Input elements
*
* @author Kees Schepers <[email protected]>
*/
final class Form_Field_Input extends Form_Field {
public function __construct($type,$name) {
parent::__construct();
$this->allowedProperties[] = 'class';
$this->allowedProperties[] = 'name';
$this->allowedProperties[] = 'type';
$this->allowedProperties[] = 'value';
$this->selfClosing = true;
$this->tagName = 'input';
$this->type = $type;
$this->name = $name;
}
}
/**
* Class for drawing textarea's
*
* @author Kees Schepers <[email protected]>
*/
final class Form_Field_TextArea extends Form_Field {
const SAFE_LABEL = true;
protected $text;
/**
*
* @param string $text innertext of the element
* @param string $name html property name
* @param integer $cols number of editable columns
* @param integer $rows number of editable rows
*/
public function __construct($text,$name,$cols,$rows) {
parent::__construct();
$this->allowedProperties[] = 'name';
$this->allowedProperties[] = 'id';
$this->allowedProperties[] = 'class';
$this->allowedProperties[] = 'cols';
$this->allowedProperties[] = 'rows';
$this->text = self::SAFE_LABEL ? htmlentities($text) : $text;
$this->tagName = 'textarea';
$this->selfClosing = false;
$this->name = $name;
$this->cols = $cols;
$this->rows = $rows;
}
/**
*
* @param string $propertyName
* @param string $propertyValue
* @return boolean
*/
public function __set($propertyName,$propertyValue) {
switch ($propertyName) {
case 'text':
$this->text = self::SAFE_LABEL ? htmlentities($propertyValue) : $propertyValue;
return true;
default:
return parent::__set($propertyName,$propertyValue);
}
}
/**
*
* @param string $propertyName
* @return string
*/
public function __get($propertyName) {
switch ($propertyName) {
case 'text' :
return $this->text;
default :
return parent::__get($propertyName);
}
}
/**
*
* @return string
*/
public function getHTML() {
return parent::getHTML($this->text);
}
}
/**
* A field label with a DIV before it.
* @author Kees Schepers <[email protected]>
*/
final class Form_Label_Div extends Form_Label {
/**
*
* @param string $text innertext of the element
*/
public function __construct($text) {
parent::__construct($text);
$this->text = $text;
$this->tagName = 'div';
$this->selfClosing = false;
}
/**
*
* @return string HTML contents
*/
public function getHTML() {
return parent::getHTML($this->text);
}
}
/**
* Voorbeeld
*/
$form = new Form();
$form->addField(new Form_Label_Div('Gebruikersnaam'),new Form_Field_Input('text','username'));
$form->addField(new Form_Label_Div('E-mail'),new Form_Field_Input('text','e-mail'));
$form->addField(null,new Form_Field_TextArea('Test','comments',40,10));
echo $form->getHTML();
?>