You are here

public function Interpolator::interpolate in Drupal 8

Same name and namespace in other branches
  1. 9 composer/Plugin/Scaffold/Interpolator.php \Drupal\Composer\Plugin\Scaffold\Interpolator::interpolate()

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.

File

composer/Plugin/Scaffold/Interpolator.php, line 97

Class

Interpolator
Injects config values from an associative array into a string.

Namespace

Drupal\Composer\Plugin\Scaffold

Code

public function interpolate($message, array $extra = [], $default = '') {
  $data = $extra + $this->data;
  $replacements = $this
    ->replacements($message, $data, $default);
  return strtr($message, $replacements);
}