| 42 | | * Rules for converting an English singular word to plural form |
| 43 | | */ |
| 44 | | private static $plural_rules = |
| 45 | | array( '/(x|ch|ss|sh)$/' => '\1es', # search, switch, fix, box, process, address |
| 46 | | '/series$/' => '\1series', |
| 47 | | '/([^aeiouy]|qu)ies$/' => '\1y', |
| 48 | | '/([^aeiouy]|qu)y$/' => '\1ies', # query, ability, agency |
| 49 | | '/(?:([^f])fe|([lr])f)$/' => '\1\2ves', # half, safe, wife |
| 50 | | '/sis$/' => 'ses', # basis, diagnosis |
| 51 | | '/([ti])um$/' => '\1a', # datum, medium |
| 52 | | '/person$/' => 'people', # person, salesperson |
| 53 | | '/man$/' => 'men', # man, woman, spokesman |
| 54 | | '/child$/' => 'children', # child |
| 55 | | '/(.*)status$/' => '\1statuses', |
| 56 | | '/s$/' => 's', # no change (compatibility) |
| 57 | | '/$/' => 's' |
| 58 | | ); |
| 59 | | |
| 60 | | /** |
| 61 | | * Rules for converting an English plural word to singular form |
| 62 | | */ |
| 63 | | private static $singular_rules = |
| 64 | | array( '/(x|ch|ss)es$/' => '\1', |
| 65 | | '/movies$/' => 'movie', |
| 66 | | '/series$/' => 'series', |
| 67 | | '/([^aeiouy]|qu)ies$/' => '\1y', |
| 68 | | '/([lr])ves$/' => '\1f', |
| 69 | | '/([^f])ves$/' => '\1fe', |
| 70 | | '/(analy|ba|diagno|parenthe|progno|synop|the)ses$/' => '\1sis', |
| 71 | | '/([ti])a$/' => '\1um', |
| 72 | | '/people$/' => 'person', |
| 73 | | '/men$/' => 'man', |
| 74 | | '/(.*)statuses$/' => '\1status', |
| 75 | | '/children$/' => 'child', |
| 76 | | '/news$/' => 'news', |
| 77 | | '/s$/' => '' |
| 78 | | ); |
| 79 | | |
| 80 | | /** |
| 88 | | $original = $word; |
| 89 | | foreach(self::$plural_rules as $rule => $replacement) { |
| 90 | | $word = preg_replace($rule,$replacement,$word); |
| 91 | | if($original != $word) break; |
| | 52 | if(!in_array($word, Inflections::$uncountables)) { |
| | 53 | $original = $word; |
| | 54 | foreach(Inflections::$plurals as $plural_rule) { |
| | 55 | $word = preg_replace($plural_rule['rule'], $plural_rule['replacement'], $word); |
| | 56 | if($original != $word) break; |
| | 57 | } |
| 103 | | $original = $word; |
| 104 | | foreach(self::$singular_rules as $rule => $replacement) { |
| 105 | | $word = preg_replace($rule,$replacement,$word); |
| 106 | | if($original != $word) break; |
| | 69 | if(!in_array($word, Inflections::$uncountables)) { |
| | 70 | $original = $word; |
| | 71 | foreach(Inflections::$singulars as $singular_rule) { |
| | 72 | $word = preg_replace($singular_rule['rule'], $singular_rule['replacement'], $word); |
| | 73 | if($original != $word) break; |
| | 74 | } |
| | 123 | } |
| | 124 | |
| | 125 | /** |
| | 126 | * Convert a word or phrase into a title format "Welcome To My Site" |
| | 127 | * |
| | 128 | * @param string $word A word or phrase |
| | 129 | * @return string A string that has all words capitalized and splits on existing caps. |
| | 130 | */ |
| | 131 | function titleize($word) { |
| | 132 | return preg_replace('/\b([a-z])/', self::capitalize('$1'), self::humanize(self::underscore($word))); |
| | 133 | } |
| | 134 | |
| | 135 | /** |
| | 136 | * Convert a word's underscores into dashes |
| | 137 | * |
| | 138 | * @param string $underscored_word Word to convert |
| | 139 | * @return string All underscores converted to dashes |
| | 140 | */ |
| | 141 | function dasherize($underscored_word) { |
| | 142 | return str_replace('_', '-', self::underscore($underscored_word)); |
| | 178 | |
| | 179 | |
| | 180 | /** |
| | 181 | * Add to a number st, nd, rd, th |
| | 182 | * |
| | 183 | * @param integer $number Number to append to |
| | 184 | * key |
| | 185 | * @return string Number formatted with correct st, nd, rd, or th |
| | 186 | */ |
| | 187 | function ordinalize($number) { |
| | 188 | $test = (intval($number) % 100); |
| | 189 | if($test >= 11 && $test <= 13) { |
| | 190 | $number = "{$number}th"; |
| | 191 | } else { |
| | 192 | switch((intval($number) % 10)) { |
| | 193 | case 1: |
| | 194 | $number = "{$number}st"; |
| | 195 | break; |
| | 196 | case 2: |
| | 197 | $number = "{$number}nd"; |
| | 198 | break; |
| | 199 | case 3: |
| | 200 | $number = "{$number}rd"; |
| | 201 | break; |
| | 202 | default: |
| | 203 | $number = "{$number}th"; |
| | 204 | } |
| | 205 | } |
| | 206 | return $number; |
| | 207 | } |