ICanBoogie /
CLDR
| 1 | <?php |
||
| 2 | |||
| 3 | namespace ICanBoogie\CLDR\Numbers; |
||
| 4 | |||
| 5 | use ICanBoogie\CLDR\Core\Formatter; |
||
| 6 | use ICanBoogie\CLDR\Core\Locale; |
||
| 7 | use ICanBoogie\CLDR\Core\Localizable; |
||
| 8 | |||
| 9 | use function str_replace; |
||
| 10 | |||
| 11 | /** |
||
| 12 | * A currency formatter. |
||
| 13 | * |
||
| 14 | * @implements Localizable<CurrencyFormatter, CurrencyFormatterLocalized> |
||
| 15 | */ |
||
| 16 | final class CurrencyFormatter implements Formatter, Localizable |
||
| 17 | { |
||
| 18 | public const DEFAULT_CURRENCY_SYMBOL = 'ยค'; |
||
| 19 | |||
| 20 | public function __construct( |
||
| 21 | private readonly NumberFormatter $number_formatter, |
||
| 22 | ) { |
||
| 23 | } |
||
| 24 | |||
| 25 | /** |
||
| 26 | * Formats a number with the specified pattern. |
||
| 27 | * |
||
| 28 | * @param float|int|numeric-string $number |
||
|
0 ignored issues
–
show
Documentation
Bug
introduced
by
Loading history...
|
|||
| 29 | * The number to format. |
||
| 30 | * @param string|NumberPattern $pattern |
||
| 31 | * The pattern used to format the number. |
||
| 32 | */ |
||
| 33 | public function format( |
||
| 34 | float|int|string $number, |
||
| 35 | NumberPattern|string $pattern, |
||
| 36 | ?Symbols $symbols = null, |
||
| 37 | string $currencySymbol = self::DEFAULT_CURRENCY_SYMBOL |
||
| 38 | ): string { |
||
| 39 | return str_replace( |
||
| 40 | self::DEFAULT_CURRENCY_SYMBOL, |
||
| 41 | $currencySymbol, |
||
| 42 | $this->number_formatter->format($number, $pattern, $symbols) |
||
| 43 | ); |
||
| 44 | } |
||
| 45 | |||
| 46 | public function localized(Locale $locale): CurrencyFormatterLocalized |
||
| 47 | { |
||
| 48 | return new CurrencyFormatterLocalized($this, $locale); |
||
| 49 | } |
||
| 50 | } |
||
| 51 |