PHPackages                             enesdayanc/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. enesdayanc/money-bundle

ActiveSymfony-bundle

enesdayanc/money-bundle
=======================

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

v4.0.1(4y ago)011MITPHPPHP &gt;=7.1

Since Jul 3Pushed 4y agoCompare

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

READMEChangelogDependencies (17)Versions (33)Used By (0)

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

[](#tbbcmoneybundle)

[![Build Status](https://camo.githubusercontent.com/438fe08486034317e93734feba3f600d1ebb1c1fc627d8f0054532b37b180e35/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f546865426967427261696e73436f6d70616e792f546262634d6f6e657942756e646c652f6d61737465722e7376673f7374796c653d666c61742d737175617265)](https://travis-ci.org/TheBigBrainsCompany/TbbcMoneyBundle)[![PHP](https://camo.githubusercontent.com/dcd4b4aec2c1709157fa6a2c050f709d75cde9552a79cfff0b70a97fad7281ae/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f7068702d253345253344253230372e312d3838393242462e7376673f7374796c653d666c61742d737175617265)](https://php.net)[![Symfony](https://camo.githubusercontent.com/03a0e80eca2560fa1325e1fd8e8ce0316ef5c6afea608a8b8ae8cd85e9d0b910/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f73796d666f6e792d25354534253743253545352d677265656e2e7376673f7374796c653d666c61742d737175617265)](https://symfony.com)[![Downloads](https://camo.githubusercontent.com/32c36769bbe1f9a11ef83a41b05a8f2a9942f212f35749c51107a1cf8b791405/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f746262632f6d6f6e65792d62756e646c652e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/tbbc/money-bundle/stats)[![Latest Stable Version](https://camo.githubusercontent.com/5d439f7807933fd6ac5fd709513b43dba0c3fe96e0eba24c3ab0d57467fa11cb/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f746262632f6d6f6e65792d62756e646c652e737667)](https://github.com/TheBigBrainsCompany/TbbcMoneyBundle/releases/latest)[![license](https://camo.githubusercontent.com/36f3251c8610ce5881a4648e1efbbd7debde92ff6a4689bae4d7c04194922056/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f546865426967427261696e73436f6d70616e792f546262634d6f6e657942756e646c652e7376673f7374796c653d666c61742d737175617265)](https://github.com/TheBigBrainsCompany/TbbcMoneyBundle/blob/master/LICENSE)

[![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 3.4, 4.3, 4.4, 5.0

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`

If you use Symfony 3 then add the bundle to AppKernel:

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

For Symfony 4 and higher add the bundle to config/bundles.php (if it was not automatically added during the installation of the package):

```
    return [
        // ...
        Tbbc\MoneyBundle\TbbcMoneyBundle::class => ['all' => true],
    ];
```

For Symfony 3, in your config.yml, add the currencies you want to use and the reference currency. For Symfony 4 and higher create a file like config/packages/tbbc\_money.yml and add it there.

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

In your config.yml or config/packages/tbbc\_money.yml, add the form fields presentations

```
twig:
    form_themes:
        - '@TbbcMoney/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();
```

Manipulating the form

With `MoneyType` you can manipulate the form elements with

`amount_options` for the amount field, and `currency_options` for the currency field, fx if you want to change the label.

```
$form = $this->createFormBuilder()
    ->add('price', MoneyType::class, [
        'data' => Money::EUR(1000), //EUR 10
        'amount_options' => array(
            'label' => 'Amount',
        ),
        'currency_options' => array(
            'label' => 'Currency',
        ),
    ])
    ->getForm();
```

With `CurrencyType` only `currency_options` can be used, and with `SimpleMoneyType` only `amount_options` can be used.

### 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.

```
