makeabledk / laravel-currencies
Requires
- php: ^8.1
- illuminate/container: ^10.0|^11.0
- illuminate/support: ^10.0|^11.0
Requires (Dev)
- calebporzio/sushi: ^2.0
- fakerphp/faker: ^1.4
- laravel/laravel: ^10.0|^11.0
- mockery/mockery: ^1.5
- phpunit/phpunit: ^9.5
This package is auto-updated.
Last update: 2024-09-19 15:15:34 UTC
README
本包提供了一种方便且强大的方法,用于在Laravel中与货币和金额进行交互。
安装
您可以通过composer安装此包
composer require makeabledk/laravel-currencies
在Laravel版本 < 5.5 中,您必须将服务提供者在您的 config/app.php
中包含
'providers' => [ ... /* * Package Service Providers... */ \Makeable\LaravelCurrencies\CurrenciesServiceProvider::class, ]
安装后,您必须发布并运行迁移以创建 currencies
表
php artisan vendor:publish --provider="Makeable\LaravelCurrencies\CurrenciesServiceProvider"
php artisan migrate
设置
推荐:播种货币
本包提供了一个eloquent 'Currency'模型,该模型可以从您的数据库中检索可用的货币。
通常,您可能希望从代码中播种这些货币,以便在各个环境中规范化它们。这可以通过使用Laravel Production Seeding包轻松实现。
创建以下播种器并在每次部署时运行它
<?php class CurrencySeeder extends \Illuminate\Database\Seeder { use \Makeable\ProductionSeeding\SyncStrategy; /** * @var array */ protected $currencies = [ [ 'code' => 'EUR', 'exchange_rate' => 100 ], [ 'code' => 'DKK', 'exchange_rate' => 750 ], // ... ]; /** * Seed the currencies */ public function run() { $this->apply($this->currencies, \Makeable\LaravelCurrencies\Currency::class, 'code'); } }
现在,您的数据库表应该看起来像这样
提示:考虑使用https://github.com/dwightwatson/rememberable来缓存货币并限制数据库查询。
提示:如果您不想硬编码汇率,可以创建一个控制台命令,从外部服务获取并更新,并在播种器中省略该字段。
注册基础货币(必需)
金额对象需要一个基础货币,该货币用于在货币之间进行转换。
您提供的货币汇率必须都与基础货币相关。
在您的 AppServiceProvider@boot
中定义它
public function boot() { $this->app->singleton(\Makeable\LaravelCurrencies\Contracts\BaseCurrency::class, function () { return \Makeable\LaravelCurrencies\Currency::fromCode('EUR'); }); }
注册默认货币(可选)
此外,您还可以选择定义一个默认货币,如果它不是基础货币。
您可能希望有一个全球货币,如USD或EUR,作为基础货币进行转换,同时您的应用程序默认显示本地货币。
这可以通过在您的 AppServiceProvider@boot
中定义默认货币来实现
public function boot() { // Define base currency // [...] // Define default currency $this->app->singleton(\Makeable\LaravelCurrencies\Contracts\BaseCurrency::class, function () { return \Makeable\LaravelCurrencies\Currency::fromCode('DKK'); }); }
现在,当没有显式货币时,实例化的金额将默认为'DKK'
new Amount (100); // 100 DKK Amount::zero(); // 0 DKK
示例用法
快速创建金额
new Amount(100); // EUR since that's our default new Amount(100, Currency::fromCode('DKK')); new Amount(100, 'DKK'); // It automatically instantiates a currency instance given a currency-code
在货币之间转换
$eur = new Amount(100); $dkk = $eur->convertTo('DKK'); // 750 DKK
执行简单计算 - 即使是在货币之间!
$amount = new Amount(100, 'EUR'); $amount->subtract(new Amount(50)); // 50 eur $amount->subtract(new Amount(375, 'DKK')); // 50 eur
假设您有一个Product eloquent模型,它有一个@getPriceAttribute()访问器,返回Amount对象,您甚至可以这样做
$products = Product::all(); $productsTotalSum = Amount::sum($products, 'price');
使用流畅的修饰符进行轻松操作
$amount = new Amount(110); // Ensure that the amount at least a certain amount $amount->minimum(new Amount(80)); // 110 EUR $amount->minimum(new Amount(120)); // 120 EUR // Ensure that the amount is no bigger than a certain amount $amount->maximum(new Amount(80)); // 80 EUR $amount->maximum(new Amount(750, 'DKK'); // 100 EUR (eq. 750 DKK)
轻松导出为数组,并在需要时重新实例化。非常适合服务客户端API。
$exported = (new Amount(100))->toArray(); // ['amount' => 100, 'currency' => 'EUR', 'formatted' => 'EUR 100'] $imported = Amount::fromArray($exported);
注意:它实现了illuminate/support Arrayable契约,因此对于eloquent模型自动转换为数组。
变更日志
有关最近更改的更多信息,请参阅CHANGELOG
测试
您可以使用以下命令运行测试
composer test
贡献
我们很高兴接受为额外功能添加功能的拉取请求。有关详细信息,请参阅CONTRIBUTING
致谢
许可
署名-相同4.0国际。有关更多信息,请参阅许可文件