Changeset 2883

Show
Ignore:
Timestamp:
06/23/08 19:35:44 (2 months ago)
Author:
Shadowhand
Message:

Updated inflector:

  • Removed all i18n inflector files
  • Created an inflector configuration file
  • Added "irregular" support to inflector
  • Added some extra uncountable defintions

This fixes #649

Location:
trunk/system
Files:
1 added
14 removed
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/system/helpers/inflector.php

    r2512 r2883  
    1212class inflector_Core { 
    1313 
     14    // Cached inflections 
     15    protected static $cache = array(); 
     16 
     17    // Uncountable and irregular words 
     18    protected static $uncountable; 
     19    protected static $irregular; 
     20 
    1421    /** 
    1522     * Checks if a word is defined as uncountable. 
     
    2027    public static function uncountable($str) 
    2128    { 
    22         static $uncountables = NULL; 
    23  
    24         if ($uncountables === NULL) 
    25         { 
    26             // Makes a mirrored array, eg: foo => foo 
    27             $uncountables = array_flip(Kohana::lang('inflector')); 
     29        if (self::$uncountable === NULL) 
     30        { 
     31            // Cache uncountables 
     32            self::$uncountable = Config::item('inflector.uncountable'); 
     33 
     34            // Make uncountables mirroed 
     35            self::$uncountable = array_combine(self::$uncountable, self::$uncountable); 
    2836        } 
    2937 
     
    3442     * Makes a plural word singular. 
    3543     * 
    36      * @param   string  word to singularize 
     44     * @param   string   word to singularize 
     45     * @param   integer  number of things 
    3746     * @return  string 
    3847     */ 
    3948    public static function singular($str, $count = NULL) 
    4049    { 
    41         static $cache; 
    42  
     50        // Remove garbage 
    4351        $str = trim($str); 
     52 
     53        if (is_string($count)) 
     54        { 
     55            // Convert to integer when using a digit string 
     56            $count = (int) $count; 
     57        } 
     58 
     59        // Do nothing with a single count 
     60        if ($count === 0 OR $count > 1) 
     61            return $str; 
    4462 
    4563        // Cache key name 
    4664        $key = $str.$count; 
    4765 
    48         // We can just return uncountable words 
     66        if (isset(self::$cache[$key])) 
     67            return self::$cache[$key]; 
     68 
    4969        if (inflector::uncountable($str)) 
    50             return $str; 
    51  
    52         if (is_string($count) AND ctype_digit($count)) 
     70            return self::$cache[$key] = $str; 
     71 
     72        if (empty(self::$irregular)) 
     73        { 
     74            // Cache irregular words 
     75            self::$irregular = Config::item('inflector.irregular'); 
     76        } 
     77 
     78        if ($irregular = array_search($str, self::$irregular)) 
     79        { 
     80            $str = $irregular; 
     81        } 
     82        elseif (substr($str, -3) === 'ies') 
     83        { 
     84            $str = substr($str, 0, strlen($str) - 3).'y'; 
     85        } 
     86        elseif (substr($str, -4) === 'sses' OR substr($str, -3) === 'xes') 
     87        { 
     88            $str = substr($str, 0, strlen($str) - 2); 
     89        } 
     90        elseif (substr($str, -1) === 's') 
     91        { 
     92            $str = substr($str, 0, strlen($str) - 1); 
     93        } 
     94 
     95        return self::$cache[$key] = $str; 
     96    } 
     97 
     98    /** 
     99     * Makes a singular word plural. 
     100     * 
     101     * @param   string  word to pluralize 
     102     * @return  string 
     103     */ 
     104    public static function plural($str, $count = NULL) 
     105    { 
     106        // Remove garbage 
     107        $str = trim($str); 
     108 
     109        if (is_string($count)) 
    53110        { 
    54111            // Convert to integer when using a digit string 
     
    56113        } 
    57114 
    58         if ($cache === NULL) 
    59         { 
    60             // Initialize the cache 
    61             $cache = array(); 
    62         } 
    63         else 
    64         { 
    65             // Already pluralized 
    66             if (isset($cache[$key])) 
    67                 return $cache[$key]; 
    68         } 
    69  
    70         // Do nothing with a single count 
    71         if (is_int($count) AND $count === 0 OR $count > 1) 
     115        // Do nothing with singular 
     116        if ($count === 1) 
    72117            return $str; 
    73  
    74         if (substr($str, -3) === 'ies') 
    75         { 
    76             $str = substr($str, 0, strlen($str) - 3).'y'; 
    77         } 
    78         elseif (substr($str, -4) === 'sses' OR substr($str, -3) === 'xes') 
    79         { 
    80             $str = substr($str, 0, strlen($str) - 2); 
    81         } 
    82         elseif (substr($str, -1) === 's') 
    83         { 
    84             $str = substr($str, 0, strlen($str) - 1); 
    85         } 
    86  
    87         return $cache[$key] = $str; 
    88     } 
    89  
    90     /** 
    91      * Makes a singular word plural. 
    92      * 
    93      * @param   string  word to pluralize 
    94      * @return  string 
    95      */ 
    96     public static function plural($str, $count = NULL) 
    97     { 
    98         static $cache; 
    99  
    100         $str = trim($str); 
    101118 
    102119        // Cache key name 
    103120        $key = $str.$count; 
    104121 
    105         // We can just return uncountable words 
     122        if (isset(self::$cache[$key])) 
     123            return self::$cache[$key]; 
     124 
    106125        if (inflector::uncountable($str)) 
    107             return $str; 
    108  
    109         if (is_string($count) AND ctype_digit($count)) 
    110         { 
    111             // Convert to integer when using a digit string 
    112             $count = (int) $count; 
    113         } 
    114  
    115         if ($cache === NULL) 
    116         { 
    117             // Initialize the cache 
    118             $cache = array(); 
    119         } 
    120         else 
    121         { 
    122             // Already pluralized 
    123             if (isset($cache[$key])) 
    124                 return $cache[$key]; 
    125         } 
    126  
    127         // If the count is one, do not pluralize 
    128         if ($count === 1) 
    129             return $str; 
     126            return self::$cache[$key] = $str; 
    130127 
    131128        $end = substr($str, -1); 
    132129        $low = (strcmp($end, strtolower($end)) === 0) ? TRUE : FALSE; 
    133130 
    134         if (preg_match('/[sxz]$/i', $str) OR preg_match('/[^aeioudgkprt]h$/i', $str)) 
     131        if (empty(self::$irregular)) 
     132        { 
     133            // Cache irregular words 
     134            self::$irregular = Config::item('inflector.irregular'); 
     135        } 
     136 
     137        if (isset(self::$irregular[strtolower($str)])) 
     138        { 
     139            $str = self::$irregular[$str]; 
     140 
     141            if ($low === FALSE) 
     142            { 
     143                // Make uppercase 
     144                $str = strtoupper($str); 
     145            } 
     146        } 
     147        elseif (preg_match('/[sxz]$/i', $str) OR preg_match('/[^aeioudgkprt]h$/i', $str)) 
    135148        { 
    136149            $end = 'es';