blob: 6f1086a0059e4c58aa90d6fe274d542aed508192 [file] [log] [blame]
Andrey Andreevc5536aa2012-11-01 17:33:58 +02001<?php
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 */
Andrey Andreevc5536aa2012-11-01 17:33:58 +020027defined('BASEPATH') OR exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +000028
Derek Allard2067d1a2008-11-13 22:59:24 +000029/**
30 * CodeIgniter Inflector Helpers
31 *
32 * @package CodeIgniter
33 * @subpackage Helpers
34 * @category Helpers
Derek Jonesf4a4bd82011-10-20 12:18:42 -050035 * @author EllisLab Dev Team
Phil Sturgeon002b4be2012-02-29 12:12:49 +000036 * @link http://codeigniter.com/user_guide/helpers/inflector_helper.html
Derek Allard2067d1a2008-11-13 22:59:24 +000037 */
38
Derek Allard2067d1a2008-11-13 22:59:24 +000039// --------------------------------------------------------------------
40
Derek Allard2067d1a2008-11-13 22:59:24 +000041if ( ! function_exists('singular'))
Barry Mienydd671972010-10-04 16:33:58 +020042{
Timothy Warrenb75faa12012-04-27 12:03:32 -040043 /**
44 * Singular
45 *
46 * Takes a plural word and makes it singular
47 *
48 * @param string
Timothy Warren16642c72012-05-17 08:20:16 -040049 * @return string
Timothy Warrenb75faa12012-04-27 12:03:32 -040050 */
Derek Allard2067d1a2008-11-13 22:59:24 +000051 function singular($str)
52 {
Phil Sturgeon50e57bc2011-08-13 10:26:04 -060053 $result = strval($str);
Barry Mienydd671972010-10-04 16:33:58 +020054
Phil Sturgeon002b4be2012-02-29 12:12:49 +000055 if ( ! is_countable($result))
56 {
57 return $result;
58 }
Andrey Andreev52a31ba2012-03-10 15:38:49 +020059
Phil Sturgeon50e57bc2011-08-13 10:26:04 -060060 $singular_rules = array(
Phil Sturgeon002b4be2012-02-29 12:12:49 +000061 '/(matr)ices$/' => '\1ix',
62 '/(vert|ind)ices$/' => '\1ex',
63 '/^(ox)en/' => '\1',
64 '/(alias)es$/' => '\1',
65 '/([octop|vir])i$/' => '\1us',
66 '/(cris|ax|test)es$/' => '\1is',
67 '/(shoe)s$/' => '\1',
68 '/(o)es$/' => '\1',
69 '/(bus|campus)es$/' => '\1',
70 '/([m|l])ice$/' => '\1ouse',
71 '/(x|ch|ss|sh)es$/' => '\1',
72 '/(m)ovies$/' => '\1\2ovie',
73 '/(s)eries$/' => '\1\2eries',
74 '/([^aeiouy]|qu)ies$/' => '\1y',
75 '/([lr])ves$/' => '\1f',
76 '/(tive)s$/' => '\1',
77 '/(hive)s$/' => '\1',
78 '/([^f])ves$/' => '\1fe',
79 '/(^analy)ses$/' => '\1sis',
Phil Sturgeon50e57bc2011-08-13 10:26:04 -060080 '/((a)naly|(b)a|(d)iagno|(p)arenthe|(p)rogno|(s)ynop|(t)he)ses$/' => '\1\2sis',
Phil Sturgeon002b4be2012-02-29 12:12:49 +000081 '/([ti])a$/' => '\1um',
82 '/(p)eople$/' => '\1\2erson',
83 '/(m)en$/' => '\1an',
84 '/(s)tatuses$/' => '\1\2tatus',
85 '/(c)hildren$/' => '\1\2hild',
86 '/(n)ews$/' => '\1\2ews',
87 '/([^us])s$/' => '\1',
Phil Sturgeon50e57bc2011-08-13 10:26:04 -060088 );
Eric Barnescde712c2011-12-09 11:27:51 -050089
Phil Sturgeon002b4be2012-02-29 12:12:49 +000090 foreach ($singular_rules as $rule => $replacement)
91 {
92 if (preg_match($rule, $result))
93 {
94 $result = preg_replace($rule, $replacement, $result);
95 break;
96 }
97 }
98
99 return $result;
Derek Allard2067d1a2008-11-13 22:59:24 +0000100 }
101}
102
103// --------------------------------------------------------------------
104
Derek Allard2067d1a2008-11-13 22:59:24 +0000105if ( ! function_exists('plural'))
Barry Mienydd671972010-10-04 16:33:58 +0200106{
Timothy Warrenb75faa12012-04-27 12:03:32 -0400107 /**
108 * Plural
109 *
110 * Takes a singular word and makes it plural
111 *
112 * @param string
113 * @param bool
Timothy Warren16642c72012-05-17 08:20:16 -0400114 * @return string
Timothy Warrenb75faa12012-04-27 12:03:32 -0400115 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000116 function plural($str, $force = FALSE)
Andrey Andreev52a31ba2012-03-10 15:38:49 +0200117 {
Phil Sturgeon50e57bc2011-08-13 10:26:04 -0600118 $result = strval($str);
Eric Barnescde712c2011-12-09 11:27:51 -0500119
Phil Sturgeon002b4be2012-02-29 12:12:49 +0000120 if ( ! is_countable($result))
121 {
122 return $result;
123 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000124
Phil Sturgeon002b4be2012-02-29 12:12:49 +0000125 $plural_rules = array(
126 '/^(ox)$/' => '\1\2en', // ox
127 '/([m|l])ouse$/' => '\1ice', // mouse, louse
128 '/(matr|vert|ind)ix|ex$/' => '\1ices', // matrix, vertex, index
129 '/(x|ch|ss|sh)$/' => '\1es', // search, switch, fix, box, process, address
130 '/([^aeiouy]|qu)y$/' => '\1ies', // query, ability, agency
131 '/(hive)$/' => '\1s', // archive, hive
132 '/(?:([^f])fe|([lr])f)$/' => '\1\2ves', // half, safe, wife
133 '/sis$/' => 'ses', // basis, diagnosis
134 '/([ti])um$/' => '\1a', // datum, medium
135 '/(p)erson$/' => '\1eople', // person, salesperson
136 '/(m)an$/' => '\1en', // man, woman, spokesman
137 '/(c)hild$/' => '\1hildren', // child
138 '/(buffal|tomat)o$/' => '\1\2oes', // buffalo, tomato
139 '/(bu|campu)s$/' => '\1\2ses', // bus, campus
140 '/(alias|status|virus)$/' => '\1es', // alias
141 '/(octop)us$/' => '\1i', // octopus
142 '/(ax|cris|test)is$/' => '\1es', // axis, crisis
143 '/s$/' => 's', // no change (compatibility)
144 '/$/' => 's',
145 );
Andrey Andreev52a31ba2012-03-10 15:38:49 +0200146
Phil Sturgeon002b4be2012-02-29 12:12:49 +0000147 foreach ($plural_rules as $rule => $replacement)
148 {
149 if (preg_match($rule, $result))
150 {
151 $result = preg_replace($rule, $replacement, $result);
152 break;
153 }
154 }
155
156 return $result;
Derek Allard2067d1a2008-11-13 22:59:24 +0000157 }
158}
159
160// --------------------------------------------------------------------
161
Derek Allard2067d1a2008-11-13 22:59:24 +0000162if ( ! function_exists('camelize'))
Barry Mienydd671972010-10-04 16:33:58 +0200163{
Timothy Warrenb75faa12012-04-27 12:03:32 -0400164 /**
165 * Camelize
166 *
167 * Takes multiple words separated by spaces or underscores and camelizes them
168 *
169 * @param string
Timothy Warren16642c72012-05-17 08:20:16 -0400170 * @return string
Timothy Warrenb75faa12012-04-27 12:03:32 -0400171 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000172 function camelize($str)
Barry Mienydd671972010-10-04 16:33:58 +0200173 {
Phil Sturgeone40c7632012-03-10 13:05:08 +0000174 return strtolower($str[0]).substr(str_replace(' ', '', ucwords(preg_replace('/[\s_]+/', ' ', $str))), 1);
Derek Allard2067d1a2008-11-13 22:59:24 +0000175 }
176}
177
178// --------------------------------------------------------------------
179
Derek Allard2067d1a2008-11-13 22:59:24 +0000180if ( ! function_exists('underscore'))
181{
Timothy Warrenb75faa12012-04-27 12:03:32 -0400182 /**
183 * Underscore
184 *
185 * Takes multiple words separated by spaces and underscores them
186 *
187 * @param string
Timothy Warren16642c72012-05-17 08:20:16 -0400188 * @return string
Timothy Warrenb75faa12012-04-27 12:03:32 -0400189 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000190 function underscore($str)
191 {
192 return preg_replace('/[\s]+/', '_', strtolower(trim($str)));
193 }
194}
195
196// --------------------------------------------------------------------
197
Derek Allard2067d1a2008-11-13 22:59:24 +0000198if ( ! function_exists('humanize'))
Barry Mienydd671972010-10-04 16:33:58 +0200199{
Timothy Warrenb75faa12012-04-27 12:03:32 -0400200 /**
201 * Humanize
202 *
203 * Takes multiple words separated by the separator and changes them to spaces
204 *
205 * @param string $str
206 * @param string $separator
Timothy Warren16642c72012-05-17 08:20:16 -0400207 * @return string
Timothy Warrenb75faa12012-04-27 12:03:32 -0400208 */
Eric Barnescde712c2011-12-09 11:27:51 -0500209 function humanize($str, $separator = '_')
Derek Allard2067d1a2008-11-13 22:59:24 +0000210 {
Eric Barnescde712c2011-12-09 11:27:51 -0500211 return ucwords(preg_replace('/['.$separator.']+/', ' ', strtolower(trim($str))));
Derek Allard2067d1a2008-11-13 22:59:24 +0000212 }
213}
Barry Mienydd671972010-10-04 16:33:58 +0200214
Timothy Warrenb75faa12012-04-27 12:03:32 -0400215// --------------------------------------------------------------------
216
Phil Sturgeon002b4be2012-02-29 12:12:49 +0000217if ( ! function_exists('is_countable'))
218{
Timothy Warrenb75faa12012-04-27 12:03:32 -0400219 /**
220 * Checks if the given word has a plural version.
221 *
222 * @param string the word to check
223 * @return bool if the word is countable
224 */
Phil Sturgeon002b4be2012-02-29 12:12:49 +0000225 function is_countable($word)
226 {
Andrey Andreeve92df332012-03-26 22:44:20 +0300227 return ! in_array(strtolower(strval($word)),
228 array(
229 'equipment', 'information', 'rice', 'money',
230 'species', 'series', 'fish', 'meta'
231 )
232 );
Phil Sturgeon002b4be2012-02-29 12:12:49 +0000233 }
234}
235
Derek Allard2067d1a2008-11-13 22:59:24 +0000236/* End of file inflector_helper.php */
Andrey Andreeve92df332012-03-26 22:44:20 +0300237/* Location: ./system/helpers/inflector_helper.php */