# CapturePolicy

> Applies the default redaction and size limits before debug data reaches persistent storage.

[API reference](/docs/api/index.md) › [Debug Core](/docs/extensions/debug-core/reference/index.md) › [Capture](/docs/extensions/debug-core/reference/capture/index.md) › CapturePolicy

| final readonly class | `PHPForge\Debug\Capture\CapturePolicy` |
| --- | --- |
| Source | [`src/Capture/CapturePolicy.php`](https://github.com/php-forge/debug-core/blob/main/src/Capture/CapturePolicy.php#L31) |

Applies the default redaction and size limits before debug data reaches persistent storage.

## Methods

| Method | Description | Defined by |
| --- | --- | --- |
| [`__construct()`](#method-__construct) | Creates a capture policy with redaction and size limits. | `CapturePolicy` |
| [`isSensitiveKey()`](#method-isSensitiveKey) | Returns whether a key is denied by this policy. | `CapturePolicy` |
| [`maxBodyBytes()`](#method-maxBodyBytes) | Returns the maximum number of body bytes that may reach persistent capture. | `CapturePolicy` |
| [`redact()`](#method-redact) | Redacts sensitive keys throughout a bounded value tree. | `CapturePolicy` |
| [`redactBody()`](#method-redactBody) | Redacts a decoded body, suppressing its raw representation whenever redaction was required and truncating it at the configured byte boundary otherwise. | `CapturePolicy` |
| [`redactText()`](#method-redactText) | Redacts common `key=value` and `key: value` secret fragments in diagnostic text. | `CapturePolicy` |
| [`redactUrl()`](#method-redactUrl) | Redacts sensitive values in a URL query string without changing the URL outside its query component. | `CapturePolicy` |
| [`withAdditionalSensitiveKeys()`](#method-withAdditionalSensitiveKeys) | Returns a new instance whose exact-key list also covers the given keys. | `CapturePolicy` |

## Constants

| Constant | Value | Description | Defined by |
| --- | --- | --- | --- |
| <span id="const-DEFAULT_MAX_BODY_BYTES"></span>`DEFAULT_MAX_BODY_BYTES` | `65536` | Raw body bytes retained by persistent capture when no explicit limit is configured. | `CapturePolicy` |

## Method details

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

```php
public function __construct(list<string> $sensitiveKeys = \PHPForge\Debug\Helper\SensitiveDataRedactor::DEFAULT_KEYS, int $maxBodyBytes = \self::DEFAULT_MAX_BODY_BYTES, list<string> $sensitiveKeyPrefixes = [], list<string>|null $sensitiveKeyPatterns = null)
```

Creates a capture policy with redaction and size limits.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$sensitiveKeys` | `list<string>` | Exact, case-insensitive keys to redact recursively. |
| `$maxBodyBytes` | `int` | Maximum raw request or response body bytes to retain; must be positive. |
| `$sensitiveKeyPrefixes` | `list<string>` | Literal, case-insensitive key prefixes to redact recursively. |
| `$sensitiveKeyPatterns` | `list<string>\|null` | PCRE patterns applied to complete original keys. `null` uses segment-aware defaults only with the default exact-key list; `[]` explicitly disables pattern matching. |

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

```php
public function isSensitiveKey(string $key): bool
```

Returns whether a key is denied by this policy.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$key` | `string` | Original key to check case-insensitively. |

**Returns**

`bool`: `true` when the key matches an exact name, a prefix, or a pattern rule; `false` otherwise.

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

```php
public function maxBodyBytes(): int
```

Returns the maximum number of body bytes that may reach persistent capture.

**Returns**

`int`: Configured body byte limit.

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

```php
public function redact(array<PHPForge\Debug\Capture\TKey, mixed> $value): array<PHPForge\Debug\Capture\TKey, mixed>
```

Redacts sensitive keys throughout a bounded value tree.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$value` | `array<PHPForge\Debug\Capture\TKey, mixed>` | Value tree to sanitize. |

**Returns**

`array<PHPForge\Debug\Capture\TKey, mixed>`: Sanitized value tree.

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

```php
public function redactBody(string $raw, mixed $decoded): array{decoded: mixed, raw: string}
```

Redacts a decoded body, suppressing its raw representation whenever redaction was required and truncating it at
the configured byte boundary otherwise.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$raw` | `string` | Raw body exactly as received. |
| `$decoded` | `mixed` | Decoded body, or `null` when the body could not be decoded. |

**Returns**

`array{decoded: mixed, raw: string}`: Sanitized decoded body and its bounded raw representation.

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

```php
public function redactText(string $text): string
```

Redacts common `key=value` and `key: value` secret fragments in diagnostic text.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$text` | `string` | Diagnostic text to sanitize. |

**Returns**

`string`: Text with every sensitive assignment value replaced.

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

```php
public function redactUrl(string $url): string
```

Redacts sensitive values in a URL query string without changing the URL outside its query component.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$url` | `string` | URL to sanitize. |

**Returns**

`string`: URL with every sensitive query value replaced.

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

```php
public function withAdditionalSensitiveKeys(list<string> $sensitiveKeys): self
```

Returns a new instance whose exact-key list also covers the given keys.

The already resolved pattern rules are carried over, so extending the default key list keeps the segment-aware
defaults active instead of silently dropping them.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$sensitiveKeys` | `list<string>` | Extra exact, case-insensitive keys to redact recursively. |

**Returns**

`self`: New instance denying both the configured and the additional keys.