|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* WebHemi. |
|
4
|
|
|
* |
|
5
|
|
|
* PHP version 7.1 |
|
6
|
|
|
* |
|
7
|
|
|
* @copyright 2012 - 2017 Gixx-web (http://www.gixx-web.com) |
|
8
|
|
|
* @license https://opensource.org/licenses/MIT The MIT License (MIT) |
|
9
|
|
|
* |
|
10
|
|
|
* @link http://www.gixx-web.com |
|
11
|
|
|
*/ |
|
12
|
|
|
declare(strict_types=1); |
|
13
|
|
|
|
|
14
|
|
|
namespace WebHemi\Form\Html; |
|
15
|
|
|
|
|
16
|
|
|
use InvalidArgumentException; |
|
17
|
|
|
use WebHemi\Form\FormElementInterface; |
|
18
|
|
|
use WebHemi\Traits\CamelCaseToUnderScoreTrait; |
|
19
|
|
|
use WebHemi\Validator\ValidatorInterface; |
|
20
|
|
|
|
|
21
|
|
|
class HtmlFormElement implements FormElementInterface |
|
22
|
|
|
{ |
|
23
|
|
|
public const HTML_ELEMENT_BUTTON = 'button'; |
|
24
|
|
|
public const HTML_ELEMENT_FORM = 'form'; |
|
25
|
|
|
public const HTML_ELEMENT_INPUT_CHECKBOX = 'checkbox'; |
|
26
|
|
|
public const HTML_ELEMENT_INPUT_FILE = 'file'; |
|
27
|
|
|
public const HTML_ELEMENT_INPUT_HIDDEN = 'hidden'; |
|
28
|
|
|
public const HTML_ELEMENT_INPUT_IMAGE = 'IMAGE'; |
|
29
|
|
|
public const HTML_ELEMENT_INPUT_PASSWORD = 'password'; |
|
30
|
|
|
public const HTML_ELEMENT_INPUT_RADIO = 'radio'; |
|
31
|
|
|
public const HTML_ELEMENT_INPUT_RESET = 'reset'; |
|
32
|
|
|
public const HTML_ELEMENT_INPUT_SUBMIT = 'submit'; |
|
33
|
|
|
public const HTML_ELEMENT_INPUT_TEXT = 'text'; |
|
34
|
|
|
public const HTML_ELEMENT_SELECT = 'select'; |
|
35
|
|
|
public const HTML_ELEMENT_TEXTAREA = 'textarea'; |
|
36
|
|
|
|
|
37
|
|
|
/** @var string */ |
|
38
|
|
|
private $name; |
|
39
|
|
|
/** @var string */ |
|
40
|
|
|
private $id; |
|
41
|
|
|
/** @var string */ |
|
42
|
|
|
private $label; |
|
43
|
|
|
/** @var string */ |
|
44
|
|
|
private $type; |
|
45
|
|
|
/** @var array */ |
|
46
|
|
|
private $values = []; |
|
47
|
|
|
/** @var array */ |
|
48
|
|
|
private $valueRange = []; |
|
49
|
|
|
/** @var array<ValidatorInterface> */ |
|
50
|
|
|
private $validators = []; |
|
51
|
|
|
/** @var array */ |
|
52
|
|
|
private $errors = []; |
|
53
|
|
|
/** @var array */ |
|
54
|
|
|
protected $validTypes = [ |
|
55
|
|
|
self::HTML_ELEMENT_BUTTON, |
|
56
|
|
|
self::HTML_ELEMENT_FORM, |
|
57
|
|
|
self::HTML_ELEMENT_INPUT_CHECKBOX, |
|
58
|
|
|
self::HTML_ELEMENT_INPUT_FILE, |
|
59
|
|
|
self::HTML_ELEMENT_INPUT_HIDDEN, |
|
60
|
|
|
self::HTML_ELEMENT_INPUT_IMAGE, |
|
61
|
|
|
self::HTML_ELEMENT_INPUT_PASSWORD, |
|
62
|
|
|
self::HTML_ELEMENT_INPUT_RADIO, |
|
63
|
|
|
self::HTML_ELEMENT_INPUT_RESET, |
|
64
|
|
|
self::HTML_ELEMENT_INPUT_SUBMIT, |
|
65
|
|
|
self::HTML_ELEMENT_INPUT_TEXT, |
|
66
|
|
|
self::HTML_ELEMENT_SELECT, |
|
67
|
|
|
self::HTML_ELEMENT_TEXTAREA, |
|
68
|
|
|
]; |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Converts CamelCase text to under_score equivalent. |
|
72
|
|
|
* Converts any text to Identifier-like string |
|
73
|
|
|
*/ |
|
74
|
|
|
use CamelCaseToUnderScoreTrait; |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* HtmlFormElement constructor. |
|
78
|
|
|
* |
|
79
|
|
|
* @param string $type The type of the form element. |
|
80
|
|
|
* @param string $name For HTML forms it is the name attribute. |
|
81
|
|
|
* @param string $label Optional. The label of the element. If not set the $name should be used. |
|
82
|
|
|
* @param array $values Optional. The values of the element. |
|
83
|
|
|
* @param array $valueRange Optional. The range of interpretation. |
|
84
|
|
|
*/ |
|
85
|
|
|
public function __construct( |
|
86
|
|
|
string $type, |
|
87
|
|
|
string $name, |
|
88
|
|
|
string $label = null, |
|
89
|
|
|
array $values = [], |
|
90
|
|
|
array $valueRange = [] |
|
91
|
|
|
) { |
|
92
|
|
|
if (!in_array($type, $this->validTypes)) { |
|
93
|
|
|
throw new InvalidArgumentException( |
|
94
|
|
|
sprintf( |
|
95
|
|
|
'The given type "%s" is not a valid %s type.', |
|
96
|
|
|
$type, |
|
97
|
|
|
__CLASS__ |
|
98
|
|
|
) |
|
99
|
|
|
); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
$this->setName($name); |
|
103
|
|
|
|
|
104
|
|
|
$this->type = $type; |
|
105
|
|
|
$this->label = $label; |
|
106
|
|
|
$this->values = $values; |
|
107
|
|
|
$this->valueRange = $valueRange; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* Sets element's name. |
|
112
|
|
|
* |
|
113
|
|
|
* @param string $name |
|
114
|
|
|
* @throws InvalidArgumentException |
|
115
|
|
|
* @return FormElementInterface |
|
116
|
|
|
*/ |
|
117
|
|
|
public function setName(string $name) : FormElementInterface |
|
118
|
|
|
{ |
|
119
|
|
|
if (empty($name)) { |
|
120
|
|
|
throw new InvalidArgumentException('During conversion the argument value become an empty string!'); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
$this->name = $name; |
|
124
|
|
|
|
|
125
|
|
|
$name = $this->camelCaseToUnderscore($name); |
|
126
|
|
|
$name = preg_replace('/[^a-zA-Z0-9]/', '_', $name); |
|
127
|
|
|
$this->id = trim(str_replace('__', '_', $name), '_'); |
|
128
|
|
|
|
|
129
|
|
|
return $this; |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* Gets the element's name. |
|
134
|
|
|
* |
|
135
|
|
|
* @return string |
|
136
|
|
|
*/ |
|
137
|
|
|
public function getName() : string |
|
138
|
|
|
{ |
|
139
|
|
|
return $this->name; |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* Gets the element's name. |
|
144
|
|
|
* |
|
145
|
|
|
* @return string |
|
146
|
|
|
*/ |
|
147
|
|
|
public function getId() : string |
|
148
|
|
|
{ |
|
149
|
|
|
return $this->id; |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* Gets the element's type. |
|
154
|
|
|
* |
|
155
|
|
|
* @return string |
|
156
|
|
|
*/ |
|
157
|
|
|
public function getType() : string |
|
158
|
|
|
{ |
|
159
|
|
|
return $this->type; |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
/** |
|
163
|
|
|
* Sets the element's label. |
|
164
|
|
|
* |
|
165
|
|
|
* @param string $label |
|
166
|
|
|
* @return FormElementInterface |
|
167
|
|
|
*/ |
|
168
|
|
|
public function setLabel(string $label) : FormElementInterface |
|
169
|
|
|
{ |
|
170
|
|
|
$this->label = $label; |
|
171
|
|
|
|
|
172
|
|
|
return $this; |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
/** |
|
176
|
|
|
* Gets the element's label. |
|
177
|
|
|
* |
|
178
|
|
|
* @return string |
|
179
|
|
|
*/ |
|
180
|
|
|
public function getLabel() : string |
|
181
|
|
|
{ |
|
182
|
|
|
return $this->label ?? $this->name; |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
/** |
|
186
|
|
|
* Sets the values. |
|
187
|
|
|
* |
|
188
|
|
|
* @param array $values |
|
189
|
|
|
* @return FormElementInterface |
|
190
|
|
|
*/ |
|
191
|
|
|
public function setValues(array $values) : FormElementInterface |
|
192
|
|
|
{ |
|
193
|
|
|
$this->values = $values; |
|
194
|
|
|
|
|
195
|
|
|
return $this; |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
|
|
/** |
|
199
|
|
|
* Gets the values. |
|
200
|
|
|
* |
|
201
|
|
|
* @return array |
|
202
|
|
|
*/ |
|
203
|
|
|
public function getValues() : array |
|
204
|
|
|
{ |
|
205
|
|
|
return $this->values; |
|
206
|
|
|
} |
|
207
|
|
|
|
|
208
|
|
|
/** |
|
209
|
|
|
* Sets the range of interpretation. Depends on the element type how it is used: exact element list or a min/max. |
|
210
|
|
|
* |
|
211
|
|
|
* @param array $valueRange |
|
212
|
|
|
* @return FormElementInterface |
|
213
|
|
|
*/ |
|
214
|
|
|
public function setValueRange(array $valueRange) : FormElementInterface |
|
215
|
|
|
{ |
|
216
|
|
|
$this->valueRange = $valueRange; |
|
217
|
|
|
|
|
218
|
|
|
return $this; |
|
219
|
|
|
} |
|
220
|
|
|
|
|
221
|
|
|
/** |
|
222
|
|
|
* Get the range of interpretation. |
|
223
|
|
|
* |
|
224
|
|
|
* @return array |
|
225
|
|
|
*/ |
|
226
|
|
|
public function getValueRange() : array |
|
227
|
|
|
{ |
|
228
|
|
|
return $this->valueRange; |
|
229
|
|
|
} |
|
230
|
|
|
|
|
231
|
|
|
/** |
|
232
|
|
|
* Adds a validator to the element. |
|
233
|
|
|
* |
|
234
|
|
|
* @param ValidatorInterface $validator |
|
235
|
|
|
* @return FormElementInterface |
|
236
|
|
|
*/ |
|
237
|
|
|
public function addValidator(ValidatorInterface $validator) : FormElementInterface |
|
238
|
|
|
{ |
|
239
|
|
|
$this->validators[] = $validator; |
|
240
|
|
|
} |
|
241
|
|
|
|
|
242
|
|
|
/** |
|
243
|
|
|
* Validates the element. |
|
244
|
|
|
* |
|
245
|
|
|
* @return FormElementInterface |
|
246
|
|
|
*/ |
|
247
|
|
|
public function validate() : FormElementInterface |
|
248
|
|
|
{ |
|
249
|
|
|
$result = []; |
|
250
|
|
|
|
|
251
|
|
|
/** @var ValidatorInterface $validator */ |
|
252
|
|
|
foreach ($this->validators as $validator) { |
|
253
|
|
|
if (!$validator->validate($this->values)) { |
|
254
|
|
|
$result[get_class($validator)] = $validator->getErrors(); |
|
255
|
|
|
} |
|
256
|
|
|
} |
|
257
|
|
|
|
|
258
|
|
|
return $this; |
|
259
|
|
|
} |
|
260
|
|
|
|
|
261
|
|
|
/** |
|
262
|
|
|
* Returns the errors collected during the validation. |
|
263
|
|
|
* |
|
264
|
|
|
* @return array |
|
265
|
|
|
*/ |
|
266
|
|
|
public function getErrors() : array |
|
267
|
|
|
{ |
|
268
|
|
|
return $this->errors; |
|
269
|
|
|
} |
|
270
|
|
|
} |
|
271
|
|
|
|