phspa / laravel-currencies
Requires
- php: >=7.4.0|^8.0
- illuminate/container: ^8.0|^9.0|^10.0
- illuminate/support: ^8.0|^9.0|^10.0
Requires (Dev)
- calebporzio/sushi: ^2.0
- fakerphp/faker: ^1.4
- mockery/mockery: ^1.5
- nunomaduro/larastan: ^1.0|^2.0
- orchestra/testbench: ^6.0|^7.0|^8.0
- phpunit/phpunit: ^9.5|^10.0
This package is auto-updated.
Last update: 2024-09-11 04:04:27 UTC
README
本包提供了在Laravel中与货币和金额进行交互的便捷且强大的方式。
本包最初来自:https://github.com/makeabledk/laravel-currencies
安装
您可以通过composer安装此包
composer require phpsa/laravel-currencies
安装后,您必须发布并运行迁移来创建currencies
表
php artisan vendor:publish --provider="Phpsa\LaravelCurrencies\CurrenciesServiceProvider"
php artisan migrate
设置
推荐:播种货币
此包提供了一种开箱即用的eloquent 'Currency'模型,可以从您的数据库中获取可用的货币。
通常,您会希望从您的代码中播种这些货币,以便在各个环境中进行标准化。这可以通过Laravel Production Seeding包轻松实现。
创建以下播种器并在每次部署时运行它
<?php class CurrencySeeder extends \Illuminate\Database\Seeder { use \Phpsa\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, \Phpsa\LaravelCurrencies\Currency::class, 'code'); } }
现在,您的数据库表应该看起来像这样
提示:考虑使用https://github.com/dwightwatson/rememberable来缓存货币和限制数据库查询。
提示:如果您不希望硬编码汇率,创建一个控制台命令从外部服务获取并更新,并在播种器中省略该字段。
注册基本货币(必需)
金额对象需要一个基本货币,它使用该货币在货币之间进行转换。
您提供的货币汇率必须都与基本货币相关。
在您的AppServiceProvider@boot
中定义它
public function boot() { $this->app->singleton(\Phpsa\LaravelCurrencies\Contracts\BaseCurrency::class, function () { return \Phpsa\LaravelCurrencies\Currency::fromCode('EUR'); }); }
注册默认货币(可选)
此外,如果您的基本货币不是默认货币,您还有定义默认货币的选项。
您可能希望有一个全局货币,如USD或EUR,用于基本货币进行转换,同时您的应用程序默认显示本地货币。
这可以通过在您的AppServiceProvider@boot
中定义默认货币来实现
public function boot() { // Define base currency // [...] // Define default currency $this->app->singleton(\Phpsa\LaravelCurrencies\Contracts\BaseCurrency::class, function () { return \Phpsa\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模型,它有一个返回Amount对象的@getPriceAttribute()访问器,您甚至可以这样做
$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国际。有关更多信息,请参阅许可文件