PHPackages                             clippings/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. [Payment Processing](/categories/payments)
4. /
5. clippings/money-bundle

AbandonedArchivedSymfony-bundle[Payment Processing](/categories/payments)

clippings/money-bundle
======================

This is a Symfony bundle that integrates moneyphp/money library (Fowler pattern): https://github.com/moneyphp/money.

v3.1.3(9y ago)075.0kMITPHPPHP &gt;=5.3.9

Since Jul 3Pushed 6y agoCompare

[ Source](https://github.com/clippings/TbbcMoneyBundle)[ Packagist](https://packagist.org/packages/clippings/money-bundle)[ Docs](https://github.com/TheBigBrainsCompany/TbbcMoneyBundle)[ RSS](/packages/clippings-money-bundle/feed)WikiDiscussions master Synced 2d ago

READMEChangelog (4)Dependencies (18)Versions (32)Used By (0)

TbbcMoneyBundle
===============

[](#tbbcmoneybundle)

[![Build Status](https://camo.githubusercontent.com/438fe08486034317e93734feba3f600d1ebb1c1fc627d8f0054532b37b180e35/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f546865426967427261696e73436f6d70616e792f546262634d6f6e657942756e646c652f6d61737465722e7376673f7374796c653d666c61742d737175617265)](https://travis-ci.org/TheBigBrainsCompany/TbbcMoneyBundle)![PHP](https://camo.githubusercontent.com/0aa7445f06e06d72b9552b4ace117e3765a60fa64d6c973cac177557fd20368f/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f7068702d253345253344253230352e352d3838393242462e7376673f7374796c653d666c61742d737175617265)![Symfony](https://camo.githubusercontent.com/00b3752134145328308fce17c951233732b5d228b8949e52fc784d3260b666f8/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f73796d666f6e792d7e322e382537437e332e302d677265656e2e7376673f7374796c653d666c61742d737175617265)![Downloads](https://camo.githubusercontent.com/32c36769bbe1f9a11ef83a41b05a8f2a9942f212f35749c51107a1cf8b791405/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f746262632f6d6f6e65792d62756e646c652e7376673f7374796c653d666c61742d737175617265)![license](https://camo.githubusercontent.com/36f3251c8610ce5881a4648e1efbbd7debde92ff6a4689bae4d7c04194922056/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f546865426967427261696e73436f6d70616e792f546262634d6f6e657942756e646c652e7376673f7374796c653d666c61742d737175617265)

[![SensioLabsInsight](https://camo.githubusercontent.com/4c4a2d33896e2ac1522ba409a8f5c3dde419152ed2b3a1294261081d4774ffc8/68747470733a2f2f696e73696768742e73656e73696f6c6162732e636f6d2f70726f6a656374732f63623639653832302d313335622d343930362d393366642d3739323162613436613665362f6269672e706e67)](https://insight.sensiolabs.com/projects/cb69e820-135b-4906-93fd-7921ba46a6e6)

This bundle is used to integrate the [Money library from mathiasverraes](https://github.com/mathiasverraes/money) into a Symfony project.

This library is based on Fowler's [Money pattern](http://blog.verraes.net/2011/04/fowler-money-pattern-in-php/)

- This bundle is tested and is stable with Symfony 2.8 and Symfony 3.1

Quick Start
-----------

[](#quick-start)

```
use Money\Money;
use Tbbc\MoneyBundle\Form\Type\MoneyType;

// the money library
$fiveEur = Money::EUR(500);
$tenEur = $fiveEur->add($fiveEur);
list($part1, $part2, $part3) = $tenEur->allocate(array(1, 1, 1));
assert($part1->equals(Money::EUR(334)));
assert($part2->equals(Money::EUR(333)));
assert($part3->equals(Money::EUR(333)));

// a service that stores conversion ratios
$pairManager = $this->get('tbbc_money.pair_manager');
$usd = $pairManager->convert($tenEur, 'USD');

// a form integration
$formBuilder->add('price', MoneyType::class);
```

Features
--------

[](#features)

- Integrates money library from mathiasverraes
- Twig filters and PHP helpers for helping with money and currencies in templates
- A storage system for currency ratios
- A ratioProvider system for fetching ratio from externals api
- Symfony form integration
- Console commands for different operations
- A configuration parser for specifying website used currencies
- Access to the history of currency ratio fetched
- Money formatter i18n

Table of contents
-----------------

[](#table-of-contents)

- [Installation](#installation)
- [Usage](#usage)
- [Storage](#storage)
- [Contributing](#contributing)
- [Requirements](#requirements)
- [Authors](#authors)
- [Status](#status)

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

[](#installation)

Use [Composer](http://getcomposer.org/) and install with `$ composer require tbbc/money-bundle`

Then add the bundle in AppKernel :

```
    public function registerBundles()
    {
        $bundles = array(
            // ...
            new Tbbc\MoneyBundle\TbbcMoneyBundle(),
        );
    }
```

In your config.yml, add the currencies you want to use and the reference currency.

```
tbbc_money:
    currencies: ["USD", "EUR"]
    reference_currency: "EUR"
    decimals: 2
```

In your config.yml, add the form fields presentations

```
twig:
    form:
        resources:
            - 'TbbcMoneyBundle:Form:fields.html.twig'
```

You should also register custom Doctrine Money type:

```
doctrine:
    dbal:
        types:
            money: Tbbc\MoneyBundle\Type\MoneyType
```

Usage
-----

[](#usage)

### Money Library integration

[](#money-library-integration)

```
use Money\Money;

$fiveEur = Money::EUR(500);
$tenEur = $fiveEur->add($fiveEur);
list($part1, $part2, $part3) = $tenEur->allocate(array(1, 1, 1));
assert($part1->equals(Money::EUR(334)));
assert($part2->equals(Money::EUR(333)));
assert($part3->equals(Money::EUR(333)));

$pair = new CurrencyPair(new Currency('EUR'), new Currency('USD'), 1.2500);
$usd = $pair->convert($tenEur);
$this->assertEquals(Money::USD(1250), $usd);
```

### Form integration

[](#form-integration)

You have 3 new form types (under Tbbc\\MoneyBundle\\Form\\Type namespace):

- CurrencyType : asks for a currency among currencies defined in config.yml
- MoneyType : asks for an amount and a currency
- SimpleMoneyType : asks for an amount and sets the currency to the reference currency set in config.yml

Example :

```
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;

// I create my form
$form = $this->createFormBuilder()
    ->add('name', TextType::class)
    ->add('price', MoneyType::class, [
        'data' => Money::EUR(1000), //EUR 10
    ])
    ->add('save', SubmitType::class)
    ->getForm();
```

### Saving Money with Doctrine

[](#saving-money-with-doctrine)

#### Solution 1 : two fields in the database

[](#solution-1--two-fields-in-the-database)

Note that there are 2 columns in the DB table : $priceAmount and $priceCurrency and only one getter/setter : getPrice and setPrice.

The get/setPrice methods are dealing with these two columns transparently.

- Advantage : your DB is clean and you can do sql sum, group by, sort,... with the amount and the currency in two different columns in your db
- Disadvantage : it is ugly in the entity.

```
