| 1 | <?php defined('SYSPATH') or die('No direct script access.'); |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | class inflector_Core { |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | |
|---|
| 19 | |
|---|
| 20 | public static function uncountable($str) |
|---|
| 21 | { |
|---|
| 22 | static $uncountables = NULL; |
|---|
| 23 | |
|---|
| 24 | if ($uncountables === NULL) |
|---|
| 25 | { |
|---|
| 26 | |
|---|
| 27 | $uncountables = array_flip(Kohana::lang('inflector')); |
|---|
| 28 | } |
|---|
| 29 | |
|---|
| 30 | return isset($uncountables[$str]); |
|---|
| 31 | } |
|---|
| 32 | |
|---|
| 33 | |
|---|
| 34 | |
|---|
| 35 | |
|---|
| 36 | |
|---|
| 37 | |
|---|
| 38 | |
|---|
| 39 | public static function singular($str, $count = NULL) |
|---|
| 40 | { |
|---|
| 41 | static $cache; |
|---|
| 42 | |
|---|
| 43 | $str = trim($str); |
|---|
| 44 | |
|---|
| 45 | |
|---|
| 46 | $key = $str.$count; |
|---|
| 47 | |
|---|
| 48 | |
|---|
| 49 | if (inflector::uncountable($str)) |
|---|
| 50 | return $str; |
|---|
| 51 | |
|---|
| 52 | if (is_string($count) AND ctype_digit($count)) |
|---|
| 53 | { |
|---|
| 54 | |
|---|
| 55 | $count = (int) $count; |
|---|
| 56 | } |
|---|
| 57 | |
|---|
| 58 | if ($cache === NULL) |
|---|
| 59 | { |
|---|
| 60 | |
|---|
| 61 | $cache = array(); |
|---|
| 62 | } |
|---|
| 63 | else |
|---|
| 64 | { |
|---|
| 65 | |
|---|
| 66 | if (isset($cache[$key])) |
|---|
| 67 | return $cache[$key]; |
|---|
| 68 | } |
|---|
| 69 | |
|---|
| 70 | |
|---|
| 71 | if (is_int($count) AND $count === 0 OR $count > 1) |
|---|
| 72 | return $str; |
|---|
| 73 | |
|---|
| 74 | $end = substr($str, -3); |
|---|
| 75 | |
|---|
| 76 | if ($end == 'ies') |
|---|
| 77 | { |
|---|
| 78 | $str = substr($str, 0, strlen($str) - 3).'y'; |
|---|
| 79 | } |
|---|
| 80 | elseif (substr($str, -4)=='sses' OR $end == 'xes') |
|---|
| 81 | { |
|---|
| 82 | $str = substr($str, 0, strlen($str) - 2); |
|---|
| 83 | } |
|---|
| 84 | elseif (substr($str, -1) == 's') |
|---|
| 85 | { |
|---|
| 86 | $str = substr($str, 0, strlen($str) - 1); |
|---|
| 87 | } |
|---|
| 88 | |
|---|
| 89 | return $cache[$key] = $str; |
|---|
| 90 | } |
|---|
| 91 | |
|---|
| 92 | |
|---|
| 93 | |
|---|
| 94 | |
|---|
| 95 | |
|---|
| 96 | |
|---|
| 97 | |
|---|
| 98 | public static function plural($str, $count = NULL) |
|---|
| 99 | { |
|---|
| 100 | static $cache; |
|---|
| 101 | |
|---|
| 102 | $str = trim($str); |
|---|
| 103 | |
|---|
| 104 | |
|---|
| 105 | $key = $str.$count; |
|---|
| 106 | |
|---|
| 107 | |
|---|
| 108 | if (inflector::uncountable($str)) |
|---|
| 109 | return $str; |
|---|
| 110 | |
|---|
| 111 | if (is_string($count) AND ctype_digit($count)) |
|---|
| 112 | { |
|---|
| 113 | |
|---|
| 114 | $count = (int) $count; |
|---|
| 115 | } |
|---|
| 116 | |
|---|
| 117 | if ($cache === NULL) |
|---|
| 118 | { |
|---|
| 119 | |
|---|
| 120 | $cache = array(); |
|---|
| 121 | } |
|---|
| 122 | else |
|---|
| 123 | { |
|---|
| 124 | |
|---|
| 125 | if (isset($cache[$key])) |
|---|
| 126 | return $cache[$key]; |
|---|
| 127 | } |
|---|
| 128 | |
|---|
| 129 | |
|---|
| 130 | if ($count === 1) |
|---|
| 131 | return $str; |
|---|
| 132 | |
|---|
| 133 | $end = substr($str, -1); |
|---|
| 134 | $low = (strcmp($end, strtolower($end)) === 0) ? TRUE : FALSE; |
|---|
| 135 | |
|---|
| 136 | if (preg_match('/[sxz]$/i', $str) OR preg_match('/[^aeioudgkprt]h$/i', $str)) |
|---|
| 137 | { |
|---|
| 138 | $end = 'es'; |
|---|
| 139 | $str .= ($low == FALSE) ? strtoupper($end) : $end; |
|---|
| 140 | } |
|---|
| 141 | elseif (preg_match('/[^aeiou]y$/i', $str)) |
|---|
| 142 | { |
|---|
| 143 | $end = 'ies'; |
|---|
| 144 | $end = ($low == FALSE) ? strtoupper($end) : $end; |
|---|
| 145 | $str = substr_replace($str, $end, -1); |
|---|
| 146 | } |
|---|
| 147 | else |
|---|
| 148 | { |
|---|
| 149 | $end = 's'; |
|---|
| 150 | $str .= ($low == FALSE) ? strtoupper($end) : $end; |
|---|
| 151 | } |
|---|
| 152 | |
|---|
| 153 | |
|---|
| 154 | return $cache[$key] = $str; |
|---|
| 155 | } |
|---|
| 156 | |
|---|
| 157 | |
|---|
| 158 | |
|---|
| 159 | |
|---|
| 160 | |
|---|
| 161 | |
|---|
| 162 | |
|---|
| 163 | public static function camelize($str) |
|---|
| 164 | { |
|---|
| 165 | $str = 'x'.strtolower(trim($str)); |
|---|
| 166 | $str = ucwords(preg_replace('/[\s_]+/', ' ', $str)); |
|---|
| 167 | |
|---|
| 168 | return substr(str_replace(' ', '', $str), 1); |
|---|
| 169 | } |
|---|
| 170 | |
|---|
| 171 | |
|---|
| 172 | |
|---|
| 173 | |
|---|
| 174 | |
|---|
| 175 | |
|---|
| 176 | |
|---|
| 177 | public static function underscore($str) |
|---|
| 178 | { |
|---|
| 179 | return preg_replace('/\s+/', '_', trim($str)); |
|---|
| 180 | } |
|---|
| 181 | |
|---|
| 182 | |
|---|
| 183 | |
|---|
| 184 | |
|---|
| 185 | |
|---|
| 186 | |
|---|
| 187 | |
|---|
| 188 | public static function humanize($str) |
|---|
| 189 | { |
|---|
| 190 | return preg_replace('/[_-]+/', ' ', trim($str)); |
|---|
| 191 | } |
|---|
| 192 | |
|---|
| 193 | } |
|---|