Andrey Andreev | 1e6b72c | 2012-01-06 19:09:22 +0200 | [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 | * |
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 | */ |
| 27 | |
| 28 | // ------------------------------------------------------------------------ |
| 29 | |
| 30 | /** |
| 31 | * CodeIgniter Inflector Helpers |
| 32 | * |
| 33 | * @package CodeIgniter |
| 34 | * @subpackage Helpers |
| 35 | * @category Helpers |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 36 | * @author EllisLab Dev Team |
Phil Sturgeon | 002b4be | 2012-02-29 12:12:49 +0000 | [diff] [blame] | 37 | * @link http://codeigniter.com/user_guide/helpers/inflector_helper.html |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 38 | */ |
| 39 | |
| 40 | |
| 41 | // -------------------------------------------------------------------- |
| 42 | |
| 43 | /** |
| 44 | * Singular |
| 45 | * |
| 46 | * Takes a plural word and makes it singular |
| 47 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 48 | * @param string |
| 49 | * @return str |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 50 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 51 | if ( ! function_exists('singular')) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 52 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 53 | function singular($str) |
| 54 | { |
Phil Sturgeon | 50e57bc | 2011-08-13 10:26:04 -0600 | [diff] [blame] | 55 | $result = strval($str); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 56 | |
Phil Sturgeon | 002b4be | 2012-02-29 12:12:49 +0000 | [diff] [blame] | 57 | if ( ! is_countable($result)) |
| 58 | { |
| 59 | return $result; |
| 60 | } |
| 61 | |
Phil Sturgeon | 50e57bc | 2011-08-13 10:26:04 -0600 | [diff] [blame] | 62 | $singular_rules = array( |
Phil Sturgeon | 002b4be | 2012-02-29 12:12:49 +0000 | [diff] [blame] | 63 | '/(matr)ices$/' => '\1ix', |
| 64 | '/(vert|ind)ices$/' => '\1ex', |
| 65 | '/^(ox)en/' => '\1', |
| 66 | '/(alias)es$/' => '\1', |
| 67 | '/([octop|vir])i$/' => '\1us', |
| 68 | '/(cris|ax|test)es$/' => '\1is', |
| 69 | '/(shoe)s$/' => '\1', |
| 70 | '/(o)es$/' => '\1', |
| 71 | '/(bus|campus)es$/' => '\1', |
| 72 | '/([m|l])ice$/' => '\1ouse', |
| 73 | '/(x|ch|ss|sh)es$/' => '\1', |
| 74 | '/(m)ovies$/' => '\1\2ovie', |
| 75 | '/(s)eries$/' => '\1\2eries', |
| 76 | '/([^aeiouy]|qu)ies$/' => '\1y', |
| 77 | '/([lr])ves$/' => '\1f', |
| 78 | '/(tive)s$/' => '\1', |
| 79 | '/(hive)s$/' => '\1', |
| 80 | '/([^f])ves$/' => '\1fe', |
| 81 | '/(^analy)ses$/' => '\1sis', |
Phil Sturgeon | 50e57bc | 2011-08-13 10:26:04 -0600 | [diff] [blame] | 82 | '/((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] | 83 | '/([ti])a$/' => '\1um', |
| 84 | '/(p)eople$/' => '\1\2erson', |
| 85 | '/(m)en$/' => '\1an', |
| 86 | '/(s)tatuses$/' => '\1\2tatus', |
| 87 | '/(c)hildren$/' => '\1\2hild', |
| 88 | '/(n)ews$/' => '\1\2ews', |
| 89 | '/([^us])s$/' => '\1', |
Phil Sturgeon | 50e57bc | 2011-08-13 10:26:04 -0600 | [diff] [blame] | 90 | ); |
Eric Barnes | cde712c | 2011-12-09 11:27:51 -0500 | [diff] [blame] | 91 | |
Phil Sturgeon | 002b4be | 2012-02-29 12:12:49 +0000 | [diff] [blame] | 92 | foreach ($singular_rules as $rule => $replacement) |
| 93 | { |
| 94 | if (preg_match($rule, $result)) |
| 95 | { |
| 96 | $result = preg_replace($rule, $replacement, $result); |
| 97 | break; |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | return $result; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 102 | } |
| 103 | } |
| 104 | |
| 105 | // -------------------------------------------------------------------- |
| 106 | |
| 107 | /** |
| 108 | * Plural |
| 109 | * |
| 110 | * Takes a singular word and makes it plural |
| 111 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 112 | * @param string |
| 113 | * @param bool |
| 114 | * @return str |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 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 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 118 | function plural($str, $force = FALSE) |
Phil Sturgeon | 002b4be | 2012-02-29 12:12:49 +0000 | [diff] [blame] | 119 | { |
Phil Sturgeon | 50e57bc | 2011-08-13 10:26:04 -0600 | [diff] [blame] | 120 | $result = strval($str); |
Eric Barnes | cde712c | 2011-12-09 11:27:51 -0500 | [diff] [blame] | 121 | |
Phil Sturgeon | 002b4be | 2012-02-29 12:12:49 +0000 | [diff] [blame] | 122 | if ( ! is_countable($result)) |
| 123 | { |
| 124 | return $result; |
| 125 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 126 | |
Phil Sturgeon | 002b4be | 2012-02-29 12:12:49 +0000 | [diff] [blame] | 127 | $plural_rules = array( |
| 128 | '/^(ox)$/' => '\1\2en', // ox |
| 129 | '/([m|l])ouse$/' => '\1ice', // mouse, louse |
| 130 | '/(matr|vert|ind)ix|ex$/' => '\1ices', // matrix, vertex, index |
| 131 | '/(x|ch|ss|sh)$/' => '\1es', // search, switch, fix, box, process, address |
| 132 | '/([^aeiouy]|qu)y$/' => '\1ies', // query, ability, agency |
| 133 | '/(hive)$/' => '\1s', // archive, hive |
| 134 | '/(?:([^f])fe|([lr])f)$/' => '\1\2ves', // half, safe, wife |
| 135 | '/sis$/' => 'ses', // basis, diagnosis |
| 136 | '/([ti])um$/' => '\1a', // datum, medium |
| 137 | '/(p)erson$/' => '\1eople', // person, salesperson |
| 138 | '/(m)an$/' => '\1en', // man, woman, spokesman |
| 139 | '/(c)hild$/' => '\1hildren', // child |
| 140 | '/(buffal|tomat)o$/' => '\1\2oes', // buffalo, tomato |
| 141 | '/(bu|campu)s$/' => '\1\2ses', // bus, campus |
| 142 | '/(alias|status|virus)$/' => '\1es', // alias |
| 143 | '/(octop)us$/' => '\1i', // octopus |
| 144 | '/(ax|cris|test)is$/' => '\1es', // axis, crisis |
| 145 | '/s$/' => 's', // no change (compatibility) |
| 146 | '/$/' => 's', |
| 147 | ); |
| 148 | |
| 149 | foreach ($plural_rules as $rule => $replacement) |
| 150 | { |
| 151 | if (preg_match($rule, $result)) |
| 152 | { |
| 153 | $result = preg_replace($rule, $replacement, $result); |
| 154 | break; |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | return $result; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 159 | } |
| 160 | } |
| 161 | |
| 162 | // -------------------------------------------------------------------- |
| 163 | |
| 164 | /** |
| 165 | * Camelize |
| 166 | * |
| 167 | * Takes multiple words separated by spaces or underscores and camelizes them |
| 168 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 169 | * @param string |
| 170 | * @return str |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 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 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 174 | function camelize($str) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 175 | { |
Phil Sturgeon | e40c763 | 2012-03-10 13:05:08 +0000 | [diff] [blame^] | 176 | 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] | 177 | } |
| 178 | } |
| 179 | |
| 180 | // -------------------------------------------------------------------- |
| 181 | |
| 182 | /** |
| 183 | * Underscore |
| 184 | * |
| 185 | * Takes multiple words separated by spaces and underscores them |
| 186 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 187 | * @param string |
| 188 | * @return str |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 189 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 190 | if ( ! function_exists('underscore')) |
| 191 | { |
| 192 | function underscore($str) |
| 193 | { |
| 194 | return preg_replace('/[\s]+/', '_', strtolower(trim($str))); |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | // -------------------------------------------------------------------- |
| 199 | |
| 200 | /** |
| 201 | * Humanize |
| 202 | * |
Eric Barnes | cde712c | 2011-12-09 11:27:51 -0500 | [diff] [blame] | 203 | * Takes multiple words separated by the separator and changes them to spaces |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 204 | * |
Eric Barnes | cde712c | 2011-12-09 11:27:51 -0500 | [diff] [blame] | 205 | * @param string $str |
| 206 | * @param string $separator |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 207 | * @return str |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 208 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 209 | if ( ! function_exists('humanize')) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 210 | { |
Eric Barnes | cde712c | 2011-12-09 11:27:51 -0500 | [diff] [blame] | 211 | function humanize($str, $separator = '_') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 212 | { |
Eric Barnes | cde712c | 2011-12-09 11:27:51 -0500 | [diff] [blame] | 213 | return ucwords(preg_replace('/['.$separator.']+/', ' ', strtolower(trim($str)))); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 214 | } |
| 215 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 216 | |
Phil Sturgeon | 002b4be | 2012-02-29 12:12:49 +0000 | [diff] [blame] | 217 | /** |
| 218 | * Checks if the given word has a plural version. |
| 219 | * |
| 220 | * @param string the word to check |
| 221 | * @return bool if the word is countable |
| 222 | */ |
| 223 | if ( ! function_exists('is_countable')) |
| 224 | { |
| 225 | function is_countable($word) |
| 226 | { |
| 227 | return ! (in_array(strtolower(strval($word)), array( |
| 228 | 'equipment', 'information', 'rice', 'money', |
| 229 | 'species', 'series', 'fish', 'meta' |
| 230 | ))); |
| 231 | } |
| 232 | } |
| 233 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 234 | /* End of file inflector_helper.php */ |
Phil Sturgeon | 002b4be | 2012-02-29 12:12:49 +0000 | [diff] [blame] | 235 | /* Location: ./system/helpers/inflector_helper.php */ |