AttributesInterface
API reference › HTML Contracts › Attribute › AttributesInterface
Implemented by 4 types
Defines the public contract for objects that manage HTML attributes.
Provides an immutable API for adding, updating, reading, and removing HTML attributes on elements.
Methods
Section titled “Methods”| Method | Description | Defined by |
|---|---|---|
addAttribute() abstract | Adds or updates a single HTML attribute for the element. | AttributesInterface |
attributes() abstract | Adds or updates the supplied HTML attributes, preserving all other attributes. | AttributesInterface |
class() abstract | Sets the class attribute. | AttributesInterface |
getAttribute() abstract | Returns the value of a single HTML attribute. | AttributesInterface |
getAttributes() abstract | Returns the array of HTML attributes for the element. | AttributesInterface |
removeAttribute() abstract | Removes a specific HTML attribute from the element. | AttributesInterface |
Method details
Section titled “Method details”addAttribute()
Section titled “addAttribute() ”abstract public function addAttribute(string|UnitEnum $key, mixed $value): staticAdds or updates a single HTML attribute for the element.
Parameters
| Parameter | Type | Description |
|---|---|---|
$key | string|UnitEnum | Attribute name. |
$value | mixed | Attribute value. Use null to remove the attribute. |
Returns
static: New instance with the updated attribute.
attributes()
Section titled “attributes() ”abstract public function attributes(mixed[] $values): staticAdds or updates the supplied HTML attributes, preserving all other attributes.
Parameters
| Parameter | Type | Description |
|---|---|---|
$values | mixed[] | Associative array of attribute keys and values. |
Returns
static: New instance with the updated attributes.
class()
Section titled “class() ”abstract public function class(string|Stringable|UnitEnum|null $value, bool $override = false): staticSets the class attribute.
Parameters
| Parameter | Type | Description |
|---|---|---|
$value | string|Stringable|UnitEnum| | CSS class value, or null to remove the attribute. |
$override | bool | Whether to override existing classes (true) or merge (false). |
Returns
static: New instance with the updated class attribute.
getAttribute()
Section titled “getAttribute() ”abstract public function getAttribute(string|UnitEnum $key, mixed $default = null): mixedReturns the value of a single HTML attribute.
Parameters
| Parameter | Type | Description |
|---|---|---|
$key | string|UnitEnum | Attribute name. |
$default | mixed | Default value when the attribute is missing. |
Returns
mixed: Attribute value or default.
getAttributes()
Section titled “getAttributes() ”abstract public function getAttributes(): mixed[]Returns the array of HTML attributes for the element.
Returns
mixed[]: Attributes array assigned to the element.
removeAttribute()
Section titled “removeAttribute() ”abstract public function removeAttribute(string|UnitEnum $key): staticRemoves a specific HTML attribute from the element.
Parameters
| Parameter | Type | Description |
|---|---|---|
$key | string|UnitEnum | Attribute name to remove. |
Returns
static: New instance without the specified attribute.