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

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

ph-il/money-bundle
==================

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

v6.4.1(4mo ago)11.6kMITPHPPHP &gt;=8.1

Since Jul 3Pushed 4mo agoCompare

[ Source](https://github.com/ph-il/PhilMoneyBundle)[ Packagist](https://packagist.org/packages/ph-il/money-bundle)[ Docs](https://github.com/ph-il/PhilMoneyBundle)[ RSS](/packages/ph-il-money-bundle/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (9)Dependencies (17)Versions (42)Used By (0)

PhilMoneyBundle
===============

[](#philmoneybundle)

[![Build Status](https://camo.githubusercontent.com/7bc79ed729b22c3ef48e3c1dabe36457db0c020bf8c1cbc3d83b684a4812ebfd/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f70682d696c2f5068696c4d6f6e657942756e646c652f6d61737465722e7376673f7374796c653d666c61742d737175617265)](https://travis-ci.org/TheBigBrainsCompany/PhilMoneyBundle)[![PHP](https://camo.githubusercontent.com/2d18ce514c7016022dad012ac9e39a8b6f47cc411b2daff6627cbf208f8cea63/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f7068702d253345253344253230382e312d3838393242462e7376673f7374796c653d666c61742d737175617265)](https://php.net)[![Symfony](https://camo.githubusercontent.com/8b3647fa640601d6e7f9b8e824e17514111f9a85ad8d74abb212db9ab2905a35/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f73796d666f6e792d253545362e332d677265656e2e7376673f7374796c653d666c61742d737175617265)](https://symfony.com)[![Downloads](https://camo.githubusercontent.com/1203a9b9a3aba38401cd532d6f1b52b70c728c62b486a201f9e4ec47be072a5d/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f70682d696c2f6d6f6e65792d62756e646c652e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/phil/money-bundle/stats)[![Latest Stable Version](https://camo.githubusercontent.com/7c4cd98b83122fd63d31bcf556335425c2c7ac37b4d2db5f1b4076646c692d4f/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f70682d696c2f6d6f6e65792d62756e646c652e737667)](https://github.com/TheBigBrainsCompany/PhilMoneyBundle/releases/latest)[![license](https://camo.githubusercontent.com/5a28ae27c73e1a67347eade5465a9e5cc7269731fcea58a7431320def7521030/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f70682d696c2f5068696c4d6f6e657942756e646c652e7376673f7374796c653d666c61742d737175617265)](https://github.com/ph-il/PhilMoneyBundle/blob/main/LICENSE)

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

This bundle is a fork of [TbbcMoneyBundle](https://github.com/TheBigBrainsCompany/TbbcMoneyBundle)

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 6.3+ and 7

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

[](#quick-start)

```
use Money\Money;
use Phil\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('phil_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 phil/money-bundle`

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

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

For create a file like config/packages/phil\_money.yml and add it there.

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

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

```
twig:
    form_themes:
        - '@PhilMoney/Form/fields.html.twig'
```

You should also register custom Doctrine Money type:

```
doctrine:
    dbal:
        types:
            money: Phil\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 Phil\\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.

```
