mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2025-01-19 02:56:15 +00:00
Squashed commit of the following:
commit 22311a1e62934da757d53d7aecfce884f5a1ca10
Merge: fda331e d051b69
Author: Ceeram <c33ram@gmail.com>
Date: Fri Oct 26 14:40:24 2012 +0200
Merge branch '2.3' into currency
commit fda331eab169068763717f88838032b7c0c02c29
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 967bf8e27ea2438f1972390b7ef78ae62e17a762
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
This commit is contained in:
parent
ab11d507f2
commit
f4f4aa4a2a
3 changed files with 70 additions and 2 deletions
|
@ -281,6 +281,39 @@ class CakeNumberTest extends CakeTestCase {
|
||||||
$this->assertEquals($expected, $result);
|
$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 = '€1.000,00';
|
||||||
|
$this->assertEquals($expected, $result);
|
||||||
|
|
||||||
|
$result = $this->Number->currency(2000);
|
||||||
|
$expected = '€2.000,00';
|
||||||
|
$this->assertEquals($expected, $result);
|
||||||
|
|
||||||
|
$this->Number->defaultCurrency('USD');
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* testCurrencyPositive method
|
* testCurrencyPositive method
|
||||||
*
|
*
|
||||||
|
|
|
@ -60,6 +60,13 @@ class CakeNumber {
|
||||||
'zero' => '0', 'places' => 2, 'thousands' => ',', 'decimals' => '.','negative' => '()', 'escape' => true,
|
'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
|
* If native number_format() should be used. If >= PHP5.4
|
||||||
*
|
*
|
||||||
|
@ -275,8 +282,11 @@ class CakeNumber {
|
||||||
* @return string Number formatted as a currency.
|
* @return string Number formatted as a currency.
|
||||||
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/number.html#NumberHelper::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;
|
$default = self::$_currencyDefaults;
|
||||||
|
if ($currency === null) {
|
||||||
|
$currency = self::defaultCurrency();
|
||||||
|
}
|
||||||
|
|
||||||
if (isset(self::$_currencies[$currency])) {
|
if (isset(self::$_currencies[$currency])) {
|
||||||
$default = self::$_currencies[$currency];
|
$default = self::$_currencies[$currency];
|
||||||
|
@ -348,4 +358,17 @@ class CakeNumber {
|
||||||
self::$_currencies[$formatName] = $options + self::$_currencyDefaults;
|
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 @@ class NumberHelper extends AppHelper {
|
||||||
* @param float $number
|
* @param float $number
|
||||||
* @param string $currency Shortcut to default options. Valid values are 'USD', 'EUR', 'GBP', otherwise
|
* @param string $currency Shortcut to default options. Valid values are 'USD', 'EUR', 'GBP', otherwise
|
||||||
* set at least 'before' and 'after' options.
|
* set at least 'before' and 'after' options.
|
||||||
|
* 'USD' is the default currency, use CakeNumber::defaultCurrency() to change this default.
|
||||||
* @param array $options
|
* @param array $options
|
||||||
* @return string Number formatted as a currency.
|
* @return string Number formatted as a currency.
|
||||||
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/number.html#NumberHelper::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);
|
return $this->_engine->currency($number, $currency, $options);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -147,4 +148,15 @@ class NumberHelper extends AppHelper {
|
||||||
return $this->_engine->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);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue