amoori/laravel-exchange-rates
Composer 安装命令:
composer require amoori/laravel-exchange-rates
包简介
A wrapper package for interacting with the exchangeratesapi.io API.
README 文档
README
Table of Contents
Overview
A simple Laravel package used for interacting with the exchangeratesapi.io API. 'Laravel Exchange Rates' allow you to get the latest or historical exchange rates and convert monetary values between different currencies.
Installation
You can install the package via Composer:
composer require amoori/laravel-exchange-rates
The package has been developed and tested to work with the following minimum requirements:
- PHP 7.2
- Laravel 5.8
Usage
Methods
Available Currencies
$exchangeRates = new ExchangeRate(); $exchangeRates->currencies();
Exchange Rate
<?php $exchangeRates = new ExchangeRate(); $exchangeRates->exchangeRate('GBP', 'EUR');
Note: If a Carbon date is passed as the third parameter, the exchange rate for that day will be returned (if valid). If no date is passed, today's exchange rate will be used.
Exchange Rates Between Date Range
$exchangeRates = new ExchangeRate(); $exchangeRates->exchangeRateBetweenDateRange('GBP', 'EUR', Carbon::now()->subWeek(), Carbon::now());
Convert Currencies
When passing in the monetary value (first parameter) that is to be converted, it's important that you pass it in the lowest denomination of that currency. For example, £1 GBP would be passed in as 100 (as £1 = 100 pence).
$exchangeRates = new ExchangeRate(); $exchangeRates->convert(100, 'GBP', 'EUR', Carbon::now());
Note: If a Carbon date is passed as the third parameter, the exchange rate for that day will be returned (if valid). If no date is passed, today's exchange rate will be used.
Convert Currencies Between Date Range
When passing in the monetary value (first parameter) that is to be converted, it's important that you pass it in the lowest denomination of that currency. For example, £1 GBP would be passed in as 100 (as £1 = 100 pence).
$exchangeRates = new ExchangeRate(); $exchangeRates->convert(100, 'GBP', 'EUR', Carbon::now()->subWeek(), Carbon::now());
Facade
If you prefer to use facades in Laravel, you can choose to use the provided ExchangeRate facade instead of instantiating the amoori\LaravelExchangeRates\Classes\ExchangeRate
class manually.
The example below shows an example of how you could use the facade to get the available currencies:
<?php namespace App\Http\Controllers; use ExchangeRate; class TestController extends Controller { public function index() { return ExchangeRate::currencies(); } }
Examples
This example shows how to convert 100 pence (£1) from Great British Pounds to Euros. The current exchange rate will be used (unless a cached rate for this date already exists).
<?php namespace App\Http\Controllers; use amoori\LaravelExchangeRates\Classes\ExchangeRate; class TestController extends Controller { public function index() { $exchangeRates = new ExchangeRate(); return $exchangeRates->convert(100, 'GBP', 'EUR', Carbon::now()); } }
Caching
By default, the responses all of the requests to the exchangeratesapi.io API are cached. This allows for significant performance improvements and reduced bandwidth from your server.
However, if for any reason you require a fresh result from the API and not a cached result, the ->shouldBustCache()
method can be used. The example below shows how to ignore the cached value (if one exists) and make a new API request.
<?php namespace App\Http\Controllers; use amoori\LaravelExchangeRates\Classes\ExchangeRate; class TestController extends Controller { public function index() { $exchangeRates = new ExchangeRate(); return $exchangeRates->shouldBustCache()->convert(100, 'GBP', 'EUR', Carbon::now()); } }
Note: The caching works by storing exchange rates after fetching them from the API. As an example, if you were to fetch the exchange rates for 'GBP' to 'EUR' for 20-11-2019 - 27-11-2019, the rates between these dates will be cached as a single cache item. This cache item will only be retrieved if you attempt to fetch the same exchange rates on with the exact same currencies and date range.
Therefore, if you were to try and get 'GBP' to 'EUR' for 20-11-2019 - 26-11-2019, a new API request would be made because the date range is different.
Supported Currencies
Laravel Exchange Rates supports working with the following currencies (sorted in A-Z order):
| Code | Currency Name |
|---|---|
| AUD | Australian dollar |
| BGN | Bulgarian lev |
| BRL | Brazilian real |
| CAD | Canadian |
| CHF | Swiss franc |
| CNY | Chinese yuan renminbi |
| CZK | Czech koruna |
| DKK | Danish krone |
| EUR | Euro |
| GBP | Pound sterling |
| HKD | Hong Kong dollar |
| HRK | Croatian kuna |
| HUF | Hungarian forint |
| IDR | Indonesian rupiah |
| ILS | Israeli shekel |
| INR | Indian rupee |
| ISK | Icelandic krone |
| JPY | Japanese yen |
| KRW | South Korean won |
| MXN | Mexican peso |
| MYR | Malaysian ringgit |
| NOK | Norwegian krone |
| NZD | New Zealand dollar |
| PHP | Philippine peso |
| PLN | Polish zloty |
| RON | Romanian leu |
| RUB | Russian rouble |
| SEK | Swedish krona |
| SGD | Singapore dollar |
| THB | Thai baht |
| TRY | Turkish lira |
| USD | US dollar |
| ZAR | South African rand |
Note: Please note that the currencies are available because they are exposed in the exchangeratesapi.io API.
Testing
vendor/bin/phpunit
Security
If you find any security related issues, please contact me directly at mail@amoori.co.uk to report it.
Contribution
If you wish to make any changes or improvements to the package, feel free to make a pull request.
Note: A contribution guide will be added soon.
Credits
- Ash Allen
- Jess Pickup (Logo)
- All Contributors
Changelog
Check the CHANGELOG to get more information about the latest changes.
Upgrading
Check the UPGRADE guide to get more information on how to update this library to newer versions.
License
The MIT License (MIT). Please see License File for more information.
amoori/laravel-exchange-rates 适用场景与选型建议
amoori/laravel-exchange-rates 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 168 次下载、GitHub Stars 达 1, 最近一次更新时间为 2022 年 08 月 03 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「exchange-rate」 「exchangeratesapi.io」 「laravel-exchange-rates」 「amoori」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 amoori/laravel-exchange-rates 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 amoori/laravel-exchange-rates 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 amoori/laravel-exchange-rates 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A wrapper package for interacting with the exchangeratesapi.io API.
grep foreign exchange reference rates from the ecb
Allows you to receive current exchange rates from various sources and convert them.
Foreign exchange rates API Against The Naira
A wrapper package for interacting with the exchangeratesapi.io API.
Foreign exchange rates API Against The Naira
统计信息
- 总下载量: 168
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 20
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2022-08-03