class FormatResolver (View source)

A class for replacing placeholders in strings with formatted values.

Placeholders have following syntax: {id[,name[,attribute1[,attribute2...]]]}

Where 'id' is an index of argument to insert in place of placeholder, an optional 'name' is a name of formatter to use for formatting the argument (if no name given, provided argument will be just string-casted), and optional attributes are strings directly passed to the formatter (what they do depends on concrete formatter which is being used).

Examples: {0} {0,number,decimal} {1,datetime,time,full}

Properties

protected ObjectManagerInterface $objectManager
protected Service $localizationService
protected ReflectionService $reflectionService
protected FormatterInterface[] $formatters

Array of concrete formatters used by this class.

Methods

void
injectObjectManager(ObjectManagerInterface $objectManager)

No description

void
injectLocalizationService(Service $localizationService)

No description

string
resolvePlaceholders(string $textWithPlaceholders, array $arguments, Locale $locale = null)

Replaces all placeholders in text with corresponding values.

getFormatter(string $formatterType)

Returns instance of concrete formatter.

Details

void injectObjectManager(ObjectManagerInterface $objectManager)

No description

Parameters

ObjectManagerInterface $objectManager

Return Value

void

void injectLocalizationService(Service $localizationService)

No description

Parameters

Service $localizationService

Return Value

void

string resolvePlaceholders(string $textWithPlaceholders, array $arguments, Locale $locale = null)

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.

Parameters

string $textWithPlaceholders

String message with placeholder(s)

array $arguments

An array of values to replace placeholders with

Locale $locale

Locale to use (NULL for default one)

Return Value

string

The $text with placeholders resolved

Exceptions

InvalidFormatPlaceholderException
IndexOutOfBoundsException

protected FormatterInterface getFormatter(string $formatterType)

Returns instance of concrete formatter.

The type provided has to be either a name of existing class placed in I18n\Formatter namespace or a fully qualified class name; in both cases implementing this' package's FormatterInterface. For example, when $formatterName is 'number', the I18n\Formatter\NumberFormatter class has to exist; when $formatterName is 'Acme\Foobar\I18nFormatter\SampleFormatter', this class must exist and implement I18n\Formatter\FormatterInterface.

Throws exception if there is no formatter for name given or one could be retrieved but does not satisfy the FormatterInterface.

Parameters

string $formatterType

Either one of the built-in formatters or fully qualified formatter class name

Return Value

FormatterInterface

The concrete formatter class

Exceptions

UnknownFormatterException
InvalidFormatterException