1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PhpOffice\PhpSpreadsheet\Style\NumberFormat\Wizard; |
4
|
|
|
|
5
|
|
|
use NumberFormatter; |
6
|
|
|
use PhpOffice\PhpSpreadsheet\Exception; |
7
|
|
|
|
8
|
|
|
class Currency extends Number |
9
|
|
|
{ |
10
|
|
|
public const LEADING_SYMBOL = true; |
11
|
|
|
|
12
|
|
|
public const TRAILING_SYMBOL = false; |
13
|
|
|
|
14
|
|
|
public const SYMBOL_WITH_SPACING = true; |
15
|
|
|
|
16
|
|
|
public const SYMBOL_WITHOUT_SPACING = false; |
17
|
|
|
|
18
|
|
|
protected string $currencyCode = '$'; |
19
|
|
|
|
20
|
|
|
protected bool $currencySymbolPosition = self::LEADING_SYMBOL; |
21
|
|
|
|
22
|
|
|
protected bool $currencySymbolSpacing = self::SYMBOL_WITHOUT_SPACING; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @param string $currencyCode the currency symbol or code to display for this mask |
26
|
|
|
* @param int $decimals number of decimal places to display, in the range 0-30 |
27
|
|
|
* @param bool $thousandsSeparator indicator whether the thousands separator should be used, or not |
28
|
|
|
* @param bool $currencySymbolPosition indicates whether the currency symbol comes before or after the value |
29
|
|
|
* Possible values are Currency::LEADING_SYMBOL and Currency::TRAILING_SYMBOL |
30
|
|
|
* @param bool $currencySymbolSpacing indicates whether there is spacing between the currency symbol and the value |
31
|
|
|
* Possible values are Currency::SYMBOL_WITH_SPACING and Currency::SYMBOL_WITHOUT_SPACING |
32
|
|
|
* @param ?string $locale Set the locale for the currency format; or leave as the default null. |
33
|
|
|
* If provided, Locale values must be a valid formatted locale string (e.g. 'en-GB', 'fr', uz-Arab-AF). |
34
|
|
|
* Note that setting a locale will override any other settings defined in this class |
35
|
|
|
* other than the currency code. |
36
|
|
|
* |
37
|
|
|
* @throws Exception If a provided locale code is not a valid format |
38
|
|
|
*/ |
39
|
17 |
|
public function __construct( |
40
|
|
|
string $currencyCode = '$', |
41
|
|
|
int $decimals = 2, |
42
|
|
|
bool $thousandsSeparator = true, |
43
|
|
|
bool $currencySymbolPosition = self::LEADING_SYMBOL, |
44
|
|
|
bool $currencySymbolSpacing = self::SYMBOL_WITHOUT_SPACING, |
45
|
|
|
?string $locale = null |
46
|
|
|
) { |
47
|
17 |
|
$this->setCurrencyCode($currencyCode); |
48
|
17 |
|
$this->setThousandsSeparator($thousandsSeparator); |
49
|
17 |
|
$this->setDecimals($decimals); |
50
|
17 |
|
$this->setCurrencySymbolPosition($currencySymbolPosition); |
51
|
17 |
|
$this->setCurrencySymbolSpacing($currencySymbolSpacing); |
52
|
17 |
|
$this->setLocale($locale); |
53
|
|
|
} |
54
|
|
|
|
55
|
34 |
|
public function setCurrencyCode(string $currencyCode): void |
56
|
|
|
{ |
57
|
34 |
|
$this->currencyCode = $currencyCode; |
58
|
|
|
} |
59
|
|
|
|
60
|
34 |
|
public function setCurrencySymbolPosition(bool $currencySymbolPosition = self::LEADING_SYMBOL): void |
61
|
|
|
{ |
62
|
34 |
|
$this->currencySymbolPosition = $currencySymbolPosition; |
63
|
|
|
} |
64
|
|
|
|
65
|
34 |
|
public function setCurrencySymbolSpacing(bool $currencySymbolSpacing = self::SYMBOL_WITHOUT_SPACING): void |
66
|
|
|
{ |
67
|
34 |
|
$this->currencySymbolSpacing = $currencySymbolSpacing; |
68
|
|
|
} |
69
|
|
|
|
70
|
10 |
|
protected function getLocaleFormat(): string |
71
|
|
|
{ |
72
|
10 |
|
$formatter = new Locale($this->fullLocale, NumberFormatter::CURRENCY); // @phpstan-ignore-line |
|
|
|
|
73
|
|
|
|
74
|
9 |
|
return str_replace('¤', $this->formatCurrencyCode(), $formatter->format()); |
75
|
|
|
} |
76
|
|
|
|
77
|
15 |
|
private function formatCurrencyCode(): string |
78
|
|
|
{ |
79
|
15 |
|
if ($this->locale === null) { |
80
|
6 |
|
return $this->currencyCode; |
81
|
|
|
} |
82
|
|
|
|
83
|
9 |
|
return "[\${$this->currencyCode}-{$this->locale}]"; |
84
|
|
|
} |
85
|
|
|
|
86
|
15 |
|
public function format(): string |
87
|
|
|
{ |
88
|
15 |
|
if ($this->localeFormat !== null) { |
89
|
9 |
|
return $this->localeFormat; |
90
|
|
|
} |
91
|
|
|
|
92
|
6 |
|
return sprintf( |
93
|
6 |
|
'%s%s%s0%s%s%s', |
94
|
6 |
|
$this->currencySymbolPosition === self::LEADING_SYMBOL ? $this->formatCurrencyCode() : null, |
95
|
6 |
|
( |
96
|
6 |
|
$this->currencySymbolPosition === self::LEADING_SYMBOL && |
97
|
6 |
|
$this->currencySymbolSpacing === self::SYMBOL_WITH_SPACING |
98
|
6 |
|
) ? "\u{a0}" : '', |
99
|
6 |
|
$this->thousandsSeparator ? '#,##' : null, |
100
|
6 |
|
$this->decimals > 0 ? '.' . str_repeat('0', $this->decimals) : null, |
101
|
6 |
|
( |
102
|
6 |
|
$this->currencySymbolPosition === self::TRAILING_SYMBOL && |
103
|
6 |
|
$this->currencySymbolSpacing === self::SYMBOL_WITH_SPACING |
104
|
6 |
|
) ? "\u{a0}" : '', |
105
|
6 |
|
$this->currencySymbolPosition === self::TRAILING_SYMBOL ? $this->formatCurrencyCode() : null |
106
|
6 |
|
); |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|