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

* add MoneyTestCase for automated loading of RubyMoney extended currencies

* add MoneyFormater for usage in loops to minimize memory usage
* add static format/legibly method in currency for minimize memory usage
* add Currency Getter and make factory (cache instances
parent a959a2a3
......@@ -12,6 +12,9 @@ namespace Money;
class Currency
{
/** @var Currency[] */
static $instances = array();
/** @var string */
private $iso_string;
......@@ -54,6 +57,19 @@ class Currency
}
}
/**
* return instance of MOneyObj - for single use to reduce memory usage in loops
* @param null $currency
* @return Money
*/
public static function getInstance($currency = null) {
// get isocode from currency, direct or default
$iso_code = $currency instanceof Currency ? $currency->getIsostring() : ($currency ? : Currency::getDefaultCurrency());
if (!array_key_exists($iso_code, static::$instances)) {
static::$instances[$iso_code] = new static($iso_code);
}
return static::$instances[$iso_code];
}
/**
* @return string
......@@ -116,7 +132,7 @@ class Currency
*/
protected function assertExtendedCurrency() {
if (!$this->is_extended_currency)
throw new CurrencyIsNoExtendedCurrencyException(sprintf('currency %s is no extended currency, which is neede for this method. Add CurrencyLookup', $this->getIsostring()));
throw new CurrencyIsNoExtendedCurrencyException(sprintf('currency %s is no extended currency, which is need for this method. Add CurrencyLookup', $this->getIsostring()));
}
/**
......@@ -196,6 +212,38 @@ class Currency
return $this->get('html_entity');
}
/**
* return the numeric iso code
* @return int
*/
public function getIsoNumeric() {
return (int)$this->get('iso_numeric');
}
/**
* return the name of the currency
* @return string
*/
public function getName() {
return $this->get('name');
}
/**
* return the name of the subunit
* @return string
*/
public function getSubunit() {
return $this->get('subunit');
}
/**
* return list of alternate symbols - maybe empty
* @return array
*/
public function getAlternateSymbols() {
return $this->get('alternate_symbols');
}
/**
* return a extended currencyvalue if currency is extended and value exists
* @param $string
......@@ -208,6 +256,34 @@ class Currency
return $this->$key;
}
/**
* Format Amount with given currency
*
* @see Money::format()
* @see Money::getInstance()
* @param int $amount as subunit
* @param string|Currency $currency isoCode or currencyObj
* @param array $params params to format
* @return string
*/
public static function format($amount, $currency=null, $params = array()) {
return Money::getInstance($currency, $amount)->format($params);
}
/**
* return subunit amount legibly - make human readable
*
* @see Money::getAmount()
* @see Money::getInstance()
* @param int $amount as subunit
* @param string|Currency $currency isoCode or currencyObj
* @param array $params params for Money::getAmount()
* @return string
*/
public static function legibly($amount, $currency=null, $params = array()) {
return Money::getInstance($currency, $amount)->getAmount(true, $params);
}
}
// set the defaultCurrency
// set the defaultCurrency - EUR if DEFAULT_CURRENCY const undefined
Currency::setDefaultCurrency(@constant('DEFAULT_CURRENCY') ?: 'EUR');
\ No newline at end of file
......@@ -17,6 +17,9 @@ class Money
const ROUND_HALF_EVEN = PHP_ROUND_HALF_EVEN;
const ROUND_HALF_ODD = PHP_ROUND_HALF_ODD;
/** @var Money[] */
static $instances = array();
/**
* @var int
*/
......@@ -42,6 +45,21 @@ class Money
$this->currency = $currency;
}
/**
* return instance of MOneyObj - for single use to reduce memory usage in loops
* @param null $currency
* @param int $amount
* @return Money
*/
public static function getInstance($currency=null, $amount=0) {
// get isocode from currency, direct or default
$iso_code = $currency instanceof Currency ? $currency->getIsostring() : ($currency ? : Currency::getDefaultCurrency());
if (!array_key_exists($iso_code, static::$instances)) {
static::$instances[$iso_code] = new static($amount, new Currency($iso_code));
}
return static::$instances[$iso_code];
}
/**
* Convenience factory method for a Money object
* @example $fiveDollar = Money::USD(500);
......@@ -54,6 +72,15 @@ class Money
return new Money($arguments[0], new Currency($method));
}
/**
* change the amount
* @param int $amount amount in subunit
*/
public function setAmount($amount) {
$this->amount = $amount;
return $this;
}
/**
* @param \Money\Money $other
* @return bool
......
<?php
/**
* User: thorsten
* Date: 07.08.13
* Time: 15:31
*/
namespace Money;
/**
* Class MoneyFormater for memory minimized usage in loops
* @package Money
*/
class MoneyFormater {
public function __construct($currency, $default_format_params=array()) {
$this->money = new Money(0, Currency::getInstance($currency));
$this->default_format_params = $default_format_params;
}
/**
* Format Amount with given currency
*
* @see Money::format()
* @param int $amount as subunit
* @param array $params params to format
* @return string
*/
public function format($amount, $params = array()) {
return $this->money->setAmount($amount)->format($this->getParams($params));
}
/**
* return subunit amount legibly - make human readable
*
* @see Money::getAmount()
* @param int $amount as subunit
* @param array $params params for Money::getAmount()
* @return string
*/
public function legibly($amount, $params = array()) {
return $this->money->setAmount($amount)->getAmount(true, $this->getParams($params));
}
/**
* merge defaultParams with given
* @param array $params
* @return array
*/
protected function getParams($params) {
return array_merge($this->default_format_params, $params);
}
}
\ No newline at end of file
......@@ -4,7 +4,7 @@
<phpunit bootstrap="vendor/autoload.php"
backupGlobals="false"
backupStaticAttributes="false"
strict="true"
strict="false"
colors="true"
verbose="true">
<testsuites>
......
......@@ -10,13 +10,15 @@
namespace Money\Tests;
require_once('MoneyTestCase.php');
use PHPUnit_Framework_TestCase;
use Money\Money;
use Money\Currency;
use Money\CurrencyPair;
class CurrencyPairTest extends PHPUnit_Framework_TestCase
{
class CurrencyPairTest extends MoneyTestCase {
/** @test */
public function ConvertsEurToUsdAndBack()
{
......
......@@ -10,13 +10,16 @@
namespace Money\Tests;
require_once('MoneyTestCase.php');
use PHPUnit_Framework_TestCase;
use Money\Currency;
class CurrencyTest extends PHPUnit_Framework_TestCase
{
public function setUp()
class CurrencyTest extends MoneyTestCase {
public function setUp()
{
parent::setUp();
$this->euro1 = new Currency('EUR');
$this->euro2 = new Currency('EUR');
$this->usd1 = new Currency('USD');
......
<?php
/**
* User: thorsten
* Date: 07.08.13
* Time: 15:48
*/
namespace Money\Tests;
require_once('MoneyTestCase.php');
use Money\MoneyFormater;
class MoneyFormaterTest extends MoneyTestCase {
public function testFormater() {
$m = new MoneyFormater('EUR');
$this->assertEquals('€1.234,56', $m->format(123456));
$this->assertEquals('€1.234', $m->format(123456, array('no_cents' => true)));
// with defaultParams
$m = new MoneyFormater('EUR', array('symbol' => '~'));
$this->assertEquals('~1.234,56', $m->format(123456));
$this->assertEquals('~1.234', $m->format(123456, array('no_cents' => true)));
}
public function testLegibly() {
$m = new MoneyFormater('EUR');
$this->assertEquals('1234,56', $m->legibly(123456));
$this->assertEquals('1234', $m->legibly(123400));
$this->assertEquals('1234,00', $m->legibly(123400, array('force_decimal' => true)));
// with defaultParams
$m = new MoneyFormater('EUR', array('symbol' => '~'));
$this->assertEquals('1234,56', $m->legibly(123456));
$this->assertEquals('1234,00', $m->legibly(123400, array('force_decimal' => true)));
$this->assertEquals('1234', $m->legibly(123400));
}
}
......@@ -10,19 +10,14 @@
namespace Money\Tests;
require_once('MoneyTestCase.php');
use Money\CurrencyLookupRubyMoney;
use PHPUnit_Framework_TestCase;
use Money\Money;
use Money\Currency;
class MoneyTest extends PHPUnit_Framework_TestCase
{
protected function setUp() {
parent::setUp();
// load RubyMoney CurrencyList and register lookup
Currency::setCurrencyLookup(new CurrencyLookupRubyMoney());
}
class MoneyTest extends MoneyTestCase {
public function testFactoryMethods()
{
......
<?php
/**
* User: thorsten
* Date: 07.08.13
* Time: 16:48
*/
namespace Money\Tests;
use Money\Currency;
use Money\CurrencyLookupRubyMoney;
class MoneyTestCase extends \PHPUnit_Framework_TestCase {
protected function setUp() {
parent::setUp();
// load RubyMoney CurrencyList and register lookup
Currency::setCurrencyLookup(new CurrencyLookupRubyMoney());
}
}
\ No newline at end of file
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