abdelrahmanbadr/currency-rates

与exchangeratesapi货币汇率API交互的库

dev-master 2020-06-13 22:14 UTC

This package is auto-updated.

Last update: 2024-09-04 14:23:09 UTC


README

安装

Currency Rates Package支持Laravel 5.5及以上版本。

通过composer安装包

  • composer require abdelrahmanbadr/currency-rates
  • 在.env文件中设置exchangeratesapi URL到EXCHANGE_RATES_API_URL
  • 在.env文件中设置缓存启用或禁用(true或false)CURRENCY_RATES_CACHE_IS_ENABLED
  • 在.env文件中设置缓存过期时间(整数)CURRENCY_RATES_CACHE_EXPIRY

使用方法

让我们获取以USD为基础货币的EUR和GBP的汇率

use Abdelrahman_badr\CurrencyRates\Core\Facades\CurrencyService;

$currency = CurrencyService::getLatest("EUR",["GBP","USD"]);

$rates = $currency->rates;

默认情况下,基础货币是EUR,因此如果您的基准货币是EUR,则无需设置它。符号也可以省略,因为Exchange Rates Api将返回所有支持货币。

没有基础货币和货币的简化示例

$currency = CurrencyService::getLatest();

historical选项将返回您指定日期以来每一天的货币汇率。基础货币和符号也可以省略,但让我们看一个完整的示例

$currency = CurrencyService::getHistorical(new \DateTime(),(new \DateTime())->modify('-15 days'),"EUR",["GBP","USD"]);

这将获取从15天前到今天的可用汇率

export latest rates to csv将文件名和与getLatest函数相同的参数

CurrencyService::exportLatest('fliename');

export historical rates to csv将文件名和与getHistorical函数相同的参数

CurrencyService::exportHistorical('fliename',new \DateTime(),new \DateTime("2019-01-01"));

运行单元测试

$ ./vendor/bin/phpunit

希望有(@Todo)

1. Add Cli Command To Get Currency Exchange Rates
2- More validation for package user inputs
3- Create convert service like convert 100 USD to EUR 
4- Add more providers such as Fixerio and make config switch between them
5- Add logs to trace errors
6- Use transform layer for the result
7- Use config file