Giter Site home page Giter Site logo

money-1's Introduction

Money

Build Status

PHP 5.4+ Money library using BCMath, inspired by the works of Mathias Verraes and Commerce Guys

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')));

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.

money-1's People

Contributors

acasademont avatar

Watchers

James Cloos avatar Wang Chao avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.