|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
use VinaiKopp\CurrencyInfo\StaticAccess\CurrencyInfo; |
|
4
|
|
|
|
|
5
|
|
|
require __DIR__ . '/../vendor/autoload.php'; |
|
6
|
|
|
|
|
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
|
|
|
|
|
35
|
|
|
echo "---------------\n"; |
|
36
|
|
|
$deEuroFormatter = new CurrencyFormatter('EUR', 'de_DE'); |
|
37
|
|
|
echo 'de_DE/EUR: ' . $deEuroFormatter->format(123456.78) . PHP_EOL; |
|
38
|
|
|
|
|
39
|
|
|
$usEuroFormatter = new CurrencyFormatter('EUR', 'en_US'); |
|
40
|
|
|
echo 'en_US/EUR: ' . $usEuroFormatter->format(123456.78) . PHP_EOL; |
|
41
|
|
|
echo "---------------\n"; |
|
42
|
|
|
$deYenFormatter = new CurrencyFormatter('JPY', 'de_DE'); |
|
43
|
|
|
echo 'de_DE/JPY: ' . $deYenFormatter->format(123456.78) . PHP_EOL; |
|
44
|
|
|
|
|
45
|
|
|
$usYenFormatter = new CurrencyFormatter('JPY', 'en_US'); |
|
46
|
|
|
echo 'en_US/JPY: ' . $usYenFormatter->format(123456.78) . PHP_EOL; |
|
47
|
|
|
echo "---------------\n"; |
|
48
|
|
|
|
|
49
|
|
|
// Output: |
|
50
|
|
|
//--------------- |
|
51
|
|
|
//de_DE/EUR: 123.456,78 € |
|
52
|
|
|
//en_US/EUR: €123,456.78 |
|
53
|
|
|
//--------------- |
|
54
|
|
|
//de_DE/JPY: 123.457 ¥ |
|
55
|
|
|
//en_US/JPY: ¥123,457 |
|
56
|
|
|
|