|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace ntentan\honam\engines\php\helpers\form; |
|
4
|
|
|
|
|
5
|
|
|
use ntentan\honam\TemplateRenderer; |
|
6
|
|
|
|
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* The form element class. |
|
10
|
|
|
* An element can be anything from the form itself to the elements on the form. This class provides an abstract render |
|
11
|
|
|
* method that elements use to output their HTML code. Any visible element of the form must have this class as a parent. |
|
12
|
|
|
*/ |
|
13
|
|
|
abstract class Element |
|
14
|
|
|
{ |
|
15
|
|
|
use ElementFactory; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* The id of the form useful for CSS styling and DOM access. |
|
19
|
|
|
* |
|
20
|
|
|
* @var string |
|
21
|
|
|
*/ |
|
22
|
|
|
protected $id = ""; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* The label of the form element. |
|
26
|
|
|
* |
|
27
|
|
|
* @var string |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $label = ""; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* The description of the form element. |
|
33
|
|
|
* |
|
34
|
|
|
* @var string |
|
35
|
|
|
*/ |
|
36
|
|
|
protected $description = ""; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* An array of all the CSS classes associated with this element. |
|
40
|
|
|
* |
|
41
|
|
|
* @var array |
|
42
|
|
|
*/ |
|
43
|
|
|
protected $classes = []; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* An array of all HTML attributes. These attributes are stored as |
|
47
|
|
|
* objects of the Attribute class. Attributes in this array are applied |
|
48
|
|
|
* directly to the form element. |
|
49
|
|
|
* |
|
50
|
|
|
* @var array |
|
51
|
|
|
*/ |
|
52
|
|
|
protected $attributes = []; |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* An array of all HTML attributes. These attributes are stored as |
|
56
|
|
|
* objects of the Attribute class. Attributes in this array are applied |
|
57
|
|
|
* directly to the wrapper which wraps the form element. |
|
58
|
|
|
* |
|
59
|
|
|
* @var array |
|
60
|
|
|
*/ |
|
61
|
|
|
//protected $wrapperAttributes = []; |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* An array of all error messages associated with this element. |
|
65
|
|
|
* Error messages are setup during validation, when any element |
|
66
|
|
|
* fails its validation test. |
|
67
|
|
|
* |
|
68
|
|
|
* @var array |
|
69
|
|
|
*/ |
|
70
|
|
|
protected $errors = array(); |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* A boolean value which is set to true whenever there is an error |
|
74
|
|
|
* assiciated with the field element in one way or the other. |
|
75
|
|
|
* @var boolean |
|
76
|
|
|
*/ |
|
77
|
|
|
protected $error = false; |
|
78
|
|
|
|
|
79
|
|
|
protected $name; |
|
80
|
|
|
|
|
81
|
|
|
protected $hasLabel = true; |
|
82
|
|
|
|
|
83
|
|
|
protected $templateRenderer; |
|
84
|
|
|
|
|
85
|
|
|
public function __construct(string $label = "") |
|
86
|
|
|
{ |
|
87
|
|
|
$this->setLabel($label); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
public function setId(string $id): Element |
|
91
|
|
|
{ |
|
92
|
|
|
$this->id = $id; |
|
93
|
|
|
$this->setAttribute('id', $this->id); |
|
94
|
|
|
return $this; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
public function getId(): string |
|
98
|
|
|
{ |
|
99
|
|
|
return $this->id; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* Public accessor for setting the name property of the field. |
|
104
|
|
|
* |
|
105
|
|
|
* @param string $name |
|
106
|
|
|
* @return Element |
|
107
|
|
|
*/ |
|
108
|
|
|
public function setName($name): Element |
|
109
|
|
|
{ |
|
110
|
|
|
$this->name = $name; |
|
111
|
|
|
return $this; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* Public accessor for getting the name property of the field. |
|
116
|
|
|
* |
|
117
|
|
|
* @return string |
|
118
|
|
|
*/ |
|
119
|
|
|
public function getName(): string |
|
120
|
|
|
{ |
|
121
|
|
|
return $this->name; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* Set a label for the form element. |
|
126
|
|
|
*/ |
|
127
|
|
|
public function setLabel(string $label): Element |
|
128
|
|
|
{ |
|
129
|
|
|
$this->label = $label; |
|
130
|
|
|
return $this; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* Get the label for the form element. |
|
135
|
|
|
*/ |
|
136
|
|
|
public function getLabel(): string |
|
137
|
|
|
{ |
|
138
|
|
|
return $this->label; |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* Gets the description which is attached to this element. The description is normally displayed under the element |
|
143
|
|
|
* when rendering HTML. |
|
144
|
|
|
* |
|
145
|
|
|
* @return string |
|
146
|
|
|
*/ |
|
147
|
|
|
public function getDescription(): string |
|
148
|
|
|
{ |
|
149
|
|
|
return $this->description; |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* Sets the description which is attached to this element. The description is normally displayed under the element |
|
154
|
|
|
* when rendering HTML. |
|
155
|
|
|
* |
|
156
|
|
|
* @return Element |
|
157
|
|
|
*/ |
|
158
|
|
|
public function setDescription($description): Element |
|
159
|
|
|
{ |
|
160
|
|
|
$this->description = $description; |
|
161
|
|
|
return $this; |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* Returns all the arrays associated with this document. |
|
166
|
|
|
* |
|
167
|
|
|
* @return array |
|
168
|
|
|
*/ |
|
169
|
|
|
public function getErrors(): array |
|
170
|
|
|
{ |
|
171
|
|
|
return $this->errors; |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
public function hasError(): bool |
|
175
|
|
|
{ |
|
176
|
|
|
return $this->error; |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
/** |
|
180
|
|
|
* Renders the form element by outputing the HTML associated with the element. This method is abstract and it is |
|
181
|
|
|
* implemented by all the other classes which inherit the Element class. |
|
182
|
|
|
* |
|
183
|
|
|
* @return string |
|
184
|
|
|
*/ |
|
185
|
|
|
abstract public function render(); |
|
186
|
|
|
|
|
187
|
|
|
public function __toString() |
|
188
|
|
|
{ |
|
189
|
|
|
return $this->templateRenderer->render("forms_layout_element.tpl.php", array('element' => $this)); |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
/** |
|
193
|
|
|
* Adds an attribute to the element or its surrounding scope. |
|
194
|
|
|
*/ |
|
195
|
|
|
public function setAttribute(string $attribute, string $value) : Element |
|
196
|
|
|
{ |
|
197
|
|
|
$this->attributes[$attribute] = $value; |
|
198
|
|
|
return $this; |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
/** |
|
202
|
|
|
* Returns an HTML representation of all the attributes. This method is normally called when rendering the HTML for |
|
203
|
|
|
* the element. |
|
204
|
|
|
*/ |
|
205
|
|
|
public function getAttributes() : array |
|
206
|
|
|
{ |
|
207
|
|
|
return $this->attributes; |
|
208
|
|
|
} |
|
209
|
|
|
|
|
210
|
|
|
public function getAttribute(string $attribute) : ?string |
|
211
|
|
|
{ |
|
212
|
|
|
return $this->attributes[$attribute] ?? null; |
|
213
|
|
|
} |
|
214
|
|
|
|
|
215
|
|
|
public function setErrors(array $errors) : Element |
|
216
|
|
|
{ |
|
217
|
|
|
$this->errors = $errors; |
|
218
|
|
|
$this->error = true; |
|
219
|
|
|
return $this; |
|
220
|
|
|
} |
|
221
|
|
|
|
|
222
|
|
|
public function setTemplateRenderer(TemplateRenderer $templateRenderer) |
|
223
|
|
|
{ |
|
224
|
|
|
$this->templateRenderer = $templateRenderer; |
|
225
|
|
|
} |
|
226
|
|
|
|
|
227
|
|
|
public function getHasLabel(): bool |
|
228
|
|
|
{ |
|
229
|
|
|
return $this->hasLabel; |
|
230
|
|
|
} |
|
231
|
|
|
} |
|
232
|
|
|
|