Skip to content
Permalink
Browse files

Squashed commit of the following:

commit 22311a1
Merge: fda331e d051b69
Author: Ceeram <c33ram@gmail.com>
Date:   Fri Oct 26 14:40:24 2012 +0200

    Merge branch '2.3' into currency

commit fda331e
Author: Ceeram <c33ram@gmail.com>
Date:   Sun Oct 7 23:53:03 2012 +0200

    NumberHelper now also uses the default currency from CakeNumber, instead of default argument value

commit 967bf8e
Author: Ceeram <c33ram@gmail.com>
Date:   Sun Oct 7 18:01:35 2012 +0200

    Adding feature to set default currency on CakeNumber, to make repetetive calls to CakeNumber::currency() more DRY
  • Loading branch information...
ceeram committed Oct 26, 2012
1 parent ab11d50 commit f4f4aa4a2a0e1b273458c031daa1f8dc0f8abf71
@@ -281,6 +281,39 @@ public function testCurrencyAddFormat() {
$this->assertEquals($expected, $result);
}
/**
* Test default currency
*
* @return void
*/
public function testDefaultCurrency() {
$result = $this->Number->defaultCurrency();
$this->assertEquals('USD', $result);
$this->Number->addFormat('NOK', array('before' => 'Kr. '));
$this->Number->defaultCurrency('NOK');
$result = $this->Number->defaultCurrency();
$this->assertEquals('NOK', $result);
$result = $this->Number->currency(1000);
$expected = 'Kr. 1,000.00';
$this->assertEquals($expected, $result);
$result = $this->Number->currency(2000);
$expected = 'Kr. 2,000.00';
$this->assertEquals($expected, $result);
$this->Number->defaultCurrency('EUR');
$result = $this->Number->currency(1000);
$expected = '&#8364;1.000,00';
$this->assertEquals($expected, $result);
$result = $this->Number->currency(2000);
$expected = '&#8364;2.000,00';
$this->assertEquals($expected, $result);
$this->Number->defaultCurrency('USD');
}
/**
* testCurrencyPositive method
*
@@ -60,6 +60,13 @@ class CakeNumber {
'zero' => '0', 'places' => 2, 'thousands' => ',', 'decimals' => '.','negative' => '()', 'escape' => true,
);
/**
* Default currency used by CakeNumber::currency()
*
* @var string
*/
protected static $_defaultCurrency = 'USD';
/**
* If native number_format() should be used. If >= PHP5.4
*
@@ -275,8 +282,11 @@ protected static function _numberFormat($value, $places = 0, $decimals = '.', $t
* @return string Number formatted as a currency.
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/number.html#NumberHelper::currency
*/
public static function currency($value, $currency = 'USD', $options = array()) {
public static function currency($value, $currency = null, $options = array()) {
$default = self::$_currencyDefaults;
if ($currency === null) {
$currency = self::defaultCurrency();
}
if (isset(self::$_currencies[$currency])) {
$default = self::$_currencies[$currency];
@@ -348,4 +358,17 @@ public static function addFormat($formatName, $options) {
self::$_currencies[$formatName] = $options + self::$_currencyDefaults;
}
/**
* Getter/setter for default currency
*
* @param string $currency Default currency string used by currency() if $currency argument is not provided
* @return string Currency
*/
public static function defaultCurrency($currency = null) {
if ($currency) {
self::$_defaultCurrency = $currency;
}
return self::$_defaultCurrency;
}
}
@@ -126,11 +126,12 @@ public function format($number, $options = false) {
* @param float $number
* @param string $currency Shortcut to default options. Valid values are 'USD', 'EUR', 'GBP', otherwise
* set at least 'before' and 'after' options.
* 'USD' is the default currency, use CakeNumber::defaultCurrency() to change this default.
* @param array $options
* @return string Number formatted as a currency.
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/number.html#NumberHelper::currency
*/
public function currency($number, $currency = 'USD', $options = array()) {
public function currency($number, $currency = null, $options = array()) {
return $this->_engine->currency($number, $currency, $options);
}
@@ -147,4 +148,15 @@ public function addFormat($formatName, $options) {
return $this->_engine->addFormat($formatName, $options);
}
/**
* @see: CakeNumber::defaultCurrency()
*
* @param string $currency The currency to be used in the future.
* @return void
* @see NumberHelper::currency()
*/
public function defaultCurrency($currency) {
return $this->_engine->defaultCurrency($currency);
}
}

0 comments on commit f4f4aa4

Please sign in to comment.
You can’t perform that action at this time.