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 |
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( |
Greg Aker | 03abee3 | 2011-12-25 00:31:29 -0600 | [diff] [blame] | 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', |
Phil Sturgeon | 50e57bc | 2011-08-13 10:26:04 -0600 | [diff] [blame] | 78 | '/((a)naly|(b)a|(d)iagno|(p)arenthe|(p)rogno|(s)ynop|(t)he)ses$/' => '\1\2sis', |
Greg Aker | 03abee3 | 2011-12-25 00:31:29 -0600 | [diff] [blame] | 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', |
Phil Sturgeon | 50e57bc | 2011-08-13 10:26:04 -0600 | [diff] [blame] | 86 | ); |
Eric Barnes | cde712c | 2011-12-09 11:27:51 -0500 | [diff] [blame] | 87 | |
Andrey Andreev | 1e6b72c | 2012-01-06 19:09:22 +0200 | [diff] [blame^] | 88 | return preg_replace(array_keys($singular_values), $singular_values, $result); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 89 | } |
| 90 | } |
| 91 | |
| 92 | // -------------------------------------------------------------------- |
| 93 | |
| 94 | /** |
| 95 | * Plural |
| 96 | * |
| 97 | * Takes a singular word and makes it plural |
| 98 | * |
| 99 | * @access public |
| 100 | * @param string |
| 101 | * @param bool |
| 102 | * @return str |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 103 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 104 | if ( ! function_exists('plural')) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 105 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 106 | function plural($str, $force = FALSE) |
Phil Sturgeon | 50e57bc | 2011-08-13 10:26:04 -0600 | [diff] [blame] | 107 | { |
| 108 | $result = strval($str); |
Eric Barnes | cde712c | 2011-12-09 11:27:51 -0500 | [diff] [blame] | 109 | |
Phil Sturgeon | 50e57bc | 2011-08-13 10:26:04 -0600 | [diff] [blame] | 110 | $plural_rules = array( |
Greg Aker | 03abee3 | 2011-12-25 00:31:29 -0600 | [diff] [blame] | 111 | '/^(ox)$/' => '\1\2en', // ox |
| 112 | '/([m|l])ouse$/' => '\1ice', // mouse, louse |
| 113 | '/(matr|vert|ind)ix|ex$/' => '\1ices', // matrix, vertex, index |
| 114 | '/(x|ch|ss|sh)$/' => '\1es', // search, switch, fix, box, process, address |
| 115 | '/([^aeiouy]|qu)y$/' => '\1ies', // query, ability, agency |
| 116 | '/(hive)$/' => '\1s', // archive, hive |
| 117 | '/(?:([^f])fe|([lr])f)$/' => '\1\2ves', // half, safe, wife |
| 118 | '/sis$/' => 'ses', // basis, diagnosis |
| 119 | '/([ti])um$/' => '\1a', // datum, medium |
| 120 | '/(p)erson$/' => '\1eople', // person, salesperson |
| 121 | '/(m)an$/' => '\1en', // man, woman, spokesman |
| 122 | '/(c)hild$/' => '\1hildren', // child |
| 123 | '/(buffal|tomat)o$/' => '\1\2oes', // buffalo, tomato |
| 124 | '/(bu|campu)s$/' => '\1\2ses', // bus, campus |
| 125 | '/(alias|status|virus)/' => '\1es', // alias |
| 126 | '/(octop)us$/' => '\1i', // octopus |
| 127 | '/(ax|cris|test)is$/' => '\1es', // axis, crisis |
| 128 | '/s$/' => 's', // no change (compatibility) |
| 129 | '/$/' => 's', |
Phil Sturgeon | 50e57bc | 2011-08-13 10:26:04 -0600 | [diff] [blame] | 130 | ); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 131 | |
Andrey Andreev | 1e6b72c | 2012-01-06 19:09:22 +0200 | [diff] [blame^] | 132 | return preg_replace(array_keys($plural_rules), $plural_rules, $result); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 133 | } |
| 134 | } |
| 135 | |
| 136 | // -------------------------------------------------------------------- |
| 137 | |
| 138 | /** |
| 139 | * Camelize |
| 140 | * |
| 141 | * Takes multiple words separated by spaces or underscores and camelizes them |
| 142 | * |
| 143 | * @access public |
| 144 | * @param string |
| 145 | * @return str |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 146 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 147 | if ( ! function_exists('camelize')) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 148 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 149 | function camelize($str) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 150 | { |
Andrey Andreev | 1e6b72c | 2012-01-06 19:09:22 +0200 | [diff] [blame^] | 151 | return substr(str_replace(' ', '', ucwords(preg_replace('/[\s_]+/', ' ', $str))), 1); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 152 | } |
| 153 | } |
| 154 | |
| 155 | // -------------------------------------------------------------------- |
| 156 | |
| 157 | /** |
| 158 | * Underscore |
| 159 | * |
| 160 | * Takes multiple words separated by spaces and underscores them |
| 161 | * |
| 162 | * @access public |
| 163 | * @param string |
| 164 | * @return str |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 165 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 166 | if ( ! function_exists('underscore')) |
| 167 | { |
| 168 | function underscore($str) |
| 169 | { |
| 170 | return preg_replace('/[\s]+/', '_', strtolower(trim($str))); |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | // -------------------------------------------------------------------- |
| 175 | |
| 176 | /** |
| 177 | * Humanize |
| 178 | * |
Eric Barnes | cde712c | 2011-12-09 11:27:51 -0500 | [diff] [blame] | 179 | * Takes multiple words separated by the separator and changes them to spaces |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 180 | * |
| 181 | * @access public |
Eric Barnes | cde712c | 2011-12-09 11:27:51 -0500 | [diff] [blame] | 182 | * @param string $str |
| 183 | * @param string $separator |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 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('humanize')) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 187 | { |
Eric Barnes | cde712c | 2011-12-09 11:27:51 -0500 | [diff] [blame] | 188 | function humanize($str, $separator = '_') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 189 | { |
Eric Barnes | cde712c | 2011-12-09 11:27:51 -0500 | [diff] [blame] | 190 | return ucwords(preg_replace('/['.$separator.']+/', ' ', strtolower(trim($str)))); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 191 | } |
| 192 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 193 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 194 | /* End of file inflector_helper.php */ |
Andrey Andreev | 1e6b72c | 2012-01-06 19:09:22 +0200 | [diff] [blame^] | 195 | /* Location: ./system/helpers/inflector_helper.php */ |