blob: cd05c8d6d43d378aa5646c4e888887e5fb5b3bab [file] [log] [blame]
Derek Allard2067d1a2008-11-13 22:59:24 +00001<?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 Jones7f3719f2010-01-05 13:35:37 +00009 * @copyright Copyright (c) 2008 - 2010, EllisLab, Inc.
Derek Allard2067d1a2008-11-13 22:59:24 +000010 * @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 Mienydd671972010-10-04 16:33:58 +020039 */
Derek Allard2067d1a2008-11-13 22:59:24 +000040if ( ! function_exists('singular'))
Barry Mienydd671972010-10-04 16:33:58 +020041{
Derek Allard2067d1a2008-11-13 22:59:24 +000042 function singular($str)
43 {
44 $str = strtolower(trim($str));
45 $end = substr($str, -3);
Barry Mienydd671972010-10-04 16:33:58 +020046
Derek Allard2067d1a2008-11-13 22:59:24 +000047 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 Mienydd671972010-10-04 16:33:58 +020058
Derek Allard2067d1a2008-11-13 22:59:24 +000059 if ($end == 's')
60 {
61 $str = substr($str, 0, strlen($str)-1);
62 }
63 }
Barry Mienydd671972010-10-04 16:33:58 +020064
Derek Allard2067d1a2008-11-13 22:59:24 +000065 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 Mienydd671972010-10-04 16:33:58 +020080 */
Derek Allard2067d1a2008-11-13 22:59:24 +000081if ( ! function_exists('plural'))
Barry Mienydd671972010-10-04 16:33:58 +020082{
Derek Allard2067d1a2008-11-13 22:59:24 +000083 function plural($str, $force = FALSE)
84 {
85 $str = strtolower(trim($str));
86 $end = substr($str, -1);
87
88 if ($end == 'y')
89 {
Derek Jonesa6aabaf2009-02-10 17:37:52 +000090 // 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 Allard2067d1a2008-11-13 22:59:24 +000093 }
Greg Akera3f47182009-11-04 17:23:32 +000094 elseif ($end == 'h')
95 {
Barry Mienydd671972010-10-04 16:33:58 +020096 if (substr($str, -2) == 'ch' || substr($str, -2) == 'sh')
97 {
98 $str .= 'es';
99 }
100 else
101 {
102 $str .= 's';
103 }
Greg Akera3f47182009-11-04 17:23:32 +0000104 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000105 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 Mienydd671972010-10-04 16:33:58 +0200131 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000132if ( ! function_exists('camelize'))
Barry Mienydd671972010-10-04 16:33:58 +0200133{
Derek Allard2067d1a2008-11-13 22:59:24 +0000134 function camelize($str)
Barry Mienydd671972010-10-04 16:33:58 +0200135 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000136 $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 Mienydd671972010-10-04 16:33:58 +0200152 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000153if ( ! 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 Mienydd671972010-10-04 16:33:58 +0200171 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000172if ( ! function_exists('humanize'))
Barry Mienydd671972010-10-04 16:33:58 +0200173{
Derek Allard2067d1a2008-11-13 22:59:24 +0000174 function humanize($str)
175 {
176 return ucwords(preg_replace('/[_]+/', ' ', strtolower(trim($str))));
177 }
178}
Barry Mienydd671972010-10-04 16:33:58 +0200179
Derek Allard2067d1a2008-11-13 22:59:24 +0000180
181/* End of file inflector_helper.php */
Derek Jonesa3ffbbb2008-05-11 18:18:29 +0000182/* Location: ./system/helpers/inflector_helper.php */