Andrey Andreev | c5536aa | 2012-11-01 17:33:58 +0200 | [diff] [blame] | 1 | <?php |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2 | /** |
| 3 | * CodeIgniter |
| 4 | * |
Andrey Andreev | fe9309d | 2015-01-09 17:48:58 +0200 | [diff] [blame^] | 5 | * An open source application development framework for PHP |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 6 | * |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame] | 7 | * This content is released under the MIT License (MIT) |
Eric Barnes | cde712c | 2011-12-09 11:27:51 -0500 | [diff] [blame] | 8 | * |
Andrey Andreev | fe9309d | 2015-01-09 17:48:58 +0200 | [diff] [blame^] | 9 | * Copyright (c) 2014 - 2015, British Columbia Institute of Technology |
Eric Barnes | cde712c | 2011-12-09 11:27:51 -0500 | [diff] [blame] | 10 | * |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame] | 11 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 12 | * of this software and associated documentation files (the "Software"), to deal |
| 13 | * in the Software without restriction, including without limitation the rights |
| 14 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 15 | * copies of the Software, and to permit persons to whom the Software is |
| 16 | * furnished to do so, subject to the following conditions: |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 17 | * |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame] | 18 | * The above copyright notice and this permission notice shall be included in |
| 19 | * all copies or substantial portions of the Software. |
| 20 | * |
| 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 22 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 23 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 24 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 25 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 26 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 27 | * THE SOFTWARE. |
| 28 | * |
| 29 | * @package CodeIgniter |
| 30 | * @author EllisLab Dev Team |
darwinel | 871754a | 2014-02-11 17:34:57 +0100 | [diff] [blame] | 31 | * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (http://ellislab.com/) |
Andrey Andreev | fe9309d | 2015-01-09 17:48:58 +0200 | [diff] [blame^] | 32 | * @copyright Copyright (c) 2014 - 2015, British Columbia Institute of Technology (http://bcit.ca/) |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame] | 33 | * @license http://opensource.org/licenses/MIT MIT License |
| 34 | * @link http://codeigniter.com |
| 35 | * @since Version 1.0.0 |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 36 | * @filesource |
| 37 | */ |
Andrey Andreev | c5536aa | 2012-11-01 17:33:58 +0200 | [diff] [blame] | 38 | defined('BASEPATH') OR exit('No direct script access allowed'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 39 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 40 | /** |
| 41 | * CodeIgniter Inflector Helpers |
| 42 | * |
| 43 | * @package CodeIgniter |
| 44 | * @subpackage Helpers |
| 45 | * @category Helpers |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 46 | * @author EllisLab Dev Team |
Phil Sturgeon | 002b4be | 2012-02-29 12:12:49 +0000 | [diff] [blame] | 47 | * @link http://codeigniter.com/user_guide/helpers/inflector_helper.html |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 48 | */ |
| 49 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 50 | // -------------------------------------------------------------------- |
| 51 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 52 | if ( ! function_exists('singular')) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 53 | { |
Timothy Warren | b75faa1 | 2012-04-27 12:03:32 -0400 | [diff] [blame] | 54 | /** |
| 55 | * Singular |
| 56 | * |
| 57 | * Takes a plural word and makes it singular |
| 58 | * |
Andrey Andreev | 53b8ef5 | 2012-11-08 21:38:53 +0200 | [diff] [blame] | 59 | * @param string $str Input string |
Timothy Warren | 16642c7 | 2012-05-17 08:20:16 -0400 | [diff] [blame] | 60 | * @return string |
Timothy Warren | b75faa1 | 2012-04-27 12:03:32 -0400 | [diff] [blame] | 61 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 62 | function singular($str) |
| 63 | { |
Phil Sturgeon | 50e57bc | 2011-08-13 10:26:04 -0600 | [diff] [blame] | 64 | $result = strval($str); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 65 | |
Phil Sturgeon | 002b4be | 2012-02-29 12:12:49 +0000 | [diff] [blame] | 66 | if ( ! is_countable($result)) |
| 67 | { |
| 68 | return $result; |
| 69 | } |
Andrey Andreev | 52a31ba | 2012-03-10 15:38:49 +0200 | [diff] [blame] | 70 | |
Phil Sturgeon | 50e57bc | 2011-08-13 10:26:04 -0600 | [diff] [blame] | 71 | $singular_rules = array( |
Andrey Andreev | 838a9d6 | 2012-12-03 14:37:47 +0200 | [diff] [blame] | 72 | '/(matr)ices$/' => '\1ix', |
| 73 | '/(vert|ind)ices$/' => '\1ex', |
| 74 | '/^(ox)en/' => '\1', |
| 75 | '/(alias)es$/' => '\1', |
| 76 | '/([octop|vir])i$/' => '\1us', |
| 77 | '/(cris|ax|test)es$/' => '\1is', |
| 78 | '/(shoe)s$/' => '\1', |
| 79 | '/(o)es$/' => '\1', |
| 80 | '/(bus|campus)es$/' => '\1', |
| 81 | '/([m|l])ice$/' => '\1ouse', |
| 82 | '/(x|ch|ss|sh)es$/' => '\1', |
| 83 | '/(m)ovies$/' => '\1\2ovie', |
| 84 | '/(s)eries$/' => '\1\2eries', |
| 85 | '/([^aeiouy]|qu)ies$/' => '\1y', |
| 86 | '/([lr])ves$/' => '\1f', |
| 87 | '/(tive)s$/' => '\1', |
| 88 | '/(hive)s$/' => '\1', |
| 89 | '/([^f])ves$/' => '\1fe', |
| 90 | '/(^analy)ses$/' => '\1sis', |
Phil Sturgeon | 50e57bc | 2011-08-13 10:26:04 -0600 | [diff] [blame] | 91 | '/((a)naly|(b)a|(d)iagno|(p)arenthe|(p)rogno|(s)ynop|(t)he)ses$/' => '\1\2sis', |
Andrey Andreev | 838a9d6 | 2012-12-03 14:37:47 +0200 | [diff] [blame] | 92 | '/([ti])a$/' => '\1um', |
| 93 | '/(p)eople$/' => '\1\2erson', |
| 94 | '/(m)en$/' => '\1an', |
| 95 | '/(s)tatuses$/' => '\1\2tatus', |
| 96 | '/(c)hildren$/' => '\1\2hild', |
| 97 | '/(n)ews$/' => '\1\2ews', |
| 98 | '/([^us])s$/' => '\1' |
Phil Sturgeon | 50e57bc | 2011-08-13 10:26:04 -0600 | [diff] [blame] | 99 | ); |
Eric Barnes | cde712c | 2011-12-09 11:27:51 -0500 | [diff] [blame] | 100 | |
Phil Sturgeon | 002b4be | 2012-02-29 12:12:49 +0000 | [diff] [blame] | 101 | foreach ($singular_rules as $rule => $replacement) |
| 102 | { |
| 103 | if (preg_match($rule, $result)) |
| 104 | { |
| 105 | $result = preg_replace($rule, $replacement, $result); |
| 106 | break; |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | return $result; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 111 | } |
| 112 | } |
| 113 | |
| 114 | // -------------------------------------------------------------------- |
| 115 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 116 | if ( ! function_exists('plural')) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 117 | { |
Timothy Warren | b75faa1 | 2012-04-27 12:03:32 -0400 | [diff] [blame] | 118 | /** |
| 119 | * Plural |
| 120 | * |
| 121 | * Takes a singular word and makes it plural |
| 122 | * |
Andrey Andreev | 53b8ef5 | 2012-11-08 21:38:53 +0200 | [diff] [blame] | 123 | * @param string $str Input string |
Timothy Warren | 16642c7 | 2012-05-17 08:20:16 -0400 | [diff] [blame] | 124 | * @return string |
Timothy Warren | b75faa1 | 2012-04-27 12:03:32 -0400 | [diff] [blame] | 125 | */ |
Andrey Andreev | 53b8ef5 | 2012-11-08 21:38:53 +0200 | [diff] [blame] | 126 | function plural($str) |
Andrey Andreev | 52a31ba | 2012-03-10 15:38:49 +0200 | [diff] [blame] | 127 | { |
Phil Sturgeon | 50e57bc | 2011-08-13 10:26:04 -0600 | [diff] [blame] | 128 | $result = strval($str); |
Eric Barnes | cde712c | 2011-12-09 11:27:51 -0500 | [diff] [blame] | 129 | |
Phil Sturgeon | 002b4be | 2012-02-29 12:12:49 +0000 | [diff] [blame] | 130 | if ( ! is_countable($result)) |
| 131 | { |
| 132 | return $result; |
| 133 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 134 | |
Phil Sturgeon | 002b4be | 2012-02-29 12:12:49 +0000 | [diff] [blame] | 135 | $plural_rules = array( |
| 136 | '/^(ox)$/' => '\1\2en', // ox |
| 137 | '/([m|l])ouse$/' => '\1ice', // mouse, louse |
| 138 | '/(matr|vert|ind)ix|ex$/' => '\1ices', // matrix, vertex, index |
| 139 | '/(x|ch|ss|sh)$/' => '\1es', // search, switch, fix, box, process, address |
| 140 | '/([^aeiouy]|qu)y$/' => '\1ies', // query, ability, agency |
| 141 | '/(hive)$/' => '\1s', // archive, hive |
| 142 | '/(?:([^f])fe|([lr])f)$/' => '\1\2ves', // half, safe, wife |
| 143 | '/sis$/' => 'ses', // basis, diagnosis |
| 144 | '/([ti])um$/' => '\1a', // datum, medium |
| 145 | '/(p)erson$/' => '\1eople', // person, salesperson |
| 146 | '/(m)an$/' => '\1en', // man, woman, spokesman |
| 147 | '/(c)hild$/' => '\1hildren', // child |
| 148 | '/(buffal|tomat)o$/' => '\1\2oes', // buffalo, tomato |
| 149 | '/(bu|campu)s$/' => '\1\2ses', // bus, campus |
| 150 | '/(alias|status|virus)$/' => '\1es', // alias |
| 151 | '/(octop)us$/' => '\1i', // octopus |
| 152 | '/(ax|cris|test)is$/' => '\1es', // axis, crisis |
| 153 | '/s$/' => 's', // no change (compatibility) |
| 154 | '/$/' => 's', |
| 155 | ); |
Andrey Andreev | 52a31ba | 2012-03-10 15:38:49 +0200 | [diff] [blame] | 156 | |
Phil Sturgeon | 002b4be | 2012-02-29 12:12:49 +0000 | [diff] [blame] | 157 | foreach ($plural_rules as $rule => $replacement) |
| 158 | { |
| 159 | if (preg_match($rule, $result)) |
| 160 | { |
| 161 | $result = preg_replace($rule, $replacement, $result); |
| 162 | break; |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | return $result; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 167 | } |
| 168 | } |
| 169 | |
| 170 | // -------------------------------------------------------------------- |
| 171 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 172 | if ( ! function_exists('camelize')) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 173 | { |
Timothy Warren | b75faa1 | 2012-04-27 12:03:32 -0400 | [diff] [blame] | 174 | /** |
| 175 | * Camelize |
| 176 | * |
| 177 | * Takes multiple words separated by spaces or underscores and camelizes them |
| 178 | * |
Andrey Andreev | 53b8ef5 | 2012-11-08 21:38:53 +0200 | [diff] [blame] | 179 | * @param string $str Input string |
Timothy Warren | 16642c7 | 2012-05-17 08:20:16 -0400 | [diff] [blame] | 180 | * @return string |
Timothy Warren | b75faa1 | 2012-04-27 12:03:32 -0400 | [diff] [blame] | 181 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 182 | function camelize($str) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 183 | { |
Phil Sturgeon | e40c763 | 2012-03-10 13:05:08 +0000 | [diff] [blame] | 184 | return strtolower($str[0]).substr(str_replace(' ', '', ucwords(preg_replace('/[\s_]+/', ' ', $str))), 1); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 185 | } |
| 186 | } |
| 187 | |
| 188 | // -------------------------------------------------------------------- |
| 189 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 190 | if ( ! function_exists('underscore')) |
| 191 | { |
Timothy Warren | b75faa1 | 2012-04-27 12:03:32 -0400 | [diff] [blame] | 192 | /** |
| 193 | * Underscore |
| 194 | * |
| 195 | * Takes multiple words separated by spaces and underscores them |
| 196 | * |
Andrey Andreev | 53b8ef5 | 2012-11-08 21:38:53 +0200 | [diff] [blame] | 197 | * @param string $str Input string |
Timothy Warren | 16642c7 | 2012-05-17 08:20:16 -0400 | [diff] [blame] | 198 | * @return string |
Timothy Warren | b75faa1 | 2012-04-27 12:03:32 -0400 | [diff] [blame] | 199 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 200 | function underscore($str) |
| 201 | { |
Andrey Andreev | 6ce4746 | 2014-02-13 03:28:00 +0200 | [diff] [blame] | 202 | return preg_replace('/[\s]+/', '_', trim(MB_ENABLED ? mb_strtolower($str) : strtolower($str))); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 203 | } |
| 204 | } |
| 205 | |
| 206 | // -------------------------------------------------------------------- |
| 207 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 208 | if ( ! function_exists('humanize')) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 209 | { |
Timothy Warren | b75faa1 | 2012-04-27 12:03:32 -0400 | [diff] [blame] | 210 | /** |
| 211 | * Humanize |
| 212 | * |
| 213 | * Takes multiple words separated by the separator and changes them to spaces |
| 214 | * |
Andrey Andreev | 53b8ef5 | 2012-11-08 21:38:53 +0200 | [diff] [blame] | 215 | * @param string $str Input string |
| 216 | * @param string $separator Input separator |
Timothy Warren | 16642c7 | 2012-05-17 08:20:16 -0400 | [diff] [blame] | 217 | * @return string |
Timothy Warren | b75faa1 | 2012-04-27 12:03:32 -0400 | [diff] [blame] | 218 | */ |
Eric Barnes | cde712c | 2011-12-09 11:27:51 -0500 | [diff] [blame] | 219 | function humanize($str, $separator = '_') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 220 | { |
Andrey Andreev | 6ce4746 | 2014-02-13 03:28:00 +0200 | [diff] [blame] | 221 | return ucwords(preg_replace('/['.$separator.']+/', ' ', trim(MB_ENABLED ? mb_strtolower($str) : strtolower($str)))); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 222 | } |
| 223 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 224 | |
Timothy Warren | b75faa1 | 2012-04-27 12:03:32 -0400 | [diff] [blame] | 225 | // -------------------------------------------------------------------- |
| 226 | |
Phil Sturgeon | 002b4be | 2012-02-29 12:12:49 +0000 | [diff] [blame] | 227 | if ( ! function_exists('is_countable')) |
| 228 | { |
Timothy Warren | b75faa1 | 2012-04-27 12:03:32 -0400 | [diff] [blame] | 229 | /** |
| 230 | * Checks if the given word has a plural version. |
| 231 | * |
Andrey Andreev | 53b8ef5 | 2012-11-08 21:38:53 +0200 | [diff] [blame] | 232 | * @param string $word Word to check |
| 233 | * @return bool |
Timothy Warren | b75faa1 | 2012-04-27 12:03:32 -0400 | [diff] [blame] | 234 | */ |
Phil Sturgeon | 002b4be | 2012-02-29 12:12:49 +0000 | [diff] [blame] | 235 | function is_countable($word) |
| 236 | { |
Andrey Andreev | 6ce4746 | 2014-02-13 03:28:00 +0200 | [diff] [blame] | 237 | return ! in_array( |
| 238 | strtolower($word), |
| 239 | array( |
| 240 | 'equipment', 'information', 'rice', 'money', |
| 241 | 'species', 'series', 'fish', 'meta' |
| 242 | ) |
| 243 | ); |
Phil Sturgeon | 002b4be | 2012-02-29 12:12:49 +0000 | [diff] [blame] | 244 | } |
| 245 | } |
| 246 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 247 | /* End of file inflector_helper.php */ |
Andrey Andreev | e92df33 | 2012-03-26 22:44:20 +0300 | [diff] [blame] | 248 | /* Location: ./system/helpers/inflector_helper.php */ |