11namespace Depage\HtmlForm\Abstracts;
13use Depage\HtmlForm\Exceptions;
102 $this->checkName(
$name);
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;
124 $this->defaults[
'log'] =
null;
125 $this->defaults[
'class'] =
null;
126 $this->defaults[
'dataAttr'] = [];
127 $this->defaults[
'disabled'] =
false;
142 public function __call(
string $function, array $arguments): mixed
144 if (substr($function, 0, 4) ===
'html') {
145 $attribute = str_replace(
'html',
'', $function);
146 $attribute[0] = strtolower($attribute[0]);
148 $escapedAttribute = $attribute .
"Html";
150 if (!empty($this->$escapedAttribute)) {
151 return $this->$escapedAttribute;
153 if (!isset($this->$attribute)) {
217 private function checkName(
$name): void
221 || trim(
$name) ===
''
222 || preg_match(
'/[^a-zA-Z0-9_\-\[\]]/',
$name)
238 protected function log(
string $argument, ?
string $type =
null): void
240 if (is_callable([$this->
log,
'log'])) {
241 $this->
log->log($argument, $type);
243 if (gettype($argument) !=
'string') {
246 $message = ob_get_contents();
249 $message = $argument;
263 protected function htmlEscape(array|
string $options = []): array|string
265 if (is_string($options)) {
266 $htmlOptions = htmlspecialchars($options, ENT_QUOTES);
267 } elseif (is_array($options)) {
270 foreach ($options as $index => $option) {
271 if (is_string($index)) {
272 $index = htmlspecialchars($index, ENT_QUOTES);
274 if (is_string($option)) {
275 $option = htmlspecialchars($option, ENT_QUOTES);
278 $htmlOptions[$index] = $option;
281 $htmlOptions = $options;
294 protected function htmlList(?array $options =
null, array|
string|
null $value =
null): string
305 if (is_array($this->dataAttr)) {
306 foreach ($this->dataAttr as $key => $val) {
308 $key = mb_strtolower($key);
311 if (!is_string($key) || !preg_match(
'/^[a-z][a-z0-9]*(-[a-z0-9]+)*$/', $key)) {
315 $attributes .=
" data-$key=\"" . $this->
htmlEscape((
string) $val) .
"\"";
$valid
Contains element validation status/result.
$class
CSS class of the container element.
__call(string $function, array $arguments)
HTML escaping.
$defaults
holds default values for element attributes
getName()
Returns the element name.
clearValue()
resets the value to null
htmlDataAttributes()
Returns dataAttr escaped as attribute string.
__construct(string $name, array $parameters, ?object $form)
element class constructor
$disabled
wether a input element will be disabled
setDisabled(bool $disabled=true)
Sets the HTML disabled-attribute of the current input element.
log(string $argument, ?string $type=null)
error & warning logger
$log
Log object reference.
htmlList(?array $options=null, array|string|null $value=null)
Renders HTML datalist.
$validated
True if the element has been validated before.
setDefaults()
Collects initial values across subclasses.
getDisabled()
Gets if input is currently disabled.
$dataAttr
Extra information about the data that is saved inside the element.
htmlEscape(array|string $options=[])
Escapes HTML in strings and arrays of strings.
thrown when element name is empty or contains invalid characters
thrown when attemting to instantiate an inexistent element class