定制 finller/laravel-forex 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

finller/laravel-forex

最新稳定版本:v2.2.0

Composer 安装命令:

composer require finller/laravel-forex

包简介

Forex for Laravel

README 文档

README

Latest Version on Packagist Tests Code Style PHPStan Level Laravel Pint Total Downloads

Laravel Forex is a simple and flexible package for retrieving the latest and historical foreign exchange rates in your Laravel application.

By default, it uses the free tier from exchangerate-api.com, but you can easily configure it to use any other Forex provider.

Installation

Install via Composer:

composer require elegantly/laravel-forex

Publish the configuration file:

php artisan vendor:publish --tag="forex-config"

Configuration

Here’s the default configuration that will be published to config/forex.php:

use Elegantly\Forex\Integrations\ExchangeRateApiFree\ExchangeRateApiFreeConnector;

return [

    'cache' => [
        'enabled' => true,
        'driver' => env('FOREX_CACHE_DRIVER', env('CACHE_STORE', env('CACHE_DRIVER', 'file'))),
        'expiry_seconds' => 86_400, // 1 day
    ],

    'rate_limit' => [
        'enabled' => false,
        'driver' => env('FOREX_RATE_LIMIT_DRIVER', env('CACHE_STORE', env('CACHE_DRIVER', 'file'))),
        'every_seconds' => 3_600, // 1 hour
    ],

    'client' => ExchangeRateApiFreeConnector::class,

    'clients' => [
        'exchange-rate-api' => [
            'token' => env('EXCHANGE_RATE_API_TOKEN'),
        ],
    ],

];

Usage

Get Latest Rates

use Elegantly\Forex\Facades\Forex;

$rates = Forex::latest('USD');

$usdToEur = $rates['EUR'];

Get Historical Rates

use Carbon\Carbon;
use Elegantly\Forex\Facades\Forex;

$rates = Forex::rates(Carbon::create(2022, 4, 25), 'USD');

$usdToEur = $rates['EUR'];

Convert Money Between Currencies

You can easily convert a Money instance from one currency to another using the Forex::convert() method.

This method ensures high-precision conversions by leveraging the brick/money PHP library.

use Elegantly\Forex\Facades\Forex;
use Brick\Money\Money;

$convertedMoney = Forex::convert(
    money: Money::of(100, 'USD'),
    currency: 'EUR',
);

$convertedMoney->__toString(); // (EUR) 88.84

Note: The returned value is a Money object representing the amount in the target currency.

Testing

Run the test suite with:

composer test

Changelog

See the CHANGELOG for details on recent updates.

Contributing

Contributions are welcome! Please read the CONTRIBUTING guide for details.

Security

If you discover any security-related issues, please refer to our security policy.

Credits

License

This package is open-source software licensed under the MIT license.

统计信息

  • 总下载量: 2.39k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 4
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 4
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-03-03