Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Adding feature to set default currency on CakeNumber, to make repetetive... #891

Closed
wants to merge 3 commits into from

4 participants

@ceeram
Collaborator

... calls to CakeNumber::currency() more DRY

@ADmad ADmad commented on the diff
lib/Cake/Utility/CakeNumber.php
@@ -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;
@ADmad Collaborator
ADmad added a note

Perhaps add a check for valid currency using CakeNumber::$_currencies and return false in case of invalid value.

@ceeram Collaborator
ceeram added a note

That would make it impossible to first set a default and later addFormat(), imo its up to the developer to use a sane value, but if you think it should do the check i can add it

@ADmad Collaborator
ADmad added a note

If someone else shares my opinion add it, else ignore :smiley:

@markstory Owner

Wouldn't setting the default to a value that doesn't exist cause a number of errors downstream?

@ceeram Collaborator
ceeram added a note

sure, just like passing not existing value as 2nd parameter to currency() all the method does is predefine the default value for 2nd parameter, so you dont need to write it out in the application.

For instance i used burzums Cart plugin, this method allows to change the default currency without the need to change the plugin code or use a Configure::write() in the app, and Configure::read() in the plugin.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@markstory
Owner

Would it also be useful to expose the default currency on the helper? That would save people having to define it in several places. It might also make sense to allow formats to be defined in the helper definition. All of this feels like a separate change though :)

@ceeram
Collaborator

Changed the helper to use no default argument value anymore, and use the default defined in CakeNumber

@markstory doesnt the helper already allow formats to be defined?

@markstory
Owner

@ceeram Sure, but only via method calls, I was thinking it could be helpful to define the default and additional currencies in a controller's $helpers array.

@ceeram
Collaborator
diff --git a/lib/Cake/View/Helper/NumberHelper.php b/lib/Cake/View/Helper/NumberHelper.php
index e286c36..ba1861a 100644
--- a/lib/Cake/View/Helper/NumberHelper.php
+++ b/lib/Cake/View/Helper/NumberHelper.php
@@ -63,6 +63,9 @@ class NumberHelper extends AppHelper {
                } else {
                        throw new CakeException(__d('cake_dev', '%s could not be found', $engineClass));
                }
+               if (!empty($settings['defaultCurrency'])) {
+                       $this->defaultCurrency($settings['defaultCurrency']);
+               }
        }

 /**

Along these lines you mean?

@ceeram
Collaborator

While we are at it, do we even want to keep number helper? as its just a wrapper for CakeNumber? one can use CakeNumber::currency() just as easily in the views?

@markstory
Owner

NumberHelper has some HTML formatting bits that CakeNumber doesn't. I think if we can keep that separation, then its worth its weight.

@ceeram
Collaborator

I must be missing something then, i dont see where it does that.

@dereuromark
Collaborator

NumberHelper does not. But Time and Text do. But to keep things unified we should leave the NumberHelper. Especially with helper lazyloading the access is way more convenient than the manual Lib call.
Also, if you as a developer plan on extending and aliasing it having the helper in place makes things easier (as with the other ones).

@markstory
Owner

Bah, I'm sorry. I mixed up the TextHelper and NumberHelper in my head when replying last time.

@ceeram
Collaborator

apart from the separate changes @markstory proposed, what more needs to be done with this?

@markstory
Owner

Nothing as far as I know :smile:

@ceeram
Collaborator

Then i like to merge this into 2.3 if no one objects

@dereuromark
Collaborator

:+1:

@markstory
Owner

@ceeram go for it.

@ceeram ceeram closed this
@ceeram
Collaborator

merged as single commit: f4f4aa4

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Oct 7, 2012
  1. @ceeram

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

    ceeram authored
    …ive calls to CakeNumber::currency() more DRY
  2. @ceeram

    NumberHelper now also uses the default currency from CakeNumber, inst…

    ceeram authored
    …ead of default argument value
Commits on Oct 26, 2012
  1. @ceeram
This page is out of date. Refresh to see the latest.
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;
@ADmad Collaborator
ADmad added a note

Perhaps add a check for valid currency using CakeNumber::$_currencies and return false in case of invalid value.

@ceeram Collaborator
ceeram added a note

That would make it impossible to first set a default and later addFormat(), imo its up to the developer to use a sane value, but if you think it should do the check i can add it

@ADmad Collaborator
ADmad added a note

If someone else shares my opinion add it, else ignore :smiley:

@markstory Owner

Wouldn't setting the default to a value that doesn't exist cause a number of errors downstream?

@ceeram Collaborator
ceeram added a note

sure, just like passing not existing value as 2nd parameter to currency() all the method does is predefine the default value for 2nd parameter, so you dont need to write it out in the application.

For instance i used burzums Cart plugin, this method allows to change the default currency without the need to change the plugin code or use a Configure::write() in the app, and Configure::read() in the plugin.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ }
+ return self::$_defaultCurrency;
+ }
+
}
View
14 lib/Cake/View/Helper/NumberHelper.php
@@ -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);
+ }
+
}
Something went wrong with that request. Please try again.