blob: bbc75c74761bcf310a83ec20a2e3b31a8c08d8cd [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 *
Phil Sturgeon07c1ac82012-03-09 17:03:37 +00005 * An open source application development framework for PHP 5.2.4 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
Derek Allard2067d1a2008-11-13 22:59:24 +000028/**
29 * CodeIgniter Inflector Helpers
30 *
31 * @package CodeIgniter
32 * @subpackage Helpers
33 * @category Helpers
Derek Jonesf4a4bd82011-10-20 12:18:42 -050034 * @author EllisLab Dev Team
Phil Sturgeon002b4be2012-02-29 12:12:49 +000035 * @link http://codeigniter.com/user_guide/helpers/inflector_helper.html
Derek Allard2067d1a2008-11-13 22:59:24 +000036 */
37
Derek Allard2067d1a2008-11-13 22:59:24 +000038// --------------------------------------------------------------------
39
40/**
41 * Singular
42 *
43 * Takes a plural word and makes it singular
44 *
Derek Allard2067d1a2008-11-13 22:59:24 +000045 * @param string
46 * @return str
Barry Mienydd671972010-10-04 16:33:58 +020047 */
Derek Allard2067d1a2008-11-13 22:59:24 +000048if ( ! function_exists('singular'))
Barry Mienydd671972010-10-04 16:33:58 +020049{
Derek Allard2067d1a2008-11-13 22:59:24 +000050 function singular($str)
51 {
Phil Sturgeon50e57bc2011-08-13 10:26:04 -060052 $result = strval($str);
Barry Mienydd671972010-10-04 16:33:58 +020053
Phil Sturgeon002b4be2012-02-29 12:12:49 +000054 if ( ! is_countable($result))
55 {
56 return $result;
57 }
Andrey Andreev52a31ba2012-03-10 15:38:49 +020058
Phil Sturgeon50e57bc2011-08-13 10:26:04 -060059 $singular_rules = array(
Phil Sturgeon002b4be2012-02-29 12:12:49 +000060 '/(matr)ices$/' => '\1ix',
61 '/(vert|ind)ices$/' => '\1ex',
62 '/^(ox)en/' => '\1',
63 '/(alias)es$/' => '\1',
64 '/([octop|vir])i$/' => '\1us',
65 '/(cris|ax|test)es$/' => '\1is',
66 '/(shoe)s$/' => '\1',
67 '/(o)es$/' => '\1',
68 '/(bus|campus)es$/' => '\1',
69 '/([m|l])ice$/' => '\1ouse',
70 '/(x|ch|ss|sh)es$/' => '\1',
71 '/(m)ovies$/' => '\1\2ovie',
72 '/(s)eries$/' => '\1\2eries',
73 '/([^aeiouy]|qu)ies$/' => '\1y',
74 '/([lr])ves$/' => '\1f',
75 '/(tive)s$/' => '\1',
76 '/(hive)s$/' => '\1',
77 '/([^f])ves$/' => '\1fe',
78 '/(^analy)ses$/' => '\1sis',
Phil Sturgeon50e57bc2011-08-13 10:26:04 -060079 '/((a)naly|(b)a|(d)iagno|(p)arenthe|(p)rogno|(s)ynop|(t)he)ses$/' => '\1\2sis',
Phil Sturgeon002b4be2012-02-29 12:12:49 +000080 '/([ti])a$/' => '\1um',
81 '/(p)eople$/' => '\1\2erson',
82 '/(m)en$/' => '\1an',
83 '/(s)tatuses$/' => '\1\2tatus',
84 '/(c)hildren$/' => '\1\2hild',
85 '/(n)ews$/' => '\1\2ews',
86 '/([^us])s$/' => '\1',
Phil Sturgeon50e57bc2011-08-13 10:26:04 -060087 );
Eric Barnescde712c2011-12-09 11:27:51 -050088
Phil Sturgeon002b4be2012-02-29 12:12:49 +000089 foreach ($singular_rules as $rule => $replacement)
90 {
91 if (preg_match($rule, $result))
92 {
93 $result = preg_replace($rule, $replacement, $result);
94 break;
95 }
96 }
97
98 return $result;
Derek Allard2067d1a2008-11-13 22:59:24 +000099 }
100}
101
102// --------------------------------------------------------------------
103
104/**
105 * Plural
106 *
107 * Takes a singular word and makes it plural
108 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000109 * @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)
Andrey Andreev52a31ba2012-03-10 15:38:49 +0200116 {
Phil Sturgeon50e57bc2011-08-13 10:26:04 -0600117 $result = strval($str);
Eric Barnescde712c2011-12-09 11:27:51 -0500118
Phil Sturgeon002b4be2012-02-29 12:12:49 +0000119 if ( ! is_countable($result))
120 {
121 return $result;
122 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000123
Phil Sturgeon002b4be2012-02-29 12:12:49 +0000124 $plural_rules = array(
125 '/^(ox)$/' => '\1\2en', // ox
126 '/([m|l])ouse$/' => '\1ice', // mouse, louse
127 '/(matr|vert|ind)ix|ex$/' => '\1ices', // matrix, vertex, index
128 '/(x|ch|ss|sh)$/' => '\1es', // search, switch, fix, box, process, address
129 '/([^aeiouy]|qu)y$/' => '\1ies', // query, ability, agency
130 '/(hive)$/' => '\1s', // archive, hive
131 '/(?:([^f])fe|([lr])f)$/' => '\1\2ves', // half, safe, wife
132 '/sis$/' => 'ses', // basis, diagnosis
133 '/([ti])um$/' => '\1a', // datum, medium
134 '/(p)erson$/' => '\1eople', // person, salesperson
135 '/(m)an$/' => '\1en', // man, woman, spokesman
136 '/(c)hild$/' => '\1hildren', // child
137 '/(buffal|tomat)o$/' => '\1\2oes', // buffalo, tomato
138 '/(bu|campu)s$/' => '\1\2ses', // bus, campus
139 '/(alias|status|virus)$/' => '\1es', // alias
140 '/(octop)us$/' => '\1i', // octopus
141 '/(ax|cris|test)is$/' => '\1es', // axis, crisis
142 '/s$/' => 's', // no change (compatibility)
143 '/$/' => 's',
144 );
Andrey Andreev52a31ba2012-03-10 15:38:49 +0200145
Phil Sturgeon002b4be2012-02-29 12:12:49 +0000146 foreach ($plural_rules as $rule => $replacement)
147 {
148 if (preg_match($rule, $result))
149 {
150 $result = preg_replace($rule, $replacement, $result);
151 break;
152 }
153 }
154
155 return $result;
Derek Allard2067d1a2008-11-13 22:59:24 +0000156 }
157}
158
159// --------------------------------------------------------------------
160
161/**
162 * Camelize
163 *
164 * Takes multiple words separated by spaces or underscores and camelizes them
165 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000166 * @param string
167 * @return str
Barry Mienydd671972010-10-04 16:33:58 +0200168 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000169if ( ! function_exists('camelize'))
Barry Mienydd671972010-10-04 16:33:58 +0200170{
Derek Allard2067d1a2008-11-13 22:59:24 +0000171 function camelize($str)
Barry Mienydd671972010-10-04 16:33:58 +0200172 {
Andrey Andreev52a31ba2012-03-10 15:38:49 +0200173 return ucwords(str_replace(' ', '', preg_replace('/[\s_]+/', ' ', strtolower($str))));
Derek Allard2067d1a2008-11-13 22:59:24 +0000174 }
175}
176
177// --------------------------------------------------------------------
178
179/**
180 * Underscore
181 *
182 * Takes multiple words separated by spaces and underscores them
183 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000184 * @param string
185 * @return str
Barry Mienydd671972010-10-04 16:33:58 +0200186 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000187if ( ! function_exists('underscore'))
188{
189 function underscore($str)
190 {
191 return preg_replace('/[\s]+/', '_', strtolower(trim($str)));
192 }
193}
194
195// --------------------------------------------------------------------
196
197/**
198 * Humanize
199 *
Eric Barnescde712c2011-12-09 11:27:51 -0500200 * Takes multiple words separated by the separator and changes them to spaces
Derek Allard2067d1a2008-11-13 22:59:24 +0000201 *
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
Phil Sturgeon002b4be2012-02-29 12:12:49 +0000214/**
215 * Checks if the given word has a plural version.
216 *
217 * @param string the word to check
218 * @return bool if the word is countable
219 */
220if ( ! function_exists('is_countable'))
221{
222 function is_countable($word)
223 {
224 return ! (in_array(strtolower(strval($word)), array(
225 'equipment', 'information', 'rice', 'money',
226 'species', 'series', 'fish', 'meta'
227 )));
228 }
229}
230
Derek Allard2067d1a2008-11-13 22:59:24 +0000231/* End of file inflector_helper.php */
Andrey Andreev52a31ba2012-03-10 15:38:49 +0200232/* Location: ./system/helpers/inflector_helper.php */