# ComponentContext

> Describes the semantic component and qualifiers used to resolve config recipes.

[API reference](/docs/api/index.md) › [HTML Core](/docs/extensions/html-core/reference/index.md) › [Config](/docs/extensions/html-core/reference/config/index.md) › ComponentContext

| final readonly class | `UIAwesome\Html\Core\Config\ComponentContext` |
| --- | --- |
| Source | [`src/Config/ComponentContext.php`](https://github.com/ui-awesome/html-core/blob/main/src/Config/ComponentContext.php#L24) |

Describes the semantic component and qualifiers used to resolve config recipes.

The component identifier, qualifiers, and states accept a string or a string-backed enum. Enum cases are normalized
to their backing values, so the public properties and [`ComponentContext::hasState()`](/docs/extensions/html-core/reference/config/componentcontext/index.md#method-hasState) always work with plain
strings, regardless of how the context was created.

## Properties

| Property | Type | Description | Defined by |
| --- | --- | --- | --- |
| <span id="property-component"></span>`$component` readonly | `string` | Semantic component identifier, normalized to a string. | `ComponentContext` |
| [`$metadata`](#property-metadata) readonly | `array<string, mixed>` | Extensible semantic metadata available to config providers. | `ComponentContext` |
| <span id="property-scheme"></span>`$scheme` readonly | `string\|null` | Optional color scheme, normalized to a string. | `ComponentContext` |
| <span id="property-size"></span>`$size` readonly | `string\|null` | Optional component size, normalized to a string. | `ComponentContext` |
| <span id="property-states"></span>`$states` readonly | `list<string>` | Active component states, normalized to strings. | `ComponentContext` |
| <span id="property-variant"></span>`$variant` readonly | `string\|null` | Optional presentation variant, normalized to a string. | `ComponentContext` |

## Methods

| Method | Description | Defined by |
| --- | --- | --- |
| [`__construct()`](#method-__construct) | Creates a semantic component context. | `ComponentContext` |
| [`get()`](#method-get) | Returns semantic metadata or the provided default value. | `ComponentContext` |
| [`hasState()`](#method-hasState) | Whether the component has the specified state. | `ComponentContext` |

## Property details

### $metadata <span id="property-metadata"></span>

```php
public readonly array<string, mixed> $metadata
```

Extensible semantic metadata available to config providers.

Object values are stored by reference, so pass immutable values when deep immutability is required.

## Method details

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

```php
public function __construct(BackedEnum|string $component, BackedEnum|string|null $variant = null, BackedEnum|string|null $size = null, BackedEnum|string|null $scheme = null, array<array-key, mixed> $states = [], array<array-key, mixed> $metadata = [])
```

Creates a semantic component context.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$component` | [`BackedEnum`](https://www.php.net/manual/en/class.backedenum.php)`\|string` | Semantic component identifier, for example `field.control.email`. |
| `$variant` | [`BackedEnum`](https://www.php.net/manual/en/class.backedenum.php)`\|string\|null` | Optional presentation variant. |
| `$size` | [`BackedEnum`](https://www.php.net/manual/en/class.backedenum.php)`\|string\|null` | Optional component size. |
| `$scheme` | [`BackedEnum`](https://www.php.net/manual/en/class.backedenum.php)`\|string\|null` | Optional color scheme. |
| `$states` | `array<array-key, mixed>` | Active component states, as strings or string-backed enums. |
| `$metadata` | `array<array-key, mixed>` | Additional semantic metadata. |

**Throws**

- [`InvalidArgumentException`](https://www.php.net/manual/en/class.invalidargumentexception.php): If the component identifier, a qualifier, or a state is an int-backed enum, is empty, or contains whitespace, the states are not a list of strings or backed enums, or a metadata key is not a non-empty string.

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

```php
public function get(string $key, mixed $default = null): mixed
```

Returns semantic metadata or the provided default value.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$key` | `string` | Metadata key to look up. |
| `$default` | `mixed` | Value returned when the key is missing. |

**Returns**

`mixed`: Stored metadata value, or the default when the key is missing.

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

```php
public function hasState(BackedEnum|string $state): bool
```

Whether the component has the specified state.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$state` | [`BackedEnum`](https://www.php.net/manual/en/class.backedenum.php)`\|string` | State name, or a string-backed enum whose backing value is the state name. |

**Returns**

`bool`: `true` if the state is active, `false` otherwise.

**Throws**

- [`InvalidArgumentException`](https://www.php.net/manual/en/class.invalidargumentexception.php): If the state is an int-backed enum.