diff --git a/src/Forms/Components/MoneyInput.php b/src/Forms/Components/MoneyInput.php index 351945f..f97d8e7 100644 --- a/src/Forms/Components/MoneyInput.php +++ b/src/Forms/Components/MoneyInput.php @@ -15,31 +15,40 @@ 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 MoneyFormatter::decimalToMoneyString($state/100, $component->getLocale()); + + $this->formatStateUsing(function (MoneyInput $component, $state): ?string { + + $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); - return (int) ($state*100); + $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; + } }