Skip to content
PHP Ecosystem
Explore

HasAttributes

API reference › HTML Mixin › HasAttributes

traitUIAwesome\Html\Mixin\HasAttributes
Sourcesrc/HasAttributes.php
Used by 4 types

Provides an immutable API for managing HTML attributes.

See also: https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes.

PropertyTypeDescriptionDefined by
$attributes protectedmixed[]HTML attributes array used by the implementing class. Default: [].HasAttributes
MethodDescriptionDefined by
addAttribute()Adds or updates a single HTML attribute for the element.HasAttributes
attributes()Adds or updates multiple HTML attributes for the element.HasAttributes
getAttribute()Returns the value of a single HTML attribute.HasAttributes
getAttributes()Returns the array of HTML attributes for the element.HasAttributes
removeAttribute()Removes a specific HTML attribute from the element.HasAttributes
replaceAttributes()Replaces all HTML attributes for the element.HasAttributes
public function addAttribute(string|UnitEnum $key, mixed $value): static

Adds or updates a single HTML attribute for the element.

Parameters

ParameterTypeDescription
$keystring|UnitEnumAttribute name.
$valuemixedAttribute value. Use null to remove the attribute.

Returns

static: New instance with the updated attributes value.

public function attributes(mixed[] $values): static

Adds or updates multiple HTML attributes for the element.

Parameters

ParameterTypeDescription
$valuesmixed[]Associative array of attribute keys and values.

Returns

static: New instance with the updated attributes value.

public function getAttribute(string|UnitEnum $key, mixed $default = null): mixed

Returns the value of a single HTML attribute.

Parameters

ParameterTypeDescription
$keystring|UnitEnumAttribute name.
$defaultmixedDefault value when the attribute is missing.

Returns

mixed: Attribute value or default.

public function getAttributes(): mixed[]

Returns the array of HTML attributes for the element.

Returns

mixed[]: Attributes array assigned to the element.

public function removeAttribute(string|UnitEnum $key): static

Removes a specific HTML attribute from the element.

Parameters

ParameterTypeDescription
$keystring|UnitEnumAttribute name to remove.

Returns

static: New instance without the specified attribute value.

public function replaceAttributes(mixed[] $values): static

Replaces all HTML attributes for the element.

Parameters

ParameterTypeDescription
$valuesmixed[]Associative array of attribute keys and values.

Returns

static: New instance with the replaced attributes value.