tomlankhorst / eurofxref-exchange-provider
砖/money 的 EuroFXRef 兑换率提供商
1.0.1
2018-10-10 11:48 UTC
Requires
- php: ^7.1.3
- brick/money: 0.3.*
- guzzlehttp/guzzle: ^6.3
- psr/http-message: ^1.0
- psr/simple-cache: ^1.0
Requires (Dev)
- mockery/mockery: ^1.2
- phpunit/phpunit: ^7
This package is auto-updated.
Last update: 2024-09-19 10:46:18 UTC
README
为 brick/money 提供兑换率转换的提供商,使用欧洲中央银行(ECB)的每日参考汇率。
https://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml
使用 PSR-16 SimpleCache 和 PSR-7 HTTP-Messages。通常与 GuzzleHTTP 和所选缓存一起工作。
在项目中使用提供商
在项目中除了 brick/money 之外,还需要这个包。
composer require tomlankhorst/eurofxref-exchange-provider ^1.0
将 EuroProvider 注册为所选的 ExchangeRateProvider。在 Laravel 中
<?php namespace App\Providers; use Brick\Money\CurrencyConverter; use Brick\Money\ExchangeRateProvider; use GuzzleHttp\Client; use GuzzleHttp\ClientInterface; use Illuminate\Container\Container; use Illuminate\Contracts\Cache\Repository; use Illuminate\Support\ServiceProvider; use Psr\SimpleCache\CacheInterface; use tomlankhorst\EuroFXRefExchangeProvider\EuroProvider; class MoneyServiceProvider extends ServiceProvider { public function register() { // PSR-7 HTTP Messages $this->app->bind(ClientInterface::class, Client::class); // PSR-16 SimpleCache $this->app->bind(CacheInterface::class, Repository::class); // Bind the ExchangeProvider to the EuroProvider with a BaseCurrencyProvider // wrapper because it is a one-way conversion in this case. $this->app->bind(ExchangeRateProvider::class, function(Container $app){ $provider = $app->makeWith(EuroProvider::class, [ // Do not forget to set the TTL 'ttl' => config('services.eurofxref.cache.ttl') ]); return new ExchangeRateProvider\BaseCurrencyProvider($provider, 'EUR'); }); $this->app->singleton(CurrencyConverter::class, function(Container $app){ return new CurrencyConverter($app->make(ExchangeRateProvider::class)); }); } }
运行测试
安装依赖并运行 PHPUnit
composer install
./vendor/bin/phpunit