blob: 3393bda8fdfe42842a4d9a490798dbe9dd8d8674 [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 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05007 * NOTICE OF LICENSE
Eric Barnescde712c2011-12-09 11:27:51 -05008 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05009 * Licensed under the Open Software License version 3.0
Eric Barnescde712c2011-12-09 11:27:51 -050010 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -050011 * This source file is subject to the Open Software License (OSL 3.0) that is
12 * bundled with this package in the files license.txt / license.rst. It is
13 * also available through the world wide web at this URL:
14 * http://opensource.org/licenses/OSL-3.0
15 * If you did not receive a copy of the license and are unable to obtain it
16 * through the world wide web, please send an email to
17 * licensing@ellislab.com so we can send you a copy immediately.
18 *
Derek Allard2067d1a2008-11-13 22:59:24 +000019 * @package CodeIgniter
Derek Jonesf4a4bd82011-10-20 12:18:42 -050020 * @author EllisLab Dev Team
21 * @copyright Copyright (c) 2008 - 2011, EllisLab, Inc. (http://ellislab.com/)
22 * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
Derek Allard2067d1a2008-11-13 22:59:24 +000023 * @link http://codeigniter.com
24 * @since Version 1.0
25 * @filesource
26 */
27
28// ------------------------------------------------------------------------
29
30/**
31 * CodeIgniter Inflector Helpers
32 *
33 * @package CodeIgniter
34 * @subpackage Helpers
35 * @category Helpers
Derek Jonesf4a4bd82011-10-20 12:18:42 -050036 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000037 * @link http://codeigniter.com/user_guide/helpers/directory_helper.html
38 */
39
40
41// --------------------------------------------------------------------
42
43/**
44 * Singular
45 *
46 * Takes a plural word and makes it singular
47 *
48 * @access public
49 * @param string
50 * @return str
Barry Mienydd671972010-10-04 16:33:58 +020051 */
Derek Allard2067d1a2008-11-13 22:59:24 +000052if ( ! function_exists('singular'))
Barry Mienydd671972010-10-04 16:33:58 +020053{
Derek Allard2067d1a2008-11-13 22:59:24 +000054 function singular($str)
55 {
Phil Sturgeon50e57bc2011-08-13 10:26:04 -060056 $result = strval($str);
Barry Mienydd671972010-10-04 16:33:58 +020057
Phil Sturgeon50e57bc2011-08-13 10:26:04 -060058 $singular_rules = array(
59 '/(matr)ices$/' => '\1ix',
60 '/(vert|ind)ices$/' => '\1ex',
61 '/^(ox)en/' => '\1',
62 '/(alias)es$/' => '\1',
63 '/([octop|vir])i$/' => '\1us',
64 '/(cris|ax|test)es$/' => '\1is',
65 '/(shoe)s$/' => '\1',
66 '/(o)es$/' => '\1',
67 '/(bus|campus)es$/' => '\1',
68 '/([m|l])ice$/' => '\1ouse',
69 '/(x|ch|ss|sh)es$/' => '\1',
70 '/(m)ovies$/' => '\1\2ovie',
71 '/(s)eries$/' => '\1\2eries',
72 '/([^aeiouy]|qu)ies$/' => '\1y',
73 '/([lr])ves$/' => '\1f',
74 '/(tive)s$/' => '\1',
75 '/(hive)s$/' => '\1',
76 '/([^f])ves$/' => '\1fe',
77 '/(^analy)ses$/' => '\1sis',
78 '/((a)naly|(b)a|(d)iagno|(p)arenthe|(p)rogno|(s)ynop|(t)he)ses$/' => '\1\2sis',
79 '/([ti])a$/' => '\1um',
80 '/(p)eople$/' => '\1\2erson',
81 '/(m)en$/' => '\1an',
82 '/(s)tatuses$/' => '\1\2tatus',
83 '/(c)hildren$/' => '\1\2hild',
84 '/(n)ews$/' => '\1\2ews',
85 '/([^u])s$/' => '\1',
86 );
Eric Barnescde712c2011-12-09 11:27:51 -050087
Phil Sturgeon50e57bc2011-08-13 10:26:04 -060088 foreach ($singular_rules as $rule => $replacement)
89 {
90 if (preg_match($rule, $result))
Derek Allard2067d1a2008-11-13 22:59:24 +000091 {
Phil Sturgeon50e57bc2011-08-13 10:26:04 -060092 $result = preg_replace($rule, $replacement, $result);
93 break;
Derek Allard2067d1a2008-11-13 22:59:24 +000094 }
95 }
Barry Mienydd671972010-10-04 16:33:58 +020096
Phil Sturgeon50e57bc2011-08-13 10:26:04 -060097 return $result;
Derek Allard2067d1a2008-11-13 22:59:24 +000098 }
99}
100
101// --------------------------------------------------------------------
102
103/**
104 * Plural
105 *
106 * Takes a singular word and makes it plural
107 *
108 * @access public
109 * @param string
110 * @param bool
111 * @return str
Barry Mienydd671972010-10-04 16:33:58 +0200112 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000113if ( ! function_exists('plural'))
Barry Mienydd671972010-10-04 16:33:58 +0200114{
Derek Allard2067d1a2008-11-13 22:59:24 +0000115 function plural($str, $force = FALSE)
Phil Sturgeon50e57bc2011-08-13 10:26:04 -0600116 {
117 $result = strval($str);
Eric Barnescde712c2011-12-09 11:27:51 -0500118
Phil Sturgeon50e57bc2011-08-13 10:26:04 -0600119 $plural_rules = array(
120 '/^(ox)$/' => '\1\2en', // ox
121 '/([m|l])ouse$/' => '\1ice', // mouse, louse
122 '/(matr|vert|ind)ix|ex$/' => '\1ices', // matrix, vertex, index
123 '/(x|ch|ss|sh)$/' => '\1es', // search, switch, fix, box, process, address
124 '/([^aeiouy]|qu)y$/' => '\1ies', // query, ability, agency
125 '/(hive)$/' => '\1s', // archive, hive
126 '/(?:([^f])fe|([lr])f)$/' => '\1\2ves', // half, safe, wife
127 '/sis$/' => 'ses', // basis, diagnosis
128 '/([ti])um$/' => '\1a', // datum, medium
129 '/(p)erson$/' => '\1eople', // person, salesperson
130 '/(m)an$/' => '\1en', // man, woman, spokesman
131 '/(c)hild$/' => '\1hildren', // child
132 '/(buffal|tomat)o$/' => '\1\2oes', // buffalo, tomato
133 '/(bu|campu)s$/' => '\1\2ses', // bus, campus
134 '/(alias|status|virus)/' => '\1es', // alias
135 '/(octop)us$/' => '\1i', // octopus
136 '/(ax|cris|test)is$/' => '\1es', // axis, crisis
137 '/s$/' => 's', // no change (compatibility)
138 '/$/' => 's',
139 );
Derek Allard2067d1a2008-11-13 22:59:24 +0000140
Phil Sturgeon50e57bc2011-08-13 10:26:04 -0600141 foreach ($plural_rules as $rule => $replacement)
Derek Allard2067d1a2008-11-13 22:59:24 +0000142 {
Phil Sturgeon50e57bc2011-08-13 10:26:04 -0600143 if (preg_match($rule, $result))
Barry Mienydd671972010-10-04 16:33:58 +0200144 {
Phil Sturgeon50e57bc2011-08-13 10:26:04 -0600145 $result = preg_replace($rule, $replacement, $result);
146 break;
Barry Mienydd671972010-10-04 16:33:58 +0200147 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000148 }
149
Phil Sturgeon50e57bc2011-08-13 10:26:04 -0600150 return $result;
Derek Allard2067d1a2008-11-13 22:59:24 +0000151 }
152}
153
154// --------------------------------------------------------------------
155
156/**
157 * Camelize
158 *
159 * Takes multiple words separated by spaces or underscores and camelizes them
160 *
161 * @access public
162 * @param string
163 * @return str
Barry Mienydd671972010-10-04 16:33:58 +0200164 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000165if ( ! function_exists('camelize'))
Barry Mienydd671972010-10-04 16:33:58 +0200166{
Derek Allard2067d1a2008-11-13 22:59:24 +0000167 function camelize($str)
Barry Mienydd671972010-10-04 16:33:58 +0200168 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000169 $str = 'x'.strtolower(trim($str));
170 $str = ucwords(preg_replace('/[\s_]+/', ' ', $str));
171 return substr(str_replace(' ', '', $str), 1);
172 }
173}
174
175// --------------------------------------------------------------------
176
177/**
178 * Underscore
179 *
180 * Takes multiple words separated by spaces and underscores them
181 *
182 * @access public
183 * @param string
184 * @return str
Barry Mienydd671972010-10-04 16:33:58 +0200185 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000186if ( ! function_exists('underscore'))
187{
188 function underscore($str)
189 {
190 return preg_replace('/[\s]+/', '_', strtolower(trim($str)));
191 }
192}
193
194// --------------------------------------------------------------------
195
196/**
197 * Humanize
198 *
Eric Barnescde712c2011-12-09 11:27:51 -0500199 * Takes multiple words separated by the separator and changes them to spaces
Derek Allard2067d1a2008-11-13 22:59:24 +0000200 *
201 * @access public
Eric Barnescde712c2011-12-09 11:27:51 -0500202 * @param string $str
203 * @param string $separator
Derek Allard2067d1a2008-11-13 22:59:24 +0000204 * @return str
Barry Mienydd671972010-10-04 16:33:58 +0200205 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000206if ( ! function_exists('humanize'))
Barry Mienydd671972010-10-04 16:33:58 +0200207{
Eric Barnescde712c2011-12-09 11:27:51 -0500208 function humanize($str, $separator = '_')
Derek Allard2067d1a2008-11-13 22:59:24 +0000209 {
Eric Barnescde712c2011-12-09 11:27:51 -0500210 return ucwords(preg_replace('/['.$separator.']+/', ' ', strtolower(trim($str))));
Derek Allard2067d1a2008-11-13 22:59:24 +0000211 }
212}
Barry Mienydd671972010-10-04 16:33:58 +0200213
Derek Allard2067d1a2008-11-13 22:59:24 +0000214/* End of file inflector_helper.php */
Derek Jonesa3ffbbb2008-05-11 18:18:29 +0000215/* Location: ./system/helpers/inflector_helper.php */