blob: 7b99bc5b4f99973abcab85c14e503c65ae5f7e36 [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 {
Phil Sturgeon50e57bc2011-08-13 10:26:04 -060044 $result = strval($str);
Barry Mienydd671972010-10-04 16:33:58 +020045
Phil Sturgeon50e57bc2011-08-13 10:26:04 -060046 $singular_rules = array(
47 '/(matr)ices$/' => '\1ix',
48 '/(vert|ind)ices$/' => '\1ex',
49 '/^(ox)en/' => '\1',
50 '/(alias)es$/' => '\1',
51 '/([octop|vir])i$/' => '\1us',
52 '/(cris|ax|test)es$/' => '\1is',
53 '/(shoe)s$/' => '\1',
54 '/(o)es$/' => '\1',
55 '/(bus|campus)es$/' => '\1',
56 '/([m|l])ice$/' => '\1ouse',
57 '/(x|ch|ss|sh)es$/' => '\1',
58 '/(m)ovies$/' => '\1\2ovie',
59 '/(s)eries$/' => '\1\2eries',
60 '/([^aeiouy]|qu)ies$/' => '\1y',
61 '/([lr])ves$/' => '\1f',
62 '/(tive)s$/' => '\1',
63 '/(hive)s$/' => '\1',
64 '/([^f])ves$/' => '\1fe',
65 '/(^analy)ses$/' => '\1sis',
66 '/((a)naly|(b)a|(d)iagno|(p)arenthe|(p)rogno|(s)ynop|(t)he)ses$/' => '\1\2sis',
67 '/([ti])a$/' => '\1um',
68 '/(p)eople$/' => '\1\2erson',
69 '/(m)en$/' => '\1an',
70 '/(s)tatuses$/' => '\1\2tatus',
71 '/(c)hildren$/' => '\1\2hild',
72 '/(n)ews$/' => '\1\2ews',
73 '/([^u])s$/' => '\1',
74 );
75
76 foreach ($singular_rules as $rule => $replacement)
77 {
78 if (preg_match($rule, $result))
Derek Allard2067d1a2008-11-13 22:59:24 +000079 {
Phil Sturgeon50e57bc2011-08-13 10:26:04 -060080 $result = preg_replace($rule, $replacement, $result);
81 break;
Derek Allard2067d1a2008-11-13 22:59:24 +000082 }
83 }
Barry Mienydd671972010-10-04 16:33:58 +020084
Phil Sturgeon50e57bc2011-08-13 10:26:04 -060085 return $result;
Derek Allard2067d1a2008-11-13 22:59:24 +000086 }
87}
88
89// --------------------------------------------------------------------
90
91/**
92 * Plural
93 *
94 * Takes a singular word and makes it plural
95 *
96 * @access public
97 * @param string
98 * @param bool
99 * @return str
Barry Mienydd671972010-10-04 16:33:58 +0200100 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000101if ( ! function_exists('plural'))
Barry Mienydd671972010-10-04 16:33:58 +0200102{
Derek Allard2067d1a2008-11-13 22:59:24 +0000103 function plural($str, $force = FALSE)
Phil Sturgeon50e57bc2011-08-13 10:26:04 -0600104 {
105 $result = strval($str);
106
107 $plural_rules = array(
108 '/^(ox)$/' => '\1\2en', // ox
109 '/([m|l])ouse$/' => '\1ice', // mouse, louse
110 '/(matr|vert|ind)ix|ex$/' => '\1ices', // matrix, vertex, index
111 '/(x|ch|ss|sh)$/' => '\1es', // search, switch, fix, box, process, address
112 '/([^aeiouy]|qu)y$/' => '\1ies', // query, ability, agency
113 '/(hive)$/' => '\1s', // archive, hive
114 '/(?:([^f])fe|([lr])f)$/' => '\1\2ves', // half, safe, wife
115 '/sis$/' => 'ses', // basis, diagnosis
116 '/([ti])um$/' => '\1a', // datum, medium
117 '/(p)erson$/' => '\1eople', // person, salesperson
118 '/(m)an$/' => '\1en', // man, woman, spokesman
119 '/(c)hild$/' => '\1hildren', // child
120 '/(buffal|tomat)o$/' => '\1\2oes', // buffalo, tomato
121 '/(bu|campu)s$/' => '\1\2ses', // bus, campus
122 '/(alias|status|virus)/' => '\1es', // alias
123 '/(octop)us$/' => '\1i', // octopus
124 '/(ax|cris|test)is$/' => '\1es', // axis, crisis
125 '/s$/' => 's', // no change (compatibility)
126 '/$/' => 's',
127 );
Derek Allard2067d1a2008-11-13 22:59:24 +0000128
Phil Sturgeon50e57bc2011-08-13 10:26:04 -0600129 foreach ($plural_rules as $rule => $replacement)
Derek Allard2067d1a2008-11-13 22:59:24 +0000130 {
Phil Sturgeon50e57bc2011-08-13 10:26:04 -0600131 if (preg_match($rule, $result))
Barry Mienydd671972010-10-04 16:33:58 +0200132 {
Phil Sturgeon50e57bc2011-08-13 10:26:04 -0600133 $result = preg_replace($rule, $replacement, $result);
134 break;
Barry Mienydd671972010-10-04 16:33:58 +0200135 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000136 }
137
Phil Sturgeon50e57bc2011-08-13 10:26:04 -0600138 return $result;
Derek Allard2067d1a2008-11-13 22:59:24 +0000139 }
140}
141
142// --------------------------------------------------------------------
143
144/**
145 * Camelize
146 *
147 * Takes multiple words separated by spaces or underscores and camelizes 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('camelize'))
Barry Mienydd671972010-10-04 16:33:58 +0200154{
Derek Allard2067d1a2008-11-13 22:59:24 +0000155 function camelize($str)
Barry Mienydd671972010-10-04 16:33:58 +0200156 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000157 $str = 'x'.strtolower(trim($str));
158 $str = ucwords(preg_replace('/[\s_]+/', ' ', $str));
159 return substr(str_replace(' ', '', $str), 1);
160 }
161}
162
163// --------------------------------------------------------------------
164
165/**
166 * Underscore
167 *
168 * Takes multiple words separated by spaces and underscores them
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('underscore'))
175{
176 function underscore($str)
177 {
178 return preg_replace('/[\s]+/', '_', strtolower(trim($str)));
179 }
180}
181
182// --------------------------------------------------------------------
183
184/**
185 * Humanize
186 *
187 * Takes multiple words separated by underscores and changes them to spaces
188 *
189 * @access public
190 * @param string
191 * @return str
Barry Mienydd671972010-10-04 16:33:58 +0200192 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000193if ( ! function_exists('humanize'))
Barry Mienydd671972010-10-04 16:33:58 +0200194{
Derek Allard2067d1a2008-11-13 22:59:24 +0000195 function humanize($str)
196 {
197 return ucwords(preg_replace('/[_]+/', ' ', strtolower(trim($str))));
198 }
199}
Barry Mienydd671972010-10-04 16:33:58 +0200200
Derek Allard2067d1a2008-11-13 22:59:24 +0000201
202/* End of file inflector_helper.php */
Derek Jonesa3ffbbb2008-05-11 18:18:29 +0000203/* Location: ./system/helpers/inflector_helper.php */