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 | * |
Phil Sturgeon | 07c1ac8 | 2012-03-09 17:03:37 +0000 | [diff] [blame] | 5 | * An open source application development framework for PHP 5.2.4 or newer |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 6 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 7 | * NOTICE OF LICENSE |
Eric Barnes | cde712c | 2011-12-09 11:27:51 -0500 | [diff] [blame] | 8 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 9 | * Licensed under the Open Software License version 3.0 |
Eric Barnes | cde712c | 2011-12-09 11:27:51 -0500 | [diff] [blame] | 10 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 11 | * This source file is subject to the Open Software License (OSL 3.0) that is |
| 12 | * bundled with this package in the files license.txt / license.rst. It is |
| 13 | * also available through the world wide web at this URL: |
| 14 | * http://opensource.org/licenses/OSL-3.0 |
| 15 | * If you did not receive a copy of the license and are unable to obtain it |
| 16 | * through the world wide web, please send an email to |
| 17 | * licensing@ellislab.com so we can send you a copy immediately. |
| 18 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 19 | * @package CodeIgniter |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 20 | * @author EllisLab Dev Team |
Greg Aker | 0defe5d | 2012-01-01 18:46:41 -0600 | [diff] [blame] | 21 | * @copyright Copyright (c) 2008 - 2012, EllisLab, Inc. (http://ellislab.com/) |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 22 | * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 23 | * @link http://codeigniter.com |
| 24 | * @since Version 1.0 |
| 25 | * @filesource |
| 26 | */ |
Andrey Andreev | c5536aa | 2012-11-01 17:33:58 +0200 | [diff] [blame] | 27 | defined('BASEPATH') OR exit('No direct script access allowed'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 28 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 29 | /** |
| 30 | * CodeIgniter Inflector Helpers |
| 31 | * |
| 32 | * @package CodeIgniter |
| 33 | * @subpackage Helpers |
| 34 | * @category Helpers |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 35 | * @author EllisLab Dev Team |
Phil Sturgeon | 002b4be | 2012-02-29 12:12:49 +0000 | [diff] [blame] | 36 | * @link http://codeigniter.com/user_guide/helpers/inflector_helper.html |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 37 | */ |
| 38 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 39 | // -------------------------------------------------------------------- |
| 40 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 41 | if ( ! function_exists('singular')) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 42 | { |
Timothy Warren | b75faa1 | 2012-04-27 12:03:32 -0400 | [diff] [blame] | 43 | /** |
| 44 | * Singular |
| 45 | * |
| 46 | * Takes a plural word and makes it singular |
| 47 | * |
Andrey Andreev | 53b8ef5 | 2012-11-08 21:38:53 +0200 | [diff] [blame^] | 48 | * @param string $str Input string |
Timothy Warren | 16642c7 | 2012-05-17 08:20:16 -0400 | [diff] [blame] | 49 | * @return string |
Timothy Warren | b75faa1 | 2012-04-27 12:03:32 -0400 | [diff] [blame] | 50 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 51 | function singular($str) |
| 52 | { |
Phil Sturgeon | 50e57bc | 2011-08-13 10:26:04 -0600 | [diff] [blame] | 53 | $result = strval($str); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 54 | |
Phil Sturgeon | 002b4be | 2012-02-29 12:12:49 +0000 | [diff] [blame] | 55 | if ( ! is_countable($result)) |
| 56 | { |
| 57 | return $result; |
| 58 | } |
Andrey Andreev | 52a31ba | 2012-03-10 15:38:49 +0200 | [diff] [blame] | 59 | |
Phil Sturgeon | 50e57bc | 2011-08-13 10:26:04 -0600 | [diff] [blame] | 60 | $singular_rules = array( |
Phil Sturgeon | 002b4be | 2012-02-29 12:12:49 +0000 | [diff] [blame] | 61 | '/(matr)ices$/' => '\1ix', |
| 62 | '/(vert|ind)ices$/' => '\1ex', |
| 63 | '/^(ox)en/' => '\1', |
| 64 | '/(alias)es$/' => '\1', |
| 65 | '/([octop|vir])i$/' => '\1us', |
| 66 | '/(cris|ax|test)es$/' => '\1is', |
| 67 | '/(shoe)s$/' => '\1', |
| 68 | '/(o)es$/' => '\1', |
| 69 | '/(bus|campus)es$/' => '\1', |
| 70 | '/([m|l])ice$/' => '\1ouse', |
| 71 | '/(x|ch|ss|sh)es$/' => '\1', |
| 72 | '/(m)ovies$/' => '\1\2ovie', |
| 73 | '/(s)eries$/' => '\1\2eries', |
| 74 | '/([^aeiouy]|qu)ies$/' => '\1y', |
| 75 | '/([lr])ves$/' => '\1f', |
| 76 | '/(tive)s$/' => '\1', |
| 77 | '/(hive)s$/' => '\1', |
| 78 | '/([^f])ves$/' => '\1fe', |
| 79 | '/(^analy)ses$/' => '\1sis', |
Phil Sturgeon | 50e57bc | 2011-08-13 10:26:04 -0600 | [diff] [blame] | 80 | '/((a)naly|(b)a|(d)iagno|(p)arenthe|(p)rogno|(s)ynop|(t)he)ses$/' => '\1\2sis', |
Phil Sturgeon | 002b4be | 2012-02-29 12:12:49 +0000 | [diff] [blame] | 81 | '/([ti])a$/' => '\1um', |
| 82 | '/(p)eople$/' => '\1\2erson', |
| 83 | '/(m)en$/' => '\1an', |
| 84 | '/(s)tatuses$/' => '\1\2tatus', |
| 85 | '/(c)hildren$/' => '\1\2hild', |
| 86 | '/(n)ews$/' => '\1\2ews', |
| 87 | '/([^us])s$/' => '\1', |
Phil Sturgeon | 50e57bc | 2011-08-13 10:26:04 -0600 | [diff] [blame] | 88 | ); |
Eric Barnes | cde712c | 2011-12-09 11:27:51 -0500 | [diff] [blame] | 89 | |
Phil Sturgeon | 002b4be | 2012-02-29 12:12:49 +0000 | [diff] [blame] | 90 | foreach ($singular_rules as $rule => $replacement) |
| 91 | { |
| 92 | if (preg_match($rule, $result)) |
| 93 | { |
| 94 | $result = preg_replace($rule, $replacement, $result); |
| 95 | break; |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | return $result; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 100 | } |
| 101 | } |
| 102 | |
| 103 | // -------------------------------------------------------------------- |
| 104 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 105 | if ( ! function_exists('plural')) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 106 | { |
Timothy Warren | b75faa1 | 2012-04-27 12:03:32 -0400 | [diff] [blame] | 107 | /** |
| 108 | * Plural |
| 109 | * |
| 110 | * Takes a singular word and makes it plural |
| 111 | * |
Andrey Andreev | 53b8ef5 | 2012-11-08 21:38:53 +0200 | [diff] [blame^] | 112 | * @param string $str Input string |
Timothy Warren | 16642c7 | 2012-05-17 08:20:16 -0400 | [diff] [blame] | 113 | * @return string |
Timothy Warren | b75faa1 | 2012-04-27 12:03:32 -0400 | [diff] [blame] | 114 | */ |
Andrey Andreev | 53b8ef5 | 2012-11-08 21:38:53 +0200 | [diff] [blame^] | 115 | function plural($str) |
Andrey Andreev | 52a31ba | 2012-03-10 15:38:49 +0200 | [diff] [blame] | 116 | { |
Phil Sturgeon | 50e57bc | 2011-08-13 10:26:04 -0600 | [diff] [blame] | 117 | $result = strval($str); |
Eric Barnes | cde712c | 2011-12-09 11:27:51 -0500 | [diff] [blame] | 118 | |
Phil Sturgeon | 002b4be | 2012-02-29 12:12:49 +0000 | [diff] [blame] | 119 | if ( ! is_countable($result)) |
| 120 | { |
| 121 | return $result; |
| 122 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 123 | |
Phil Sturgeon | 002b4be | 2012-02-29 12:12:49 +0000 | [diff] [blame] | 124 | $plural_rules = array( |
| 125 | '/^(ox)$/' => '\1\2en', // ox |
| 126 | '/([m|l])ouse$/' => '\1ice', // mouse, louse |
| 127 | '/(matr|vert|ind)ix|ex$/' => '\1ices', // matrix, vertex, index |
| 128 | '/(x|ch|ss|sh)$/' => '\1es', // search, switch, fix, box, process, address |
| 129 | '/([^aeiouy]|qu)y$/' => '\1ies', // query, ability, agency |
| 130 | '/(hive)$/' => '\1s', // archive, hive |
| 131 | '/(?:([^f])fe|([lr])f)$/' => '\1\2ves', // half, safe, wife |
| 132 | '/sis$/' => 'ses', // basis, diagnosis |
| 133 | '/([ti])um$/' => '\1a', // datum, medium |
| 134 | '/(p)erson$/' => '\1eople', // person, salesperson |
| 135 | '/(m)an$/' => '\1en', // man, woman, spokesman |
| 136 | '/(c)hild$/' => '\1hildren', // child |
| 137 | '/(buffal|tomat)o$/' => '\1\2oes', // buffalo, tomato |
| 138 | '/(bu|campu)s$/' => '\1\2ses', // bus, campus |
| 139 | '/(alias|status|virus)$/' => '\1es', // alias |
| 140 | '/(octop)us$/' => '\1i', // octopus |
| 141 | '/(ax|cris|test)is$/' => '\1es', // axis, crisis |
| 142 | '/s$/' => 's', // no change (compatibility) |
| 143 | '/$/' => 's', |
| 144 | ); |
Andrey Andreev | 52a31ba | 2012-03-10 15:38:49 +0200 | [diff] [blame] | 145 | |
Phil Sturgeon | 002b4be | 2012-02-29 12:12:49 +0000 | [diff] [blame] | 146 | foreach ($plural_rules as $rule => $replacement) |
| 147 | { |
| 148 | if (preg_match($rule, $result)) |
| 149 | { |
| 150 | $result = preg_replace($rule, $replacement, $result); |
| 151 | break; |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | return $result; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 156 | } |
| 157 | } |
| 158 | |
| 159 | // -------------------------------------------------------------------- |
| 160 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 161 | if ( ! function_exists('camelize')) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 162 | { |
Timothy Warren | b75faa1 | 2012-04-27 12:03:32 -0400 | [diff] [blame] | 163 | /** |
| 164 | * Camelize |
| 165 | * |
| 166 | * Takes multiple words separated by spaces or underscores and camelizes them |
| 167 | * |
Andrey Andreev | 53b8ef5 | 2012-11-08 21:38:53 +0200 | [diff] [blame^] | 168 | * @param string $str Input string |
Timothy Warren | 16642c7 | 2012-05-17 08:20:16 -0400 | [diff] [blame] | 169 | * @return string |
Timothy Warren | b75faa1 | 2012-04-27 12:03:32 -0400 | [diff] [blame] | 170 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 171 | function camelize($str) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 172 | { |
Phil Sturgeon | e40c763 | 2012-03-10 13:05:08 +0000 | [diff] [blame] | 173 | 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] | 174 | } |
| 175 | } |
| 176 | |
| 177 | // -------------------------------------------------------------------- |
| 178 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 179 | if ( ! function_exists('underscore')) |
| 180 | { |
Timothy Warren | b75faa1 | 2012-04-27 12:03:32 -0400 | [diff] [blame] | 181 | /** |
| 182 | * Underscore |
| 183 | * |
| 184 | * Takes multiple words separated by spaces and underscores them |
| 185 | * |
Andrey Andreev | 53b8ef5 | 2012-11-08 21:38:53 +0200 | [diff] [blame^] | 186 | * @param string $str Input string |
Timothy Warren | 16642c7 | 2012-05-17 08:20:16 -0400 | [diff] [blame] | 187 | * @return string |
Timothy Warren | b75faa1 | 2012-04-27 12:03:32 -0400 | [diff] [blame] | 188 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 189 | function underscore($str) |
| 190 | { |
| 191 | return preg_replace('/[\s]+/', '_', strtolower(trim($str))); |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | // -------------------------------------------------------------------- |
| 196 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 197 | if ( ! function_exists('humanize')) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 198 | { |
Timothy Warren | b75faa1 | 2012-04-27 12:03:32 -0400 | [diff] [blame] | 199 | /** |
| 200 | * Humanize |
| 201 | * |
| 202 | * Takes multiple words separated by the separator and changes them to spaces |
| 203 | * |
Andrey Andreev | 53b8ef5 | 2012-11-08 21:38:53 +0200 | [diff] [blame^] | 204 | * @param string $str Input string |
| 205 | * @param string $separator Input separator |
Timothy Warren | 16642c7 | 2012-05-17 08:20:16 -0400 | [diff] [blame] | 206 | * @return string |
Timothy Warren | b75faa1 | 2012-04-27 12:03:32 -0400 | [diff] [blame] | 207 | */ |
Eric Barnes | cde712c | 2011-12-09 11:27:51 -0500 | [diff] [blame] | 208 | function humanize($str, $separator = '_') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 209 | { |
Eric Barnes | cde712c | 2011-12-09 11:27:51 -0500 | [diff] [blame] | 210 | return ucwords(preg_replace('/['.$separator.']+/', ' ', strtolower(trim($str)))); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 211 | } |
| 212 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 213 | |
Timothy Warren | b75faa1 | 2012-04-27 12:03:32 -0400 | [diff] [blame] | 214 | // -------------------------------------------------------------------- |
| 215 | |
Phil Sturgeon | 002b4be | 2012-02-29 12:12:49 +0000 | [diff] [blame] | 216 | if ( ! function_exists('is_countable')) |
| 217 | { |
Timothy Warren | b75faa1 | 2012-04-27 12:03:32 -0400 | [diff] [blame] | 218 | /** |
| 219 | * Checks if the given word has a plural version. |
| 220 | * |
Andrey Andreev | 53b8ef5 | 2012-11-08 21:38:53 +0200 | [diff] [blame^] | 221 | * @param string $word Word to check |
| 222 | * @return bool |
Timothy Warren | b75faa1 | 2012-04-27 12:03:32 -0400 | [diff] [blame] | 223 | */ |
Phil Sturgeon | 002b4be | 2012-02-29 12:12:49 +0000 | [diff] [blame] | 224 | function is_countable($word) |
| 225 | { |
Andrey Andreev | 53b8ef5 | 2012-11-08 21:38:53 +0200 | [diff] [blame^] | 226 | return ! in_array(strtolower($word), |
Andrey Andreev | e92df33 | 2012-03-26 22:44:20 +0300 | [diff] [blame] | 227 | array( |
| 228 | 'equipment', 'information', 'rice', 'money', |
| 229 | 'species', 'series', 'fish', 'meta' |
| 230 | ) |
| 231 | ); |
Phil Sturgeon | 002b4be | 2012-02-29 12:12:49 +0000 | [diff] [blame] | 232 | } |
| 233 | } |
| 234 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 235 | /* End of file inflector_helper.php */ |
Andrey Andreev | e92df33 | 2012-03-26 22:44:20 +0300 | [diff] [blame] | 236 | /* Location: ./system/helpers/inflector_helper.php */ |