Explorar o código

Consolidate getting and setting encoding in Zend_Locale_Format

Rob Allen %!s(int64=11) %!d(string=hai) anos
pai
achega
425aa48ae2
Modificáronse 1 ficheiros con 42 adicións e 57 borrados
  1. 42 57
      library/Zend/Locale/Format.php

+ 42 - 57
library/Zend/Locale/Format.php

@@ -309,12 +309,8 @@ class Zend_Locale_Format
 
         // Get correct signs for this locale
         $symbols = Zend_Locale_Data::getList($options['locale'], 'symbols');
-        $oenc = iconv_get_encoding('internal_encoding');
-        if (PHP_VERSION_ID < 50600) {
-            iconv_set_encoding('internal_encoding', 'UTF-8');
-        } else {
-            ini_set('default_charset', 'UTF-8');
-        }
+        $oenc = self::_getEncoding();
+        self::_setEncoding('UTF-8');
         
         // Get format
         $format = $options['number_format'];
@@ -349,11 +345,7 @@ class Zend_Locale_Format
         }
 
         if (iconv_strpos($format, '0') === false) {
-            if (PHP_VERSION_ID < 50600) {
-                iconv_set_encoding('internal_encoding', $oenc);
-            } else {
-                ini_set('default_charset', $oenc);
-            }
+            self::_setEncoding($oenc);
             require_once 'Zend/Locale/Exception.php';
             throw new Zend_Locale_Exception('Wrong format... missing 0');
         }
@@ -479,11 +471,7 @@ class Zend_Locale_Format
             }
         }
 
-        if (PHP_VERSION_ID < 50600) {
-            iconv_set_encoding('internal_encoding', $oenc);
-        } else {
-            ini_set('default_charset', $oenc);
-        }
+        self::_setEncoding($oenc);
         return (string) $format;
     }
 
@@ -801,12 +789,8 @@ class Zend_Locale_Format
         $result['date_format'] = $format; // save the format used to normalize $number (convenience)
         $result['locale'] = $options['locale']; // save the locale used to normalize $number (convenience)
 
-        $oenc = iconv_get_encoding('internal_encoding');
-        if (PHP_VERSION_ID < 50600) {
-            iconv_set_encoding('internal_encoding', 'UTF-8');
-        } else {
-            ini_set('default_charset', 'UTF-8');
-        }
+        $oenc = self::_getEncoding();
+        self::_setEncoding('UTF-8');
         $day   = iconv_strpos($format, 'd');
         $month = iconv_strpos($format, 'M');
         $year  = iconv_strpos($format, 'y');
@@ -871,11 +855,7 @@ class Zend_Locale_Format
         }
 
         if (empty($parse)) {
-            if (PHP_VERSION_ID < 50600) {
-                iconv_set_encoding('internal_encoding', $oenc);
-            } else {
-                ini_set('default_charset', $oenc);
-            }
+            self::_setEncoding($oenc);
             require_once 'Zend/Locale/Exception.php';
             throw new Zend_Locale_Exception("Unknown date format, neither date nor time in '" . $format . "' found");
         }
@@ -895,11 +875,7 @@ class Zend_Locale_Format
         preg_match_all('/\d+/u', $number, $splitted);
 
         if (count($splitted[0]) == 0) {
-            if (PHP_VERSION_ID < 50600) {
-                iconv_set_encoding('internal_encoding', $oenc);
-            } else {
-                ini_set('default_charset', $oenc);
-            }
+            self::_setEncoding($oenc);
             require_once 'Zend/Locale/Exception.php';
             throw new Zend_Locale_Exception("No date part in '$date' found.");
         }
@@ -1005,11 +981,7 @@ class Zend_Locale_Format
                 if (($position !== false) and ((iconv_strpos($date, $result['day']) === false) or
                                                (isset($result['year']) and (iconv_strpos($date, $result['year']) === false)))) {
                     if ($options['fix_date'] !== true) {
-                        if (PHP_VERSION_ID < 50600) {
-                            iconv_set_encoding('internal_encoding', $oenc);
-                        } else {
-                            ini_set('default_charset', $oenc);
-                        }
+                        self::_setEncoding($oenc);
                         require_once 'Zend/Locale/Exception.php';
                         throw new Zend_Locale_Exception("Unable to parse date '$date' using '" . $format
                             . "' (false month, $position, $month)");
@@ -1025,11 +997,7 @@ class Zend_Locale_Format
             if (isset($result['day']) and isset($result['year'])) {
                 if ($result['day'] > 31) {
                     if ($options['fix_date'] !== true) {
-                        if (PHP_VERSION_ID < 50600) {
-                            iconv_set_encoding('internal_encoding', $oenc);
-                        } else {
-                            ini_set('default_charset', $oenc);
-                        }
+                        self::_setEncoding($oenc);
                         require_once 'Zend/Locale/Exception.php';
                         throw new Zend_Locale_Exception("Unable to parse date '$date' using '"
                                                       . $format . "' (d <> y)");
@@ -1045,11 +1013,7 @@ class Zend_Locale_Format
             if (isset($result['month']) and isset($result['year'])) {
                 if ($result['month'] > 31) {
                     if ($options['fix_date'] !== true) {
-                        if (PHP_VERSION_ID < 50600) {
-                            iconv_set_encoding('internal_encoding', $oenc);
-                        } else {
-                            ini_set('default_charset', $oenc);
-                        }
+                        self::_setEncoding($oenc);
                         require_once 'Zend/Locale/Exception.php';
                         throw new Zend_Locale_Exception("Unable to parse date '$date' using '"
                                                       . $format . "' (M <> y)");
@@ -1065,11 +1029,7 @@ class Zend_Locale_Format
             if (isset($result['month']) and isset($result['day'])) {
                 if ($result['month'] > 12) {
                     if ($options['fix_date'] !== true || $result['month'] > 31) {
-                        if (PHP_VERSION_ID < 50600) {
-                            iconv_set_encoding('internal_encoding', $oenc);
-                        } else {
-                            ini_set('default_charset', $oenc);
-                        }
+                        self::_setEncoding($oenc);
                         require_once 'Zend/Locale/Exception.php';
                         throw new Zend_Locale_Exception("Unable to parse date '$date' using '"
                                                       . $format . "' (M <> d)");
@@ -1096,11 +1056,7 @@ class Zend_Locale_Format
             }
         }
 
-        if (PHP_VERSION_ID < 50600) {
-            iconv_set_encoding('internal_encoding', $oenc);
-        } else {
-            ini_set('default_charset', $oenc);
-        }
+        self::_setEncoding($oenc);
         return $result;
     }
 
@@ -1319,4 +1275,33 @@ class Zend_Locale_Format
     {
         return (@preg_match('/\pL/u', 'a')) ? true : false;
     }
+
+    /**
+     * Internal method to retrieve the current encoding via the ini setting
+     * default_charset for PHP >= 5.6 or iconv_get_encoding otherwise.
+     *
+     * @return string
+     */
+    protected static function _getEncoding()
+    {
+        $oenc = PHP_VERSION_ID < 50600
+            ? iconv_get_encoding('internal_encoding')
+            : ini_get('default_charset');
+
+    }
+
+    /**
+     * Internal method to set the encoding via the ini setting
+     * default_charset for PHP >= 5.6 or iconv_set_encoding otherwise.
+     *
+     * @return void
+     */
+    protected static function _setEncoding($enc)
+    {
+        if (PHP_VERSION_ID < 50600) {
+            iconv_set_encoding('internal_encoding', $enc);
+        } else {
+            ini_set('default_charset', $enc);
+        }
+    }
 }