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

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

tbbc/money-bundle
=================

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

7.1(3mo ago)1961.9M↓21.7%76MITPHPPHP ^8.2CI passing

Since Jul 3Pushed 1mo ago11 watchersCompare

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

READMEChangelog (10)Dependencies (25)Versions (60)Used By (0)

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

[](#tbbcmoneybundle)

[![Build Status](https://github.com/TheBigBrainsCompany/TbbcMoneyBundle/actions/workflows/code_checks.yaml/badge.svg)](https://github.com/TheBigBrainsCompany/TbbcMoneyBundle/actions/workflows/code_checks.yaml)[![PHP](https://camo.githubusercontent.com/8c32682ae90a5572e9197155d70259dafa8239f353150d19f9214037ad67f32d/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f7068702d253345253344253230382e322d3838393242462e7376673f7374796c653d666c61742d737175617265)](https://php.net)[![Symfony](https://camo.githubusercontent.com/2adb0eb3959b50cad7e1204dd1a8d9ced6025a738a8b3c5a40909788372fa2f2/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f73796d666f6e792d2535453625374325354537253743253545382d677265656e2e7376673f7374796c653d666c61742d737175617265)](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](https://github.com/moneyphp/money) into a Symfony project.

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

- This bundle is tested and is stable with Symfony 6.4, 7.4 and 8.0

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

[](#quick-start)

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

public function convertAction(PairManagerInterface $pairManager): Response
{
    // the money library
    $fiveEur = Money::EUR(500);
    $tenEur = $fiveEur->add($fiveEur);
    [$part1, $part2, $part3] = $tenEur->allocate([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
    $usd = $pairManager->convert($tenEur, 'USD');

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

Features
--------

[](#features)

- Integrates money library from Mathias Verraes
- 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](#RatioStorage)
- [Contributing](#contributing)
- [Requirements](#requirements)
- [Authors](#authors)
- [Status](#status)

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

[](#installation)

Use [Composer](http://getcomposer.org/) and install with

```
composer require tbbc/money-bundle
```

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],
    ];
```

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
```

Development
-----------

[](#development)

TbbcMoneybundle comes with docker compose to ease development, to build dev docker containers, run:

```
docker compose up --build -d
```

And once it is built, enter the php 8.2, 8.3, 8.4 or 8.5 container:

```
docker exec -it tbbc-moneybundle-php82 bash
docker exec -it tbbc-moneybundle-php83 bash
docker exec -it tbbc-moneybundle-php84 bash
docker exec -it tbbc-moneybundle-php85 bash
```

All the tooling is added as composer scripts, start by installing deps:

```
composer install
```

Then you can use the code style (cs) commands to check/fix issues, rector to run the automatic refactors, or phpunit.

```
composer cs-check
composer cs-fix
composer rector-check
composer rector-fix
composer phpunit
composer phpstan
```

To generate a new baseline for phpstan:

```
composer phpstan-generate-baseline
```

Usage
-----

[](#usage)

### Money Library integration

[](#money-library-integration)

```
use Money\Money;

$fiveEur = Money::EUR(500);
$tenEur = $fiveEur->add($fiveEur);
[$part1, $part2, $part3] = $tenEur->allocate([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' => [
            'label' => 'Amount',
        ],
        'currency_options' => [
            '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.

```
