Commit fbf1901d authored by Thorsten Buss's avatar Thorsten Buss
Browse files

Merge commit 'a684be9b' origin money library

Conflicts:
	README.md
	composer.json
parents 30c716d7 a684be9b
......@@ -5,6 +5,7 @@ Money Changelog
2.0.0.dev
---------
- 2013-10-13 Base currency and counter currency in CurrencyPair named correctly.
- 2013-01-08 Removed the Doctrine2\MoneyType helper, to be replaced by something better in the future. It's available
at https://gist.github.com/4485025 in case you need it.
- 2013-01-08 Use vendor/autoload.php instead of lib/bootstrap.php (or use PSR-0 autolaoding)
......
......@@ -14,21 +14,21 @@ namespace Money;
class CurrencyPair
{
/** @var Currency */
private $counterCurrency;
private $baseCurrency;
/** @var Currency */
private $baseCurrency;
private $counterCurrency;
/** @var float */
private $ratio;
/**
* @param \Money\Currency $counterCurrency
* @param \Money\Currency $baseCurrency
* @param \Money\Currency $counterCurrency
* @param float $ratio
* @throws \Money\InvalidArgumentException
*/
public function __construct(Currency $counterCurrency, Currency $baseCurrency, $ratio)
public function __construct(Currency $baseCurrency, Currency $counterCurrency, $ratio)
{
if(!is_numeric($ratio)) {
throw new InvalidArgumentException("Ratio must be numeric");
......@@ -52,8 +52,12 @@ class CurrencyPair
$matches = array();
if (!preg_match($pattern, $iso, $matches)) {
// @todo better exception
throw new \Exception();
throw new InvalidArgumentException(
sprintf(
"Can't create currency pair from ISO string '%s', format of string is invalid",
$iso
)
);
}
return new static(new Currency($matches[1]), new Currency($matches[2]), $matches[3]);
......@@ -66,12 +70,12 @@ class CurrencyPair
*/
public function convert(Money $money)
{
if (!$money->getCurrency()->equals($this->counterCurrency)) {
if (!$money->getCurrency()->equals($this->baseCurrency)) {
throw new InvalidArgumentException("The Money has the wrong currency");
}
// @todo add rounding mode?
return new Money((int) round($money->getAmount() * $this->ratio), $this->baseCurrency);
return new Money((int) round($money->getAmount() * $this->ratio), $this->counterCurrency);
}
/** @return \Money\Currency */
......
......@@ -40,4 +40,66 @@ class CurrencyPairTest extends MoneyTestCase {
$expected = new CurrencyPair(new Currency('EUR'), new Currency('USD'), 1.2500);
$this->assertEquals($expected, $pair);
}
/**
* @expectedException \Money\InvalidArgumentException
* @expectedExceptionMessage Can't create currency pair from ISO string '1.2500', format of string is invalid
*/
public function ParsesIsoWithException()
{
CurrencyPair::createFromIso('1.2500');
}
/**
* @expectedException \Money\InvalidArgumentException
* @expectedExceptionMessage Ratio must be numeric
* @dataProvider provideNonNumericRatio
*/
public function testConstructorWithNonNumericRatio($nonNumericRatio)
{
new CurrencyPair(new Currency('EUR'), new Currency('USD'), $nonNumericRatio);
}
public function testGetRatio()
{
$ratio = 1.2500;
$pair = new CurrencyPair(new Currency('EUR'), new Currency('USD'), $ratio);
$this->assertEquals($ratio, $pair->getRatio());
}
public function testGetBaseCurrency()
{
$pair = new CurrencyPair(new Currency('EUR'), new Currency('USD'), 1.2500);
$this->assertEquals(new Currency('EUR'), $pair->getBaseCurrency());
}
public function testGetCounterCurrency()
{
$pair = new CurrencyPair(new Currency('EUR'), new Currency('USD'), 1.2500);
$this->assertEquals(new Currency('USD'), $pair->getCounterCurrency());
}
/**
* @expectedException \Money\InvalidArgumentException
* @expectedExceptionMessage The Money has the wrong currency
*/
public function testConvertWithInvalidCurrency()
{
$money = new Money(100, new Currency('JPY'));
$pair = new CurrencyPair(new Currency('EUR'), new Currency('USD'), 1.2500);
$pair->convert($money);
}
public function provideNonNumericRatio()
{
return array(
array('NonNumericRatio'),
array('16AlsoIncorrect'),
array('10.00ThisIsToo')
);
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment