13 private $attributes = [];
15 private $dataAttributes = [];
37 return isset($this[$name])? $this[$name] : $defaultValue;
61 return $this->
getAttribute(self::addDataPrefix($name), $defaultValue);
69 return $this->dataAttributes;
74 if (!isset($this[self::JSON_OPTIONS_DATA_ATTR]))
86 unset($this[self::JSON_OPTIONS_DATA_ATTR][$key]);
110 if (!isset($this[
'class']))
115 if (!in_array($className, $this[
'class'],
true))
117 $this[
'class'][] = $className;
130 return isset($this[
'class']) && in_array($className, $this[
'class'],
true);
135 $this[
'class'] = $classList;
147 if (!isset($this[
'class']))
152 $index = array_search($className, $this[
'class'],
true);
153 if ($index !==
false)
155 unset($this[
'class'][$index]);
170 return new \ArrayIterator($this->
toArray());
189 $offset = mb_strtolower($offset);
191 $asAttribute = isset($this->attributes[$offset]) || array_key_exists($offset, $this->attributes);
197 if (!self::hasDataPrefix($offset))
204 return isset($this->dataAttributes[$offset]) || array_key_exists($offset, $this->dataAttributes);
220 $offset = mb_strtolower($offset);
221 if (isset($this->attributes[$offset]) || array_key_exists($offset, $this->attributes))
223 return $this->attributes[$offset];
226 if (!self::hasDataPrefix($offset))
232 if (isset($this->dataAttributes[$offset]) || array_key_exists($offset, $this->dataAttributes))
234 return $this->dataAttributes[$offset];
258 $this->attributes[] = $value;
262 $offset = mb_strtolower($offset);
263 if (self::hasDataPrefix($offset))
269 $this->attributes[$offset] = $value;
287 $offset = mb_strtolower($offset);
288 if (isset($this->attributes[$offset]) || array_key_exists($offset, $this->attributes))
290 unset($this->attributes[$offset]);
295 if (!self::hasDataPrefix($offset))
301 if (isset($this->dataAttributes[$offset]) || array_key_exists($offset, $this->dataAttributes))
303 unset($this->dataAttributes[$offset]);
318 return count($this->dataAttributes) +
count($this->attributes);
326 return (
string)$this;
335 foreach ($this as $key => $value)
339 $string .=
"{$value} ";
343 if ($key ===
'class')
347 elseif ($key ===
'style')
351 elseif ($key === self::JSON_OPTIONS_DATA_ATTR)
356 $value = htmlspecialcharsbx($value);
357 $string .=
"{$key}=\"{$value}\" ";
366 if (is_string($classes))
371 return implode(
' ', $classes);
376 if (is_string($styles))
382 foreach ($styles as $name => $value)
384 $string .=
"{$name}:{$value};";
397 self::convertDataAttributesToAttributes($this->dataAttributes)
403 return "data-{$name}";
408 return is_string($name) && mb_substr($name, 0, 5) ===
'data-';
413 if (self::hasDataPrefix($name))
415 return mb_substr($name, 5);
424 foreach ($dataAttributes as $name => $attribute)
434 $anotherAttributes = $dataAttributes = [];
435 foreach ($attributes as $name => $attribute)
437 $name = mb_strtolower($name);
438 if (self::hasDataPrefix($name))
440 $dataAttributes[mb_substr($name, 5)] = $attribute;
444 $anotherAttributes[$name] = $attribute;
448 return [$dataAttributes, $anotherAttributes];