Oihana PHP

resolvePlaceholders.php

Table of Contents

Functions

resolvePlaceholders()  : void
Hydrates or formats a document (array or object) in place by replacing placeholders with corresponding values from a source document.

Functions

resolvePlaceholders()

Hydrates or formats a document (array or object) in place by replacing placeholders with corresponding values from a source document.

resolvePlaceholders(array<string|int, mixed>|object &$target, array<string|int, mixed>|object $source[, string $prefix = '{{' ][, string $suffix = '}}' ][, string $separator = '.' ][, string|null $pattern = null ][, callable|null $formatter = null ][, bool $preserveMissing = false ]) : void
Parameters
$target : array<string|int, mixed>|object

The target document to format.

$source : array<string|int, mixed>|object

The source document used for placeholder resolution.

$prefix : string = '{{'

Placeholder prefix (default '{{').

$suffix : string = '}}'

Placeholder suffix (default '}}').

$separator : string = '.'

Separator used in nested keys (default '.').

$pattern : string|null = null

Optional regex pattern to match placeholders.

$formatter : callable|null = null

Optional custom formatter callable with signature fn(string $value, array|object $source, string $prefix, string $suffix, string $separator, ?string $pattern, bool $preserveMissing): string

$preserveMissing : bool = false

If true, preserves unresolved placeholders instead of removing them (default false).

Tags
example
$target =
[
    'host'        => '{{server.name}}',
    'port'        => '{{server.port}}',
    'enabled'     => '{{feature.enabled}}',
    'description' => 'Connect to {{server.name}} on port {{server.port}}',
];

$source =
[
    'server' => [
        'name' => 'localhost',
        'port' => 8080,
    ],
    'feature' =>
    [
        'enabled' => false,
    ],
];

resolvePlaceholders($target, $source);

// Result:
// $target['host']    === 'localhost' (string)
// $target['port']    === 8080 (int)
// $target['enabled'] === false (bool)
// $

        
On this page

Search results