Browse Source

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
Ceeram 13 years ago
parent
commit
f4f4aa4a2a

+ 33 - 0
lib/Cake/Test/Case/Utility/CakeNumberTest.php

@@ -282,6 +282,39 @@ class CakeNumberTest extends CakeTestCase {
 	}
 
 /**
+ * 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
  *
  * @return void

+ 24 - 1
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 @@ class CakeNumber {
  * @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 @@ class CakeNumber {
 		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;
+	}
+
 }

+ 13 - 1
lib/Cake/View/Helper/NumberHelper.php

@@ -126,11 +126,12 @@ class NumberHelper extends AppHelper {
  * @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 @@ class NumberHelper extends AppHelper {
 		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);
+	}
+
 }