Browse Source

Allow configuring number formatters.

Refs #7193
ADmad 10 years ago
parent
commit
ff389359ae
2 changed files with 58 additions and 7 deletions
  1. 39 7
      src/I18n/Number.php
  2. 19 0
      tests/TestCase/I18n/NumberTest.php

+ 39 - 7
src/I18n/Number.php

@@ -281,19 +281,51 @@ class Number
         }
 
         $formatter = static::$_formatters[$locale][$type];
-        $hasPlaces = isset($options['places']);
-        $hasPrecision = isset($options['precision']);
-        $hasPattern = !empty($options['pattern']) || !empty($options['useIntlCode']);
 
-        if ($hasPlaces || $hasPrecision || $hasPattern) {
-            $formatter = clone $formatter;
+        $options = array_intersect_key($options, [
+            'places' => null,
+            'precision' => null,
+            'pattern' => null,
+            'useIntlCode' => null
+        ]);
+        if (empty($options)) {
+            return $formatter;
         }
 
-        if ($hasPlaces) {
+        $formatter = clone $formatter;
+        return static::_setAttributes($formatter, $options);
+    }
+
+    /**
+     * Configure formatters.
+     *
+     * @param string $locale The locale name to use for formatting the number, e.g. fr_FR
+     * @param int $type The formatter type to construct. Defaults to NumberFormatter::DECIMAL.
+     * @param array $options See Number::formatter() for possible options.
+     * @return void
+     */
+    public static function config($locale, $type = NumberFormatter::DECIMAL, array $options = [])
+    {
+        static::$_formatters[$locale][$type] = static::_setAttributes(
+            new NumberFormatter($locale, $type),
+            $options
+        );
+    }
+
+    /**
+     * Set formatter attributes
+     *
+     * @param \NumberFormatter $formatter Number formatter instance.
+     * @param array $options See Number::formatter() for possible options.
+     * @return \NumberFormatter
+     */
+    protected static function _setAttributes(NumberFormatter $formatter, array $options = [])
+    {
+        if (isset($options['places'])) {
             $formatter->setAttribute(NumberFormatter::MIN_FRACTION_DIGITS, $options['places']);
         }
 
-        if ($hasPrecision) {
+        if (isset($options['precision'])) {
             $formatter->setAttribute(NumberFormatter::MAX_FRACTION_DIGITS, $options['precision']);
         }
 

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

@@ -538,4 +538,23 @@ class NumberTest extends TestCase
         $result = $this->Number->toReadableSize(512.05 * 1024 * 1024 * 1024);
         $this->assertEquals('512,05 GB', $result);
     }
+
+    /**
+     * test config()
+     *
+     * @return void
+     */
+    public function testConfig()
+    {
+        $result = $this->Number->currency(15000, 'INR', ['locale' => 'en_IN']);
+        $this->assertEquals('₹ 15,000.00', $result);
+
+        Number::config('en_IN', \NumberFormatter::CURRENCY, [
+            'pattern' => '¤ #,##,##0',
+            'persistOptions' => true
+        ]);
+
+        $result = $this->Number->currency(15000, 'INR', ['locale' => 'en_IN']);
+        $this->assertEquals('₹ 15,000', $result);
+    }
 }