first pass at refactoring these methods
authorChuck Hagenbuch <chuck@horde.org>
Wed, 19 Nov 2008 04:49:41 +0000 (23:49 -0500)
committerChuck Hagenbuch <chuck@horde.org>
Wed, 26 Nov 2008 22:59:46 +0000 (17:59 -0500)
framework/Horde_Date_Parser/lib/Horde/Support/Numerizer/Locale/Base.php
framework/Horde_Date_Parser/lib/Horde/Support/Numerizer/Locale/De.php

index 9701279..b6cc380 100644 (file)
@@ -48,17 +48,49 @@ class Horde_Support_Numerizer_Locale_Base
     public function numerize($string)
     {
         // preprocess
-        // will mutilate hyphenated-words but shouldn't matter for date extraction
-        $string = preg_replace('/ +|([^\d])-([^d])/', '$1 $2', $string);
-        // take the 'a' out so it doesn't turn into a 1, save the half for the end
-        $string = str_replace('a half', 'haAlf', $string);
+        $string = $this->_splitHyphenatedWords($string);
+        $string = $this->_hideAHalf($string);
 
-        // easy/direct replacements
+        $string = $this->_directReplacements($string);
+        $string = $this->_replaceTenPrefixes($string);
+        $string = $this->_replaceBigPrefixes($string);
+        $string = $this->_fractionalAddition($string);
+
+        return $string;
+    }
+
+    /**
+     * will mutilate hyphenated-words but shouldn't matter for date extraction
+     */
+    protected function _splitHyphenatedWords($string)
+    {
+        return preg_replace('/ +|([^\d])-([^d])/', '$1 $2', $string);
+    }
+
+    /**
+     * take the 'a' out so it doesn't turn into a 1, save the half for the end
+     */
+    protected function _hideAHalf($string)
+    {
+        return str_replace('a half', 'haAlf', $string);
+    }
+
+    /**
+     * easy/direct replacements
+     */
+    protected function _directReplacements($string)
+    {
         foreach ($this->DIRECT_NUMS as $dn => $dn_replacement) {
             $string = preg_replace("/$dn/i", $dn_replacement, $string);
         }
+        return $string;
+    }
 
-        // ten, twenty, etc.
+    /**
+     * ten, twenty, etc.
+     */
+    protected function _replaceTenPrefixes($string)
+    {
         foreach ($this->TEN_PREFIXES as $tp => $tp_replacement) {
             $string = preg_replace_callback(
                 "/(?:$tp)( *\d(?=[^\d]|\$))*/i",
@@ -68,8 +100,14 @@ class Horde_Support_Numerizer_Locale_Base
                 ),
                 $string);
         }
+        return $string;
+    }
 
-        // hundreds, thousands, millions, etc.
+    /**
+     * hundreds, thousands, millions, etc.
+     */
+    protected function _replaceBigPrefixes($string)
+    {
         foreach ($this->BIG_PREFIXES as $bp => $bp_replacement) {
             $string = preg_replace_callback(
                 '/(\d*) *' . $bp . '/i',
@@ -80,18 +118,6 @@ class Horde_Support_Numerizer_Locale_Base
                 $string);
             $string = $this->_andition($string);
         }
-
-        // fractional addition
-        // I'm not combining this with the previous block as using float addition complicates the strings
-        // (with extraneous .0's and such )
-        $string = preg_replace_callback(
-            '/(\d+)(?: | and |-)*haAlf/i',
-            create_function(
-                '$m',
-                'return (string)((float)$m[1] + 0.5);'
-            ),
-            $string);
-
         return $string;
     }
 
@@ -109,4 +135,15 @@ class Horde_Support_Numerizer_Locale_Base
         return $string;
     }
 
+    protected function _fractionalAddition($string)
+    {
+        return preg_replace_callback(
+            '/(\d+)(?: | and |-)*haAlf/i',
+            create_function(
+                '$m',
+                'return (string)((float)$m[1] + 0.5);'
+            ),
+            $string);
+    }
+
 }
index cd4d259..597478c 100644 (file)
@@ -1,7 +1,6 @@
 <?php
 class Horde_Support_Numerizer_Locale_De extends Horde_Support_Numerizer_Locale_Base
 {
-
     public $DIRECT_NUMS = array(
         'dreizehn' => 13,
         'vierzehn' => 14,
@@ -45,4 +44,33 @@ class Horde_Support_Numerizer_Locale_De extends Horde_Support_Numerizer_Locale_B
         'billion' => 1000000000000,
     );
 
-}
\ No newline at end of file
+    public function numerize($string)
+    {
+        // preprocess?
+
+        $string = $this->_directReplacements($string);
+        $string = $this->_replaceTenPrefixes($string);
+        $string = $this->_replaceBigPrefixes($string);
+        $string = $this->_fractionalAddition($string);
+
+        return $string;
+    }
+
+    /**
+     * ten, twenty, etc.
+     */
+    protected function _replaceTenPrefixes($string)
+    {
+        foreach ($this->TEN_PREFIXES as $tp => $tp_replacement) {
+            $string = preg_replace_callback(
+                "/(?:$tp)( *\d(?=[^\d]|\$))*/i",
+                create_function(
+                    '$m',
+                    'return ' . $tp_replacement . ' + (isset($m[1]) ? (int)$m[1] : 0);'
+                ),
+                $string);
+        }
+        return $string;
+    }
+
+}