depage-forms v1.4.1
html forms made easy
Loading...
Searching...
No Matches
Element.php
Go to the documentation of this file.
1<?php
2
10
11namespace Depage\HtmlForm\Abstracts;
12
13use Depage\HtmlForm\Exceptions;
14
31abstract class Element
32{
36 protected $name;
37
41 public $valid;
42
46 protected $validated = false;
47
51 protected $log;
52
56 protected $defaults = [];
57
67 protected $disabled = false;
68
79 public $dataAttr;
80
90 protected $class;
91
100 public function __construct(string $name, array $parameters, ?object $form)
101 {
102 $this->checkName($name);
103
104 $this->name = $name;
105
106 $this->setDefaults();
107 $parameters = array_change_key_case($parameters);
108 foreach ($this->defaults as $parameter => $default) {
109 $this->$parameter = isset($parameters[strtolower($parameter)]) ? $parameters[strtolower($parameter)] : $default;
110 }
111 }
112
122 protected function setDefaults(): void
123 {
124 $this->defaults['log'] = null;
125 $this->defaults['class'] = null;
126 $this->defaults['dataAttr'] = [];
127 $this->defaults['disabled'] = false;
128 }
129
142 public function __call(string $function, array $arguments): mixed
143 {
144 if (substr($function, 0, 4) === 'html') {
145 $attribute = str_replace('html', '', $function);
146 $attribute[0] = strtolower($attribute[0]);
147
148 $escapedAttribute = $attribute . "Html";
149
150 if (!empty($this->$escapedAttribute)) {
151 return $this->$escapedAttribute;
152 }
153 if (!isset($this->$attribute)) {
154 throw new Exceptions\UndefinedMethodException($function);
155 }
156
157 return $this->htmlEscape($this->$attribute);
158 } else {
159 throw new Exceptions\UndefinedMethodException($function);
160 }
161 }
162
169 public function setDisabled(bool $disabled = true): self
170 {
171 $this->disabled = (bool) $disabled;
172
173 return $this;
174 }
175
182 public function getDisabled(): bool
183 {
184 return $this->disabled;
185 }
186
196 public function clearValue(): void {}
197
203 public function getName(): string
204 {
205 return $this->name;
206 }
207
217 private function checkName($name): void
218 {
219 if (
220 !is_string($name)
221 || trim($name) === ''
222 || preg_match('/[^a-zA-Z0-9_\-\[\]]/', $name)
223 ) {
224 throw new Exceptions\InvalidElementNameException('"' . $name . '" is not a valid element name.');
225 }
226 }
227
238 protected function log(string $argument, ?string $type = null): void
239 {
240 if (is_callable([$this->log, 'log'])) {
241 $this->log->log($argument, $type);
242 } else {
243 if (gettype($argument) != 'string') {
244 ob_start();
245 print_r($argument);
246 $message = ob_get_contents();
247 ob_end_clean();
248 } else {
249 $message = $argument;
250 }
251 error_log($message);
252 }
253 }
254
263 protected function htmlEscape(array|string $options = []): array|string
264 {
265 if (is_string($options)) {
266 $htmlOptions = htmlspecialchars($options, ENT_QUOTES);
267 } elseif (is_array($options)) {
268 $htmlOptions = [];
269
270 foreach ($options as $index => $option) {
271 if (is_string($index)) {
272 $index = htmlspecialchars($index, ENT_QUOTES);
273 }
274 if (is_string($option)) {
275 $option = htmlspecialchars($option, ENT_QUOTES);
276 }
277
278 $htmlOptions[$index] = $option;
279 }
280 } else {
281 $htmlOptions = $options;
282 }
283
284 return $htmlOptions;
285 }
286
294 protected function htmlList(?array $options = null, array|string|null $value = null): string
295 {
296 return "";
297 }
298
302 protected function htmlDataAttributes(): string
303 {
304 $attributes = "";
305 if (is_array($this->dataAttr)) {
306 foreach ($this->dataAttr as $key => $val) {
307 // normalize key to lowercase
308 $key = mb_strtolower($key);
309
310 // Validate that key is a safe HTML5 data attribute name
311 if (!is_string($key) || !preg_match('/^[a-z][a-z0-9]*(-[a-z0-9]+)*$/', $key)) {
312 continue;
313 }
314 // Also ensure the value is safely escaped
315 $attributes .= " data-$key=\"" . $this->htmlEscape((string) $val) . "\"";
316 }
317 }
318
319 return $attributes;
320 }
321}
322
323/* vim:set ft=php sw=4 sts=4 fdm=marker et : */
behold: the über-class
Definition Element.php:32
$valid
Contains element validation status/result.
Definition Element.php:41
$class
CSS class of the container element.
Definition Element.php:90
__call(string $function, array $arguments)
HTML escaping.
Definition Element.php:142
$defaults
holds default values for element attributes
Definition Element.php:56
getName()
Returns the element name.
Definition Element.php:203
clearValue()
resets the value to null
Definition Element.php:196
htmlDataAttributes()
Returns dataAttr escaped as attribute string.
Definition Element.php:302
__construct(string $name, array $parameters, ?object $form)
element class constructor
Definition Element.php:100
$disabled
wether a input element will be disabled
Definition Element.php:67
setDisabled(bool $disabled=true)
Sets the HTML disabled-attribute of the current input element.
Definition Element.php:169
log(string $argument, ?string $type=null)
error & warning logger
Definition Element.php:238
$log
Log object reference.
Definition Element.php:51
htmlList(?array $options=null, array|string|null $value=null)
Renders HTML datalist.
Definition Element.php:294
$validated
True if the element has been validated before.
Definition Element.php:46
setDefaults()
Collects initial values across subclasses.
Definition Element.php:122
getDisabled()
Gets if input is currently disabled.
Definition Element.php:182
$dataAttr
Extra information about the data that is saved inside the element.
Definition Element.php:79
htmlEscape(array|string $options=[])
Escapes HTML in strings and arrays of strings.
Definition Element.php:263
thrown when element name is empty or contains invalid characters
thrown when attemting to instantiate an inexistent element class