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;
    }
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;
    }
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 ; }
Replaces all placeholders in text with corresponding values.

A placeholder is a group of elements separated with comma. First element
is required and defines index of value to insert (numeration starts from
0, and is directly used to access element from $values array). Second
element is a name of formatter to use. It's optional, and if not given,
value will be simply string-casted. Remaining elements are formatter-
specific and they are directly passed to the formatter class.

@param string $textWithPlaceholders String message with placeholder(s)
@param array $arguments An array of values to replace placeholders with
@param Locale $locale Locale to use (NULL for default one)
@return string The $text with placeholders resolved
@throws Exception\InvalidFormatPlaceholderException When encountered incorrectly formatted placeholder
@throws Exception\IndexOutOfBoundsException When trying to format nonexistent value
@api
Replaces all placeholders in text with corresponding values .