Initial Kohana install
[speedfreak] / Server / system / helpers / inflector.php
1 <?php defined('SYSPATH') OR die('No direct access allowed.');
2 /**
3  * Inflector helper class.
4  *
5  * $Id: inflector.php 4072 2009-03-13 17:20:38Z jheathco $
6  *
7  * @package    Core
8  * @author     Kohana Team
9  * @copyright  (c) 2007-2008 Kohana Team
10  * @license    http://kohanaphp.com/license.html
11  */
12 class inflector_Core {
13
14         // Cached inflections
15         protected static $cache = array();
16
17         // Uncountable and irregular words
18         protected static $uncountable;
19         protected static $irregular;
20
21         /**
22          * Checks if a word is defined as uncountable.
23          *
24          * @param   string   word to check
25          * @return  boolean
26          */
27         public static function uncountable($str)
28         {
29                 if (inflector::$uncountable === NULL)
30                 {
31                         // Cache uncountables
32                         inflector::$uncountable = Kohana::config('inflector.uncountable');
33
34                         // Make uncountables mirroed
35                         inflector::$uncountable = array_combine(inflector::$uncountable, inflector::$uncountable);
36                 }
37
38                 return isset(inflector::$uncountable[strtolower($str)]);
39         }
40
41         /**
42          * Makes a plural word singular.
43          *
44          * @param   string   word to singularize
45          * @param   integer  number of things
46          * @return  string
47          */
48         public static function singular($str, $count = NULL)
49         {
50                 // Remove garbage
51                 $str = strtolower(trim($str));
52
53                 if (is_string($count))
54                 {
55                         // Convert to integer when using a digit string
56                         $count = (int) $count;
57                 }
58
59                 // Do nothing with a single count
60                 if ($count === 0 OR $count > 1)
61                         return $str;
62
63                 // Cache key name
64                 $key = 'singular_'.$str.$count;
65
66                 if (isset(inflector::$cache[$key]))
67                         return inflector::$cache[$key];
68
69                 if (inflector::uncountable($str))
70                         return inflector::$cache[$key] = $str;
71
72                 if (empty(inflector::$irregular))
73                 {
74                         // Cache irregular words
75                         inflector::$irregular = Kohana::config('inflector.irregular');
76                 }
77
78                 if ($irregular = array_search($str, inflector::$irregular))
79                 {
80                         $str = $irregular;
81                 }
82                 elseif (preg_match('/[sxz]es$/', $str) OR preg_match('/[^aeioudgkprt]hes$/', $str))
83                 {
84                         // Remove "es"
85                         $str = substr($str, 0, -2);
86                 }
87                 elseif (preg_match('/[^aeiou]ies$/', $str))
88                 {
89                         $str = substr($str, 0, -3).'y';
90                 }
91                 elseif (substr($str, -1) === 's' AND substr($str, -2) !== 'ss')
92                 {
93                         $str = substr($str, 0, -1);
94                 }
95
96                 return inflector::$cache[$key] = $str;
97         }
98
99         /**
100          * Makes a singular word plural.
101          *
102          * @param   string  word to pluralize
103          * @return  string
104          */
105         public static function plural($str, $count = NULL)
106         {
107                 // Remove garbage
108                 $str = strtolower(trim($str));
109
110                 if (is_string($count))
111                 {
112                         // Convert to integer when using a digit string
113                         $count = (int) $count;
114                 }
115
116                 // Do nothing with singular
117                 if ($count === 1)
118                         return $str;
119
120                 // Cache key name
121                 $key = 'plural_'.$str.$count;
122
123                 if (isset(inflector::$cache[$key]))
124                         return inflector::$cache[$key];
125
126                 if (inflector::uncountable($str))
127                         return inflector::$cache[$key] = $str;
128
129                 if (empty(inflector::$irregular))
130                 {
131                         // Cache irregular words
132                         inflector::$irregular = Kohana::config('inflector.irregular');
133                 }
134
135                 if (isset(inflector::$irregular[$str]))
136                 {
137                         $str = inflector::$irregular[$str];
138                 }
139                 elseif (preg_match('/[sxz]$/', $str) OR preg_match('/[^aeioudgkprt]h$/', $str))
140                 {
141                         $str .= 'es';
142                 }
143                 elseif (preg_match('/[^aeiou]y$/', $str))
144                 {
145                         // Change "y" to "ies"
146                         $str = substr_replace($str, 'ies', -1);
147                 }
148                 else
149                 {
150                         $str .= 's';
151                 }
152
153                 // Set the cache and return
154                 return inflector::$cache[$key] = $str;
155         }
156
157         /**
158          * Makes a phrase camel case.
159          *
160          * @param   string  phrase to camelize
161          * @return  string
162          */
163         public static function camelize($str)
164         {
165                 $str = 'x'.strtolower(trim($str));
166                 $str = ucwords(preg_replace('/[\s_]+/', ' ', $str));
167
168                 return substr(str_replace(' ', '', $str), 1);
169         }
170
171         /**
172          * Makes a phrase underscored instead of spaced.
173          *
174          * @param   string  phrase to underscore
175          * @return  string
176          */
177         public static function underscore($str)
178         {
179                 return preg_replace('/\s+/', '_', trim($str));
180         }
181
182         /**
183          * Makes an underscored or dashed phrase human-reable.
184          *
185          * @param   string  phrase to make human-reable
186          * @return  string
187          */
188         public static function humanize($str)
189         {
190                 return preg_replace('/[_-]+/', ' ', trim($str));
191         }
192
193 } // End inflector