-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6657704
commit 3779dfa
Showing
5 changed files
with
120 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?php | ||
/** | ||
* Created by levan on 25/09/2014. | ||
*/ | ||
|
||
namespace Tbbc\MoneyBundle\Money; | ||
|
||
|
||
use Money\Currency; | ||
use Money\Money; | ||
|
||
class MoneyManager | ||
implements MoneyManagerInterface | ||
{ | ||
/** @var int */ | ||
protected $decimals; | ||
|
||
/** @var string */ | ||
protected $referenceCurrencyCode; | ||
|
||
public function __construct( | ||
$referenceCurrencyCode, | ||
$decimals = 2 | ||
) | ||
{ | ||
$this->referenceCurrencyCode = $referenceCurrencyCode; | ||
$this->decimals = $decimals; | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function createMoneyFromFloat($floatAmount, $currencyCode = null) | ||
{ | ||
if (is_null($currencyCode)) { | ||
$currencyCode = $this->referenceCurrencyCode; | ||
} | ||
$currency = new Currency($currencyCode); | ||
$amountAsInt = $floatAmount * pow(10, $this->decimals); | ||
$amountAsInt = round($amountAsInt); | ||
$amountAsInt = intval($amountAsInt); | ||
$money = new Money($amountAsInt, $currency); | ||
return $money; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
namespace Tbbc\MoneyBundle\Money; | ||
|
||
use Money\Money; | ||
|
||
interface MoneyManagerInterface | ||
{ | ||
/** | ||
* convert the float amount into a money object | ||
* | ||
* @param float $floatAmount amount before multiplication. Ex: 32.15 | ||
* @param string|null $currencyCode in the list of currencies from config.yml. If null, use the reference currency | ||
* @return Money | ||
*/ | ||
public function createMoneyFromFloat($floatAmount, $currencyCode = null); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?php | ||
namespace Tbbc\MoneyBundle\Tests\Money; | ||
|
||
use Money\Money; | ||
use Tbbc\MoneyBundle\Money\MoneyManager; | ||
use Tbbc\MoneyBundle\MoneyException; | ||
|
||
/** | ||
* @group moneymanager | ||
*/ | ||
class MoneyManagerTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
/** @var MoneyManager */ | ||
protected $manager; | ||
|
||
public function setUp() | ||
{ | ||
$this->manager = new MoneyManager("EUR", 2); | ||
} | ||
|
||
public function tearDown() | ||
{ | ||
} | ||
|
||
public function testCreateMoneyFromFloat() | ||
{ | ||
$money = $this->manager->createMoneyFromFloat(2.5); | ||
$this->assertEquals("EUR", $money->getCurrency()->getName()); | ||
$this->assertEquals(250, $money->getAmount()); | ||
|
||
$money = $this->manager->createMoneyFromFloat(2.5, 'USD'); | ||
$this->assertEquals("USD", $money->getCurrency()->getName()); | ||
$this->assertEquals(250, $money->getAmount()); | ||
|
||
$money = $this->manager->createMoneyFromFloat(2.49999999999999); | ||
$this->assertEquals(250, $money->getAmount()); | ||
|
||
$money = $this->manager->createMoneyFromFloat(2.529999999999); | ||
$this->assertEquals(253, $money->getAmount()); | ||
} | ||
|
||
} |