- Timestamp:
- 06/23/2008 07:37:34 PM (5 months ago)
- Location:
- tags/2.1.3/system
- Files:
-
- 13 removed
- 1 modified
- 1 copied
-
config/inflector.php (copied) (copied from trunk/system/config/inflector.php)
-
helpers/inflector.php (modified) (4 diffs)
-
i18n/13_37/inflector.php (deleted)
-
i18n/de_DE/inflector.php (deleted)
-
i18n/en_GB/inflector.php (deleted)
-
i18n/en_US/inflector.php (deleted)
-
i18n/es_AR/inflector.php (deleted)
-
i18n/es_ES/inflector.php (deleted)
-
i18n/fr_FR/inflector.php (deleted)
-
i18n/it_IT/inflector.php (deleted)
-
i18n/mk_MK/inflector.php (deleted)
-
i18n/nl_NL/inflector.php (deleted)
-
i18n/pl_PL/inflector.php (deleted)
-
i18n/pt_BR/inflector.php (deleted)
-
i18n/zh_CN/inflector.php (deleted)
Legend:
- Unmodified
- Added
- Removed
-
tags/2.1.3/system/helpers/inflector.php
r2513 r2884 12 12 class inflector_Core { 13 13 14 // Cached inflections 15 protected static $cache = array(); 16 17 // Uncountable and irregular words 18 protected static $uncountable; 19 protected static $irregular; 20 14 21 /** 15 22 * Checks if a word is defined as uncountable. … … 20 27 public static function uncountable($str) 21 28 { 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); 28 36 } 29 37 … … 34 42 * Makes a plural word singular. 35 43 * 36 * @param string word to singularize 44 * @param string word to singularize 45 * @param integer number of things 37 46 * @return string 38 47 */ 39 48 public static function singular($str, $count = NULL) 40 49 { 41 static $cache; 42 50 // Remove garbage 43 51 $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; 44 62 45 63 // Cache key name 46 64 $key = $str.$count; 47 65 48 // We can just return uncountable words 66 if (isset(self::$cache[$key])) 67 return self::$cache[$key]; 68 49 69 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)) 53 110 { 54 111 // Convert to integer when using a digit string … … 56 113 } 57 114 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) 72 117 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 pluralize94 * @return string95 */96 public static function plural($str, $count = NULL)97 {98 static $cache;99 100 $str = trim($str);101 118 102 119 // Cache key name 103 120 $key = $str.$count; 104 121 105 // We can just return uncountable words 122 if (isset(self::$cache[$key])) 123 return self::$cache[$key]; 124 106 125 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; 130 127 131 128 $end = substr($str, -1); 132 129 $low = (strcmp($end, strtolower($end)) === 0) ? TRUE : FALSE; 133 130 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)) 135 148 { 136 149 $end = 'es';
