Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1 | <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); |
| 2 | /** |
| 3 | * CodeIgniter |
| 4 | * |
| 5 | * An open source application development framework for PHP 4.3.2 or newer |
| 6 | * |
| 7 | * @package CodeIgniter |
| 8 | * @author ExpressionEngine Dev Team |
Derek Jones | 7f3719f | 2010-01-05 13:35:37 +0000 | [diff] [blame] | 9 | * @copyright Copyright (c) 2008 - 2010, 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 | { |
| 44 | $str = strtolower(trim($str)); |
| 45 | $end = substr($str, -3); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame^] | 46 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 47 | if ($end == 'ies') |
| 48 | { |
| 49 | $str = substr($str, 0, strlen($str)-3).'y'; |
| 50 | } |
| 51 | elseif ($end == 'ses') |
| 52 | { |
| 53 | $str = substr($str, 0, strlen($str)-2); |
| 54 | } |
| 55 | else |
| 56 | { |
| 57 | $end = substr($str, -1); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame^] | 58 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 59 | if ($end == 's') |
| 60 | { |
| 61 | $str = substr($str, 0, strlen($str)-1); |
| 62 | } |
| 63 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame^] | 64 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 65 | return $str; |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | // -------------------------------------------------------------------- |
| 70 | |
| 71 | /** |
| 72 | * Plural |
| 73 | * |
| 74 | * Takes a singular word and makes it plural |
| 75 | * |
| 76 | * @access public |
| 77 | * @param string |
| 78 | * @param bool |
| 79 | * @return str |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame^] | 80 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 81 | if ( ! function_exists('plural')) |
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 | function plural($str, $force = FALSE) |
| 84 | { |
| 85 | $str = strtolower(trim($str)); |
| 86 | $end = substr($str, -1); |
| 87 | |
| 88 | if ($end == 'y') |
| 89 | { |
Derek Jones | a6aabaf | 2009-02-10 17:37:52 +0000 | [diff] [blame] | 90 | // Y preceded by vowel => regular plural |
| 91 | $vowels = array('a', 'e', 'i', 'o', 'u'); |
| 92 | $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] | 93 | } |
Greg Aker | a3f4718 | 2009-11-04 17:23:32 +0000 | [diff] [blame] | 94 | elseif ($end == 'h') |
| 95 | { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame^] | 96 | if (substr($str, -2) == 'ch' || substr($str, -2) == 'sh') |
| 97 | { |
| 98 | $str .= 'es'; |
| 99 | } |
| 100 | else |
| 101 | { |
| 102 | $str .= 's'; |
| 103 | } |
Greg Aker | a3f4718 | 2009-11-04 17:23:32 +0000 | [diff] [blame] | 104 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 105 | elseif ($end == 's') |
| 106 | { |
| 107 | if ($force == TRUE) |
| 108 | { |
| 109 | $str .= 'es'; |
| 110 | } |
| 111 | } |
| 112 | else |
| 113 | { |
| 114 | $str .= 's'; |
| 115 | } |
| 116 | |
| 117 | return $str; |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | // -------------------------------------------------------------------- |
| 122 | |
| 123 | /** |
| 124 | * Camelize |
| 125 | * |
| 126 | * Takes multiple words separated by spaces or underscores and camelizes them |
| 127 | * |
| 128 | * @access public |
| 129 | * @param string |
| 130 | * @return str |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame^] | 131 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 132 | if ( ! function_exists('camelize')) |
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 | function camelize($str) |
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 | $str = 'x'.strtolower(trim($str)); |
| 137 | $str = ucwords(preg_replace('/[\s_]+/', ' ', $str)); |
| 138 | return substr(str_replace(' ', '', $str), 1); |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | // -------------------------------------------------------------------- |
| 143 | |
| 144 | /** |
| 145 | * Underscore |
| 146 | * |
| 147 | * Takes multiple words separated by spaces and underscores them |
| 148 | * |
| 149 | * @access public |
| 150 | * @param string |
| 151 | * @return str |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame^] | 152 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 153 | if ( ! function_exists('underscore')) |
| 154 | { |
| 155 | function underscore($str) |
| 156 | { |
| 157 | return preg_replace('/[\s]+/', '_', strtolower(trim($str))); |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | // -------------------------------------------------------------------- |
| 162 | |
| 163 | /** |
| 164 | * Humanize |
| 165 | * |
| 166 | * Takes multiple words separated by underscores and changes them to spaces |
| 167 | * |
| 168 | * @access public |
| 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('humanize')) |
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 humanize($str) |
| 175 | { |
| 176 | return ucwords(preg_replace('/[_]+/', ' ', strtolower(trim($str)))); |
| 177 | } |
| 178 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame^] | 179 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 180 | |
| 181 | /* End of file inflector_helper.php */ |
Derek Jones | a3ffbbb | 2008-05-11 18:18:29 +0000 | [diff] [blame] | 182 | /* Location: ./system/helpers/inflector_helper.php */ |