Commit 66c8bf4e authored by Mathias Verraes's avatar Mathias Verraes
Browse files

cleanup

parent f37466da
......@@ -101,6 +101,8 @@ Inspiration
* https://github.com/lucamarrocco/timeandmoney/blob/master/lib/money.rb
* http://joda-money.sourceforge.net/
* http://en.wikipedia.org/wiki/Currency_pair
* https://github.com/RubyMoney/eu_central_bank
* http://en.wikipedia.org/wiki/ISO_4217
Bibliography
============
......@@ -109,9 +111,3 @@ Bibliography
Fowler, M., D. Rice, M. Foemmel, E. Hieatt, R. Mee, and R. Stafford, Patterns of Enterprise Application Architecture, Addison-Wesley, 2002.
http://martinfowler.com/books.html#eaa
http://en.wikipedia.org/wiki/ISO_4217
Todo
====
* https://github.com/RubyMoney/eu_central_bank
\ No newline at end of file
<?php
/**
* This file is part of the Verraes\Money library
*
* Copyright (c) 2011 Mathias Verraes
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Verraes\Money;
/** @see http://en.wikipedia.org/wiki/Currency_pair */
class CurrencyPair
{
/** @var Currency */
private $counterCurrency;
/** @var Currency */
private $baseCurrency;
/** @var float */
private $ratio;
public function __construct(Currency $counterCurrency, Currency $baseCurrency, $ratio)
{
$this->counterCurrency = $counterCurrency;
$this->baseCurrency = $baseCurrency;
$this->ratio = $ratio;
}
/** @return Money */
public function convert(Money $money)
{
if(!$money->getCurrency()->equals($this->counterCurrency)) {
throw new InvalidArgumentException("The Money has the wrong currency");
}
// @todo add rounding mode?
return new Money((int) round($money->getUnits() * $this->ratio), $this->baseCurrency);
}
/** @return Currency */
public function getCounterCurrency()
{
return $this->counterCurrency;
}
/** @return Currency */
public function getBaseCurrency()
{
return $this->baseCurrency;
}
/** @return float */
public function getRatio()
{
return $this->ratio;
}
}
\ No newline at end of file
<?php
/**
* This file is part of the Verraes\Money library
*
* Copyright (c) 2011 Mathias Verraes
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
require_once 'bootstrap.php';
use Verraes\Money\Money;
use Verraes\Money\Currency;
use Verraes\Money\CurrencyPair;
class CurrencyPairTest extends PHPUnit_Framework_TestCase
{
/** @test */
public function ConvertsEurToUsdAndBack()
{
$eur = Money::EUR(100);
$pair = new CurrencyPair(new Currency('EUR'), new Currency('USD'), 1.2500);
$usd = $pair->convert($eur);
$this->assertEquals(Money::USD(125), $usd);
$pair = new CurrencyPair(new Currency('USD'), new Currency('EUR'), 0.8000);
$eur = $pair->convert($usd);
$this->assertEquals(Money::EUR(100), $eur);
}
}
\ No newline at end of file
......@@ -10,6 +10,7 @@
require_once 'PHPUnit/Framework/TestCase.php';
require_once __DIR__.'/../lib/Verraes/Money/Currency.php';
require_once __DIR__.'/../lib/Verraes/Money/CurrencyPair.php';
require_once __DIR__.'/../lib/Verraes/Money/Exception.php';
require_once __DIR__.'/../lib/Verraes/Money/InvalidArgumentException.php';
require_once __DIR__.'/../lib/Verraes/Money/UnknownCurrencyException.php';
......
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