public function resolvePlaceholders($textWithPlaceholders, array $arguments, Locale $locale = null)
{
if ($locale === null) {
$locale = $this->localizationService->getConfiguration()->getDefaultLocale();
}
$lastPlaceHolderAt = 0;
while ($lastPlaceHolderAt < strlen($textWithPlaceholders) && ($startOfPlaceholder = strpos($textWithPlaceholders, '{', $lastPlaceHolderAt)) !== false) {
$endOfPlaceholder = strpos($textWithPlaceholders, '}', $lastPlaceHolderAt);
$startOfNextPlaceholder = strpos($textWithPlaceholders, '{', $startOfPlaceholder + 1);
if ($endOfPlaceholder === false || $startOfPlaceholder + 1 >= $endOfPlaceholder || $startOfNextPlaceholder !== false && $startOfNextPlaceholder < $endOfPlaceholder) {
// There is no closing bracket, or it is placed before the opening bracket, or there is nothing between brackets
throw new Exception\InvalidFormatPlaceholderException('Text provided contains incorrectly formatted placeholders. Please make sure you conform the placeholder\'s syntax.', 1278057790);
}
$contentBetweenBrackets = substr($textWithPlaceholders, $startOfPlaceholder + 1, $endOfPlaceholder - $startOfPlaceholder - 1);
$placeholderElements = explode(',', str_replace(' ', '', $contentBetweenBrackets));
$valueIndex = $placeholderElements[0];
if (!array_key_exists($valueIndex, $arguments)) {
throw new Exception\IndexOutOfBoundsException('Placeholder "' . $valueIndex . '" was not provided, make sure you provide values for every placeholder.', 1278057791);
}
if (isset($placeholderElements[1])) {
$formatterName = $placeholderElements[1];
$formatter = $this->getFormatter($formatterName);
$formattedPlaceholder = $formatter->format($arguments[$valueIndex], $locale, array_slice($placeholderElements, 2));
} else {
// No formatter defined, just string-cast the value
$formattedPlaceholder = (string) $arguments[$valueIndex];
}
$textWithPlaceholders = str_replace('{' . $contentBetweenBrackets . '}', $formattedPlaceholder, $textWithPlaceholders);
$lastPlaceHolderAt = $startOfPlaceholder + strlen($formattedPlaceholder);
}
return $textWithPlaceholders;
}