blob: c7c113b8a109cd8c5f12eeb01898a00ce065da5d [file] [log] [blame]
Derek Jones37f4b9c2011-07-01 17:56:50 -05001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
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 {
Kyle Farris2f620fe2011-03-11 18:34:07 -050044 $str = trim($str);
Derek Allard2067d1a2008-11-13 22:59:24 +000045 $end = substr($str, -3);
Derek Jones37f4b9c2011-07-01 17:56:50 -050046
47 $str = preg_replace('/(.*)?([s|c]h)es/i','$1$2',$str);
48
Kyle Farris2f620fe2011-03-11 18:34:07 -050049 if (strtolower($end) == 'ies')
Derek Allard2067d1a2008-11-13 22:59:24 +000050 {
Kyle Farris2f620fe2011-03-11 18:34:07 -050051 $str = substr($str, 0, strlen($str)-3).(preg_match('/[a-z]/',$end) ? 'y' : 'Y');
Derek Allard2067d1a2008-11-13 22:59:24 +000052 }
Kyle Farris2f620fe2011-03-11 18:34:07 -050053 elseif (strtolower($end) == 'ses')
Derek Allard2067d1a2008-11-13 22:59:24 +000054 {
55 $str = substr($str, 0, strlen($str)-2);
56 }
57 else
58 {
Kyle Farris2f620fe2011-03-11 18:34:07 -050059 $end = strtolower(substr($str, -1));
Barry Mienydd671972010-10-04 16:33:58 +020060
Derek Allard2067d1a2008-11-13 22:59:24 +000061 if ($end == 's')
62 {
63 $str = substr($str, 0, strlen($str)-1);
64 }
65 }
Barry Mienydd671972010-10-04 16:33:58 +020066
Derek Allard2067d1a2008-11-13 22:59:24 +000067 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 Mienydd671972010-10-04 16:33:58 +020082 */
Derek Allard2067d1a2008-11-13 22:59:24 +000083if ( ! function_exists('plural'))
Barry Mienydd671972010-10-04 16:33:58 +020084{
Derek Allard2067d1a2008-11-13 22:59:24 +000085 function plural($str, $force = FALSE)
Derek Jones37f4b9c2011-07-01 17:56:50 -050086 {
87 $str = trim($str);
Derek Allard2067d1a2008-11-13 22:59:24 +000088 $end = substr($str, -1);
89
Kyle Farris7c6a5892011-03-11 17:33:05 -050090 if (preg_match('/y/i',$end))
Derek Allard2067d1a2008-11-13 22:59:24 +000091 {
Derek Jonesa6aabaf2009-02-10 17:37:52 +000092 // Y preceded by vowel => regular plural
Kyle Farris7c6a5892011-03-11 17:33:05 -050093 $vowels = array('a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U');
Derek Jonesa6aabaf2009-02-10 17:37:52 +000094 $str = in_array(substr($str, -2, 1), $vowels) ? $str.'s' : substr($str, 0, -1).'ies';
Derek Allard2067d1a2008-11-13 22:59:24 +000095 }
Kyle Farris7c6a5892011-03-11 17:33:05 -050096 elseif (preg_match('/h/i',$end))
Greg Akera3f47182009-11-04 17:23:32 +000097 {
Derek Jones37f4b9c2011-07-01 17:56:50 -050098 if(preg_match('/^[c|s]h$/i',substr($str, -2)))
Barry Mienydd671972010-10-04 16:33:58 +020099 {
100 $str .= 'es';
101 }
102 else
103 {
104 $str .= 's';
105 }
Greg Akera3f47182009-11-04 17:23:32 +0000106 }
Kyle Farris7c6a5892011-03-11 17:33:05 -0500107 elseif (preg_match('/s/i',$end))
Derek Allard2067d1a2008-11-13 22:59:24 +0000108 {
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 Mienydd671972010-10-04 16:33:58 +0200133 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000134if ( ! function_exists('camelize'))
Barry Mienydd671972010-10-04 16:33:58 +0200135{
Derek Allard2067d1a2008-11-13 22:59:24 +0000136 function camelize($str)
Barry Mienydd671972010-10-04 16:33:58 +0200137 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000138 $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 Mienydd671972010-10-04 16:33:58 +0200154 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000155if ( ! 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 Mienydd671972010-10-04 16:33:58 +0200173 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000174if ( ! function_exists('humanize'))
Barry Mienydd671972010-10-04 16:33:58 +0200175{
Derek Allard2067d1a2008-11-13 22:59:24 +0000176 function humanize($str)
177 {
178 return ucwords(preg_replace('/[_]+/', ' ', strtolower(trim($str))));
179 }
180}
Barry Mienydd671972010-10-04 16:33:58 +0200181
Derek Allard2067d1a2008-11-13 22:59:24 +0000182
183/* End of file inflector_helper.php */
Derek Jonesa3ffbbb2008-05-11 18:18:29 +0000184/* Location: ./system/helpers/inflector_helper.php */