-
-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathMoneyFormatter.php
174 lines (143 loc) · 6.4 KB
/
MoneyFormatter.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
<?php
namespace Pelmered\FilamentMoneyField;
use Illuminate\Support\Number;
use Money\Currencies\ISOCurrencies;
use Money\Currency;
use Money\Exception\ParserException;
use Money\Formatter\IntlMoneyFormatter;
use Money\Money;
use Money\Parser\IntlLocalizedDecimalParser;
use NumberFormatter;
class MoneyFormatter
{
public static function format(
null|int|string $value,
Currency $currency,
string $locale,
int $outputStyle = NumberFormatter::CURRENCY,
int $decimals = 2,
): string {
if ($value === '' || ! is_numeric($value)) {
return '';
}
$numberFormatter = self::getNumberFormatter($locale, $outputStyle, $decimals);
$moneyFormatter = new IntlMoneyFormatter($numberFormatter, new ISOCurrencies);
$money = new Money((int) $value, $currency);
return $moneyFormatter->format($money); // Outputs something like "$1.234,56"
}
public static function formatAsDecimal(
null|int|string $value,
Currency $currency,
string $locale,
int $decimals = 2,
): string {
return static::format($value, $currency, $locale, NumberFormatter::DECIMAL, $decimals);
}
public static function numberFormat(
null|int|float|string $value,
Currency $currency,
string $locale,
int $decimals = 2,
): string {
if (! is_numeric($value)) {
return '';
}
$numberFormatter = self::getNumberFormatter($locale, NumberFormatter::DECIMAL, $decimals);
return (string) $numberFormatter->format((float) $value); // Outputs something like "1.234,56"
}
public static function formatShort(
null|int|string $value,
Currency $currency,
string $locale,
int $decimals = 2,
bool $showCurrencySymbol = true
): string {
if (! is_numeric($value)) {
return '';
}
// No need to abbreviate if the value is less than 1000
if ($value < 100000) {
if (! $showCurrencySymbol) {
dump($value);
return static::numberFormat((int) $value / 100, $currency, $locale, decimals: $decimals);
}
return static::format($value, $currency, $locale, $decimals);
}
$abbreviated = (string) Number::abbreviate((int) $value / 100, 0, abs($decimals));
// Split the number and the suffix
preg_match('/^(?<number>[0-9.]+)(?<suffix>[A-Z])$/', $abbreviated, $matches1);
/** @var array{number: string, suffix: string} $matches1 */
$abbreviatedNumber = $matches1['number'];
$suffix = $matches1['suffix'];
$formattedNumber = static::numberFormat($abbreviatedNumber, $currency, $locale, decimals: $decimals);
if (! $showCurrencySymbol) {
return $formattedNumber.$suffix;
}
// Format the number
$formattedCurrency = static::format($abbreviatedNumber, $currency, $locale, decimals: $decimals);
// Find the formatted number
preg_match('/(?<number>[0-9\.,\s]+)/', $formattedCurrency, $matches2);
/** @var array{number: string} $matches2 */
return str_replace($matches2['number'], $formattedNumber.$suffix, $formattedCurrency);
}
public static function parseDecimal(
?string $moneyString,
Currency $currency,
string $locale,
int $decimals = 2
): string {
if (is_null($moneyString) || $moneyString === '') {
return '';
}
$numberFormatter = self::getNumberFormatter($locale, NumberFormatter::DECIMAL, $decimals);
$moneyParser = new IntlLocalizedDecimalParser($numberFormatter, new ISOCurrencies);
// Remove grouping separator from the money string
// This is needed to fix some parsing issues with small numbers such as
// "2,00" with "," left as thousands separator in the wrong place
// See: https://github.com/pelmered/filament-money-field/issues/20
$formattingRules = self::getFormattingRules($locale, $currency);
$moneyString = str_replace($formattingRules->groupingSeparator, '', $moneyString);
try {
return $moneyParser->parse($moneyString, $currency)->getAmount();
} catch (ParserException) {
throw new ParserException('The value must be a valid numeric value.');
}
}
public static function getFormattingRules(string $locale, Currency $currency): MoneyFormattingRules
{
$config = config('filament-money-field');
$numberFormatter = new NumberFormatter($locale, NumberFormatter::CURRENCY);
return new MoneyFormattingRules(
currencySymbol: $numberFormatter->getSymbol(
$config['intl_currency_symbol']
? NumberFormatter::INTL_CURRENCY_SYMBOL
: NumberFormatter::CURRENCY_SYMBOL
),
fractionDigits: $numberFormatter->getAttribute(NumberFormatter::FRACTION_DIGITS),
decimalSeparator: $numberFormatter->getSymbol(NumberFormatter::DECIMAL_SEPARATOR_SYMBOL),
groupingSeparator: $numberFormatter->getSymbol(NumberFormatter::GROUPING_SEPARATOR_SYMBOL),
);
}
private static function getNumberFormatter(string $locale, int $style, int $decimals = 2): NumberFormatter
{
$config = config('filament-money-field');
$numberFormatter = new NumberFormatter($locale, $style);
if ($decimals < 0) {
$numberFormatter->setAttribute(NumberFormatter::MAX_SIGNIFICANT_DIGITS, abs($decimals));
} else {
$numberFormatter->setAttribute(NumberFormatter::FRACTION_DIGITS, $decimals);
}
if ($config['intl_currency_symbol']) {
$intlCurrencySymbol = $numberFormatter->getSymbol(NumberFormatter::INTL_CURRENCY_SYMBOL);
if ($numberFormatter->getTextAttribute(NumberFormatter::POSITIVE_PREFIX) !== '') {
// "\xc2\xa0" is a non-breaking space
$numberFormatter->setTextAttribute(NumberFormatter::POSITIVE_PREFIX, $intlCurrencySymbol."\xc2\xa0");
}
if ($numberFormatter->getTextAttribute(NumberFormatter::POSITIVE_SUFFIX) !== '') {
// "\xc2\xa0" is a non-breaking space
$numberFormatter->setTextAttribute(NumberFormatter::POSITIVE_SUFFIX, "\xc2\xa0".$intlCurrencySymbol);
}
}
return $numberFormatter;
}
}