adsmurai/currency 问题修复 & 功能扩展

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

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

adsmurai/currency

Composer 安装命令:

composer require adsmurai/currency

包简介

A small library to handle currencies and money values

README 文档

README

Build Status Scrutinizer Code Quality Code Coverage

Introduction

The Adsmurai Currency library has been developed to solve some currency and architecture related problems:

  • Currency data centralization
  • Values encapsulation
  • Representation
  • Extra precision to represent very small currency amounts.
  • Conversions

Setup

Install it through composer, that's it:

composer require adsmurai/currency

Code examples

<?php

use Adsmurai\Currency\Contracts\Currency;
use Adsmurai\Currency\CurrencyFactory;
use Adsmurai\Currency\MoneyFactoriesLocator;
use Adsmurai\Currency\MoneyFactory;

// This factory will create Currency objects given the currency ISO code.
// By default, it will load the currency data from a library's internal data
// source, but we can use alternative data sources.
$currencyFactory = CurrencyFactory::fromDataPath();
$currencyFactory = CurrencyFactory::fromDataArray([
    'EUR' => [
        'numFractionalDigits' => 2,
        'symbol' => '',
        'symbolPlacement' => Currency::AFTER_PLACEMENT,
    ],
    'USD' => [
        'numFractionalDigits' => 2,
        'symbol' => '$',
        'symbolPlacement' => Currency::BEFORE_PLACEMENT,
    ]
]);

// This factory will create Money objects with the same currency type.
// The rationale behind this class is that almost always we'll work with the
// same currency type.
$moneyFactory = new MoneyFactory(
    $currencyFactory->buildFromISOCode('EUR')
);

// In fact, to avoid introducing hard dependencies on the factory implementation
// through the instantiation (via the `new` operator), we recommend to obtain
// the `MoneyFactory` instances through the `MoneyFactoriesLocator`.
//
// This will have some advantages:
//   * We can inject instances of the `MoneyFactoriesLocator` contract in our
//     domain logic without having to rely on specific implementations.
//   * We can avoid `MoneyFactory` instances proliferation, since this
//     factories locator keeps one single instances per currency ISO code.
$moneyFactoriesLocator = new MoneyFactoriesLocator($currencyFactory);
$moneyFactory = $moneyFactoriesLocator->getMoneyFactory('EUR');

// We have many ways to construct Money objects, depending on the data we
// have at the time.
$money = $moneyFactory->buildFromString('10.57');
$money = $moneyFactory->buildFromString('10.57€'); // Look! it will validate the symbol too :)
$money = $moneyFactory->buildFromString('10.57 €');
$money = $moneyFactory->buildFromString('10.57EUR'); // Look! it will validate the ISO code too :)
$money = $moneyFactory->buildFromString('10.57 EUR');
$money = $moneyFactory->buildFromFloat(10.57);
$money = $moneyFactory->buildFromFractionalUnits(1057);

// If we want to format a currency value, we can use a specific method, that
// will take into account the number of digits, symbol, symbol placement...
echo $money->format();

Troubleshooting

We haven't faced any interesting problem related with this library, if you are struggling to make it work, open an issue on the issue tracker (and we'll update this section).

统计信息

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

GitHub 信息

  • Stars: 44
  • Watchers: 2
  • Forks: 3
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2017-07-17

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固