1 | <?php |
||
7 | class CurrencyFormatter |
||
8 | { |
||
9 | /** @var string */ |
||
10 | private $currencyCode; |
||
11 | |||
12 | /** @var string */ |
||
13 | private $locale; |
||
14 | |||
15 | public function __construct($currencyCode, $localeCode) |
||
16 | { |
||
17 | $this->currencyCode = $currencyCode; |
||
18 | $this->locale = $localeCode; |
||
19 | } |
||
20 | |||
21 | public function format($amount) |
||
22 | { |
||
23 | return $this->getFormatter()->formatCurrency($amount, $this->currencyCode); |
||
24 | } |
||
25 | |||
26 | private function getFormatter() |
||
27 | { |
||
28 | $formatter = new \NumberFormatter($this->locale, \NumberFormatter::CURRENCY); |
||
29 | $decimalPlaces = CurrencyInfo::getFractionDigitsForCurrency($this->currencyCode); |
||
30 | $formatter->setAttribute(\NumberFormatter::FRACTION_DIGITS, $decimalPlaces); |
||
31 | return $formatter; |
||
32 | } |
||
33 | } |
||
34 | |||
56 |