source: sourcecode/system/helpers/inflector_helper.php @ 1

Last change on this file since 1 was 1, checked in by dungnv, 11 years ago
File size: 5.2 KB
Line 
1<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
2/**
3 * CodeIgniter
4 *
5 * An open source application development framework for PHP 5.1.6 or newer
6 *
7 * @package             CodeIgniter
8 * @author              ExpressionEngine Dev Team
9 * @copyright   Copyright (c) 2008 - 2011, EllisLab, Inc.
10 * @license             http://codeigniter.com/user_guide/license.html
11 * @link                http://codeigniter.com
12 * @since               Version 1.0
13 * @filesource
14 */
15
16// ------------------------------------------------------------------------
17
18/**
19 * CodeIgniter Inflector Helpers
20 *
21 * @package             CodeIgniter
22 * @subpackage  Helpers
23 * @category    Helpers
24 * @author              ExpressionEngine Dev Team
25 * @link                http://codeigniter.com/user_guide/helpers/directory_helper.html
26 */
27
28
29// --------------------------------------------------------------------
30
31/**
32 * Singular
33 *
34 * Takes a plural word and makes it singular
35 *
36 * @access      public
37 * @param       string
38 * @return      str
39 */
40if ( ! function_exists('singular'))
41{
42        function singular($str)
43        {
44                $result = strval($str);
45
46                $singular_rules = array(
47                        '/(matr)ices$/'         => '\1ix',
48                        '/(vert|ind)ices$/'     => '\1ex',
49                        '/^(ox)en/'             => '\1',
50                        '/(alias)es$/'          => '\1',
51                        '/([octop|vir])i$/'     => '\1us',
52                        '/(cris|ax|test)es$/'   => '\1is',
53                        '/(shoe)s$/'            => '\1',
54                        '/(o)es$/'              => '\1',
55                        '/(bus|campus)es$/'     => '\1',
56                        '/([m|l])ice$/'         => '\1ouse',
57                        '/(x|ch|ss|sh)es$/'     => '\1',
58                        '/(m)ovies$/'           => '\1\2ovie',
59                        '/(s)eries$/'           => '\1\2eries',
60                        '/([^aeiouy]|qu)ies$/'  => '\1y',
61                        '/([lr])ves$/'          => '\1f',
62                        '/(tive)s$/'            => '\1',
63                        '/(hive)s$/'            => '\1',
64                        '/([^f])ves$/'          => '\1fe',
65                        '/(^analy)ses$/'        => '\1sis',
66                        '/((a)naly|(b)a|(d)iagno|(p)arenthe|(p)rogno|(s)ynop|(t)he)ses$/' => '\1\2sis',
67                        '/([ti])a$/'            => '\1um',
68                        '/(p)eople$/'           => '\1\2erson',
69                        '/(m)en$/'              => '\1an',
70                        '/(s)tatuses$/'         => '\1\2tatus',
71                        '/(c)hildren$/'         => '\1\2hild',
72                        '/(n)ews$/'             => '\1\2ews',
73                        '/([^u])s$/'            => '\1',
74                );
75               
76                foreach ($singular_rules as $rule => $replacement)
77                {
78                        if (preg_match($rule, $result))
79                        {
80                                $result = preg_replace($rule, $replacement, $result);
81                                break;
82                        }
83                }
84
85                return $result;
86        }
87}
88
89// --------------------------------------------------------------------
90
91/**
92 * Plural
93 *
94 * Takes a singular word and makes it plural
95 *
96 * @access      public
97 * @param       string
98 * @param       bool
99 * @return      str
100 */
101if ( ! function_exists('plural'))
102{
103        function plural($str, $force = FALSE)
104        {
105                $result = strval($str);
106       
107                $plural_rules = array(
108                        '/^(ox)$/'                 => '\1\2en',     // ox
109                        '/([m|l])ouse$/'           => '\1ice',      // mouse, louse
110                        '/(matr|vert|ind)ix|ex$/'  => '\1ices',     // matrix, vertex, index
111                        '/(x|ch|ss|sh)$/'          => '\1es',       // search, switch, fix, box, process, address
112                        '/([^aeiouy]|qu)y$/'       => '\1ies',      // query, ability, agency
113                        '/(hive)$/'                => '\1s',        // archive, hive
114                        '/(?:([^f])fe|([lr])f)$/'  => '\1\2ves',    // half, safe, wife
115                        '/sis$/'                   => 'ses',        // basis, diagnosis
116                        '/([ti])um$/'              => '\1a',        // datum, medium
117                        '/(p)erson$/'              => '\1eople',    // person, salesperson
118                        '/(m)an$/'                 => '\1en',       // man, woman, spokesman
119                        '/(c)hild$/'               => '\1hildren',  // child
120                        '/(buffal|tomat)o$/'       => '\1\2oes',    // buffalo, tomato
121                        '/(bu|campu)s$/'           => '\1\2ses',    // bus, campus
122                        '/(alias|status|virus)/'   => '\1es',       // alias
123                        '/(octop)us$/'             => '\1i',        // octopus
124                        '/(ax|cris|test)is$/'      => '\1es',       // axis, crisis
125                        '/s$/'                     => 's',          // no change (compatibility)
126                        '/$/'                      => 's',
127                );
128
129                foreach ($plural_rules as $rule => $replacement)
130                {
131                        if (preg_match($rule, $result))
132                        {
133                                $result = preg_replace($rule, $replacement, $result);
134                                break;
135                        }
136                }
137
138                return $result;
139        }
140}
141
142// --------------------------------------------------------------------
143
144/**
145 * Camelize
146 *
147 * Takes multiple words separated by spaces or underscores and camelizes them
148 *
149 * @access      public
150 * @param       string
151 * @return      str
152 */
153if ( ! function_exists('camelize'))
154{
155        function camelize($str)
156        {
157                $str = 'x'.strtolower(trim($str));
158                $str = ucwords(preg_replace('/[\s_]+/', ' ', $str));
159                return substr(str_replace(' ', '', $str), 1);
160        }
161}
162
163// --------------------------------------------------------------------
164
165/**
166 * Underscore
167 *
168 * Takes multiple words separated by spaces and underscores them
169 *
170 * @access      public
171 * @param       string
172 * @return      str
173 */
174if ( ! function_exists('underscore'))
175{
176        function underscore($str)
177        {
178                return preg_replace('/[\s]+/', '_', strtolower(trim($str)));
179        }
180}
181
182// --------------------------------------------------------------------
183
184/**
185 * Humanize
186 *
187 * Takes multiple words separated by underscores and changes them to spaces
188 *
189 * @access      public
190 * @param       string
191 * @return      str
192 */
193if ( ! function_exists('humanize'))
194{
195        function humanize($str)
196        {
197                return ucwords(preg_replace('/[_]+/', ' ', strtolower(trim($str))));
198        }
199}
200
201
202/* End of file inflector_helper.php */
203/* Location: ./system/helpers/inflector_helper.php */
Note: See TracBrowser for help on using the repository browser.