blob: 3042202cb09496ef20e481f0c6d6017f1136cbbf [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 *
Greg Aker741de1c2010-11-10 14:52:57 -06005 * An open source application development framework for PHP 5.1.6 or newer
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
7 * @package CodeIgniter
8 * @author ExpressionEngine Dev Team
Greg Aker0711dc82011-01-05 10:49:40 -06009 * @copyright Copyright (c) 2008 - 2011, 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 {
Derek Allard2067d1a2008-11-13 22:59:24 +000085 $end = substr($str, -1);
86
Kyle Farris7c6a5892011-03-11 17:33:05 -050087 if (preg_match('/y/i',$end))
Derek Allard2067d1a2008-11-13 22:59:24 +000088 {
Derek Jonesa6aabaf2009-02-10 17:37:52 +000089 // Y preceded by vowel => regular plural
Kyle Farris7c6a5892011-03-11 17:33:05 -050090 $vowels = array('a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U');
Derek Jonesa6aabaf2009-02-10 17:37:52 +000091 $str = in_array(substr($str, -2, 1), $vowels) ? $str.'s' : substr($str, 0, -1).'ies';
Derek Allard2067d1a2008-11-13 22:59:24 +000092 }
Kyle Farris7c6a5892011-03-11 17:33:05 -050093 elseif (preg_match('/h/i',$end))
Greg Akera3f47182009-11-04 17:23:32 +000094 {
Kyle Farris7c6a5892011-03-11 17:33:05 -050095 if(preg_match('/^[c|s]h$/i',substr($str, -2)))
Barry Mienydd671972010-10-04 16:33:58 +020096 {
97 $str .= 'es';
98 }
99 else
100 {
101 $str .= 's';
102 }
Greg Akera3f47182009-11-04 17:23:32 +0000103 }
Kyle Farris7c6a5892011-03-11 17:33:05 -0500104 elseif (preg_match('/s/i',$end))
Derek Allard2067d1a2008-11-13 22:59:24 +0000105 {
106 if ($force == TRUE)
107 {
108 $str .= 'es';
109 }
110 }
111 else
112 {
113 $str .= 's';
114 }
115
116 return $str;
117 }
118}
119
120// --------------------------------------------------------------------
121
122/**
123 * Camelize
124 *
125 * Takes multiple words separated by spaces or underscores and camelizes them
126 *
127 * @access public
128 * @param string
129 * @return str
Barry Mienydd671972010-10-04 16:33:58 +0200130 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000131if ( ! function_exists('camelize'))
Barry Mienydd671972010-10-04 16:33:58 +0200132{
Derek Allard2067d1a2008-11-13 22:59:24 +0000133 function camelize($str)
Barry Mienydd671972010-10-04 16:33:58 +0200134 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000135 $str = 'x'.strtolower(trim($str));
136 $str = ucwords(preg_replace('/[\s_]+/', ' ', $str));
137 return substr(str_replace(' ', '', $str), 1);
138 }
139}
140
141// --------------------------------------------------------------------
142
143/**
144 * Underscore
145 *
146 * Takes multiple words separated by spaces and underscores them
147 *
148 * @access public
149 * @param string
150 * @return str
Barry Mienydd671972010-10-04 16:33:58 +0200151 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000152if ( ! function_exists('underscore'))
153{
154 function underscore($str)
155 {
156 return preg_replace('/[\s]+/', '_', strtolower(trim($str)));
157 }
158}
159
160// --------------------------------------------------------------------
161
162/**
163 * Humanize
164 *
165 * Takes multiple words separated by underscores and changes them to spaces
166 *
167 * @access public
168 * @param string
169 * @return str
Barry Mienydd671972010-10-04 16:33:58 +0200170 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000171if ( ! function_exists('humanize'))
Barry Mienydd671972010-10-04 16:33:58 +0200172{
Derek Allard2067d1a2008-11-13 22:59:24 +0000173 function humanize($str)
174 {
175 return ucwords(preg_replace('/[_]+/', ' ', strtolower(trim($str))));
176 }
177}
Barry Mienydd671972010-10-04 16:33:58 +0200178
Derek Allard2067d1a2008-11-13 22:59:24 +0000179
180/* End of file inflector_helper.php */
Derek Jonesa3ffbbb2008-05-11 18:18:29 +0000181/* Location: ./system/helpers/inflector_helper.php */