class Interpolator (View source)

internal  
 

Injects config values from an associative array into a string.

Properties

protected string $startToken

The character sequence that identifies the start of a token.

protected string $endToken

The character sequence that identifies the end of a token.

protected array $data

The associative array of replacements.

Methods

__construct(string $start_token = '\\[', string $end_token = '\\]')

Interpolator constructor.

$this
setData(array $data)

Sets the data set to use when interpolating.

$this
addData(array $data)

Adds to the data set to use when interpolating.

string
interpolate(string $message, array $extra = [], string|bool $default = '')

Replaces tokens in a string with values from an associative array.

string[]
replacements(string $message, array $data, string $default = '')

Finds the tokens that exist in a message and builds a replacement array.

string[]
findTokens(string $message)

Finds all of the tokens in the provided message.

Details

__construct(string $start_token = '\\[', string $end_token = '\\]')

Interpolator constructor.

Parameters

string $start_token

The start marker for a token, e.g. '['.

string $end_token

The end marker for a token, e.g. ']'.

$this setData(array $data)

Sets the data set to use when interpolating.

Parameters

array $data

The key:value pairs to use when interpolating.

Return Value

$this

$this addData(array $data)

Adds to the data set to use when interpolating.

Parameters

array $data

The key:value pairs to use when interpolating.

Return Value

$this

string interpolate(string $message, array $extra = [], string|bool $default = '')

Replaces tokens in a string with values from an associative array.

Tokens are surrounded by delimiters, e.g. square brackets "[key]". The characters that surround the key may be defined when the Interpolator is constructed.

Example: If the message is 'Hello, [user.name]', then the value of the user.name item is fetched from the array, and the token [user.name] is replaced with the result.

Parameters

string $message

Message containing tokens to be replaced.

array $extra

Data to use for interpolation in addition to whatever was provided to self::setData().

string|bool $default

(optional) The value to substitute for tokens that are not found in the data. If FALSE, then missing tokens are not replaced. Defaults to an empty string.

Return Value

string

The message after replacements have been made.

protected string[] replacements(string $message, array $data, string $default = '')

Finds the tokens that exist in a message and builds a replacement array.

All of the replacements in the data array are looked up given the token keys from the provided message. Keys that do not exist in the configuration are replaced with the default value.

Parameters

string $message

String with tokens.

array $data

Data to use for interpolation.

string $default

(optional) The value to substitute for tokens that are not found in the data. If FALSE, then missing tokens are not replaced. Defaults to an empty string.

Return Value

string[]

An array of replacements to make. Keyed by tokens and the replacements are the values.

protected string[] findTokens(string $message)

Finds all of the tokens in the provided message.

Parameters

string $message

String with tokens.

Return Value

string[]

map of token to key, e.g. {{key}} => key