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