steffenbrand/curr-curr 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

steffenbrand/curr-curr

Composer 安装命令:

composer require steffenbrand/curr-curr

包简介

Delivers current exchange rates for EUR provided by the ECB as PHP objects.

README 文档

README

Build Coverage Scrutinizer Code Quality Latest Stable Version Latest Unstable Version Total Downloads License composer.lock available

CurrCurr Logo

Delivers current exchange rates for EUR provided by the ECB under https://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml as PHP objects.

How to install

composer require steffenbrand/curr-curr

How to use

Request exchange rate for specific currency

try {
    $cc = new CurrCurr();
    $exchangeRate = $cc->getExchangeRateByCurrency(Currency::USD);

    $exchangeRate->getDate();
    $exchangeRate->getCurrency();
    $exchangeRate->getRate();
} catch (ExchangeRatesRequestFailedException $e) {
    // webservice might not be present
} catch (ExchangeRatesMappingFailedException $e) {
    // webservice might not deliver what we expect
} catch (CurrencyNotSupportedException $e) {
    // requested currency might not be provided
}

Request all available exchange rates

try {
    $cc = new CurrCurr();
    $exchangeRates = $cc->getExchangeRates();

    $exchangeRates[Currency::USD]->getDate();
    $exchangeRates[Currency::USD]->getCurrency();
    $exchangeRates[Currency::USD]->getRate();

    foreach ($exchangeRates as $exchangeRate) {
        $exchangeRate->getDate();
        $exchangeRate->getCurrency();
        $exchangeRate->getRate();
    }
} catch (ExchangeRatesRequestFailedException $e) {
    // webservice might not be present
} catch (ExchangeRatesMappingFailedException $e) {
    // webservice might not deliver what we expect
}

Using PSR-16 SimpleCache

CurrCurr does not provide its own SimpleCache implementation, however it does give you the possibility to inject any PSR-16 compliant implementation into the EcbClient. You just have to wrap it with a CacheConfig instance.

$cc = new CurrCurr(
    new EcbClient(
        EcbClient::DEFAULT_EXCHANGE_RATES_URL,
        new CacheConfig(
            new OpCache(sys_get_temp_dir() . '/cache')
            // Any PSR-16 compliant implementation
            // This example uses odan/cache
        )
    )
);

You can provide your own key and time to live.

new CacheConfig(
    new OpCache(sys_get_temp_dir() . '/cache')
    CacheConfig::CACHE_UNTIL_MIDNIGHT, // time to live in seconds
    CacheConfig::DEFAULT_CACHE_KEY // key used for caching
);

Mocking webservice response for Unit Testing your own project

CurrCurr allows you to inject your own implementation of the EcbClientInterface. But you can also use the provided EcbClientMock, which allows you to simulate 3 different responses.

$cc1 = new CurrCurr(new EcbClientMock(EcbClientMock::VALID_RESPONSE));
$cc2 = new CurrCurr(new EcbClientMock(EcbClientMock::USD_MISSING_RESPONSE));
$cc3 = new CurrCurr(new EcbClientMock(EcbClientMock::DATE_MISSING_RESPONSE));

统计信息

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

GitHub 信息

  • Stars: 8
  • Watchers: 2
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: Unlicense
  • 更新时间: 2016-09-18

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固