lubo13/money-bridge-bundle

moneyphp/money 库的 Symfony Bridge Bundle

安装: 10

依赖: 0

建议者: 0

安全: 0

星星: 2

关注者: 2

分支: 2

开放问题: 0

类型:symfony-bundle

v1.0.0 2020-12-26 09:32 UTC

This package is auto-updated.

Last update: 2024-09-26 17:44:44 UTC


README

v1.0.0

此桥接 Bundle 的目标是

一个简单的 Symfony Bridge Bundle,用于 moneyphp/money 库的集成,适用于与 Api Platform 集成。如果您需要更复杂的与 Symfony 表单集成、Twig 过滤器等的 Bundle,请查看 -> tbbc/money-bundle 包

  1. 与 Doctrine 兼容 - Money\Money 和 Money\Currency 被映射为可使用的 Embeddables 类
  2. 与 Symfony Serializer 组映射 - 每个类有 2 个组,用于输入和输出。(money:output, money:input, currency:output, currency:input)
  3. 与验证器约束映射 - Money 的金额属性映射到 NotBlank 和自定义 Amount 约束(存在更改行为的可能性 - 金额只能是正数或正数和非零)。货币映射到 Currency 约束,货币代码属性映射到自定义 CurrencyCodeChoice 约束(存在更改允许货币代码的可能性)

要求

PHP 7.4+;Symfony 4.4+;moneyphp/money 3.3+

安装

通过 Composer

$ composer require lubo13/money-bridge-bundle

Bundle 应自动启用,只需检查并确保其在 config/bundles.php 中

Money\Bridge\MoneyBridgeBundle::class => ['all' => true],

使用

默认情况下,Bundle 随附预配置选项,您可以根据自己的需要更改这些选项。

要更改某些预配置选项,请在新创建的 config/packages 目录中的 money_bridge.yaml 文件中添加以下内容

money_bridge:
    integer_part: 12 # How many digits will you have before the dot -> 1234.
    fractional_part: !php/const Money\Bridge\Util\AmountFormatter::MONEY_FRACTIONAL_PART # How many digits will you have after the dot -> .00
    default_currency_code: 'EUR' # With this option you can control default currency (useful with usage of Serializer or [Api Platform](https://api-platform.com/)).
    allowed_currency_code: [ 'EUR' ] # With this option you can control allowed currency when the Currency is validated.
    amount_validation_rule: positive # There are two option positive or positive_or_zero. With this option, you can control input data on Deserialization process. (useful with usage of Serializer or [Api Platform](https://api-platform.com/))

将 Money 和 Currency 库作为 embeddables 类使用

/**
* @ORM\Embedded(class="Money\Money")
*/
private Money $price;

/**
* @ORM\Embedded(class="Money\Money")
*/
private $price;

如果您编写自己的迁移手册,您需要向您的表添加两个新列

$this->addSql('ALTER TABLE YOUR_TABLE_NAME ADD price_amount BIGINT DEFAULT NULL');
$this->addSql('ALTER TABLE YOUR_TABLE_NAME ADD price_currency_code VARCHAR(3) NULL');

为了使 Money 和 Currency 的属性能够正确地序列化和反序列化,您可以使用以下组

For deserialize:

    money:input -> Moneys' amount property
    
    currency:input -> for Currencies' code property

For serialize:

    money:output -> Moneys' amount property
    
    currency:output -> for Currencies' code property

在您的应用中格式化价格金额以与 Money 库兼容

\Money\Bridge\Util\AmountFormatter::getAmountWithoutFractional('100.99', 2);

许可证

MIT 许可证(MIT)。有关更多信息,请参阅 许可证文件