This bundle is used to integrate the Money library from mathiasverraes into a symfony2 project.
This library is based on Fowler's Money pattern
This bundle is stable and tested.
use Money\Money;
// a 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", "tbbc_money");
- Integrates money library from mathiasverraes
- Twig filters and formater in order to display amounts
- A storage system for currency ratios
- A ratioProvider system for fetching ratio from externals api
- Symfony2 form integration
- Console commands for different operations
- A configuration parser for specifying website used currencies
Using Composer, just $ composer require tbbc/money-bundle
package or:
{
"require": {
"tbbc/money-bundle": "dev-master"
}
}
Then add the bundle in AppKernel :
public function registerBundles()
{
$bundles = array(
// ...
new \Tbbc\MoneyBundle\TbbcMoneyBundle(),
);
}
in your config.php, add the currencies you want to use and the reference currency.
tbbc_money:
currencies: ["USD", "EUR"]
reference_currency: "EUR"
In your config.yml, add the form fields presentations
twig:
form:
resources:
- 'TbbcMoneyBundle:Form:fields.html.twig'
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);
You have 3 new form types :
- tbbc_currency : asks for a currency among currencies defined in config.yml
- tbbc_money : asks for an amount and a currency
- tbbc_simple_money : asks for an amount and sets the currency to the reference currency set in config.yml
You can see more details on how to manage forms and doctrine binding in this page : Form And Doctrine Integration
Convert an amount into another currency
$pairManager = $this->get("tbbc_money.pair_manager");
$usd = $pairManager->convert($amount, 'USD');
Save a conversion value in a DB
use Money\Money;
$pairManager = $this->get("tbbc_money.pair_manager");
$pairManager->saveRatio('USD', 1.25); // save in ratio file in CSV
$eur = Money::EUR(100);
$usd = $pairManager->convert($amount, 'USD');
$this->assertEquals(Money::USD(125), $usd);
{{ $amount | money_format }}
{{ $amount | money_as_float }}
{{ $amount | money_get_currency | currency_symbol }}
{{ $amount | money_get_currency | currency_name }}
{{ $amount | money_convert("USD") | money_format }}
{{ $amount | money_format_currency }}
# save a ratio in the storage
./app/console tbbc:money:ratio-save USD 1.25
# display ratio list
./app/console tbbc:money:ratio-list
# fetch all the ratio for all defined currencies from an external API
./app/console tbbc:money:ratio-fetch
Two storages for storing ratios are available : CSV File, or Doctrine By default, TbbcMoneyBundle is configured with CSV File.
If you want to switch to a Doctrine storage, edit your config.yml
tbbc_money:
storage: doctrine
Update your database schema :
./app/console doctrine:schema:update --force
With the Doctrine storage, currency ratio will use the default entity manager and will store data inside the tbbc_money_doctrine_storage_ratios
- Take a look at the list of issues.
- Fork
- Write a test (for either new feature or bug)
- Make a PR
- PHP 5.3+
- Symfony 2.1 +
Philippe Le Van - kitpages.fr - twitter : @plv
Thomas Tourlourat - Wozbe - twitter: @armetiz
Stable
what is functional :
- integration of the money library
- configuration parser
- pairManager
- travis-ci integration
- form integration
- twig presentation for forms
- twig filters
- commands for ratio creation and ratio display
In progress :
- small administration interface for ratio management
1.4.0 : 26/07/2013
- fix : datatransformer returned a null values for amounts above 1000 with a space grouping separator
- new : tbbc_simple_money field type without currency (reference currency used by default)
1.3.0 : 16/07/2013
- new : doctrine storage (thanks to @armetiz)
1.2.0 : 12/07/2013
- new : ratio provider mecanism for fetch currency ratios from external api
- Warning : small BC Break : command save-ratio is renamed ratio-save
- doc enhancement
1.1.0 : 2013/07/04
- refactor : storage extracted in another service (CsvStorage)
- new : command creation : tbbc:money:ratio-save, tbbc:money:ratio-list
1.0.0 : 2013/07/03
- first realease