BaseAttributeBag
API reference › HTML Helper › Base › BaseAttributeBag
1 subclass
Provides reusable operations for associative HTML attribute bags.
Methods
Section titled “Methods”| Method | Description | Defined by |
|---|---|---|
get() static | Returns an attribute value by key. | BaseAttributeBag |
normalizeKey() static | Normalizes an attribute key ensuring it has a specific prefix. | BaseAttributeBag |
remove() static | Removes an attribute from the bag. | BaseAttributeBag |
replace() static | Replaces all attributes in the bag. | BaseAttributeBag |
set() static | Sets a plain attribute key/value pair. | BaseAttributeBag |
setMany() static | Sets multiple plain attributes. | BaseAttributeBag |
Method details
Section titled “Method details”get()
Section titled “get() ”public static function get(mixed[] $attributes, string|UnitEnum $key, mixed $default = null, string $prefix = ''): mixedReturns an attribute value by key.
Parameters
| Parameter | Type | Description |
|---|---|---|
$attributes | mixed[] | Attribute bag. |
$key | string|UnitEnum | Attribute key. |
$default | mixed | Default value when key is not present. |
$prefix | string | Prefix to ensure (for example, aria-, data-, on). |
Returns
mixed: Attribute value or default.
Throws
InvalidArgumentException: if normalized key is not a non-emptystring.
normalizeKey()
Section titled “normalizeKey() ”public static function normalizeKey(mixed $key, string $prefix): stringNormalizes an attribute key ensuring it has a specific prefix.
Returns the key unchanged when it already has the prefix.
Parameters
| Parameter | Type | Description |
|---|---|---|
$key | mixed | Key to normalize. Accepts strings, Stringable objects, or UnitEnum cases. |
$prefix | string | Prefix to ensure (for example, aria-, data-, on). |
Returns
string: Normalized key with the prefix ensured.
Throws
InvalidArgumentException: if the key is empty, not astring, or cannot be normalized to astring.
remove()
Section titled “remove() ”public static function remove(mixed[] &$attributes, string|UnitEnum $key, string $prefix = ''): voidRemoves an attribute from the bag.
Parameters
| Parameter | Type | Description |
|---|---|---|
$attributes | mixed[] | Attribute bag to update in place. |
$key | string|UnitEnum | Attribute key. |
$prefix | string | Prefix to ensure (for example, aria-, data-, on). |
Throws
InvalidArgumentException: if normalized key is not a non-emptystring.
replace()
Section titled “replace() ”public static function replace(mixed[] &$attributes, mixed[] $values, string $prefix = ''): voidReplaces all attributes in the bag.
Parameters
| Parameter | Type | Description |
|---|---|---|
$attributes | mixed[] | Attribute bag to replace in place. |
$values | mixed[] | Values to set after clearing the bag. |
$prefix | string | Prefix to ensure (for example, aria-, data-, on). |
Throws
InvalidArgumentException: if any key normalization fails.
set()
Section titled “set() ”public static function set(mixed[] &$attributes, string|UnitEnum $key, mixed $value, string $prefix = ''): voidSets a plain attribute key/value pair.
Parameters
| Parameter | Type | Description |
|---|---|---|
$attributes | mixed[] | Attribute bag to update in place. |
$key | string|UnitEnum | Attribute key to normalize. |
$value | mixed | Attribute value. Use null to remove the attribute. |
$prefix | string | Prefix to ensure (for example, aria-, data-, on). |
Throws
InvalidArgumentException: if key normalization fails.
setMany()
Section titled “setMany() ”public static function setMany(mixed[] &$attributes, mixed[] $values, string $prefix = ''): voidSets multiple plain attributes.
Parameters
| Parameter | Type | Description |
|---|---|---|
$attributes | mixed[] | Attribute bag to update in place. |
$values | mixed[] | Values to set. |
$prefix | string | Prefix to ensure (for example, aria-, data-, on). |
Throws
InvalidArgumentException: if any key normalization fails.