# HasAttributes

> Provides an immutable API for managing HTML attributes.

[API reference](/docs/api/index.md) › [HTML Mixin](/docs/extensions/html-mixin/reference/index.md) › HasAttributes

| trait | `UIAwesome\Html\Mixin\HasAttributes` |
| --- | --- |
| Source | [`src/HasAttributes.php`](https://github.com/ui-awesome/html-mixin/blob/main/src/HasAttributes.php#L15) |

Used by 4 types:

- [`UIAwesome\Html\Core\Element\BaseBlock`](/docs/extensions/html-core/reference/element/baseblock/index.md)
- [`UIAwesome\Html\Core\Element\BaseInline`](/docs/extensions/html-core/reference/element/baseinline/index.md)
- [`UIAwesome\Html\Core\Element\BaseInput`](/docs/extensions/html-core/reference/element/baseinput/index.md)
- [`UIAwesome\Html\Core\Element\BaseVoid`](/docs/extensions/html-core/reference/element/basevoid/index.md)

Provides an immutable API for managing HTML attributes.

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

## Properties

| Property | Type | Description | Defined by |
| --- | --- | --- | --- |
| <span id="property-attributes"></span>`$attributes` protected | `mixed[]` | HTML attributes array used by the implementing class. Default: `[]`. | `HasAttributes` |

## Methods

| Method | Description | Defined by |
| --- | --- | --- |
| [`addAttribute()`](#method-addAttribute) | Adds or updates a single HTML attribute for the element. | `HasAttributes` |
| [`attributes()`](#method-attributes) | Adds or updates multiple HTML attributes for the element. | `HasAttributes` |
| [`getAttribute()`](#method-getAttribute) | Returns the value of a single HTML attribute. | `HasAttributes` |
| [`getAttributes()`](#method-getAttributes) | Returns the `array` of HTML attributes for the element. | `HasAttributes` |
| [`removeAttribute()`](#method-removeAttribute) | Removes a specific HTML attribute from the element. | `HasAttributes` |
| [`replaceAttributes()`](#method-replaceAttributes) | Replaces all HTML attributes for the element. | `HasAttributes` |

## Method details

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

```php
public function addAttribute(string|UnitEnum $key, mixed $value): static
```

Adds or updates a single HTML attribute for the element.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$key` | `string\|`[`UnitEnum`](https://www.php.net/manual/en/class.unitenum.php) | Attribute name. |
| `$value` | `mixed` | Attribute value. Use `null` to remove the attribute. |

**Returns**

`static`: New instance with the updated attributes value.

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

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

Adds or updates multiple HTML attributes for the element.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$values` | `mixed[]` | Associative array of attribute keys and values. |

**Returns**

`static`: New instance with the updated attributes value.

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

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

Returns the value of a single HTML attribute.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$key` | `string\|`[`UnitEnum`](https://www.php.net/manual/en/class.unitenum.php) | Attribute name. |
| `$default` | `mixed` | Default value when the attribute is missing. |

**Returns**

`mixed`: Attribute value or default.

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

```php
public function getAttributes(): mixed[]
```

Returns the `array` of HTML attributes for the element.

**Returns**

`mixed[]`: Attributes array assigned to the element.

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

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

Removes a specific HTML attribute from the element.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$key` | `string\|`[`UnitEnum`](https://www.php.net/manual/en/class.unitenum.php) | Attribute name to remove. |

**Returns**

`static`: New instance without the specified attribute value.

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

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

Replaces all HTML attributes for the element.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$values` | `mixed[]` | Associative array of attribute keys and values. |

**Returns**

`static`: New instance with the replaced attributes value.