Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Adding feature to set default currency on CakeNumber, to make repetet…

…ive calls to CakeNumber::currency() more DRY
  • Loading branch information...
commit 967bf8e27ea2438f1972390b7ef78ae62e17a762 1 parent fa6defe
@ceeram authored
View
33 lib/Cake/Test/Case/Utility/CakeNumberTest.php
@@ -282,6 +282,39 @@ public function testCurrencyAddFormat() {
}
/**
+ * 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
*
* @return void
View
25 lib/Cake/Utility/CakeNumber.php
@@ -61,6 +61,13 @@ class CakeNumber {
);
/**
+ * Default currency used by CakeNumber::currency()
+ *
+ * @var string
+ */
+ protected static $_defaultCurrency = 'USD';
+
+/**
* If native number_format() should be used. If >= PHP5.4
*
* @var boolean
@@ -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;
+ }
+
}
Please sign in to comment.
Something went wrong with that request. Please try again.