2017 © Pedro Peláez
 

library money

Implementation of money class

image

nilz/money

Implementation of money class

  • Tuesday, December 8, 2015
  • by Nilz11
  • Repository
  • 3 Watchers
  • 1 Stars
  • 8,213 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 4 Versions
  • 5 % Grown

The README.md

, (*1)

A real world implementation of an extendable Money object using integers of smallest currency unit as calculation., (*2)

This library was created because other existing solutions had one or more of the following drawbacks: - missing extendability of Money class - dependency on fixed currency class - missing methods like format and convertTo currency - errors in implementation that caused to have rounding errors and missing cents in e.g. summing up prices - library not being maintained anymore, (*3)

Free to use. Use it or leave it., (*4)

Examples

Create a money object:, (*5)

use Nilz\Money\Money;
use Nilz\Money\Currency\ISO4217Currency;

$money = new Money(420, new ISO4217Currency('EUR'));

$money = Money::fromDefaultUnitAmount('4.20', 'EUR');

Get amount of money object:, (*6)


//420 echo $money->getAmount(); //4.20 echo $money->getDefaultUnitAmount(); //4,20 € echo $money->getFormattedAmount('de_DE');

Sum up two money objects:, (*7)


$a = Money::fromDefaultUnitAmount('4.20', 'EUR'); $b = Money::fromDefaultUnitAmount('2.10', 'EUR'); $c = $a->add($b); //6.30 echo $c->getAmount(); //4.20 echo $a->getAmount(); //2.10 echo $b->getAmount();

Other arithmetic examples:, (*8)


$a = Money::fromDefaultUnitAmount('4.20', 'EUR'); $b = Money::fromDefaultUnitAmount('2.10', 'EUR'); $a->subtract($b); $a->multiply(1.2); $a->divide(1.2);

If you need other methods to perform calculations, you can easily extend the money object or put a pull request to extend it., (*9)


Use custom currency:, (*10)

use Nilz\Money\Currency\Currency;

//alpha3 code, factor for smallest unit representation, decimal digits to round two
$money = new Money(420, new Currency('EUR', 100, 2));

You can also implement a custom currency object by implementing the CurrencyInterface., (*11)

The Versions

08/12 2015

dev-master

9999999-dev

Implementation of money class

  Sources   Download

MIT

The Requires

 

The Development Requires

by Nils Weidl
by Daniel Reichenbach

08/12 2015

1.1.0

1.1.0.0

Implementation of money class

  Sources   Download

MIT

The Requires

 

The Development Requires

by Nils Weidl
by Daniel Reichenbach

06/12 2015

1.0.1

1.0.1.0

Implementation of money class

  Sources   Download

MIT

The Requires

 

The Development Requires

by Nils Weidl
by Daniel Reichenbach

05/12 2015

1.0.0

1.0.0.0

Implementation of money class

  Sources   Download

MIT

The Requires

 

The Development Requires

by Nils Weidl