ulabox/money
Composer 安装命令:
composer require ulabox/money
包简介
Yet another PHP implementation of the Money value object using BCMath
关键字:
README 文档
README
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 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 ulabox/money 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A set of useful PHP classes.
Beyonic PHP Library
Runn Me! Value Objects Library
Laravel SDK mapping data in object
MaxAl Subscriptions is a flexible plans and subscription management system for Laravel, with the required tools to run your SAAS like services efficiently. It's simple architecture, accompanied by powerful underlying to afford solid platform for your business.
Rinvex Subscriptions is a flexible plans and subscription management system for Laravel, with the required tools to run your SAAS like services efficiently. It's simple architecture, accompanied by powerful underlying to afford solid platform for your business.
统计信息
- 总下载量: 241.87k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 79
- 点击次数: 35
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2015-02-27