From 62ba2ef11f67f3148db32b711cbb805ceb9a221c Mon Sep 17 00:00:00 2001 From: Irakli Kirkitadze Date: Tue, 30 Jan 2024 02:02:19 +0400 Subject: [PATCH 1/3] MoneyInput: return null when state is null --- src/Forms/Components/MoneyInput.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Forms/Components/MoneyInput.php b/src/Forms/Components/MoneyInput.php index 351945f..a80b10e 100644 --- a/src/Forms/Components/MoneyInput.php +++ b/src/Forms/Components/MoneyInput.php @@ -19,7 +19,7 @@ protected function setUp(): void $this->prefix($formattingRules->currencySymbol); - if(config('filament-money-field.use_input_mask')) { + if (config('filament-money-field.use_input_mask')) { $this->mask(RawJs::make('$money($input, \'' . $formattingRules->decimalSeparator . '\', \'' . $formattingRules->groupingSeparator . '\', ' . $formattingRules->fractionDigits . ')')); } $this->stripCharacters($formattingRules->groupingSeparator); @@ -28,18 +28,18 @@ protected function setUp(): void $this->step(0.01); $this->minValue = 0; - $this->formatStateUsing(static function (MoneyInput $component, $state): string { - return MoneyFormatter::decimalToMoneyString($state/100, $component->getLocale()); + $this->formatStateUsing(static function (MoneyInput $component, $state): ?string { + return is_null($state) ? null : MoneyFormatter::decimalToMoneyString($state / 100, $component->getLocale()); }); $this->dehydrateStateUsing(static function (MoneyInput $component, $state): string { $formattingRules = MoneyFormatter::getFormattingRules($component->getLocale()); - if($formattingRules->decimalSeparator === ',') { + if ($formattingRules->decimalSeparator === ',') { $state = str_replace(',', '.', $state); } - return (int) ($state*100); + return (int)($state * 100); }); } } From 232464e100ab14b155daf0b6a30882c5e03ef6bc Mon Sep 17 00:00:00 2001 From: Irakli Kirkitadze Date: Tue, 30 Jan 2024 02:14:23 +0400 Subject: [PATCH 2/3] MoneyInput: fix runtime setting currency & locale --- src/Forms/Components/MoneyInput.php | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/src/Forms/Components/MoneyInput.php b/src/Forms/Components/MoneyInput.php index a80b10e..c6ea209 100644 --- a/src/Forms/Components/MoneyInput.php +++ b/src/Forms/Components/MoneyInput.php @@ -15,21 +15,25 @@ protected function setUp(): void { parent::setUp(); - $formattingRules = MoneyFormatter::getFormattingRules($this->getLocale()); - $this->prefix($formattingRules->currencySymbol); - - if (config('filament-money-field.use_input_mask')) { - $this->mask(RawJs::make('$money($input, \'' . $formattingRules->decimalSeparator . '\', \'' . $formattingRules->groupingSeparator . '\', ' . $formattingRules->fractionDigits . ')')); - } - $this->stripCharacters($formattingRules->groupingSeparator); $this->inputMode('decimal'); $this->rule('numeric'); $this->step(0.01); $this->minValue = 0; - $this->formatStateUsing(static function (MoneyInput $component, $state): ?string { - return is_null($state) ? null : MoneyFormatter::decimalToMoneyString($state / 100, $component->getLocale()); + $this->formatStateUsing(function (MoneyInput $component, $state): ?string { + + $formattingRules = MoneyFormatter::getFormattingRules($component->getLocale()); + + $this->prefix($formattingRules->currencySymbol); + + if (config('filament-money-field.use_input_mask')) { + $this->mask(RawJs::make('$money($input, \'' . $formattingRules->decimalSeparator . '\', \'' . $formattingRules->groupingSeparator . '\', ' . $formattingRules->fractionDigits . ')')); + } + + $this->stripCharacters($formattingRules->groupingSeparator); + + return is_null($state) ? null : MoneyFormatter::decimalToMoneyString($state / 100, $component->getLocale()); }); $this->dehydrateStateUsing(static function (MoneyInput $component, $state): string { From ab600046d8773aabd947e54bcd6d8dce288ec4c5 Mon Sep 17 00:00:00 2001 From: Irakli Kirkitadze Date: Tue, 30 Jan 2024 02:48:42 +0400 Subject: [PATCH 3/3] add ability to set monetarySeparator --- src/Forms/Components/MoneyInput.php | 33 ++++++++++++++----------- src/Infolists/Components/MoneyEntry.php | 4 +-- src/MoneyFormatter.php | 14 ++++++----- src/Tables/Columns/MoneyColumn.php | 4 +-- src/hasMoneyAttributes.php | 13 ++++++++-- 5 files changed, 42 insertions(+), 26 deletions(-) diff --git a/src/Forms/Components/MoneyInput.php b/src/Forms/Components/MoneyInput.php index c6ea209..f97d8e7 100644 --- a/src/Forms/Components/MoneyInput.php +++ b/src/Forms/Components/MoneyInput.php @@ -21,29 +21,34 @@ protected function setUp(): void $this->step(0.01); $this->minValue = 0; - $this->formatStateUsing(function (MoneyInput $component, $state): ?string { - - $formattingRules = MoneyFormatter::getFormattingRules($component->getLocale()); - $this->prefix($formattingRules->currencySymbol); + $this->formatStateUsing(function (MoneyInput $component, $state): ?string { - if (config('filament-money-field.use_input_mask')) { - $this->mask(RawJs::make('$money($input, \'' . $formattingRules->decimalSeparator . '\', \'' . $formattingRules->groupingSeparator . '\', ' . $formattingRules->fractionDigits . ')')); - } - - $this->stripCharacters($formattingRules->groupingSeparator); + $this->prepare($component); return is_null($state) ? null : MoneyFormatter::decimalToMoneyString($state / 100, $component->getLocale()); }); - $this->dehydrateStateUsing(static function (MoneyInput $component, $state): string { - $formattingRules = MoneyFormatter::getFormattingRules($component->getLocale()); + $this->dehydrateStateUsing( function (MoneyInput $component, $state): string { - if ($formattingRules->decimalSeparator === ',') { - $state = str_replace(',', '.', $state); - } + $this->prepare($component); + + $state = str_replace(',', '.', $state); return (int)($state * 100); }); } + + protected function prepare(MoneyInput $component): void + { + $formattingRules = MoneyFormatter::getFormattingRules($component->getLocale()); + + $this->prefix($formattingRules->currencySymbol); + + if (config('filament-money-field.use_input_mask')) { + $this->mask(RawJs::make('$money($input, \'' . $formattingRules->decimalSeparator . '\', \'' . $formattingRules->groupingSeparator . '\', ' . $formattingRules->fractionDigits . ')')); + } + + $this->stripCharacters($formattingRules->groupingSeparator); + } } diff --git a/src/Infolists/Components/MoneyEntry.php b/src/Infolists/Components/MoneyEntry.php index 275327a..adc65ff 100644 --- a/src/Infolists/Components/MoneyEntry.php +++ b/src/Infolists/Components/MoneyEntry.php @@ -17,11 +17,11 @@ protected function setUp(): void $this->isMoney = true; $this->numeric(); - $this->formatStateUsing(static function (MoneyEntry $component, $state): ?string { + $this->formatStateUsing(function (MoneyEntry $component, $state): ?string { $currency = $component->getCurrency(); $locale = $component->getLocale(); - return MoneyFormatter::format($state, $currency, $locale); + return MoneyFormatter::format($state, $currency, $locale, $this->monetarySeparator); }); } } diff --git a/src/MoneyFormatter.php b/src/MoneyFormatter.php index 68a3b40..9041f5c 100644 --- a/src/MoneyFormatter.php +++ b/src/MoneyFormatter.php @@ -1,4 +1,5 @@ setSymbol(NumberFormatter::MONETARY_SEPARATOR_SYMBOL, $monetarySeparator); + } + $numberFormatter->setAttribute(NumberFormatter::FRACTION_DIGITS, 2); $moneyFormatter = new IntlMoneyFormatter($numberFormatter, $currencies); $money = new Money($value, $currency); - return $moneyFormatter->format($money); } @@ -37,11 +41,9 @@ public static function getFormattingRules($locale): MoneyFormattingRules public static function decimalToMoneyString($moneyString, $locale): string { $formattingRules = self::getFormattingRules($locale); - $moneyString = (string) $moneyString; + $moneyString = (string)$moneyString; - if($formattingRules->decimalSeparator === ',') { - $moneyString = str_replace('.', ',', (string) $moneyString); - } + $moneyString = str_replace(',', '.', (string)$moneyString); return $moneyString; } diff --git a/src/Tables/Columns/MoneyColumn.php b/src/Tables/Columns/MoneyColumn.php index f88373e..50a8c04 100644 --- a/src/Tables/Columns/MoneyColumn.php +++ b/src/Tables/Columns/MoneyColumn.php @@ -17,11 +17,11 @@ protected function setUp(): void $this->isMoney = true; $this->numeric(); - $this->formatStateUsing(static function (MoneyColumn $column, $state): ?string { + $this->formatStateUsing(function (MoneyColumn $column, $state): ?string { $currency = $column->getCurrency(); $locale = $column->getLocale(); - return MoneyFormatter::format($state, $currency, $locale); + return MoneyFormatter::format($state, $currency, $locale, $this->monetarySeparator); }); } diff --git a/src/hasMoneyAttributes.php b/src/hasMoneyAttributes.php index 3a8a4fa..f3ff698 100644 --- a/src/hasMoneyAttributes.php +++ b/src/hasMoneyAttributes.php @@ -1,4 +1,5 @@ currency = new Currency($currencyCode); - $currencies = new ISOCurrencies(); + $currencies = new ISOCurrencies(); - if (! $currencies->contains($this->currency)) { + if (!$currencies->contains($this->currency)) { throw new \RuntimeException('Currency not supported: ' . $currencyCode); } @@ -38,4 +40,11 @@ public function locale(string|\Closure|null $locale = null): static return $this; } + + public function monetarySeparator(string|\Closure|null $monetarySeparator = null): static + { + $this->monetarySeparator = $monetarySeparator; + + return $this; + } }