diff --git a/README.md b/README.md index 0053efe..3170dad 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/src/FilamentMoneyFieldServiceProvider.php b/src/FilamentMoneyFieldServiceProvider.php index 210b976..aaacb71 100644 --- a/src/FilamentMoneyFieldServiceProvider.php +++ b/src/FilamentMoneyFieldServiceProvider.php @@ -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([ diff --git a/src/Forms/Components/MoneyInput.php b/src/Forms/Components/MoneyInput.php index 15c00a5..2018d62 100644 --- a/src/Forms/Components/MoneyInput.php +++ b/src/Forms/Components/MoneyInput.php @@ -12,6 +12,11 @@ class MoneyInput extends TextInput { use hasMoneyAttributes; + /** + * The name of the component. + * + * @var string + */ protected function setUp(): void { parent::setUp(); @@ -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()); @@ -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) { @@ -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) { diff --git a/src/Infolists/Components/MoneyEntry.php b/src/Infolists/Components/MoneyEntry.php index 6a13346..0c5399c 100644 --- a/src/Infolists/Components/MoneyEntry.php +++ b/src/Infolists/Components/MoneyEntry.php @@ -10,6 +10,11 @@ class MoneyEntry extends TextEntry { use hasMoneyAttributes; + /** + * Setup component. + * + * @var string + */ protected function setUp(): void { parent::setUp(); diff --git a/src/MoneyFormatter.php b/src/MoneyFormatter.php index 95c9611..b1c26f5 100644 --- a/src/MoneyFormatter.php +++ b/src/MoneyFormatter.php @@ -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 === '') { @@ -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 === '') { @@ -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'); @@ -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); diff --git a/src/Tables/Columns/MoneyColumn.php b/src/Tables/Columns/MoneyColumn.php index e9ffe06..0638f02 100644 --- a/src/Tables/Columns/MoneyColumn.php +++ b/src/Tables/Columns/MoneyColumn.php @@ -10,6 +10,11 @@ class MoneyColumn extends TextColumn { use hasMoneyAttributes; + /** + * Setup component. + * + * @var string + */ protected function setUp(): void { parent::setUp(); diff --git a/src/hasMoneyAttributes.php b/src/hasMoneyAttributes.php index dd4cb26..74fde95 100644 --- a/src/hasMoneyAttributes.php +++ b/src/hasMoneyAttributes.php @@ -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); @@ -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;