2017 © Pedro Peláez
 

library money

Yet another PHP implementation of the Money value object using BCMath

image

ulabox/money

Yet another PHP implementation of the Money value object using BCMath

  • Thursday, February 8, 2018
  • by ulabox
  • Repository
  • 19 Watchers
  • 72 Stars
  • 43,667 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 9 Forks
  • 1 Open issues
  • 13 Versions
  • 18 % Grown

The README.md

Money

Build Status, (*1)

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., (*2)

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., (*3)

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., (*4)

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., (*5)

Installation

Install the library using composer. Just run the following command, (*6)

$ 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., (*7)

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., (*8)

Integration with Doctrine 2.5

Starting from version 2.5 Doctrine supports working with Value Objects in what they call [Embeddables][5]. 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., (*9)

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., (*10)

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, (*11)

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., (*12)

The Versions

08/02 2018

dev-feat/convert_to_and_from_string

dev-feat/convert_to_and_from_string https://github.com/ulabox/money

Yet another PHP implementation of the Money value object using BCMath

  Sources   Download

MIT

The Requires

  • php ^7.1

 

The Development Requires

by Albert Casademont

money value object bcmath

21/11 2017

dev-master

9999999-dev https://github.com/ulabox/money

Yet another PHP implementation of the Money value object using BCMath

  Sources   Download

MIT

The Requires

  • php ^7.1

 

The Development Requires

by Albert Casademont

money value object bcmath

21/11 2017

v2.1.1

2.1.1.0 https://github.com/ulabox/money

Yet another PHP implementation of the Money value object using BCMath

  Sources   Download

MIT

The Requires

  • php ^7.1

 

The Development Requires

by Albert Casademont

money value object bcmath

21/11 2017

v2.1.0

2.1.0.0 https://github.com/ulabox/money

Yet another PHP implementation of the Money value object using BCMath

  Sources   Download

MIT

The Requires

  • php ^7.1

 

The Development Requires

by Albert Casademont

money value object bcmath

13/11 2017

v2.0.0

2.0.0.0 https://github.com/ulabox/money

Yet another PHP implementation of the Money value object using BCMath

  Sources   Download

MIT

The Requires

  • php ^7.1

 

The Development Requires

by Albert Casademont

money value object bcmath

02/10 2017

v1.1.5

1.1.5.0 https://github.com/ulabox/money

Yet another PHP implementation of the Money value object using BCMath

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

by Albert Casademont

money value object bcmath

02/10 2017

dev-1_x

dev-1_x https://github.com/ulabox/money

Yet another PHP implementation of the Money value object using BCMath

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

by Albert Casademont

money value object bcmath

18/05 2017

v1.1.4

1.1.4.0 https://github.com/ulabox/money

Yet another PHP implementation of the Money value object using BCMath

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

by Albert Casademont

money value object bcmath

08/07 2015

v1.1.3

1.1.3.0 https://github.com/ulabox/money

Yet another PHP implementation of the Money value object using BCMath

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

by Albert Casademont

money value object bcmath

06/07 2015

v1.1.2

1.1.2.0 https://github.com/ulabox/money

Yet another PHP implementation of the Money value object using BCMath

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

by Albert Casademont

money value object bcmath

03/03 2015

1.1.1

1.1.1.0 https://github.com/ulabox/money

Yet another PHP implementation of the Money value object using BCMath

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

by Albert Casademont

money value object bcmath

02/03 2015

v1.1.0

1.1.0.0 https://github.com/ulabox/money

Yet another PHP implementation of the Money value object using BCMath

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

by Albert Casademont

money value object bcmath

27/02 2015

v1.0.0

1.0.0.0 https://github.com/ulabox/money

Yet another PHP implementation of the Money value object using BCMath

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

by Albert Casademont

money value object bcmath