CurrencyFormatter::format()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 10
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 4
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
The doc comment float|int|numeric-string at position 4 could not be parsed: Unknown type name 'numeric-string' at position 4 in float|int|numeric-string.
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