forked from ICanBoogie/CLDR
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCurrency.php
79 lines (70 loc) · 1.6 KB
/
Currency.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
<?php
/*
* This file is part of the ICanBoogie package.
*
* (c) Olivier Laviale <olivier.laviale@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace ICanBoogie\CLDR;
use ICanBoogie\Accessor\AccessorTrait;
use ICanBoogie\CLDR\Supplemental\Fraction;
/**
* A currency.
*
* ```php
* <?php
*
* use ICanBoogie\CLDR\Currency;
*
* $euro = new Currency($cldr, 'EUR');
* # or
* $euro = $cldr->currencies['EUR'];
*
* echo $euro->fraction->code; // EUR
* echo $euro->fraction->digits; // 2
* echo $euro->fraction->rounding; // 0
* echo $euro->fraction->cash_digits; // 2
* echo $euro->fraction->cash_rounding; // 0
* ```
*
* @internal
*
* @property-read Fraction $fraction
*
* @see http://unicode.org/reports/tr35/tr35-numbers.html#Supplemental_Currency_Data
*/
final class Currency
{
/**
* @uses get_fraction
*/
use AccessorTrait;
/**
* @param string $code
* The ISO 4217 code for the currency.
*/
public function __construct(
public readonly Repository $repository,
public readonly string $code
) {
}
public function __toString(): string
{
return $this->code;
}
private Fraction $fraction;
private function get_fraction(): Fraction
{
return $this->fraction ??= $this->repository->supplemental->currency_data->fraction_for($this->code);
}
/**
* Localizes the currency.
*/
public function localize(string $locale_id): LocalizedCurrency
{
/** @phpstan-ignore-next-line */
return $this->repository->locales[$locale_id]->localize($this);
}
}