Skip to content
This repository
Browse code

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...
commit f4f4aa4a2a0e1b273458c031daa1f8dc0f8abf71 1 parent ab11d50
ceeram authored
33  lib/Cake/Test/Case/Utility/CakeNumberTest.php
@@ -282,6 +282,39 @@ public function testCurrencyAddFormat() {
282 282
 	}
283 283
 
284 284
 /**
  285
+ * Test default currency
  286
+ *
  287
+ * @return void
  288
+ */
  289
+	public function testDefaultCurrency() {
  290
+		$result = $this->Number->defaultCurrency();
  291
+		$this->assertEquals('USD', $result);
  292
+		$this->Number->addFormat('NOK', array('before' => 'Kr. '));
  293
+
  294
+		$this->Number->defaultCurrency('NOK');
  295
+		$result = $this->Number->defaultCurrency();
  296
+		$this->assertEquals('NOK', $result);
  297
+
  298
+		$result = $this->Number->currency(1000);
  299
+		$expected = 'Kr. 1,000.00';
  300
+		$this->assertEquals($expected, $result);
  301
+
  302
+		$result = $this->Number->currency(2000);
  303
+		$expected = 'Kr. 2,000.00';
  304
+		$this->assertEquals($expected, $result);
  305
+		$this->Number->defaultCurrency('EUR');
  306
+		$result = $this->Number->currency(1000);
  307
+		$expected = '&#8364;1.000,00';
  308
+		$this->assertEquals($expected, $result);
  309
+
  310
+		$result = $this->Number->currency(2000);
  311
+		$expected = '&#8364;2.000,00';
  312
+		$this->assertEquals($expected, $result);
  313
+
  314
+		$this->Number->defaultCurrency('USD');
  315
+	}
  316
+
  317
+/**
285 318
  * testCurrencyPositive method
286 319
  *
287 320
  * @return void
25  lib/Cake/Utility/CakeNumber.php
@@ -61,6 +61,13 @@ class CakeNumber {
61 61
 	);
62 62
 
63 63
 /**
  64
+ * Default currency used by CakeNumber::currency()
  65
+ *
  66
+ * @var string
  67
+ */
  68
+	protected static $_defaultCurrency = 'USD';
  69
+
  70
+/**
64 71
  * If native number_format() should be used. If >= PHP5.4
65 72
  *
66 73
  * @var boolean
@@ -275,8 +282,11 @@ protected static function _numberFormat($value, $places = 0, $decimals = '.', $t
275 282
  * @return string Number formatted as a currency.
276 283
  * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/number.html#NumberHelper::currency
277 284
  */
278  
-	public static function currency($value, $currency = 'USD', $options = array()) {
  285
+	public static function currency($value, $currency = null, $options = array()) {
279 286
 		$default = self::$_currencyDefaults;
  287
+		if ($currency === null) {
  288
+			$currency = self::defaultCurrency();
  289
+		}
280 290
 
281 291
 		if (isset(self::$_currencies[$currency])) {
282 292
 			$default = self::$_currencies[$currency];
@@ -348,4 +358,17 @@ public static function addFormat($formatName, $options) {
348 358
 		self::$_currencies[$formatName] = $options + self::$_currencyDefaults;
349 359
 	}
350 360
 
  361
+/**
  362
+ * Getter/setter for default currency
  363
+ *
  364
+ * @param string $currency Default currency string  used by currency() if $currency argument is not provided
  365
+ * @return string Currency
  366
+ */
  367
+	public static function defaultCurrency($currency = null) {
  368
+		if ($currency) {
  369
+			self::$_defaultCurrency = $currency;
  370
+		}
  371
+		return self::$_defaultCurrency;
  372
+	}
  373
+
351 374
 }
14  lib/Cake/View/Helper/NumberHelper.php
@@ -126,11 +126,12 @@ public function format($number, $options = false) {
126 126
  * @param float $number
127 127
  * @param string $currency Shortcut to default options. Valid values are 'USD', 'EUR', 'GBP', otherwise
128 128
  *   set at least 'before' and 'after' options.
  129
+ * 'USD' is the default currency, use CakeNumber::defaultCurrency() to change this default.
129 130
  * @param array $options
130 131
  * @return string Number formatted as a currency.
131 132
  * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/number.html#NumberHelper::currency
132 133
  */
133  
-	public function currency($number, $currency = 'USD', $options = array()) {
  134
+	public function currency($number, $currency = null, $options = array()) {
134 135
 		return $this->_engine->currency($number, $currency, $options);
135 136
 	}
136 137
 
@@ -147,4 +148,15 @@ public function addFormat($formatName, $options) {
147 148
 		return $this->_engine->addFormat($formatName, $options);
148 149
 	}
149 150
 
  151
+/**
  152
+ * @see: CakeNumber::defaultCurrency()
  153
+ *
  154
+ * @param string $currency The currency to be used in the future.
  155
+ * @return void
  156
+ * @see NumberHelper::currency()
  157
+ */
  158
+	public function defaultCurrency($currency) {
  159
+		return $this->_engine->defaultCurrency($currency);
  160
+	}
  161
+
150 162
 }

0 notes on commit f4f4aa4

Please sign in to comment.
Something went wrong with that request. Please try again.