moltin/currency
Composer 安装命令:
composer require moltin/currency
包简介
Currency conversion and formatting
README 文档
README
The Moltin currency composer package makes it easy to implement multi-currency pricing into your application and store the exchange data using one of the numerous data stores provided. You can also inject your own data store if you would like your data to be stored elsewhere.
Installation
Download and install composer from http://www.getcomposer.org/download
Add the following to your project composer.json file
{
"require": {
"moltin/currency": "~1.0.0"
}
}
When you're done just run php composer.phar install and the package is ready to be used.
Usage
Below is a basic usage guide for this package.
Instantiating currency
Before you begin, you will need to know which storage, currencies and exchange method you are going to use. The exchange method defines where your exchange rates are retrieved from. The currencies method is used to retrieve your supported currencies for the current application.
In this example we're going to use the currencies file, exchange file and session for storage.
use Moltin\Currency\Currency as Currency; use Moltin\Currency\Format\Runtime as RuntimeFormat; use Moltin\Currency\Exchange\OpenExchangeRates as OpenExchange; $currency = new Currency(new OpenExchange($app_id), new RuntimeFormat);
Setting the value
Now that you have Currency instantiated, you will now need to tell it what value you would like to convert. You can do this using the following method.
$currency->convert(9.33)->from('GBP');
Getting the value
The most basic action you can perform is retrieve the original value back from the method.
// Returns 9.33 $value = $currency->value();
Formatting as a currency
By default the currency is set to GBP so calling currency will format the value to a string with £ and correct decimal and thousand seperators.
// Returns £9.33 $value = $currency->format();
Rounding to common values
There are a number of common pricing formats built in to make "nice" prices easy to implement. These formats changes the default value and return the object to allow for chaining.
// Sets value to 10.00 $currency->zeros(); // Sets value to 9.99 $currency->nines(); // Sets value to 9.50 $currency->fifty(); // Returns £9.50 $value = $currency->fifty()->format();
Currency Exchange
The package makes it as easy as possible to quickly switch between currencies. Before each exchange the value is reset to default to ensure the correct price is assigned.
// Returns ~$14.47 $value = $currency->convert(9.33)->from('GBP')->to('USD')->format(); // Returns ~14.50 $value = $currency->convert(9.33)->from('GBP')->to('USD')->fifty()->value();
Resetting the value
After using exchange or any of the rounding functions to retrieve the default value you must call reset.
// Returns 10.00 $value = $currency->zeros()->value(); // Returns 9.33 $value = $currency->reset()->value();
moltin/currency 适用场景与选型建议
moltin/currency 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 151.41k 次下载、GitHub Stars 达 106, 最近一次更新时间为 2013 年 05 月 01 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 moltin/currency 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 moltin/currency 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 151.41k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 110
- 点击次数: 28
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: proprietary
- 更新时间: 2013-05-01