Skip to content

Commit

Permalink
Adding documentation on methods and attributes.
Browse files Browse the repository at this point in the history
  • Loading branch information
hiuryoliveira committed Mar 19, 2024
1 parent 821912b commit 58214d5
Show file tree
Hide file tree
Showing 7 changed files with 134 additions and 5 deletions.
7 changes: 3 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,10 @@ MoneyColumn::make('price')
->locale('sv_SE');
```

## TDOO / Ideas for the future.
Contact me or create an issue if you want something of this, or something else.
I appriciate if you could tell me a bit about your use case for that feature as well.
## TODO / Ideas for the future.
Contact me or create an issue if you want something of this, or something else. I appreciate if you could tell me a bit about your use case for that feature as well.

- Improve test suite with tests for the individual components (I was struggling a bit with this. Help would be appriciated).
- Improve test suite with tests for the individual components (I was struggling a bit with this. Help would be appreciated).
- Add support for dynamic currency and locale based on current user.
- Currency conversions. Set what base currency the value in the database is and then convert to the current users preferred currency on the fly. Not sure how edit/create should be handled in this case.

17 changes: 16 additions & 1 deletion src/FilamentMoneyFieldServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,29 @@

class FilamentMoneyFieldServiceProvider extends \Spatie\LaravelPackageTools\PackageServiceProvider
{
/**
* The name of the package.
*
* @var string
*/
public static string $name = 'filament-money-field';

/**
* The package's config key.
*
* @var string
*/
public function configurePackage(Package $package): void
{
$package->name(static::$name)
->hasConfigFile();
->hasConfigFile();
}

/**
* Bootstrap the application services.
*
* @return void
*/
public function boot()
{
$this->publishes([
Expand Down
23 changes: 23 additions & 0 deletions src/Forms/Components/MoneyInput.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ class MoneyInput extends TextInput
{
use hasMoneyAttributes;

/**
* The name of the component.
*
* @var string
*/
protected function setUp(): void
{
parent::setUp();
Expand Down Expand Up @@ -46,6 +51,12 @@ protected function setUp(): void
});
}

/**
* Prepare the money input.
*
* @param MoneyInput $component
* @return void
*/
protected function prepare(MoneyInput $component): void
{
$formattingRules = MoneyFormatter::getFormattingRules($component->getLocale());
Expand All @@ -62,6 +73,12 @@ protected function prepare(MoneyInput $component): void
}
}

/**
* Set the minimum value of the money input.
*
* @param mixed $min
* @return static
*/
public function minValue(mixed $min): static
{
$this->rule(static function (MoneyInput $component, mixed $state) use ($min) {
Expand All @@ -85,6 +102,12 @@ public function minValue(mixed $min): static
return $this;
}

/**
* Set the maximum value of the money input.
*
* @param mixed $max
* @return static
*/
public function maxValue(mixed $max): static
{
$this->rule(static function (MoneyInput $component) use ($max) {
Expand Down
5 changes: 5 additions & 0 deletions src/Infolists/Components/MoneyEntry.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ class MoneyEntry extends TextEntry
{
use hasMoneyAttributes;

/**
* Setup component.
*
* @var string
*/
protected function setUp(): void
{
parent::setUp();
Expand Down
45 changes: 45 additions & 0 deletions src/MoneyFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,15 @@

class MoneyFormatter
{
/**
* Format a money value to a string.
*
* @param null|int|string $value
* @param Currency $currency
* @param string $locale
* @param int $outputStyle
* @return string
*/
public static function format(null|int|string $value, Currency $currency, string $locale, int $outputStyle = NumberFormatter::CURRENCY): string
{
if (is_null($value) || $value === '') {
Expand All @@ -24,11 +33,27 @@ public static function format(null|int|string $value, Currency $currency, string
return $moneyFormatter->format($money); // outputs $1.000,00
}

/**
* Format a money value to a string using the currency's default formatting.
*
* @param null|int|string $value
* @param Currency $currency
* @param string $locale
* @return string
*/
public static function formatAsDecimal(null|int|string $value, Currency $currency, string $locale): string
{
return static::format($value, $currency, $locale, NumberFormatter::DECIMAL); // outputs 1.000,00
}

/**
* Parse a money string to a decimal value.
*
* @param string $moneyString
* @param Currency $currency
* @param string $locale
* @return string
*/
public static function parseDecimal($moneyString, Currency $currency, string $locale): string
{
if (is_null($moneyString) || $moneyString === '') {
Expand All @@ -42,6 +67,12 @@ public static function parseDecimal($moneyString, Currency $currency, string $lo
return $moneyParser->parse($moneyString, $currency)->getAmount();
}

/**
* Get the formatting rules for a locale.
*
* @param string $locale
* @return MoneyFormattingRules
*/
public static function getFormattingRules($locale): MoneyFormattingRules
{
$config = config('filament-money-field');
Expand All @@ -55,11 +86,25 @@ public static function getFormattingRules($locale): MoneyFormattingRules
);
}

/**
* Convert a decimal value to a money string.
*
* @param string $moneyString
* @param string $locale
* @return string
*/
public static function decimalToMoneyString($moneyString, $locale): string
{
return str_replace(',', '.', (string)$moneyString);
}

/**
* Get a number formatter for a locale and style.
*
* @param string $locale
* @param int $style
* @return NumberFormatter
*/
private static function getNumberFormatter($locale, int $style): NumberFormatter
{
$numberFormatter = new NumberFormatter($locale, $style);
Expand Down
5 changes: 5 additions & 0 deletions src/Tables/Columns/MoneyColumn.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ class MoneyColumn extends TextColumn
{
use hasMoneyAttributes;

/**
* Setup component.
*
* @var string
*/
protected function setUp(): void
{
parent::setUp();
Expand Down
37 changes: 37 additions & 0 deletions src/hasMoneyAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,52 @@

trait hasMoneyAttributes
{
/**
* The currency of the money value.
*
* @var Currency
*/
protected Currency $currency;

/**
* The locale of the money value.
*
* @var string
*/
protected string $locale;

/**
* The decimal separator of the money value.
*
* @var string
*/
protected ?string $monetarySeparator = null;

/**
* Get the currency of the money value.
*
* @return Currency
*/
protected function getCurrency(): Currency
{
return $this->currency ?? $this->currency(config('filament-money-field.default_currency') ?? Infolist::$defaultCurrency)->getCurrency();
}

/**
* Get the locale of the money value.
*
* @return string
*/
protected function getLocale(): string
{
return $this->locale ?? config('filament-money-field.default_locale');
}

/**
* Get the decimal separator of the money value.
*
* @return string
*/
public function currency(string|\Closure|null $currencyCode = null): static
{
$this->currency = new Currency($currencyCode);
Expand All @@ -34,6 +66,11 @@ public function currency(string|\Closure|null $currencyCode = null): static
return $this;
}

/**
* Set the decimal separator of the money value.
*
* @return string
*/
public function locale(string|\Closure|null $locale = null): static
{
$this->locale = $locale;
Expand Down

0 comments on commit 58214d5

Please sign in to comment.