Skip to content

Commit

Permalink
add configurable decimal count
Browse files Browse the repository at this point in the history
  • Loading branch information
philippe-levan committed Jan 31, 2014
1 parent 79aba8e commit 9884f18
Show file tree
Hide file tree
Showing 16 changed files with 78 additions and 25 deletions.
4 changes: 4 additions & 0 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ private function addCurrencySection(ArrayNodeDefinition $node)
->isRequired()
->cannotBeEmpty()
->end()
->scalarNode('decimals')
->defaultValue(2)
->cannotBeEmpty()
->end()
->scalarNode('storage')
->cannotBeEmpty()
->defaultValue('csv')
Expand Down
3 changes: 2 additions & 1 deletion DependencyInjection/TbbcMoneyExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ public function load(array $configs, ContainerBuilder $container)

$this->remapParameters($config, $container, array(
'currencies' => 'tbbc_money.currencies',
'reference_currency' => 'tbbc_money.reference_currency'
'reference_currency' => 'tbbc_money.reference_currency',
'decimals' => 'tbbc_money.decimals'
));

$container->setParameter('tbbc_money.pair.storage', $config['storage']);
Expand Down
8 changes: 6 additions & 2 deletions Form/DataTransformer/MoneyToArrayTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,13 @@ class MoneyToArrayTransformer implements DataTransformerInterface
/** @var MoneyToLocalizedStringTransformer */
protected $sfTransformer;

public function __construct()
/** @var int */
protected $decimals;

public function __construct($decimals = 2)
{
$this->sfTransformer = new MoneyToLocalizedStringTransformer(null, null, null, 100);
$this->decimals = (int)$decimals;
$this->sfTransformer = new MoneyToLocalizedStringTransformer(null, null, null, pow(10, $this->decimals));
}

/**
Expand Down
4 changes: 2 additions & 2 deletions Form/DataTransformer/SimpleMoneyToArrayTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ class SimpleMoneyToArrayTransformer
{
/** @var PairManagerInterface */
protected $pairManager;
public function __construct(PairManagerInterface $pairManager)
public function __construct(PairManagerInterface $pairManager, $decimals)
{
parent::__construct();
parent::__construct($decimals);
$this->pairManager = $pairManager;
}

Expand Down
9 changes: 7 additions & 2 deletions Form/Type/MoneyType.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,16 @@ class MoneyType
/** @var CurrencyType */
protected $currencyType;

/** @var int */
protected $decimals;

public function __construct(
CurrencyType $currencyType
CurrencyType $currencyType,
$decimals
)
{
$this->currencyType = $currencyType;
$this->decimals = (int)$decimals;
}

/**
Expand All @@ -33,7 +38,7 @@ public function buildForm(FormBuilderInterface $builder, array $options)
->add('tbbc_amount', new TextType())
->add('tbbc_currency', $this->currencyType)
->addModelTransformer(
new MoneyToArrayTransformer()
new MoneyToArrayTransformer($this->decimals)
);
}

Expand Down
10 changes: 7 additions & 3 deletions Form/Type/SimpleMoneyType.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,16 @@
class SimpleMoneyType
extends MoneyType
{

/** @var PairManagerInterface */
protected $pairManager;
public function __construct(PairManagerInterface $pairManager)

/** @var int */
protected $decimals;

public function __construct(PairManagerInterface $pairManager, $decimals)
{
$this->pairManager = $pairManager;
$this->decimals = (int)$decimals;
}

/**
Expand All @@ -29,7 +33,7 @@ public function buildForm(FormBuilderInterface $builder, array $options)
$builder
->add('tbbc_amount', new TextType())
->addModelTransformer(
new SimpleMoneyToArrayTransformer($this->pairManager)
new SimpleMoneyToArrayTransformer($this->pairManager, $this->decimals)
);
}

Expand Down
11 changes: 9 additions & 2 deletions Formatter/MoneyFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@
*/
class MoneyFormatter
{
protected $decimals;

public function __construct($decimals = 2)
{
$this->decimals = $decimals;
}

/**
* Formats the given Money object
* INCLUDING the currency symbol
Expand Down Expand Up @@ -45,7 +52,7 @@ public function formatMoney(Money $money, $decPoint = ',', $thousandsSep = ' ')
public function formatAmount(Money $money, $decPoint = ',', $thousandsSep = ' ')
{
$amount = $this->asFloat($money);
$amount = number_format($amount, 2, $decPoint, $thousandsSep);
$amount = number_format($amount, $this->decimals, $decPoint, $thousandsSep);

return $amount;
}
Expand All @@ -60,7 +67,7 @@ public function asFloat(Money $money)
{
$amount = $money->getAmount();
$amount = (float)$amount;
$amount = $amount / 100;
$amount = $amount / pow(10, $this->decimals);

return $amount;
}
Expand Down
13 changes: 11 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ in your config.yml, add the currencies you want to use and the reference currenc
tbbc_money:
currencies: ["USD", "EUR"]
reference_currency: "EUR"
decimals: 2
```
In your config.yml, add the form fields presentations
Expand Down Expand Up @@ -414,13 +415,16 @@ With the Doctrine storage, currency ratio will use the default entity manager an
Optimizations
-------------

in your config.yml, you can select the templating engine to use.
By default, only twig is loaded.
in your config.yml, you can :

* select the templating engine to use. By default, only twig is loaded.
* define the decimals count after a unit (ex : 12.25€ : 2 decimals ; 11.5678€ : 4 decimals)

```yaml
tbbc_money:
currencies: ["USD", "EUR"]
reference_currency: "EUR"
decimals: 2
templating:
engines: ["twig", "php"]
```
Expand Down Expand Up @@ -470,6 +474,11 @@ In progress :
Versions
--------
2.1.0 : 2014/02/01
* no BC Break
* new parameter : decimal count in config.yml for number of decimals (for every money)
2.0.1 : 2013/12/18
* only README fixes
Expand Down
2 changes: 2 additions & 0 deletions Resources/config/form_types.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@
</service>
<service id="tbbc_money.form_type.money" class="%tbbc_money.form_type.money.class%">
<argument type="service" id="tbbc_money.form_type.currency"/>
<argument>%tbbc_money.decimals%</argument>
<tag name="form.type" alias="tbbc_money" />
</service>
<service id="tbbc_money.form_type.simple_money" class="%tbbc_money.form_type.simple_money.class%">
<argument type="service" id="tbbc_money.pair_manager"/>
<argument>%tbbc_money.decimals%</argument>
<tag name="form.type" alias="tbbc_simple_money" />
</service>
</services>
Expand Down
1 change: 1 addition & 0 deletions Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
</service>
<!-- Formatter -->
<service id="tbbc_money.formatter.money_formatter" class="%tbbc_money.formatter.money_formatter.class%">
<argument>%tbbc_money.decimals%</argument>
</service>
</services>

Expand Down
8 changes: 4 additions & 4 deletions Tests/Form/Type/MoneyTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function testBindValid()
array("EUR", "USD"),
"EUR"
);
$moneyType = new MoneyType($currencyType);
$moneyType = new MoneyType($currencyType, 2);
$form = $this->factory->create($moneyType, null, array());
$form->bind(array(
"tbbc_currency" => array("tbbc_name"=>'EUR'),
Expand All @@ -43,7 +43,7 @@ public function testBindDecimalValid()
array("EUR", "USD"),
"EUR"
);
$moneyType = new MoneyType($currencyType);
$moneyType = new MoneyType($currencyType, 2);
$form = $this->factory->create($moneyType, null, array());
$form->bind(array(
"tbbc_currency" => array("tbbc_name"=>'EUR'),
Expand All @@ -59,7 +59,7 @@ public function testGreaterThan1000Valid()
array("EUR", "USD"),
"EUR"
);
$moneyType = new MoneyType($currencyType);
$moneyType = new MoneyType($currencyType, 2);
$form = $this->factory->create($moneyType, null, array());
$form->bind(array(
"tbbc_currency" => array("tbbc_name"=>'EUR'),
Expand All @@ -75,7 +75,7 @@ public function testSetData()
array("EUR", "USD"),
"EUR"
);
$moneyType = new MoneyType($currencyType);
$moneyType = new MoneyType($currencyType, 2);
$form = $this->factory->create($moneyType, null, array());
$form->setData(Money::EUR(120));
$formView = $form->createView();
Expand Down
17 changes: 13 additions & 4 deletions Tests/Form/Type/SimpleMoneyTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,27 @@ public function setUp()

public function testBindValid()
{
$moneyType = new SimpleMoneyType($this->pairManager);
$moneyType = new SimpleMoneyType($this->pairManager, 2);
$form = $this->factory->create($moneyType, null, array());
$form->bind(array(
"tbbc_amount" => '12'
));
$this->assertEquals(Money::EUR(1200), $form->getData());
}
public function testBindValidDecimals()
{
$moneyType = new SimpleMoneyType($this->pairManager, 3);
$form = $this->factory->create($moneyType, null, array());
$form->bind(array(
"tbbc_amount" => '1,2'
));
$this->assertEquals(Money::EUR(1200), $form->getData());
}

public function testBindDecimalValid()
{
\Locale::setDefault("fr_FR");
$moneyType = new SimpleMoneyType($this->pairManager);
$moneyType = new SimpleMoneyType($this->pairManager, 2);
$form = $this->factory->create($moneyType, null, array());
$form->bind(array(
"tbbc_amount" => '12,5'
Expand All @@ -52,7 +61,7 @@ public function testBindDecimalValid()
public function testGreaterThan1000Valid()
{
\Locale::setDefault("fr_FR");
$moneyType = new SimpleMoneyType($this->pairManager);
$moneyType = new SimpleMoneyType($this->pairManager, 2);
$form = $this->factory->create($moneyType, null, array());
$form->bind(array(
"tbbc_amount" => '1 252,5'
Expand All @@ -63,7 +72,7 @@ public function testGreaterThan1000Valid()
public function testSetData()
{
\Locale::setDefault("fr_FR");
$moneyType = new SimpleMoneyType($this->pairManager);
$moneyType = new SimpleMoneyType($this->pairManager, 2);
$form = $this->factory->create($moneyType, null, array());
$form->setData(Money::EUR(120));
$formView = $form->createView();
Expand Down
8 changes: 7 additions & 1 deletion Tests/Formatter/MoneyFormatterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class MoneyFormatterTest extends \PHPUnit_Framework_TestCase
public function setUp()
{
\Locale::setDefault('fr_FR');
$this->formatter = new MoneyFormatter();
$this->formatter = new MoneyFormatter(2);
$this->inputMoney = new Money(123456789, new Currency('EUR'));
}

Expand All @@ -30,6 +30,12 @@ public function testFormatMoneyWithDefaultSeparators()
$value = $this->formatter->formatMoney($this->inputMoney);
$this->assertEquals('1 234 567,89 €', $value);
}
public function testFormatMoneyWithDefaultSeparatorsAndDecimals3()
{
$this->formatter = new MoneyFormatter(4);
$value = $this->formatter->formatMoney($this->inputMoney);
$this->assertEquals('12 345,6789 €', $value);
}

public function testFormatMoneyWithCustomSeparators()
{
Expand Down
2 changes: 1 addition & 1 deletion Tests/Twig/Extension/CurrencyExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class CurrencyExtensionTest extends \PHPUnit_Framework_TestCase
public function setUp()
{
\Locale::setDefault("fr_FR");
$this->extension = new CurrencyExtension(new MoneyFormatter());
$this->extension = new CurrencyExtension(new MoneyFormatter(2));
$this->variables = array('currency' => new Currency('EUR'));
}

Expand Down
2 changes: 1 addition & 1 deletion Tests/Twig/Extension/MoneyExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function setUp()
->method('getReferenceCurrencyCode')
->will($this->returnValue("EUR"));

$this->extension = new MoneyExtension(new MoneyFormatter(), $pairManager);
$this->extension = new MoneyExtension(new MoneyFormatter(2), $pairManager);
$this->variables = array('price' => new Money(123456789, new Currency('EUR')));
}

Expand Down
1 change: 1 addition & 0 deletions Tests/app/config/config_test.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
tbbc_money:
currencies: ["USD", "EUR"]
reference_currency: "EUR"
decimals: 3

framework:
trusted_hosts: ~
Expand Down

0 comments on commit 9884f18

Please sign in to comment.