11namespace Depage\HtmlForm\Abstracts;
13use Depage\HtmlForm\Exceptions;
100 $this->checkName(
$name);
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;
122 $this->defaults[
'log'] =
null;
123 $this->defaults[
'class'] =
null;
124 $this->defaults[
'dataAttr'] = [];
125 $this->defaults[
'disabled'] =
false;
140 public function __call(
string $function, array $arguments): mixed
142 if (substr($function, 0, 4) ===
'html') {
143 $attribute = str_replace(
'html',
'', $function);
144 $attribute[0] = strtolower($attribute[0]);
146 $escapedAttribute = $attribute .
"Html";
148 if (!empty($this->$escapedAttribute)) {
149 return $this->$escapedAttribute;
151 if (!isset($this->$attribute)) {
152 trigger_error(
"Call to undefined method $function", E_USER_ERROR);
157 trigger_error(
"Call to undefined method $function", E_USER_ERROR);
215 private function checkName(
$name)
219 || trim(
$name) ===
''
220 || preg_match(
'/[^a-zA-Z0-9_\-\[\]]/',
$name)
236 protected function log(
string $argument,
string $type =
null): void
238 if (is_callable([$this->
log,
'log'])) {
239 $this->
log->log($argument, $type);
241 if (gettype($argument) !=
'string') {
244 $message = ob_get_contents();
247 $message = $argument;
261 protected function htmlEscape(array|
string $options = []): array|string
263 if (is_string($options)) {
264 $htmlOptions = htmlspecialchars($options, ENT_QUOTES);
265 } elseif (is_array($options)) {
268 foreach ($options as $index => $option) {
269 if (is_string($index)) {
270 $index = htmlspecialchars($index, ENT_QUOTES);
272 if (is_string($option)) {
273 $option = htmlspecialchars($option, ENT_QUOTES);
276 $htmlOptions[$index] = $option;
279 $htmlOptions = $options;
291 if (is_array($this->dataAttr)) {
292 foreach ($this->dataAttr as $key => $val) {
294 $attributes .=
" data-$key=\"" . $this->
htmlEscape($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.
$disabled
wether a input element will be disabled
setDisabled(bool $disabled=true)
Sets the HTML disabled-attribute of the current input element.
$log
Log object reference.
log(string $argument, string $type=null)
error & warning logger
$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.
__construct(string $name, array $parameters, object|null $form)
element class constructor
thrown when element name is empty or contains invalid characters