Browse Source

Address suggestions, including updating pre-existing code to match new standards

gregs 6 years ago
parent
commit
743f503187
2 changed files with 32 additions and 23 deletions
  1. 16 23
      src/I18n/Number.php
  2. 16 0
      tests/TestCase/I18n/NumberTest.php

+ 16 - 23
src/I18n/Number.php

@@ -27,29 +27,22 @@ class Number
 {
     /**
      * Default locale
-     *
-     * @var string
      */
     const DEFAULT_LOCALE = 'en_US';
 
     /**
      * Format type to format as currency
-     *
-     * @var string
      */
     const FORMAT_CURRENCY = 'currency';
 
     /**
-`     * Format type to format as currency, accounting style (negative numbers in parentheses)
-     *
-     * @var string
+     * Format type to format as currency, accounting style (negative numbers in parentheses)
      */
     const FORMAT_CURRENCY_ACCOUNTING = 'currency_accounting';
 
     /**
-     * ICU Constant for accounting format; not yet supported by INTL library
-     *
-     * @var int
+     * ICU Constant for accounting format; not yet supported by INTL library.
+     * See UNUM_CURRENCY_ACCOUNTING in https://unicode-org.github.io/icu-docs/apidoc/released/icu4c/unum_8h.html
      */
     const CURRENCY_ACCOUNTING = 12;
 
@@ -267,46 +260,46 @@ class Number
      */
     public static function defaultCurrency($currency = null)
     {
-        if (!empty($currency)) {
-            return self::$_defaultCurrency = $currency;
+        if ($currency !== null && $currency !== false) {
+            return static::$_defaultCurrency = $currency;
         }
 
         if ($currency === false) {
-            return self::$_defaultCurrency = null;
+            return static::$_defaultCurrency = null;
         }
 
-        if (empty(self::$_defaultCurrency)) {
+        if (static::$_defaultCurrency === null) {
             $locale = ini_get('intl.default_locale') ?: static::DEFAULT_LOCALE;
             $formatter = new NumberFormatter($locale, NumberFormatter::CURRENCY);
-            self::$_defaultCurrency = $formatter->getTextAttribute(NumberFormatter::CURRENCY_CODE);
+            static::$_defaultCurrency = $formatter->getTextAttribute(NumberFormatter::CURRENCY_CODE);
         }
 
-        return self::$_defaultCurrency;
+        return static::$_defaultCurrency;
     }
 
     /**
      * Getter/setter for default currency format
      *
-     * @param null $currencyFormat Default currency format to be used by currency()
+     * @param string|bool|null $currencyFormat Default currency format to be used by currency()
      * if $currencyFormat argument is not provided. If boolean false is passed, it will clear the
      * currently stored value
      * @return string CurrencyFormat
      */
     public static function defaultCurrencyFormat($currencyFormat = null)
     {
-        if (!empty($currencyFormat)) {
-            return self::$_defaultCurrencyFormat = $currencyFormat;
+        if ($currencyFormat !== null && $currencyFormat !== false) {
+            return static::$_defaultCurrencyFormat = $currencyFormat;
         }
 
         if ($currencyFormat === false) {
-            return self::$_defaultCurrencyFormat = null;
+            return static::$_defaultCurrencyFormat = null;
         }
 
-        if (empty(self::$_defaultCurrencyFormat)) {
-            self::$_defaultCurrencyFormat = static::FORMAT_CURRENCY;
+        if (static::$_defaultCurrencyFormat === null) {
+            static::$_defaultCurrencyFormat = static::FORMAT_CURRENCY;
         }
 
-        return self::$_defaultCurrencyFormat;
+        return static::$_defaultCurrencyFormat;
     }
 
     /**

+ 16 - 0
tests/TestCase/I18n/NumberTest.php

@@ -49,6 +49,7 @@ class NumberTest extends TestCase
         unset($this->Number);
         I18n::setLocale($this->locale);
         Number::defaultCurrency(false);
+        Number::defaultCurrencyFormat(false);
     }
 
     /**
@@ -326,6 +327,21 @@ class NumberTest extends TestCase
     }
 
     /**
+     * Test default currency format
+     *
+     * @return void
+     */
+    public function testDefaultCurrencyFormat()
+    {
+        $this->assertEquals('currency', $this->Number->defaultCurrencyFormat());
+
+        $this->Number->defaultCurrencyFormat(Number::FORMAT_CURRENCY_ACCOUNTING);
+        $this->assertEquals('currency_accounting', $this->Number->defaultCurrencyFormat());
+
+        $this->assertEquals('($123.45)', $this->Number->currency(-123.45));
+    }
+
+    /**
      * testCurrencyCentsNegative method
      *
      * @return void