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
29abstract class Element
30{
34 protected $name;
35
39 public $valid;
40
44 protected $validated = false;
45
49 protected $log;
50
54 protected $defaults = [];
55
65 protected $disabled = false;
66
77 public $dataAttr;
78
88 protected $class;
89
98 public function __construct(string $name, array $parameters, object|null $form)
99 {
100 $this->checkName($name);
101
102 $this->name = $name;
103
104 $this->setDefaults();
105 $parameters = array_change_key_case($parameters);
106 foreach ($this->defaults as $parameter => $default) {
107 $this->$parameter = isset($parameters[strtolower($parameter)]) ? $parameters[strtolower($parameter)] : $default;
108 }
109 }
110
120 protected function setDefaults(): void
121 {
122 $this->defaults['log'] = null;
123 $this->defaults['class'] = null;
124 $this->defaults['dataAttr'] = [];
125 $this->defaults['disabled'] = false;
126 }
127
140 public function __call(string $function, array $arguments): mixed
141 {
142 if (substr($function, 0, 4) === 'html') {
143 $attribute = str_replace('html', '', $function);
144 $attribute[0] = strtolower($attribute[0]);
145
146 $escapedAttribute = $attribute . "Html";
147
148 if (!empty($this->$escapedAttribute)) {
149 return $this->$escapedAttribute;
150 }
151 if (!isset($this->$attribute)) {
152 trigger_error("Call to undefined method $function", E_USER_ERROR);
153 }
154
155 return $this->htmlEscape($this->$attribute);
156 } else {
157 trigger_error("Call to undefined method $function", E_USER_ERROR);
158 }
159 }
160
167 public function setDisabled(bool $disabled = true): self
168 {
169 $this->disabled = (bool) $disabled;
170
171 return $this;
172 }
173
180 public function getDisabled(): bool
181 {
182 return $this->disabled;
183 }
184
194 public function clearValue(): void {}
195
201 public function getName(): string
202 {
203 return $this->name;
204 }
205
215 private function checkName($name)
216 {
217 if (
218 !is_string($name)
219 || trim($name) === ''
220 || preg_match('/[^a-zA-Z0-9_\-\[\]]/', $name)
221 ) {
222 throw new Exceptions\InvalidElementNameException('"' . $name . '" is not a valid element name.');
223 }
224 }
225
236 protected function log(string $argument, string $type = null): void
237 {
238 if (is_callable([$this->log, 'log'])) {
239 $this->log->log($argument, $type);
240 } else {
241 if (gettype($argument) != 'string') {
242 ob_start();
243 print_r($argument);
244 $message = ob_get_contents();
245 ob_end_clean();
246 } else {
247 $message = $argument;
248 }
249 error_log($message);
250 }
251 }
252
261 protected function htmlEscape(array|string $options = []): array|string
262 {
263 if (is_string($options)) {
264 $htmlOptions = htmlspecialchars($options, ENT_QUOTES);
265 } elseif (is_array($options)) {
266 $htmlOptions = [];
267
268 foreach ($options as $index => $option) {
269 if (is_string($index)) {
270 $index = htmlspecialchars($index, ENT_QUOTES);
271 }
272 if (is_string($option)) {
273 $option = htmlspecialchars($option, ENT_QUOTES);
274 }
275
276 $htmlOptions[$index] = $option;
277 }
278 } else {
279 $htmlOptions = $options;
280 }
281
282 return $htmlOptions;
283 }
284
288 protected function htmlDataAttributes(): string
289 {
290 $attributes = "";
291 if (is_array($this->dataAttr)) {
292 foreach ($this->dataAttr as $key => $val) {
293 // @todo throw error when key is not plain string?
294 $attributes .= " data-$key=\"" . $this->htmlEscape($val) . "\"";
295 }
296 }
297
298 return $attributes;
299 }
300}
301
302/* vim:set ft=php sw=4 sts=4 fdm=marker et : */
behold: the über-class
Definition Element.php:30
$valid
Contains element validation status/result.
Definition Element.php:39
$class
CSS class of the container element.
Definition Element.php:88
__call(string $function, array $arguments)
HTML escaping.
Definition Element.php:140
$defaults
holds default values for element attributes
Definition Element.php:54
getName()
Returns the element name.
Definition Element.php:201
clearValue()
resets the value to null
Definition Element.php:194
htmlDataAttributes()
Returns dataAttr escaped as attribute string.
Definition Element.php:288
$disabled
wether a input element will be disabled
Definition Element.php:65
setDisabled(bool $disabled=true)
Sets the HTML disabled-attribute of the current input element.
Definition Element.php:167
$log
Log object reference.
Definition Element.php:49
log(string $argument, string $type=null)
error & warning logger
Definition Element.php:236
$validated
True if the element has been validated before.
Definition Element.php:44
setDefaults()
Collects initial values across subclasses.
Definition Element.php:120
getDisabled()
Gets if input is currently disabled.
Definition Element.php:180
$dataAttr
Extra information about the data that is saved inside the element.
Definition Element.php:77
htmlEscape(array|string $options=[])
Escapes HTML in strings and arrays of strings.
Definition Element.php:261
__construct(string $name, array $parameters, object|null $form)
element class constructor
Definition Element.php:98
thrown when element name is empty or contains invalid characters