blob: 0bd1e112f77deb0dae8fe69e18184d3567dfe9fc [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
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(
Greg Aker03abee32011-12-25 00:31:29 -060059 '/(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',
Phil Sturgeon50e57bc2011-08-13 10:26:04 -060078 '/((a)naly|(b)a|(d)iagno|(p)arenthe|(p)rogno|(s)ynop|(t)he)ses$/' => '\1\2sis',
Greg Aker03abee32011-12-25 00:31:29 -060079 '/([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',
Phil Sturgeon50e57bc2011-08-13 10:26:04 -060086 );
Eric Barnescde712c2011-12-09 11:27:51 -050087
Andrey Andreev1e6b72c2012-01-06 19:09:22 +020088 return preg_replace(array_keys($singular_values), $singular_values, $result);
Derek Allard2067d1a2008-11-13 22:59:24 +000089 }
90}
91
92// --------------------------------------------------------------------
93
94/**
95 * Plural
96 *
97 * Takes a singular word and makes it plural
98 *
99 * @access public
100 * @param string
101 * @param bool
102 * @return str
Barry Mienydd671972010-10-04 16:33:58 +0200103 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000104if ( ! function_exists('plural'))
Barry Mienydd671972010-10-04 16:33:58 +0200105{
Derek Allard2067d1a2008-11-13 22:59:24 +0000106 function plural($str, $force = FALSE)
Phil Sturgeon50e57bc2011-08-13 10:26:04 -0600107 {
108 $result = strval($str);
Eric Barnescde712c2011-12-09 11:27:51 -0500109
Phil Sturgeon50e57bc2011-08-13 10:26:04 -0600110 $plural_rules = array(
Greg Aker03abee32011-12-25 00:31:29 -0600111 '/^(ox)$/' => '\1\2en', // ox
112 '/([m|l])ouse$/' => '\1ice', // mouse, louse
113 '/(matr|vert|ind)ix|ex$/' => '\1ices', // matrix, vertex, index
114 '/(x|ch|ss|sh)$/' => '\1es', // search, switch, fix, box, process, address
115 '/([^aeiouy]|qu)y$/' => '\1ies', // query, ability, agency
116 '/(hive)$/' => '\1s', // archive, hive
117 '/(?:([^f])fe|([lr])f)$/' => '\1\2ves', // half, safe, wife
118 '/sis$/' => 'ses', // basis, diagnosis
119 '/([ti])um$/' => '\1a', // datum, medium
120 '/(p)erson$/' => '\1eople', // person, salesperson
121 '/(m)an$/' => '\1en', // man, woman, spokesman
122 '/(c)hild$/' => '\1hildren', // child
123 '/(buffal|tomat)o$/' => '\1\2oes', // buffalo, tomato
124 '/(bu|campu)s$/' => '\1\2ses', // bus, campus
125 '/(alias|status|virus)/' => '\1es', // alias
126 '/(octop)us$/' => '\1i', // octopus
127 '/(ax|cris|test)is$/' => '\1es', // axis, crisis
128 '/s$/' => 's', // no change (compatibility)
129 '/$/' => 's',
Phil Sturgeon50e57bc2011-08-13 10:26:04 -0600130 );
Derek Allard2067d1a2008-11-13 22:59:24 +0000131
Andrey Andreev1e6b72c2012-01-06 19:09:22 +0200132 return preg_replace(array_keys($plural_rules), $plural_rules, $result);
Derek Allard2067d1a2008-11-13 22:59:24 +0000133 }
134}
135
136// --------------------------------------------------------------------
137
138/**
139 * Camelize
140 *
141 * Takes multiple words separated by spaces or underscores and camelizes them
142 *
143 * @access public
144 * @param string
145 * @return str
Barry Mienydd671972010-10-04 16:33:58 +0200146 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000147if ( ! function_exists('camelize'))
Barry Mienydd671972010-10-04 16:33:58 +0200148{
Derek Allard2067d1a2008-11-13 22:59:24 +0000149 function camelize($str)
Barry Mienydd671972010-10-04 16:33:58 +0200150 {
Andrey Andreev1e6b72c2012-01-06 19:09:22 +0200151 return substr(str_replace(' ', '', ucwords(preg_replace('/[\s_]+/', ' ', $str))), 1);
Derek Allard2067d1a2008-11-13 22:59:24 +0000152 }
153}
154
155// --------------------------------------------------------------------
156
157/**
158 * Underscore
159 *
160 * Takes multiple words separated by spaces and underscores them
161 *
162 * @access public
163 * @param string
164 * @return str
Barry Mienydd671972010-10-04 16:33:58 +0200165 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000166if ( ! function_exists('underscore'))
167{
168 function underscore($str)
169 {
170 return preg_replace('/[\s]+/', '_', strtolower(trim($str)));
171 }
172}
173
174// --------------------------------------------------------------------
175
176/**
177 * Humanize
178 *
Eric Barnescde712c2011-12-09 11:27:51 -0500179 * Takes multiple words separated by the separator and changes them to spaces
Derek Allard2067d1a2008-11-13 22:59:24 +0000180 *
181 * @access public
Eric Barnescde712c2011-12-09 11:27:51 -0500182 * @param string $str
183 * @param string $separator
Derek Allard2067d1a2008-11-13 22:59:24 +0000184 * @return str
Barry Mienydd671972010-10-04 16:33:58 +0200185 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000186if ( ! function_exists('humanize'))
Barry Mienydd671972010-10-04 16:33:58 +0200187{
Eric Barnescde712c2011-12-09 11:27:51 -0500188 function humanize($str, $separator = '_')
Derek Allard2067d1a2008-11-13 22:59:24 +0000189 {
Eric Barnescde712c2011-12-09 11:27:51 -0500190 return ucwords(preg_replace('/['.$separator.']+/', ' ', strtolower(trim($str))));
Derek Allard2067d1a2008-11-13 22:59:24 +0000191 }
192}
Barry Mienydd671972010-10-04 16:33:58 +0200193
Derek Allard2067d1a2008-11-13 22:59:24 +0000194/* End of file inflector_helper.php */
Andrey Andreev1e6b72c2012-01-06 19:09:22 +0200195/* Location: ./system/helpers/inflector_helper.php */