Pārlūkot izejas kodu

[GENERIC] Zend_Currency:

- added locale option
- fixed some unittests for to CLDR 1.7.1

git-svn-id: http://framework.zend.com/svn/framework/standard/trunk@18477 44c647ce-9c0f-0410-b52a-842ac1e357ba
thomas 16 gadi atpakaļ
vecāks
revīzija
196e28962b
2 mainītis faili ar 45 papildinājumiem un 36 dzēšanām
  1. 24 27
      library/Zend/Currency.php
  2. 21 9
      tests/Zend/CurrencyTest.php

+ 24 - 27
library/Zend/Currency.php

@@ -48,13 +48,6 @@ class Zend_Currency
     const LEFT     = 32;
 
     /**
-     * Locale for this currency
-     *
-     * @var string
-     */
-    private $_locale = null;
-
-    /**
      * Options array
      *
      * The following options are available
@@ -66,6 +59,7 @@ class Zend_Currency
      * 'name'      => Name for this currency
      * 'currency'  => 3 lettered international abbreviation
      * 'symbol'    => Currency symbol
+     * 'locale'    => Locale for this currency
      *
      * @var array
      * @see Zend_Locale
@@ -78,7 +72,8 @@ class Zend_Currency
         'precision' => 2,
         'name'      => null,
         'currency'  => null,
-        'symbol'    => null
+        'symbol'    => null,
+        'locale'    => null
     );
 
     /**
@@ -99,9 +94,9 @@ class Zend_Currency
         $this->setLocale($locale);
 
         // Get currency details
-        $this->_options['currency'] = self::getShortName($currency, $this->_locale);
-        $this->_options['name']     = self::getName($currency, $this->_locale);
-        $this->_options['symbol']   = self::getSymbol($currency, $this->_locale);
+        $this->_options['currency'] = self::getShortName($currency, $this->_options['locale']);
+        $this->_options['name']     = self::getName($currency, $this->_options['locale']);
+        $this->_options['symbol']   = self::getSymbol($currency, $this->_options['locale']);
 
         if (($this->_options['currency'] === null) and ($this->_options['name'] === null)) {
             require_once 'Zend/Currency/Exception.php';
@@ -133,23 +128,23 @@ class Zend_Currency
             throw new Zend_Currency_Exception("Value '$value' has to be numeric");
         }
 
-        if (!isset($options['display'])) {
-            if (isset($options['symbol'])) {
-                $options['display'] = Zend_Currency::USE_SYMBOL;
-            } else if (isset($options['currency'])) {
-                $options['display'] = Zend_Currency::USE_SHORTNAME;
-            } else if (isset($options['name'])) {
-                $options['display'] = Zend_Currency::USE_NAME;
+        if (isset($options['currency'])) {
+            if (!isset($options['locale'])) {
+                $options['locale'] = $this->_options['locale'];
             }
+
+            $options['currency'] = self::getShortName($options['currency'], $options['locale']);
+            $options['name']     = self::getName($options['currency'], $options['locale']);
+            $options['symbol']   = self::getSymbol($options['currency'], $options['locale']);
         }
 
         $options = $this->_checkOptions($options) + $this->_options;
 
         // Format the number
         $format = $options['format'];
-        $locale = $this->_locale;
+        $locale = $options['locale'];
         if (empty($format)) {
-            $format = Zend_Locale_Data::getContent($this->_locale, 'currencynumber');
+            $format = Zend_Locale_Data::getContent($locale, 'currencynumber');
         } else if (Zend_Locale::isLocale($format, true, false)) {
             $locale = $format;
             $format = Zend_Locale_Data::getContent($format, 'currencynumber');
@@ -425,8 +420,8 @@ class Zend_Currency
     public function getCurrencyList($region = null)
     {
         if (empty($region) === true) {
-            if (strlen($this->_locale) > 4) {
-                $region = substr($this->_locale, (strpos($this->_locale, '_') + 1));
+            if (strlen($this->_options['locale']) > 4) {
+                $region = substr($this->_options['locale'], (strpos($this->_options['locale'], '_') + 1));
             }
         }
 
@@ -518,16 +513,16 @@ class Zend_Currency
     {
         require_once 'Zend/Locale.php';
         try {
-            $this->_locale = Zend_Locale::findLocale($locale);
+            $this->_options['locale'] = Zend_Locale::findLocale($locale);
         } catch (Zend_Locale_Exception $e) {
             require_once 'Zend/Currency/Exception.php';
             throw new Zend_Currency_Exception($e->getMessage());
         }
 
         // Get currency details
-        $this->_options['currency'] = $this->getShortName(null, $this->_locale);
-        $this->_options['name']     = $this->getName(null, $this->_locale);
-        $this->_options['symbol']   = $this->getSymbol(null, $this->_locale);
+        $this->_options['currency'] = $this->getShortName(null, $this->_options['locale']);
+        $this->_options['name']     = $this->getName(null, $this->_options['locale']);
+        $this->_options['symbol']   = $this->getSymbol(null, $this->_options['locale']);
 
         return $this;
     }
@@ -539,7 +534,7 @@ class Zend_Currency
      */
     public function getLocale()
     {
-        return $this->_locale;
+        return $this->_options['locale'];
     }
 
     /**
@@ -618,6 +613,8 @@ class Zend_Currency
                     // Break intentionally omitted
                 case 'currency':
                     // Break intentionally omitted
+                case 'locale':
+                    // Break intentionally omitted
                 case 'symbol':
                     // Unchecked options
                     break;

+ 21 - 9
tests/Zend/CurrencyTest.php

@@ -294,7 +294,7 @@ class Zend_CurrencyTest extends PHPUnit_Framework_TestCase
         $this->assertSame('$ 53.292,18', $USD->toCurrency(53292.18, array('format' => 'de_AT')));
 
         $this->assertSame('ج.م.‏ 53.292,18', $EGP->toCurrency(53292.18));
-        $this->assertSame('ج.م.‏ ٥٣٬٢٩٢٫١٨', $EGP->toCurrency(53292.18, array('script' => 'Arab' )));
+        $this->assertSame('ج.م.‏ ٥٣.٢٩٢,١٨', $EGP->toCurrency(53292.18, array('script' => 'Arab' )));
         $this->assertSame('ج.م.‏ ٥٣.٢٩٢,١٨', $EGP->toCurrency(53292.18, array('script' =>'Arab', 'format' => 'de_AT')));
         $this->assertSame('ج.م.‏ 53.292,18', $EGP->toCurrency(53292.18, array('format' => 'de_AT')));
 
@@ -308,10 +308,10 @@ class Zend_CurrencyTest extends PHPUnit_Framework_TestCase
         }
 
         $INR = new Zend_Currency('INR', 'de_AT');
-        $this->assertSame(' 1,20', $INR->toCurrency(1.2));
-        $this->assertSame(' 1,00', $INR->toCurrency(1));
-        $this->assertSame(' 0,00', $INR->toCurrency(0));
-        $this->assertSame('- 3,00', $INR->toCurrency(-3));
+        $this->assertSame('Rs 1,20', $INR->toCurrency(1.2));
+        $this->assertSame('Rs 1,00', $INR->toCurrency(1));
+        $this->assertSame('Rs 0,00', $INR->toCurrency(0));
+        $this->assertSame('-Rs 3,00', $INR->toCurrency(-3));
     }
 
     /**
@@ -454,12 +454,12 @@ class Zend_CurrencyTest extends PHPUnit_Framework_TestCase
         $locale   = new Zend_Locale('ar_EG');
         $currency = new Zend_Currency('ar_EG');
 
-        $this->assertSame('جنيه مصرى',       $currency->getName('EGP','ar_EG'));
+        $this->assertSame('جنيه مصري',       $currency->getName('EGP','ar_EG'));
         $this->assertSame('Estnische Krone', $currency->getName('EEK','de_AT'));
-        $this->assertSame('جنيه مصرى',       $currency->getName('EGP',$locale));
-        $this->assertSame('جنيه مصرى',       $currency->getName('ar_EG'      ));
+        $this->assertSame('جنيه مصري',       $currency->getName('EGP',$locale));
+        $this->assertSame('جنيه مصري',       $currency->getName('ar_EG'      ));
         $this->assertSame('Euro',            $currency->getName('de_AT'      ));
-        $this->assertSame('جنيه مصرى',       $currency->getName());
+        $this->assertSame('جنيه مصري',       $currency->getName());
 
         try {
             $currency->getName('EGP', 'xy_XY');
@@ -619,4 +619,16 @@ class Zend_CurrencyTest extends PHPUnit_Framework_TestCase
         $this->assertEquals('$0.00', $currency->toCurrency(1.0E-4));
         $this->assertEquals('$0.00', $currency->toCurrency(1.0E-5));
     }
+
+    /**
+     * @see ZF-7864
+     */
+    public function testCurrencyToToCurrency()
+    {
+        $currency = new Zend_Currency("de_DE");
+        $this->assertEquals('2,3000 $', $currency->toCurrency(2.3, array('currency' => 'USD', 'precision' => 4)));
+
+        $currency = new Zend_Currency("USD", "de_DE");
+        $this->assertEquals('2,3000 $', $currency->toCurrency(2.3, array('precision' => 4)));
+    }
 }