Interpolator
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
Interpolator constructor.
Sets the data set to use when interpolating.
Adds to the data set to use when interpolating.
Replaces tokens in a string with values from an associative array.
Finds the tokens that exist in a message and builds a replacement array.
Finds all of the tokens in the provided message.
Details
__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.
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.
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.
protected string[]
findTokens(string $message)
Finds all of the tokens in the provided message.