1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ICanBoogie\CLDR\Numbers; |
4
|
|
|
|
5
|
|
|
use ICanBoogie\CLDR\Core\Locale; |
6
|
|
|
use ICanBoogie\CLDR\Core\LocalizedObjectWithFormatter; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* A localized currency. |
10
|
|
|
* |
11
|
|
|
* @extends LocalizedObjectWithFormatter<Currency, CurrencyFormatterLocalized> |
12
|
|
|
*/ |
13
|
|
|
final class CurrencyLocalized extends LocalizedObjectWithFormatter |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var string The localized name of the currency. |
17
|
|
|
*/ |
18
|
|
|
public readonly string $name; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var string The localized symbol of the currency. |
22
|
|
|
*/ |
23
|
|
|
public readonly string $symbol; |
24
|
|
|
|
25
|
|
|
public function __construct(object $target, Locale $locale) |
26
|
|
|
{ |
27
|
|
|
$l = $locale['currencies'][$target->code]; |
28
|
|
|
$this->name = $l['displayName']; |
|
|
|
|
29
|
|
|
// Not all currencies have a symbol, we default to the currency code in those cases. |
30
|
|
|
$this->symbol = $l['symbol'] ?? $target->code; |
|
|
|
|
31
|
|
|
|
32
|
|
|
parent::__construct($target, $locale, $locale->currency_formatter); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Returns the localized name of the currency. |
37
|
|
|
* |
38
|
|
|
* @param int|null $count Used for pluralization. |
39
|
|
|
*/ |
40
|
|
|
public function name_for(int $count = null): string |
41
|
|
|
{ |
42
|
|
|
$offset = 'displayName'; |
43
|
|
|
|
44
|
|
|
if ($count == 1) { |
45
|
|
|
$offset .= '-count-one'; |
46
|
|
|
} elseif ($count) { |
|
|
|
|
47
|
|
|
$offset .= '-count-other'; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
return $this->locale['currencies'][$this->target->code][$offset]; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Formats currency using locale's conventions. |
55
|
|
|
* |
56
|
|
|
* @param float|int|numeric-string $number |
|
|
|
|
57
|
|
|
*/ |
58
|
|
|
public function format( |
59
|
|
|
float|int|string $number, |
60
|
|
|
string $pattern = CurrencyFormatterLocalized::PATTERN_STANDARD |
61
|
|
|
): string { |
62
|
|
|
return $this->formatter->format($number, $this->target, $pattern); |
|
|
|
|
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|