blob: 5acfd6bc5d793dbbc60500e6b4e9402115ba77cf [file] [log] [blame]
Andrey Andreev1e6b72c2012-01-06 19:09:22 +02001<?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
Greg Aker0defe5d2012-01-01 18:46:41 -060021 * @copyright Copyright (c) 2008 - 2012, EllisLab, Inc. (http://ellislab.com/)
Derek Jonesf4a4bd82011-10-20 12:18:42 -050022 * @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
Phil Sturgeon002b4be2012-02-29 12:12:49 +000037 * @link http://codeigniter.com/user_guide/helpers/inflector_helper.html
Derek Allard2067d1a2008-11-13 22:59:24 +000038 */
39
40
41// --------------------------------------------------------------------
42
43/**
44 * Singular
45 *
46 * Takes a plural word and makes it singular
47 *
Derek Allard2067d1a2008-11-13 22:59:24 +000048 * @param string
49 * @return str
Barry Mienydd671972010-10-04 16:33:58 +020050 */
Derek Allard2067d1a2008-11-13 22:59:24 +000051if ( ! function_exists('singular'))
Barry Mienydd671972010-10-04 16:33:58 +020052{
Derek Allard2067d1a2008-11-13 22:59:24 +000053 function singular($str)
54 {
Phil Sturgeon50e57bc2011-08-13 10:26:04 -060055 $result = strval($str);
Barry Mienydd671972010-10-04 16:33:58 +020056
Phil Sturgeon002b4be2012-02-29 12:12:49 +000057 if ( ! is_countable($result))
58 {
59 return $result;
60 }
61
Phil Sturgeon50e57bc2011-08-13 10:26:04 -060062 $singular_rules = array(
Phil Sturgeon002b4be2012-02-29 12:12:49 +000063 '/(matr)ices$/' => '\1ix',
64 '/(vert|ind)ices$/' => '\1ex',
65 '/^(ox)en/' => '\1',
66 '/(alias)es$/' => '\1',
67 '/([octop|vir])i$/' => '\1us',
68 '/(cris|ax|test)es$/' => '\1is',
69 '/(shoe)s$/' => '\1',
70 '/(o)es$/' => '\1',
71 '/(bus|campus)es$/' => '\1',
72 '/([m|l])ice$/' => '\1ouse',
73 '/(x|ch|ss|sh)es$/' => '\1',
74 '/(m)ovies$/' => '\1\2ovie',
75 '/(s)eries$/' => '\1\2eries',
76 '/([^aeiouy]|qu)ies$/' => '\1y',
77 '/([lr])ves$/' => '\1f',
78 '/(tive)s$/' => '\1',
79 '/(hive)s$/' => '\1',
80 '/([^f])ves$/' => '\1fe',
81 '/(^analy)ses$/' => '\1sis',
Phil Sturgeon50e57bc2011-08-13 10:26:04 -060082 '/((a)naly|(b)a|(d)iagno|(p)arenthe|(p)rogno|(s)ynop|(t)he)ses$/' => '\1\2sis',
Phil Sturgeon002b4be2012-02-29 12:12:49 +000083 '/([ti])a$/' => '\1um',
84 '/(p)eople$/' => '\1\2erson',
85 '/(m)en$/' => '\1an',
86 '/(s)tatuses$/' => '\1\2tatus',
87 '/(c)hildren$/' => '\1\2hild',
88 '/(n)ews$/' => '\1\2ews',
89 '/([^us])s$/' => '\1',
Phil Sturgeon50e57bc2011-08-13 10:26:04 -060090 );
Eric Barnescde712c2011-12-09 11:27:51 -050091
Phil Sturgeon002b4be2012-02-29 12:12:49 +000092 foreach ($singular_rules as $rule => $replacement)
93 {
94 if (preg_match($rule, $result))
95 {
96 $result = preg_replace($rule, $replacement, $result);
97 break;
98 }
99 }
100
101 return $result;
Derek Allard2067d1a2008-11-13 22:59:24 +0000102 }
103}
104
105// --------------------------------------------------------------------
106
107/**
108 * Plural
109 *
110 * Takes a singular word and makes it plural
111 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000112 * @param string
113 * @param bool
114 * @return str
Barry Mienydd671972010-10-04 16:33:58 +0200115 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000116if ( ! function_exists('plural'))
Barry Mienydd671972010-10-04 16:33:58 +0200117{
Derek Allard2067d1a2008-11-13 22:59:24 +0000118 function plural($str, $force = FALSE)
Phil Sturgeon002b4be2012-02-29 12:12:49 +0000119 {
Phil Sturgeon50e57bc2011-08-13 10:26:04 -0600120 $result = strval($str);
Eric Barnescde712c2011-12-09 11:27:51 -0500121
Phil Sturgeon002b4be2012-02-29 12:12:49 +0000122 if ( ! is_countable($result))
123 {
124 return $result;
125 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000126
Phil Sturgeon002b4be2012-02-29 12:12:49 +0000127 $plural_rules = array(
128 '/^(ox)$/' => '\1\2en', // ox
129 '/([m|l])ouse$/' => '\1ice', // mouse, louse
130 '/(matr|vert|ind)ix|ex$/' => '\1ices', // matrix, vertex, index
131 '/(x|ch|ss|sh)$/' => '\1es', // search, switch, fix, box, process, address
132 '/([^aeiouy]|qu)y$/' => '\1ies', // query, ability, agency
133 '/(hive)$/' => '\1s', // archive, hive
134 '/(?:([^f])fe|([lr])f)$/' => '\1\2ves', // half, safe, wife
135 '/sis$/' => 'ses', // basis, diagnosis
136 '/([ti])um$/' => '\1a', // datum, medium
137 '/(p)erson$/' => '\1eople', // person, salesperson
138 '/(m)an$/' => '\1en', // man, woman, spokesman
139 '/(c)hild$/' => '\1hildren', // child
140 '/(buffal|tomat)o$/' => '\1\2oes', // buffalo, tomato
141 '/(bu|campu)s$/' => '\1\2ses', // bus, campus
142 '/(alias|status|virus)$/' => '\1es', // alias
143 '/(octop)us$/' => '\1i', // octopus
144 '/(ax|cris|test)is$/' => '\1es', // axis, crisis
145 '/s$/' => 's', // no change (compatibility)
146 '/$/' => 's',
147 );
148
149 foreach ($plural_rules as $rule => $replacement)
150 {
151 if (preg_match($rule, $result))
152 {
153 $result = preg_replace($rule, $replacement, $result);
154 break;
155 }
156 }
157
158 return $result;
Derek Allard2067d1a2008-11-13 22:59:24 +0000159 }
160}
161
162// --------------------------------------------------------------------
163
164/**
165 * Camelize
166 *
167 * Takes multiple words separated by spaces or underscores and camelizes them
168 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000169 * @param string
170 * @return str
Barry Mienydd671972010-10-04 16:33:58 +0200171 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000172if ( ! function_exists('camelize'))
Barry Mienydd671972010-10-04 16:33:58 +0200173{
Derek Allard2067d1a2008-11-13 22:59:24 +0000174 function camelize($str)
Barry Mienydd671972010-10-04 16:33:58 +0200175 {
Phil Sturgeone40c7632012-03-10 13:05:08 +0000176 return strtolower($str[0]).substr(str_replace(' ', '', ucwords(preg_replace('/[\s_]+/', ' ', $str))), 1);
Derek Allard2067d1a2008-11-13 22:59:24 +0000177 }
178}
179
180// --------------------------------------------------------------------
181
182/**
183 * Underscore
184 *
185 * Takes multiple words separated by spaces and underscores them
186 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000187 * @param string
188 * @return str
Barry Mienydd671972010-10-04 16:33:58 +0200189 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000190if ( ! function_exists('underscore'))
191{
192 function underscore($str)
193 {
194 return preg_replace('/[\s]+/', '_', strtolower(trim($str)));
195 }
196}
197
198// --------------------------------------------------------------------
199
200/**
201 * Humanize
202 *
Eric Barnescde712c2011-12-09 11:27:51 -0500203 * Takes multiple words separated by the separator and changes them to spaces
Derek Allard2067d1a2008-11-13 22:59:24 +0000204 *
Eric Barnescde712c2011-12-09 11:27:51 -0500205 * @param string $str
206 * @param string $separator
Derek Allard2067d1a2008-11-13 22:59:24 +0000207 * @return str
Barry Mienydd671972010-10-04 16:33:58 +0200208 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000209if ( ! function_exists('humanize'))
Barry Mienydd671972010-10-04 16:33:58 +0200210{
Eric Barnescde712c2011-12-09 11:27:51 -0500211 function humanize($str, $separator = '_')
Derek Allard2067d1a2008-11-13 22:59:24 +0000212 {
Eric Barnescde712c2011-12-09 11:27:51 -0500213 return ucwords(preg_replace('/['.$separator.']+/', ' ', strtolower(trim($str))));
Derek Allard2067d1a2008-11-13 22:59:24 +0000214 }
215}
Barry Mienydd671972010-10-04 16:33:58 +0200216
Phil Sturgeon002b4be2012-02-29 12:12:49 +0000217/**
218 * Checks if the given word has a plural version.
219 *
220 * @param string the word to check
221 * @return bool if the word is countable
222 */
223if ( ! function_exists('is_countable'))
224{
225 function is_countable($word)
226 {
227 return ! (in_array(strtolower(strval($word)), array(
228 'equipment', 'information', 'rice', 'money',
229 'species', 'series', 'fish', 'meta'
230 )));
231 }
232}
233
Derek Allard2067d1a2008-11-13 22:59:24 +0000234/* End of file inflector_helper.php */
Phil Sturgeon002b4be2012-02-29 12:12:49 +0000235/* Location: ./system/helpers/inflector_helper.php */