Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 1 | <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2 | /** |
| 3 | * CodeIgniter |
| 4 | * |
Greg Aker | 741de1c | 2010-11-10 14:52:57 -0600 | [diff] [blame] | 5 | * An open source application development framework for PHP 5.1.6 or newer |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 6 | * |
| 7 | * @package CodeIgniter |
| 8 | * @author ExpressionEngine Dev Team |
Greg Aker | 0711dc8 | 2011-01-05 10:49:40 -0600 | [diff] [blame] | 9 | * @copyright Copyright (c) 2008 - 2011, EllisLab, Inc. |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 10 | * @license http://codeigniter.com/user_guide/license.html |
| 11 | * @link http://codeigniter.com |
| 12 | * @since Version 1.0 |
| 13 | * @filesource |
| 14 | */ |
| 15 | |
| 16 | // ------------------------------------------------------------------------ |
| 17 | |
| 18 | /** |
| 19 | * CodeIgniter Inflector Helpers |
| 20 | * |
| 21 | * @package CodeIgniter |
| 22 | * @subpackage Helpers |
| 23 | * @category Helpers |
| 24 | * @author ExpressionEngine Dev Team |
| 25 | * @link http://codeigniter.com/user_guide/helpers/directory_helper.html |
| 26 | */ |
| 27 | |
| 28 | |
| 29 | // -------------------------------------------------------------------- |
| 30 | |
| 31 | /** |
| 32 | * Singular |
| 33 | * |
| 34 | * Takes a plural word and makes it singular |
| 35 | * |
| 36 | * @access public |
| 37 | * @param string |
| 38 | * @return str |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 39 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 40 | if ( ! function_exists('singular')) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 41 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 42 | function singular($str) |
| 43 | { |
Phil Sturgeon | 50e57bc | 2011-08-13 10:26:04 -0600 | [diff] [blame^] | 44 | $result = strval($str); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 45 | |
Phil Sturgeon | 50e57bc | 2011-08-13 10:26:04 -0600 | [diff] [blame^] | 46 | $singular_rules = array( |
| 47 | '/(matr)ices$/' => '\1ix', |
| 48 | '/(vert|ind)ices$/' => '\1ex', |
| 49 | '/^(ox)en/' => '\1', |
| 50 | '/(alias)es$/' => '\1', |
| 51 | '/([octop|vir])i$/' => '\1us', |
| 52 | '/(cris|ax|test)es$/' => '\1is', |
| 53 | '/(shoe)s$/' => '\1', |
| 54 | '/(o)es$/' => '\1', |
| 55 | '/(bus|campus)es$/' => '\1', |
| 56 | '/([m|l])ice$/' => '\1ouse', |
| 57 | '/(x|ch|ss|sh)es$/' => '\1', |
| 58 | '/(m)ovies$/' => '\1\2ovie', |
| 59 | '/(s)eries$/' => '\1\2eries', |
| 60 | '/([^aeiouy]|qu)ies$/' => '\1y', |
| 61 | '/([lr])ves$/' => '\1f', |
| 62 | '/(tive)s$/' => '\1', |
| 63 | '/(hive)s$/' => '\1', |
| 64 | '/([^f])ves$/' => '\1fe', |
| 65 | '/(^analy)ses$/' => '\1sis', |
| 66 | '/((a)naly|(b)a|(d)iagno|(p)arenthe|(p)rogno|(s)ynop|(t)he)ses$/' => '\1\2sis', |
| 67 | '/([ti])a$/' => '\1um', |
| 68 | '/(p)eople$/' => '\1\2erson', |
| 69 | '/(m)en$/' => '\1an', |
| 70 | '/(s)tatuses$/' => '\1\2tatus', |
| 71 | '/(c)hildren$/' => '\1\2hild', |
| 72 | '/(n)ews$/' => '\1\2ews', |
| 73 | '/([^u])s$/' => '\1', |
| 74 | ); |
| 75 | |
| 76 | foreach ($singular_rules as $rule => $replacement) |
| 77 | { |
| 78 | if (preg_match($rule, $result)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 79 | { |
Phil Sturgeon | 50e57bc | 2011-08-13 10:26:04 -0600 | [diff] [blame^] | 80 | $result = preg_replace($rule, $replacement, $result); |
| 81 | break; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 82 | } |
| 83 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 84 | |
Phil Sturgeon | 50e57bc | 2011-08-13 10:26:04 -0600 | [diff] [blame^] | 85 | return $result; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 86 | } |
| 87 | } |
| 88 | |
| 89 | // -------------------------------------------------------------------- |
| 90 | |
| 91 | /** |
| 92 | * Plural |
| 93 | * |
| 94 | * Takes a singular word and makes it plural |
| 95 | * |
| 96 | * @access public |
| 97 | * @param string |
| 98 | * @param bool |
| 99 | * @return str |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 100 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 101 | if ( ! function_exists('plural')) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 102 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 103 | function plural($str, $force = FALSE) |
Phil Sturgeon | 50e57bc | 2011-08-13 10:26:04 -0600 | [diff] [blame^] | 104 | { |
| 105 | $result = strval($str); |
| 106 | |
| 107 | $plural_rules = array( |
| 108 | '/^(ox)$/' => '\1\2en', // ox |
| 109 | '/([m|l])ouse$/' => '\1ice', // mouse, louse |
| 110 | '/(matr|vert|ind)ix|ex$/' => '\1ices', // matrix, vertex, index |
| 111 | '/(x|ch|ss|sh)$/' => '\1es', // search, switch, fix, box, process, address |
| 112 | '/([^aeiouy]|qu)y$/' => '\1ies', // query, ability, agency |
| 113 | '/(hive)$/' => '\1s', // archive, hive |
| 114 | '/(?:([^f])fe|([lr])f)$/' => '\1\2ves', // half, safe, wife |
| 115 | '/sis$/' => 'ses', // basis, diagnosis |
| 116 | '/([ti])um$/' => '\1a', // datum, medium |
| 117 | '/(p)erson$/' => '\1eople', // person, salesperson |
| 118 | '/(m)an$/' => '\1en', // man, woman, spokesman |
| 119 | '/(c)hild$/' => '\1hildren', // child |
| 120 | '/(buffal|tomat)o$/' => '\1\2oes', // buffalo, tomato |
| 121 | '/(bu|campu)s$/' => '\1\2ses', // bus, campus |
| 122 | '/(alias|status|virus)/' => '\1es', // alias |
| 123 | '/(octop)us$/' => '\1i', // octopus |
| 124 | '/(ax|cris|test)is$/' => '\1es', // axis, crisis |
| 125 | '/s$/' => 's', // no change (compatibility) |
| 126 | '/$/' => 's', |
| 127 | ); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 128 | |
Phil Sturgeon | 50e57bc | 2011-08-13 10:26:04 -0600 | [diff] [blame^] | 129 | foreach ($plural_rules as $rule => $replacement) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 130 | { |
Phil Sturgeon | 50e57bc | 2011-08-13 10:26:04 -0600 | [diff] [blame^] | 131 | if (preg_match($rule, $result)) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 132 | { |
Phil Sturgeon | 50e57bc | 2011-08-13 10:26:04 -0600 | [diff] [blame^] | 133 | $result = preg_replace($rule, $replacement, $result); |
| 134 | break; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 135 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 136 | } |
| 137 | |
Phil Sturgeon | 50e57bc | 2011-08-13 10:26:04 -0600 | [diff] [blame^] | 138 | return $result; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 139 | } |
| 140 | } |
| 141 | |
| 142 | // -------------------------------------------------------------------- |
| 143 | |
| 144 | /** |
| 145 | * Camelize |
| 146 | * |
| 147 | * Takes multiple words separated by spaces or underscores and camelizes them |
| 148 | * |
| 149 | * @access public |
| 150 | * @param string |
| 151 | * @return str |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 152 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 153 | if ( ! function_exists('camelize')) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 154 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 155 | function camelize($str) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 156 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 157 | $str = 'x'.strtolower(trim($str)); |
| 158 | $str = ucwords(preg_replace('/[\s_]+/', ' ', $str)); |
| 159 | return substr(str_replace(' ', '', $str), 1); |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | // -------------------------------------------------------------------- |
| 164 | |
| 165 | /** |
| 166 | * Underscore |
| 167 | * |
| 168 | * Takes multiple words separated by spaces and underscores them |
| 169 | * |
| 170 | * @access public |
| 171 | * @param string |
| 172 | * @return str |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 173 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 174 | if ( ! function_exists('underscore')) |
| 175 | { |
| 176 | function underscore($str) |
| 177 | { |
| 178 | return preg_replace('/[\s]+/', '_', strtolower(trim($str))); |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | // -------------------------------------------------------------------- |
| 183 | |
| 184 | /** |
| 185 | * Humanize |
| 186 | * |
| 187 | * Takes multiple words separated by underscores and changes them to spaces |
| 188 | * |
| 189 | * @access public |
| 190 | * @param string |
| 191 | * @return str |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 192 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 193 | if ( ! function_exists('humanize')) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 194 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 195 | function humanize($str) |
| 196 | { |
| 197 | return ucwords(preg_replace('/[_]+/', ' ', strtolower(trim($str)))); |
| 198 | } |
| 199 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 200 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 201 | |
| 202 | /* End of file inflector_helper.php */ |
Derek Jones | a3ffbbb | 2008-05-11 18:18:29 +0000 | [diff] [blame] | 203 | /* Location: ./system/helpers/inflector_helper.php */ |