Issues (662)

src/Numbers/CurrencyFormatterLocalized.php (1 issue)

1
<?php
2
3
namespace ICanBoogie\CLDR\Numbers;
4
5
use ICanBoogie\CLDR\Core\Formatter;
6
use ICanBoogie\CLDR\Core\LocalizedObject;
7
8
/**
9
 * Formats currencies using locale conventions.
10
 *
11
 * @extends LocalizedObject<CurrencyFormatter>
12
 */
13
class CurrencyFormatterLocalized extends LocalizedObject implements Formatter
14
{
15
    public const PATTERN_STANDARD = 'standard';
16
    public const PATTERN_ACCOUNTING = 'accounting';
17
18
    /**
19
     * Formats currency using localized conventions.
20
     *
21
     * @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...
22
     * @param string|Currency $currency A {@see Currency} or currency code.
23
     */
24
    public function format(
25
        float|int|string $number,
26
        Currency|string $currency,
27
        string $pattern = self::PATTERN_STANDARD
28
    ): string {
29
        return $this->target->format(
30
            $number,
31
            $this->resolve_pattern($pattern),
32
            $this->locale->numbers->symbols,
33
            $this->resolve_currency_symbol($currency)
34
        );
35
    }
36
37
    private function resolve_currency_symbol(string $currency): string
38
    {
39
        return $this->locale['currencies'][$currency]['symbol'] ?? $currency;
40
    }
41
42
    /**
43
     * Resolves a pattern.
44
     *
45
     * The special patterns {@see PATTERN_STANDARD} and {@see PATTERN_ACCOUNTING} are resolved
46
     * from the currency formats.
47
     */
48
    private function resolve_pattern(string $pattern): string
49
    {
50
        return match ($pattern) {
51
            self::PATTERN_STANDARD => $this->locale->numbers->currency_formats['standard'],
52
            self::PATTERN_ACCOUNTING => $this->locale->numbers->currency_formats['accounting'],
53
            default => $pattern,
54
        };
55
    }
56
}
57