PHPackages                             matmar10/money-bundle - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Validation &amp; Sanitization](/categories/validation)
4. /
5. matmar10/money-bundle

ActiveSymfony-bundle[Validation &amp; Sanitization](/categories/validation)

matmar10/money-bundle
=====================

A series of money related utility classes wrapping integer math and money fractional allocation.

2.0.0(11y ago)1291ApachePHP

Since Oct 13Pushed 9y ago1 watchersCompare

[ Source](https://github.com/matmar10/money-bundle)[ Packagist](https://packagist.org/packages/matmar10/money-bundle)[ RSS](/packages/matmar10-money-bundle/feed)WikiDiscussions master Synced 2mo ago

READMEChangelogDependencies (7)Versions (4)Used By (0)

Money Bundle
============

[](#money-bundle)

**MAINTAINER WANTER - issue reports and pull requests will not be actively investigated. I am happy to advise, but I'm not longer actively using/maintaing this library. Please let me know if you're interested in taking over ownership of the repo.**

Overview
--------

[](#overview)

Symfony2 Bundle wrapping common Money and Currency related needs such as integer-based math, currency codes, and money conversion.

Installation
------------

[](#installation)

Add the package to your composer.json file:

```
{
    "repositories": [
        {
            "type": "vcs",
            "url": "https://github.com/matmar10/money-bundle
        }
    ],
    "require": {
        "matmar10/money-bundle": "dev-master"
    }
}

```

Creating Objects
----------------

[](#creating-objects)

Currencies are identified by a currency code and have a calculation and display precision:

```
$eur = new Currency('EUR', 2, 2);

$euros = new Money($eur);
$euros->setAmountFloat(1.99);
```

Basic Math
----------

[](#basic-math)

The Money object wraps all basic math functions using underlying integer math to avoid the (problems with floating point math)\[\].

All amounts are stored as integer values internally using the calculation precision as the scale.

```
// must use a valid iso4217 currency code
// with the exception of BTC for Bitcoin, as specified in the override configuration
$usd = new Currency('USD', 5, 2);

$usdAmount1 = new Money($usd);
$usdAmount1->setAmountFloat(1.2345);

$usdAmount2 = new Money($usd);
$usdAmount2->setAmountFloat(1.2345);

$usdAmount1->isEqual($usdAmount2); // true

$resultAmount1 = $usdAmount1->add($usdAmount2);
echo $resultAmount1->getAmountDisplay(); // 2.47

$resultAmount2 = $usdAmount1->subtract($usdAmount2);
echo $resultAmount2->getAmountFloat(); // 0

$resultAmount3 = $usdAmount1->multiply(3);
echo $resultAmount3->getAmountFloat(); // 3.7035
echo $resultAmount3->getAmountDisplay(); // 3.70

$resultAmount4 = $usdAmount1->divide(2);
echo $resultAmount3->getAmountFloat(); // 0.61725
echo $resultAmount3->getAmountDisplay(); // 0.62
```

Dealing with Fractional Cents
-----------------------------

[](#dealing-with-fractional-cents)

How do you divide $10 evenly amongst three people? In reality, you can't divide fractional cents.

Really, you want to end up with three *equal*-ish shares:

- $3.34
- $3.33
- $3.33

```
$eurAmount = new Money(new Currency('EUR', 2, 2));
$eurAmount->setAmountFloat(10);

// split the 10 euros into three equal parts using euro cents as the smallest unit
$shares = $eurAmount->allocate(array(1, 1, 1), 2);

$shares[0]->getAmountFloat(); // 3.34
$shares[1]->getAmountFloat(); // 3.33
$shares[2]->getAmountFloat(); // 3.33
```

Converting Between Currencies
-----------------------------

[](#converting-between-currencies)

Use the `CurrencyPair` object to convert between disparate currencies using an exchange rate:

Note that the rate works bi-directionally:

```
$gbp = new Currency('GBP', 2, 2);
$usd = new Currency('USD', 2, 2);

$gbpAmount = new Money($gbp);
$gbpAmount->setAmountFloat(10);

// 1 GBP = 1.5 USD
$gbpToUsd = new CurrencyPair($gbp, $usd, 1.5);

$usdAmount = $gbpToUsd->convert($gbpAmount);
echo $usdAmount->getDisplay(); // 15.00

$gbpAmount2 = $gbpToUsd->convert($usdAmount);
echo $gbpAmount2->getDisplay(); // 10.00
```

Currency Manager Service
------------------------

[](#currency-manager-service)

Instead of building up currencies and money objects manually all the time, consider using the `lmh_money.currency_manager` service that is registered into Symfony's dependency injection container.

The manager supports providing an ISO country or currency code:

```
// inside a Symfony controller, for example

$manager = $this->getContainer()->get('lmh_money.currency_manager');

$amount = $manager->getMoney('ES');
echo $amount->getCurrency(); // EUR
$amount->setAmountFloat(100);

// 1 USD = 0.75 EUR
$pair = $manager->getPair('US', 'ES', 0.75);

$converted = $pair->convert($amount);
echo $converted->getAmountDisplay(); // 75.00
```

Adding New Currencies
---------------------

[](#adding-new-currencies)

You can add new currencies that are not supported by default. This is useful, for example to add alternative currencies such as Litecoin and Ripple

```
// inside a config file, such as app/config/config.yml

matmar10_money:
    currencies:
        LTC: { displayPrecision: 5, calculationPrecision: 8, symbol: '&#0321;' }
        XRP: { displayPrecision: 8, calculationPrecision: 8 }
```

Currency Alias
--------------

[](#currency-alias)

Alias handling for currencies, to set the currency settings "Euro" equel to "EUR", extend the config like this:

```
matmar10_money:
    currencies:
        Euro:
            alias: EUR
```

Currency Validator
------------------

[](#currency-validator)

The Bundle also includes a Symfony validator for use in validating an entity's attribute is a valid currency code using (Symfony's Validator component)\[\].

Example use of the annotation:

```
