Oihana PHP

formatDocumentWith.php

Table of Contents

Functions

formatDocumentWith()  : array<string|int, mixed>|object
Formats a document (array or object) using placeholders resolved from another source document.

Functions

formatDocumentWith()

Formats a document (array or object) using placeholders resolved from another source document.

formatDocumentWith(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 ]) : array<string|int, mixed>|object
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 with signature: function(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
see
formatDocument()
example
$source =
[
    'base_dir' => '/var/www',
    'env'      => 'prod',
    'config'   =>
    [
        'prod' => [ 'url' => 'https://example.com' ]
    ]
];

$target =
[
    'htdocs' => '{{base_dir}}/htdocs',
    'api'    => '{{config.{{env}}.url}}/api'
];

$result = formatDocumentWith($target, $source);
echo $result['htdocs']; // /var/www/htdocs
echo $result['api'];    // https://example.com/api
Return values
array<string|int, mixed>|object

A new document with the same structure and class as $target, where all string placeholders have been resolved using $source.


        
On this page

Search results