Skip to content

Commit

Permalink
add ability to set monetarySeparator
Browse files Browse the repository at this point in the history
  • Loading branch information
kirkita committed Jan 29, 2024
1 parent 232464e commit ab60004
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 26 deletions.
33 changes: 19 additions & 14 deletions src/Forms/Components/MoneyInput.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
4 changes: 2 additions & 2 deletions src/Infolists/Components/MoneyEntry.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
}
}
14 changes: 8 additions & 6 deletions src/MoneyFormatter.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

namespace Pelmered\FilamentMoneyField;

use Money\Currencies\ISOCurrencies;
Expand All @@ -8,17 +9,20 @@

class MoneyFormatter
{
public static function format($value, $currency, $locale): string
public static function format($value, $currency, $locale, $monetarySeparator = null): string
{
$currencies = new ISOCurrencies();
$numberFormatter = new NumberFormatter($locale, NumberFormatter::CURRENCY);

if (!is_null($monetarySeparator)) {
$numberFormatter->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);
}

Expand All @@ -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;
}
Expand Down
4 changes: 2 additions & 2 deletions src/Tables/Columns/MoneyColumn.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});

}
Expand Down
13 changes: 11 additions & 2 deletions src/hasMoneyAttributes.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

namespace Pelmered\FilamentMoneyField;

use Filament\Infolists\Infolist;
Expand All @@ -9,6 +10,7 @@ trait hasMoneyAttributes
{
protected Currency $currency;
protected string $locale;
protected ?string $monetarySeparator = null;

protected function getCurrency(): Currency
{
Expand All @@ -23,9 +25,9 @@ protected function getLocale(): string
public function currency(string|\Closure|null $currencyCode = null): static
{
$this->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);
}

Expand All @@ -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;
}
}

0 comments on commit ab60004

Please sign in to comment.