Skip to content

CurrencyFormatter

Formats currency amounts using locale-specific patterns.

Properties

numberFormatRepository

The number format repository.

protected \CommerceGuys\Intl\NumberFormat\NumberFormatRepositoryInterface $numberFormatRepository

currencyRepository

The currency repository.

protected \CommerceGuys\Intl\Currency\CurrencyRepositoryInterface $currencyRepository

defaultLocale

The default locale.

protected string $defaultLocale

numberFormats

The loaded number formats.

protected \CommerceGuys\Intl\NumberFormat\NumberFormat[] $numberFormats

currencies

The loaded currencies.

protected \CommerceGuys\Intl\Currency\Currency[] $currencies

defaultOptions

The default options.

protected array $defaultOptions

Methods

__construct

Creates a CurrencyFormatter instance.

public __construct(\CommerceGuys\Intl\NumberFormat\NumberFormatRepositoryInterface $numberFormatRepository, \CommerceGuys\Intl\Currency\CurrencyRepositoryInterface $currencyRepository, array $defaultOptions = []): mixed

Parameters:

Parameter Type Description
$numberFormatRepository \CommerceGuys\Intl\NumberFormat\NumberFormatRepositoryInterface The number format repository.
$currencyRepository \CommerceGuys\Intl\Currency\CurrencyRepositoryInterface The currency repository.
$defaultOptions array The default options.

Throws:


format

Formats a currency amount.

public format(mixed $number, mixed $currencyCode, array $options = []): string

Parameters:

Parameter Type Description
$number mixed The number.
$currencyCode mixed The currency code.
$options array The formatting options.

Return Value:

The formatted number.


parse

Parses a formatted currency amount.

public parse(mixed $number, mixed $currencyCode, array $options = []): string|false

Parameters:

Parameter Type Description
$number mixed The formatted number.
$currencyCode mixed The currency code.
$options array The parsing options.

Return Value:

The parsed number or FALSE on error.


getNumberFormat

Gets the number format for the provided locale.

protected getNumberFormat(string $locale): \CommerceGuys\Intl\NumberFormat\NumberFormat

Parameters:

Parameter Type Description
$locale string The locale.

getCurrency

Gets the currency for the provided currency code and locale.

protected getCurrency(string $currencyCode, string $locale): \CommerceGuys\Intl\Currency\Currency

Parameters:

Parameter Type Description
$currencyCode string The currency code.
$locale string The locale.

getAvailablePatterns

{@inheritdoc}

protected getAvailablePatterns(\CommerceGuys\Intl\NumberFormat\NumberFormat $numberFormat): mixed

Parameters:

Parameter Type Description
$numberFormat \CommerceGuys\Intl\NumberFormat\NumberFormat

validateOptions

Validates the provided options.

protected validateOptions(array $options): mixed

Ensures the absence of unknown keys, correct data types and values.

Parameters:

Parameter Type Description
$options array The options.

Throws:


getLocalizedSymbols

{@inheritdoc}

protected getLocalizedSymbols(\CommerceGuys\Intl\NumberFormat\NumberFormat $numberFormat): array

Parameters:

Parameter Type Description
$numberFormat \CommerceGuys\Intl\NumberFormat\NumberFormat

Inherited methods

formatNumber

Formats the number according to the number format.

protected formatNumber(string $number, \CommerceGuys\Intl\NumberFormat\NumberFormat $numberFormat, array $options = []): string

Parameters:

Parameter Type Description
$number string The number.
$numberFormat \CommerceGuys\Intl\NumberFormat\NumberFormat The number format.
$options array

Return Value:

The formatted number.


localizeNumber

Localizes the number according to the number format.

protected localizeNumber(string $number, \CommerceGuys\Intl\NumberFormat\NumberFormat $numberFormat): string

Both the digits and the symbols are replaced with their localized equivalents.

Parameters:

Parameter Type Description
$number string The number.
$numberFormat \CommerceGuys\Intl\NumberFormat\NumberFormat The number format.

Return Value:

The localized number.

See Also:

  • http://cldr.unicode.org/translation/number-symbols -

parseNumber

Parses the number according to the number format.

protected parseNumber(string $number, \CommerceGuys\Intl\NumberFormat\NumberFormat $numberFormat): string

Both the digits and the symbols are replaced with their non-localized equivalents.

Parameters:

Parameter Type Description
$number string The number.
$numberFormat \CommerceGuys\Intl\NumberFormat\NumberFormat The number format.

Return Value:

The localized number.


getParsedPattern

Gets the pattern for the provided number format.

protected getParsedPattern(\CommerceGuys\Intl\NumberFormat\NumberFormat $numberFormat, string $style): \CommerceGuys\Intl\Formatter\ParsedPattern

Parameters:

Parameter Type Description
$numberFormat \CommerceGuys\Intl\NumberFormat\NumberFormat The number format.
$style string The formatter style.

getAvailablePatterns

Gets the available patterns for the provided number format.

protected getAvailablePatterns(\CommerceGuys\Intl\NumberFormat\NumberFormat $numberFormat): string[]
  • This method is abstract.

Parameters:

Parameter Type Description
$numberFormat \CommerceGuys\Intl\NumberFormat\NumberFormat The number format.

Return Value:

The patterns, keyed by style.


getLocalizedSymbols

Gets the localized symbols for the provided number format.

protected getLocalizedSymbols(\CommerceGuys\Intl\NumberFormat\NumberFormat $numberFormat): array

Used to localize the number in localizeNumber().

  • This method is abstract.

Parameters:

Parameter Type Description
$numberFormat \CommerceGuys\Intl\NumberFormat\NumberFormat The number format.


Automatically generated on 2025-03-18