Neos\Flow\I18n\FormatResolver::resolvePlaceholders PHP Méthode

resolvePlaceholders() public méthode

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.
public resolvePlaceholders ( string $textWithPlaceholders, array $arguments, Locale $locale = null ) : string
$textWithPlaceholders string String message with placeholder(s)
$arguments array An array of values to replace placeholders with
$locale Locale Locale to use (NULL for default one)
Résultat string The $text with placeholders resolved
    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;
    }

Usage Example

 /**
  * @test
  */
 public function formatResolverWorksCorrectlyForFullyQualifiedFormatterClassNames()
 {
     $actualFormatter = new Fixtures\SampleFormatter();
     $locale = new I18n\Locale('de');
     $testResult = $this->formatResolver->resolvePlaceholders(sprintf('{0,%s}', Fixtures\SampleFormatter::class), ['foo'], $locale);
     $this->assertEquals($actualFormatter->format('foo', $locale), $testResult);
 }
All Usage Examples Of Neos\Flow\I18n\FormatResolver::resolvePlaceholders