# HasLabelCollection

> Provides an immutable API for label content and label attributes.

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

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

Provides an immutable API for label content and label attributes.

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

## Methods

| Method | Description | Defined by |
| --- | --- | --- |
| [`getLabel()`](#method-getLabel) | Returns the label content. | `HasLabelCollection` |
| [`getLabelAttribute()`](#method-getLabelAttribute) | Returns one label attribute value. | `HasLabelCollection` |
| [`getLabelAttributes()`](#method-getLabelAttributes) | Returns label attributes. | `HasLabelCollection` |
| [`isLabel()`](#method-isLabel) | Returns whether the label element should be rendered. | `HasLabelCollection` |
| [`label()`](#method-label) | Sets the label content. | `HasLabelCollection` |
| [`labelAttributes()`](#method-labelAttributes) | Sets the label attributes. | `HasLabelCollection` |
| [`labelClass()`](#method-labelClass) | Adds a CSS class to the label element attributes. | `HasLabelCollection` |
| [`labelFor()`](#method-labelFor) | Sets the `for` attribute of the label. | `HasLabelCollection` |
| [`labelRemoveAttribute()`](#method-labelRemoveAttribute) | Removes a specific label attribute. | `HasLabelCollection` |
| [`labelSetAttribute()`](#method-labelSetAttribute) | Sets a single HTML attribute for the label element. | `HasLabelCollection` |
| [`notLabel()`](#method-notLabel) | Sets whether to skip rendering the label. | `HasLabelCollection` |

## Method details

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

```php
public function getLabel(): string
```

Returns the label content.

**Returns**

`string`: Label content.

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

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

Returns one label attribute value.

**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. |
| `$prefix` | `string` | Prefix to ensure (for example, `aria-`, `data-`, `on`). |

**Returns**

`mixed`: Attribute value, or `$default` when missing.

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

```php
public function getLabelAttributes(): array
```

Returns label attributes.

**Returns**

`array`: Label attributes.

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

```php
public function isLabel(): bool
```

Returns whether the label element should be rendered.

**Returns**

`bool`: `true` if the label element should be rendered, or `false` to skip rendering.

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

```php
public function label(string|Stringable $content): static
```

Sets the label content.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$content` | `string\|`[`Stringable`](https://www.php.net/manual/en/class.stringable.php) | Label content. |

**Returns**

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

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

```php
public function labelAttributes(array $attributes, string $prefix = ''): static
```

Sets the label attributes.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$attributes` | `array` | Label attributes. |
| `$prefix` | `string` | Prefix to ensure (for example, `aria-`, `data-`, `on`). |

**Returns**

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

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

```php
public function labelClass(string|Stringable|UnitEnum|null $value, bool $override = false): static
```

Adds a CSS class to the label element attributes.

Passing `null` preserves existing classes, even when `$override` is `true`.
Use `labelRemoveAttribute('class')` to remove the attribute explicitly.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$value` | `string\|`[`Stringable`](https://www.php.net/manual/en/class.stringable.php)`\|`[`UnitEnum`](https://www.php.net/manual/en/class.unitenum.php)`\|null` | CSS class value, or `null` to preserve existing classes. |
| `$override` | `bool` | Whether to replace existing classes (`true`) or merge (`false`) when valid classes are supplied. |

**Returns**

`static`: New instance with the updated `labelAttributes['class']` value.

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

```php
public function labelFor(string|null $value): static
```

Sets the `for` attribute of the label.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$value` | `string\|null` | `for` attribute value. |

**Returns**

`static`: New instance with the updated `labelAttributes['for']` value.

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

```php
public function labelRemoveAttribute(string|UnitEnum $key, string $prefix = ''): static
```

Removes a specific label attribute.

**Parameters**

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

**Returns**

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

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

```php
public function labelSetAttribute(string|UnitEnum $key, mixed $value, string $prefix = ''): static
```

Sets a single HTML attribute for the label element.

**Parameters**

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

**Returns**

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

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

```php
public function notLabel(bool $value = true): static
```

Sets whether to skip rendering the label.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$value` | `bool` | Whether to skip rendering the label, or `false` to re-enable a suppressed label. |

**Returns**

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