承接 ulabox/money 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

ulabox/money

Composer 安装命令:

composer require ulabox/money

包简介

Yet another PHP implementation of the Money value object using BCMath

README 文档

README

Build Status

PHP 7.1+ Money library using BCMath, inspired by the works of Mathias Verraes and Commerce Guys. Use 1.x series for PHP 5.4+ compatibility.

Motivation

Representing monetary values using floats is bad, you can lose precision an get weird results. It's better to use a Money value object that uses integers or BCMath. This library uses the latter.

Why another Money library? There are already tons out there!

Yup, but most of them use integers as the internal money representation and that doesn't fit our needs! Using integers is fine in most cases and much more performant, but there are times when you need sub-unit calculations. For example, gas pricing here in Spain is calculated using tenths of an Euro cent or 1.001 €. VAT calculations can also make use of this extra precision an Arbitrary Precision library like BCMath provides. We are an ecommerce and we need that extra precision, especially when calculating discounts.

Why BCMath and not GMP?

While the GMP library has better performance, its implementation in PHP lacks decimal arithmetic, it can only deal with integers. There's a proposal to add the decimal implementation to the PHP extension but that's still a work in progress.

Installation

Install the library using composer. Just run the following command

$ composer require ulabox/money:@stable

Usage

<?php

use Money\Money;

$tenEUR = Money::EUR('10.00');
$twentyEUR = $tenEUR->add($tenEUR);
$fifteenEUR = $twentyEUR->subtract(Money::EUR('5.00'));

assert($tenEUR->isLessThan($twentyEUR));
assert($fifteenEUR->isGreaterThan($tenEUR));
assert($fifteenEUR->equals(Money::EUR('15.00')));

Dynamic scale

Since version 2.x, the scale of the underlying BCMath operations can be changed to suit your needs.

By default the scale of the Money class is 4. You can modify the scale when you create the Money object or when you multiply and divide. The scale is also changed when you round a number. For addition and subtraction operations, the scale used is the biggest between the 2 operands.

Integration with Doctrine 2.5

Starting from version 2.5 Doctrine supports working with Value Objects in what they call Embeddables. Bear in mind that this Money object has also a Currency VO inside, this is an embeddable inside an embeddable. Doctrine should work just fine with nested embeddables.

We suggest mapping the amount field to a decimal type. Decimal fields don't lose precision and are converted to a string type in PHP by Doctrine, exactly what we need when working with BCMath. The currency code field should be mapped to a string type. This is an example schema of a Product entity that has a price Money field.

Product:
  type: entity
  embedded:
    price:
      class: Money\Money

Money\Money:
  type: embeddable
  fields:
    amount: { type: decimal, precision: 10, scale: 2 }
  embedded:
    currency:
      class: Money\Currency

Money\Currency
  type: embeddable
  fields:
    code: { type: string, length: 3 }

Disclaimer

We aim to keep this library as simple as possible. That means we don't see the need of having plenty of calculation operations inside the Money class, keep that in mind if you plan to spend some valuable time in a PR! But of course, this can change as we see fit :p

We don't check the currency code against a list of valid ISO codes as we have some fake currencies in our system that use custom currency codes.

ulabox/money 适用场景与选型建议

ulabox/money 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 241.87k 次下载、GitHub Stars 达 77, 最近一次更新时间为 2015 年 02 月 27 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「money」 「Value Object」 「bcmath」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 ulabox/money 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 ulabox/money 我们能提供哪些服务?
定制开发 / 二次开发

基于 ulabox/money 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

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

GitHub 信息

  • Stars: 77
  • Watchers: 19
  • Forks: 10
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2015-02-27