CurrencyFormatter   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 3
eloc 6
dl 0
loc 33
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A format() 0 10 1
A localized() 0 3 1
A __construct() 0 3 1
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