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 | * |
| 7 | * @package CodeIgniter |
| 8 | * @author ExpressionEngine Dev Team |
Greg Aker | 0711dc8 | 2011-01-05 10:49:40 -0600 | [diff] [blame] | 9 | * @copyright Copyright (c) 2008 - 2011, EllisLab, Inc. |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 10 | * @license http://codeigniter.com/user_guide/license.html |
| 11 | * @link http://codeigniter.com |
| 12 | * @since Version 1.0 |
| 13 | * @filesource |
| 14 | */ |
| 15 | |
| 16 | // ------------------------------------------------------------------------ |
| 17 | |
| 18 | /** |
| 19 | * CodeIgniter Inflector Helpers |
| 20 | * |
| 21 | * @package CodeIgniter |
| 22 | * @subpackage Helpers |
| 23 | * @category Helpers |
| 24 | * @author ExpressionEngine Dev Team |
| 25 | * @link http://codeigniter.com/user_guide/helpers/directory_helper.html |
| 26 | */ |
| 27 | |
| 28 | |
| 29 | // -------------------------------------------------------------------- |
| 30 | |
| 31 | /** |
| 32 | * Singular |
| 33 | * |
| 34 | * Takes a plural word and makes it singular |
| 35 | * |
| 36 | * @access public |
| 37 | * @param string |
| 38 | * @return str |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 39 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 40 | if ( ! function_exists('singular')) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 41 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 42 | function singular($str) |
| 43 | { |
Kyle Farris | 2f620fe | 2011-03-11 18:34:07 -0500 | [diff] [blame] | 44 | $str = trim($str); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 45 | $end = substr($str, -3); |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame^] | 46 | |
| 47 | $str = preg_replace('/(.*)?([s|c]h)es/i','$1$2',$str); |
| 48 | |
Kyle Farris | 2f620fe | 2011-03-11 18:34:07 -0500 | [diff] [blame] | 49 | if (strtolower($end) == 'ies') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 50 | { |
Kyle Farris | 2f620fe | 2011-03-11 18:34:07 -0500 | [diff] [blame] | 51 | $str = substr($str, 0, strlen($str)-3).(preg_match('/[a-z]/',$end) ? 'y' : 'Y'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 52 | } |
Kyle Farris | 2f620fe | 2011-03-11 18:34:07 -0500 | [diff] [blame] | 53 | elseif (strtolower($end) == 'ses') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 54 | { |
| 55 | $str = substr($str, 0, strlen($str)-2); |
| 56 | } |
| 57 | else |
| 58 | { |
Kyle Farris | 2f620fe | 2011-03-11 18:34:07 -0500 | [diff] [blame] | 59 | $end = strtolower(substr($str, -1)); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 60 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 61 | if ($end == 's') |
| 62 | { |
| 63 | $str = substr($str, 0, strlen($str)-1); |
| 64 | } |
| 65 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 66 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 67 | return $str; |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | // -------------------------------------------------------------------- |
| 72 | |
| 73 | /** |
| 74 | * Plural |
| 75 | * |
| 76 | * Takes a singular word and makes it plural |
| 77 | * |
| 78 | * @access public |
| 79 | * @param string |
| 80 | * @param bool |
| 81 | * @return str |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 82 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 83 | if ( ! function_exists('plural')) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 84 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 85 | function plural($str, $force = FALSE) |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame^] | 86 | { |
| 87 | $str = trim($str); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 88 | $end = substr($str, -1); |
| 89 | |
Kyle Farris | 7c6a589 | 2011-03-11 17:33:05 -0500 | [diff] [blame] | 90 | if (preg_match('/y/i',$end)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 91 | { |
Derek Jones | a6aabaf | 2009-02-10 17:37:52 +0000 | [diff] [blame] | 92 | // Y preceded by vowel => regular plural |
Kyle Farris | 7c6a589 | 2011-03-11 17:33:05 -0500 | [diff] [blame] | 93 | $vowels = array('a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'); |
Derek Jones | a6aabaf | 2009-02-10 17:37:52 +0000 | [diff] [blame] | 94 | $str = in_array(substr($str, -2, 1), $vowels) ? $str.'s' : substr($str, 0, -1).'ies'; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 95 | } |
Kyle Farris | 7c6a589 | 2011-03-11 17:33:05 -0500 | [diff] [blame] | 96 | elseif (preg_match('/h/i',$end)) |
Greg Aker | a3f4718 | 2009-11-04 17:23:32 +0000 | [diff] [blame] | 97 | { |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame^] | 98 | if(preg_match('/^[c|s]h$/i',substr($str, -2))) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 99 | { |
| 100 | $str .= 'es'; |
| 101 | } |
| 102 | else |
| 103 | { |
| 104 | $str .= 's'; |
| 105 | } |
Greg Aker | a3f4718 | 2009-11-04 17:23:32 +0000 | [diff] [blame] | 106 | } |
Kyle Farris | 7c6a589 | 2011-03-11 17:33:05 -0500 | [diff] [blame] | 107 | elseif (preg_match('/s/i',$end)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 108 | { |
| 109 | if ($force == TRUE) |
| 110 | { |
| 111 | $str .= 'es'; |
| 112 | } |
| 113 | } |
| 114 | else |
| 115 | { |
| 116 | $str .= 's'; |
| 117 | } |
| 118 | |
| 119 | return $str; |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | // -------------------------------------------------------------------- |
| 124 | |
| 125 | /** |
| 126 | * Camelize |
| 127 | * |
| 128 | * Takes multiple words separated by spaces or underscores and camelizes them |
| 129 | * |
| 130 | * @access public |
| 131 | * @param string |
| 132 | * @return str |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 133 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 134 | if ( ! function_exists('camelize')) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 135 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 136 | function camelize($str) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 137 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 138 | $str = 'x'.strtolower(trim($str)); |
| 139 | $str = ucwords(preg_replace('/[\s_]+/', ' ', $str)); |
| 140 | return substr(str_replace(' ', '', $str), 1); |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | // -------------------------------------------------------------------- |
| 145 | |
| 146 | /** |
| 147 | * Underscore |
| 148 | * |
| 149 | * Takes multiple words separated by spaces and underscores them |
| 150 | * |
| 151 | * @access public |
| 152 | * @param string |
| 153 | * @return str |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 154 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 155 | if ( ! function_exists('underscore')) |
| 156 | { |
| 157 | function underscore($str) |
| 158 | { |
| 159 | return preg_replace('/[\s]+/', '_', strtolower(trim($str))); |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | // -------------------------------------------------------------------- |
| 164 | |
| 165 | /** |
| 166 | * Humanize |
| 167 | * |
| 168 | * Takes multiple words separated by underscores and changes them to spaces |
| 169 | * |
| 170 | * @access public |
| 171 | * @param string |
| 172 | * @return str |
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 | if ( ! function_exists('humanize')) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 175 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 176 | function humanize($str) |
| 177 | { |
| 178 | return ucwords(preg_replace('/[_]+/', ' ', strtolower(trim($str)))); |
| 179 | } |
| 180 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 181 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 182 | |
| 183 | /* End of file inflector_helper.php */ |
Derek Jones | a3ffbbb | 2008-05-11 18:18:29 +0000 | [diff] [blame] | 184 | /* Location: ./system/helpers/inflector_helper.php */ |