admin | 0fce0f2 | 2006-09-20 21:13:41 +0000 | [diff] [blame^] | 1 | <?php if (!defined('BASEPATH')) exit('No direct script access allowed'); |
| 2 | |
| 3 | /** |
| 4 | * Code Igniter |
| 5 | * |
| 6 | * An open source application development framework for PHP 4.3.2 or newer |
| 7 | * |
| 8 | * @package CodeIgniter |
| 9 | * @author Rick Ellis |
| 10 | * @copyright Copyright (c) 2006, pMachine, Inc. |
| 11 | * @license http://www.codeignitor.com/user_guide/license.html |
| 12 | * @link http://www.codeigniter.com |
| 13 | * @since Version 1.0 |
| 14 | * @filesource |
| 15 | */ |
| 16 | |
| 17 | // ------------------------------------------------------------------------ |
| 18 | |
| 19 | /** |
| 20 | * Inflector Class |
| 21 | * |
| 22 | * @package CodeIgniter |
| 23 | * @subpackage Libraries |
| 24 | * @category Inflection |
| 25 | * @author Rick Ellis |
| 26 | * @link http://www.codeigniter.com/user_guide/general/errors.html |
| 27 | */ |
| 28 | class CI_Inflector { |
| 29 | |
| 30 | |
| 31 | /** |
| 32 | * Constructor |
| 33 | * |
| 34 | * @access public |
| 35 | */ |
| 36 | function CI_Inflector() |
| 37 | { |
| 38 | log_message('debug', "Inflector Class Initialized"); |
| 39 | } |
| 40 | // END CI_Inflector() |
| 41 | |
| 42 | // -------------------------------------------------------------------- |
| 43 | |
| 44 | /** |
| 45 | * Singular |
| 46 | * |
| 47 | * Takes a plural word and makes it singular |
| 48 | * |
| 49 | * @access public |
| 50 | * @param string |
| 51 | * @return str |
| 52 | */ |
| 53 | function singular($str) |
| 54 | { |
| 55 | $str = strtolower(trim($str)); |
| 56 | $end = substr($str, -3); |
| 57 | |
| 58 | if ($end == 'ies') |
| 59 | { |
| 60 | $str = substr($str, 0, strlen($str)-3).'y'; |
| 61 | } |
| 62 | else |
| 63 | { |
| 64 | $end = substr($str, -1); |
| 65 | |
| 66 | if ($end == 's') |
| 67 | { |
| 68 | $str = substr($str, 0, strlen($str)-1); |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | return $str; |
| 73 | } |
| 74 | // END singular() |
| 75 | |
| 76 | // -------------------------------------------------------------------- |
| 77 | |
| 78 | /** |
| 79 | * Plural |
| 80 | * |
| 81 | * Takes a singular word and makes it plural |
| 82 | * |
| 83 | * @access public |
| 84 | * @param string |
| 85 | * @return str |
| 86 | */ |
| 87 | function plural($str) |
| 88 | { |
| 89 | $str = strtolower(trim($str)); |
| 90 | $end = substr($str, -1); |
| 91 | |
| 92 | if ($end == 'y') |
| 93 | { |
| 94 | $str = substr($str, 0, strlen($str)-1).'ies'; |
| 95 | } |
| 96 | elseif ($end != 's') |
| 97 | { |
| 98 | $str .= 's'; |
| 99 | } |
| 100 | |
| 101 | return $str; |
| 102 | } |
| 103 | // END plural() |
| 104 | |
| 105 | // -------------------------------------------------------------------- |
| 106 | |
| 107 | /** |
| 108 | * Camelize |
| 109 | * |
| 110 | * Takes multiple words separated by spaces or underscores and camelizes them |
| 111 | * |
| 112 | * @access public |
| 113 | * @param string |
| 114 | * @return str |
| 115 | */ |
| 116 | function camelize($str) |
| 117 | { |
| 118 | $str = 'x'.strtolower(trim($str)); |
| 119 | $str = ucwords(preg_replace('/[\s_]+/', ' ', $str)); |
| 120 | return substr(str_replace(' ', '', $str), 1); |
| 121 | } |
| 122 | // END camelize() |
| 123 | |
| 124 | // -------------------------------------------------------------------- |
| 125 | |
| 126 | /** |
| 127 | * Underscore |
| 128 | * |
| 129 | * Takes multiple words separated by spaces and underscores them |
| 130 | * |
| 131 | * @access public |
| 132 | * @param string |
| 133 | * @return str |
| 134 | */ |
| 135 | function underscore($str) |
| 136 | { |
| 137 | return preg_replace('/[\s]+/', '_', strtolower(trim($str))); |
| 138 | } |
| 139 | // END underscore() |
| 140 | |
| 141 | // -------------------------------------------------------------------- |
| 142 | |
| 143 | /** |
| 144 | * Humanize |
| 145 | * |
| 146 | * Takes multiple words separated by underscores and changes them to spaces |
| 147 | * |
| 148 | * @access public |
| 149 | * @param string |
| 150 | * @return str |
| 151 | */ |
| 152 | function humanize($str) |
| 153 | { |
| 154 | return ucwords(preg_replace('/[_]+/', ' ', strtolower(trim($str)))); |
| 155 | } |
| 156 | // END humanize() |
| 157 | |
| 158 | } |
| 159 | // END CI_Inflector |
| 160 | ?> |