# BaseAttributeBag

> Provides reusable operations for associative HTML attribute bags.

[API reference](/docs/api/index.md) › [HTML Helper](/docs/extensions/html-helper/reference/index.md) › [Base](/docs/extensions/html-helper/reference/base/index.md) › BaseAttributeBag

| abstract class | `UIAwesome\Html\Helper\Base\BaseAttributeBag` |
| --- | --- |
| Source | [`src/Base/BaseAttributeBag.php`](https://github.com/ui-awesome/html-helper/blob/main/src/Base/BaseAttributeBag.php#L22) |

1 subclass:

- [`UIAwesome\Html\Helper\AttributeBag`](/docs/extensions/html-helper/reference/attributebag/index.md)

Provides reusable operations for associative HTML attribute bags.

## Methods

| Method | Description | Defined by |
| --- | --- | --- |
| [`get()`](#method-get) static | Returns an attribute value by key. | `BaseAttributeBag` |
| [`normalizeKey()`](#method-normalizeKey) static | Normalizes an attribute key ensuring it has a specific prefix. | `BaseAttributeBag` |
| [`remove()`](#method-remove) static | Removes an attribute from the bag. | `BaseAttributeBag` |
| [`replace()`](#method-replace) static | Replaces all attributes in the bag. | `BaseAttributeBag` |
| [`set()`](#method-set) static | Sets a plain attribute key/value pair. | `BaseAttributeBag` |
| [`setMany()`](#method-setMany) static | Sets multiple plain attributes. | `BaseAttributeBag` |

## Method details

### get() <span id="method-get"></span>

```php
public static function get(mixed[] $attributes, string|UnitEnum $key, mixed $default = null, string $prefix = ''): mixed
```

Returns an attribute value by key.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$attributes` | `mixed[]` | Attribute bag. |
| `$key` | `string\|`[`UnitEnum`](https://www.php.net/manual/en/class.unitenum.php) | 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`](https://www.php.net/manual/en/class.invalidargumentexception.php): if normalized key is not a non-empty `string`.

### normalizeKey() <span id="method-normalizeKey"></span>

```php
public static function normalizeKey(mixed $key, string $prefix): string
```

Normalizes 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`](https://www.php.net/manual/en/class.invalidargumentexception.php): if the key is empty, not a `string`, or cannot be normalized to a `string`.

### remove() <span id="method-remove"></span>

```php
public static function remove(mixed[] &$attributes, string|UnitEnum $key, string $prefix = ''): void
```

Removes an attribute from the bag.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$attributes` | `mixed[]` | Attribute bag to update in place. |
| `$key` | `string\|`[`UnitEnum`](https://www.php.net/manual/en/class.unitenum.php) | Attribute key. |
| `$prefix` | `string` | Prefix to ensure (for example, `aria-`, `data-`, `on`). |

**Throws**

- [`InvalidArgumentException`](https://www.php.net/manual/en/class.invalidargumentexception.php): if normalized key is not a non-empty `string`.

### replace() <span id="method-replace"></span>

```php
public static function replace(mixed[] &$attributes, mixed[] $values, string $prefix = ''): void
```

Replaces 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`](https://www.php.net/manual/en/class.invalidargumentexception.php): if any key normalization fails.

### set() <span id="method-set"></span>

```php
public static function set(mixed[] &$attributes, string|UnitEnum $key, mixed $value, string $prefix = ''): void
```

Sets a plain attribute key/value pair.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$attributes` | `mixed[]` | Attribute bag to update in place. |
| `$key` | `string\|`[`UnitEnum`](https://www.php.net/manual/en/class.unitenum.php) | 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`](https://www.php.net/manual/en/class.invalidargumentexception.php): if key normalization fails.

### setMany() <span id="method-setMany"></span>

```php
public static function setMany(mixed[] &$attributes, mixed[] $values, string $prefix = ''): void
```

Sets 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`](https://www.php.net/manual/en/class.invalidargumentexception.php): if any key normalization fails.