pelmered/filament-money-field

1.4.1 2024-08-09 16:42 UTC

README

由 Money PHP 驱动的货币字段。

此包为 Filament 中的货币字段提供了比大多数其他包更好的本地化支持,尤其是 TextColumns 和 TextEntries 内置的货币支持。例如,在货币符号、小数点和千位分隔符方面。特别是对于更少见的货币。这还包括一个处理本地化格式的输入字段。

瑞典本地化的货币字段示例。此包将给出 "1 234,56 kr",而大多数其他解决方案可能给出类似 "SEK 1234.56" 的内容,这不是瑞典的正确格式。

Latest Stable Version Total Downloads Monthly Downloads License

Tests Build Status Scrutinizer Code Quality Code Coverage

Tested on PHP 8.2 to 8.3 Tested on OS:es Linux, MacOS, Windows

需求

  • PHP 8.2 或更高版本
  • Filament 3.0 或更高版本
  • PHP 国际化扩展(intl)
  • 数据库列应该是小数单位(例如,分)的整数,而不是浮点数(浮点数永远不会用于存储货币)。

主要功能

  • 大多数地区完全本地化的货币字段。如果您发现某个地区不符合预期,请报告问题。
  • 包含完全本地化
    • 带有货币符号和(可选)输入掩码的输入字段。
    • 表格列。
    • 信息列表条目。
  • 全面的测试套件。
  • 全局或按字段配置货币和地区。
  • 有效数字输入的验证规则以及最小/最大值。
  • 一个可以在您的项目中使用的 货币格式化类。任何那里的公共方法都被认为是稳定的,并且不会在没有主要版本更新的情况下更改。

您是否在使用此包获得利润?请考虑 赞助我

安装

composer require pelmered/filament-money-field

配置您的地区

设置默认货币和地区

设置货币和地区的默认选项,这样您就不必为每个字段设置它们。

选项 1(推荐):将默认选项放在您的 .env 文件中。

MONEY_DEFAULT_LOCALE=sv_SE
MONEY_DEFAULT_CURRENCY=SEK

选项 2:发布配置文件并在其中设置默认选项。

php artisan vendor:publish --provider="Pelmered\FilamentMoneyField\FilamentMoneyFieldServiceProvider" --tag="config"

小数和有效数字

小数位数和有效数字可以在配置文件中设置。默认为 2。

//with input 123456
MONEY_DECIMAL_DIGITS=0 // Gives 0 decimals, e.g. $1,235
MONEY_DECIMAL_DIGITS=2 // Gives 2 decimals, e.g. $1,234.56

对于有效数字,使用负值。例如 -2 将给出 2 个有效数字。

//with input 12345678
MONEY_DECIMAL_DIGITS=-2 // Gives 2 significant digits, e.g. $120,000
MONEY_DECIMAL_DIGITS=-4 // Gives 4 significant digits, e.g. $123,400

这也可以按字段设置。

MoneyInput::make('price')->decimals(0);
MoneyEntry::make('price')->decimals(2);
MoneyColumn::make('price')->decimals(-2);

用法

表单

use Pelmered\FilamentMoneyField\Forms\Components\MoneyInput;

MoneyInput::make('price'); // Defaults to USD and the current Laravel locale, or what you have set in your .env/config.

MoneyInput::make('price')
    ->currency('USD')
    ->locale('en_US'),

MoneyInput::make('price')
    ->currency('SEK')
    ->locale('sv_SE'),

MoneyInput::make('price')
    ->currency('SEK')
    ->locale('sv_SE')
    ->minValue(0) // Do not allow negative values.
    ->maxValue(10000) // Add min and max value (in minor units, i.e. cents) to the input field. In this case no values over 100
    ->step(100) // Step value for the input field. In this case only multiples of 100 are allowed.
    ->decimals(0)
    ->getSymbolPlacement('after'), // Possible options: 'after', 'before', 'none'. Defaults to 'before'

表格列

use Pelmered\FilamentMoneyField\Tables\Columns\MoneyColumn;

MoneyColumn::make('price'); // Defaults to USD and the current Laravel locale, or what you have set in your .env/config.

MoneyColumn::make('price')
    ->currency('USD')
    ->locale('en_US'),

MoneyColumn::make('price')
    ->currency('SEK')
    ->locale('sv_SE'),

MoneyColumn::make('price')
    ->short(), // Short fromat, e.g. $1.23M instead of $1,234,567.89

信息列表

use Pelmered\FilamentMoneyField\Infolists\Components\MoneyEntry;

MoneyEntry::make('price'); // Defaults to USD and the current Laravel locale, or what you have set in your .env/config.

// The default can be set in the Infolist class with:
public static string $defaultCurrency = 'SEK';

MoneyEntry::make('price')
    ->currency('USD')
    ->locale('en_US'),

MoneyEntry::make('price')
    ->currency('SEK')
    ->locale('sv_SE'),

MoneyEntry::make('price')
    ->short(), // Short fromat, e.g. $1.23M instead of $1,234,567.89

全局配置

如果您想在 MoneyInput 组件上使用格式化掩码

这将自动格式化输入字段,您在输入时将看到格式化。

目前这有点实验性,因此默认禁用。希望未来会改进并在下一个主要版本中默认启用。请尝试使用并提供反馈。

MONEY_USE_INPUT_MASK=true // Defaults to false

使用国际货币代码(ISO 4217)

如果您想使用国际货币代码而不是它们的符号或本地简短变体。例如 USD 而不是 $,EUR 而不是 € 或 SEK 而不是 kr。

MONEY_INTL_CURRENCY_SYMBOL=true // Defaults to false

输入字段上货币符号/代码的位置

可能的选项: afterbeforenone

MONEY_UNIT_PLACEMENT=after // Defaults to before

小数和有效数字

小数位数和有效数字可以在配置文件中设置。默认为 2。

//with input 123456
MONEY_DECIMAL_DIGITS=0 // Gives 0 decimals, e.g. $1,235
MONEY_DECIMAL_DIGITS=2 // Gives 2 decimals, e.g. $1,234.56

对于有效数字,使用负值。例如 -2 将给出 2 个有效数字。

//with input 12345678
MONEY_DECIMAL_DIGITS=-2 // Gives 2 significant digits, e.g. $120,000
MONEY_DECIMAL_DIGITS=-4 // Gives 4 significant digits, e.g. $123,400

这也可以按字段设置。

MoneyInput::make('price')->decimals(0);
MoneyEntry::make('price')->decimals(2);
MoneyColumn::make('price')->decimals(-2);

// You can also pass a callback to the decimals method.
MoneyInput::make('price')->decimals(function () {
    return 0;
});

路线图/未来想法

如果您想使用这个功能或其它功能,请联系我或创建一个问题。如果您能告诉我一下您对这个功能的用法,我会很感激。

  • 改进输入掩码。
  • 根据当前用户添加对动态货币和区域的支持。
  • 货币转换。设置数据库中的值所基于的货币,然后即时转换为当前用户首选的货币。不确定在这种情况下如何处理编辑/创建。

贡献

我很高兴接受带有修复或改进的PR。如果是新功能,最好先打开一个问题,这样我可以给出反馈,并看看这个功能是否适合这个包。尤其是对于更大的功能,这样你不会浪费时间。

当您提交PR时,如果您

  • 为您的代码添加测试。这不是强制性的要求。如果您不确定,请寻求指导。如果有时间,我会尽力帮助。
  • 运行测试套件,并确保使用composer test通过。
  • 使用composer lint检查代码。这将运行PHPStan和Pint。在提交之前,看看您是否可以解决那里的问题。