# Coerce

> Narrows arbitrary mixed payloads into the typed scalars debug-panel renderers expect.

[API reference](/docs/api/index.md) › [Debug Core](/docs/extensions/debug-core/reference/index.md) › [Helper](/docs/extensions/debug-core/reference/helper/index.md) › Coerce

| final class | `PHPForge\Debug\Helper\Coerce` |
| --- | --- |
| Source | [`src/Helper/Coerce.php`](https://github.com/php-forge/debug-core/blob/main/src/Helper/Coerce.php#L17) |

Narrows arbitrary mixed payloads into the typed scalars debug-panel renderers expect.

## Methods

| Method | Description | Defined by |
| --- | --- | --- |
| [`float()`](#method-float) static | Returns a numeric value as a float or the supplied default. | `Coerce` |
| [`floatOrNull()`](#method-floatOrNull) static | Returns the value as a float when it is numeric, `null` otherwise. | `Coerce` |
| [`int()`](#method-int) static | Returns a numeric value as an int or the supplied default. | `Coerce` |
| [`intOrNull()`](#method-intOrNull) static | Returns the value as an int when it is numeric, `null` otherwise. | `Coerce` |
| [`string()`](#method-string) static | Returns a string value or the supplied default. | `Coerce` |
| [`stringKeyedArray()`](#method-stringKeyedArray) static | Returns only the entries of `$data` whose key is a string, preserving order. | `Coerce` |
| [`stringList()`](#method-stringList) static | Returns only the string entries of a raw list, preserving order. | `Coerce` |
| [`stringOrNull()`](#method-stringOrNull) static | Returns the value as a string when it is scalar or [`Stringable`](https://www.php.net/manual/en/class.stringable.php), `null` otherwise. | `Coerce` |
| [`traceFrames()`](#method-traceFrames) static | Narrows a raw trace value (as captured by Yii's logger) into the `list<array<string, mixed>>` shape every panel renderer consumes. | `Coerce` |

## Method details

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

```php
public static function float(mixed $value, float $default = 0.0): float
```

Returns a numeric value as a float or the supplied default.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$value` | `mixed` | Value to narrow. |
| `$default` | `float` | Value returned for non-numeric input. |

**Returns**

`float`: Numeric value or the configured default.

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

```php
public static function floatOrNull(mixed $value): float|null
```

Returns the value as a float when it is numeric, `null` otherwise.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$value` | `mixed` | Value to narrow. |

**Returns**

`float|null`: Numeric value or `null`.

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

```php
public static function int(mixed $value, int $default = 0): int
```

Returns a numeric value as an int or the supplied default.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$value` | `mixed` | Value to narrow. |
| `$default` | `int` | Value returned for non-numeric input. |

**Returns**

`int`: Numeric value or the configured default.

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

```php
public static function intOrNull(mixed $value): int|null
```

Returns the value as an int when it is numeric, `null` otherwise.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$value` | `mixed` | Value to narrow. |

**Returns**

`int|null`: Numeric value or `null`.

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

```php
public static function string(mixed $value, string $default = ''): string
```

Returns a string value or the supplied default.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$value` | `mixed` | Value to narrow. |
| `$default` | `string` | Value returned for non-string input. |

**Returns**

`string`: String value or the configured default.

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

```php
public static function stringKeyedArray(array<array-key, mixed> $data): array<string, mixed>
```

Returns only the entries of `$data` whose key is a string, preserving order.

Narrows a mixed/`array<array-key, mixed>` snapshot down to the `array<string, mixed>` shape downstream view-model
normalizers expect.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$data` | `array<array-key, mixed>` | Source array with arbitrary keys. |

**Returns**

`array<string, mixed>`: Entries whose key was already a string, in original order.

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

```php
public static function stringList(mixed $values): list<string>
```

Returns only the string entries of a raw list, preserving order.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$values` | `mixed` | Raw list, typically a user-configured category list. |

**Returns**

`list<string>`: String entries in original order, possibly empty.

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

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

Returns the value as a string when it is scalar or [`Stringable`](https://www.php.net/manual/en/class.stringable.php), `null` otherwise.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$value` | `mixed` | Value to narrow. |

**Returns**

`string|null`: String representation or `null`.

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

```php
public static function traceFrames(mixed $value): list<array<string, mixed>>
```

Narrows a raw trace value (as captured by Yii's logger) into the `list<array<string, mixed>>` shape every panel
renderer consumes.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$value` | `mixed` | Raw trace payload. |

**Returns**

`list<array<string, mixed>>`: Trace frames normalized to string-keyed maps.