Merge branch 'maint/7.0'
[ninja.git] / system / helpers / inflector.php
blobde3460fe2bef0acd171834dcaa82dd75251c2c01
1 <?php defined('SYSPATH') OR die('No direct access allowed.');
2 /**
3 * Inflector helper class.
5 * $Id: inflector.php 3917 2009-01-21 03:06:22Z zombor $
7 * @package Core
8 * @author Kohana Team
9 * @copyright (c) 2007-2008 Kohana Team
10 * @license http://kohanaphp.com/license.html
12 class inflector {
14 // Cached inflections
15 protected static $cache = array();
17 // Uncountable and irregular words
18 protected static $uncountable;
19 protected static $irregular;
21 /**
22 * Checks if a word is defined as uncountable.
24 * @param string word to check
25 * @return boolean
27 public static function uncountable($str)
29 if (self::$uncountable === NULL)
31 // Cache uncountables
32 self::$uncountable = Kohana::config('inflector.uncountable');
34 // Make uncountables mirroed
35 self::$uncountable = array_combine(self::$uncountable, self::$uncountable);
38 return isset(self::$uncountable[strtolower($str)]);
41 /**
42 * Makes a plural word singular.
44 * @param string word to singularize
45 * @param integer number of things
46 * @return string
48 public static function singular($str, $count = NULL)
50 // Remove garbage
51 $str = strtolower(trim($str));
53 if (is_string($count))
55 // Convert to integer when using a digit string
56 $count = (int) $count;
59 // Do nothing with a single count
60 if ($count === 0 OR $count > 1)
61 return $str;
63 // Cache key name
64 $key = 'singular_'.$str.$count;
66 if (isset(self::$cache[$key]))
67 return self::$cache[$key];
69 if (inflector::uncountable($str))
70 return self::$cache[$key] = $str;
72 if (empty(self::$irregular))
74 // Cache irregular words
75 self::$irregular = Kohana::config('inflector.irregular');
78 if ($irregular = array_search($str, self::$irregular))
80 $str = $irregular;
82 elseif (preg_match('/[sxz]es$/', $str) OR preg_match('/[^aeioudgkprt]hes$/', $str))
84 // Remove "es"
85 $str = substr($str, 0, -2);
87 elseif (preg_match('/[^aeiou]ies$/', $str))
89 $str = substr($str, 0, -3).'y';
91 elseif (substr($str, -1) === 's' AND substr($str, -2) !== 'ss')
93 $str = substr($str, 0, -1);
96 return self::$cache[$key] = $str;
99 /**
100 * Makes a singular word plural.
102 * @param string word to pluralize
103 * @return string
105 public static function plural($str, $count = NULL)
107 // Remove garbage
108 $str = strtolower(trim($str));
110 if (is_string($count))
112 // Convert to integer when using a digit string
113 $count = (int) $count;
116 // Do nothing with singular
117 if ($count === 1)
118 return $str;
120 // Cache key name
121 $key = 'plural_'.$str.$count;
123 if (isset(self::$cache[$key]))
124 return self::$cache[$key];
126 if (inflector::uncountable($str))
127 return self::$cache[$key] = $str;
129 if (empty(self::$irregular))
131 // Cache irregular words
132 self::$irregular = Kohana::config('inflector.irregular');
135 if (isset(self::$irregular[$str]))
137 $str = self::$irregular[$str];
139 elseif (preg_match('/[sxz]$/', $str) OR preg_match('/[^aeioudgkprt]h$/', $str))
141 $str .= 'es';
143 elseif (preg_match('/[^aeiou]y$/', $str))
145 // Change "y" to "ies"
146 $str = substr_replace($str, 'ies', -1);
148 else
150 $str .= 's';
153 // Set the cache and return
154 return self::$cache[$key] = $str;
158 * Makes a phrase camel case.
160 * @param string phrase to camelize
161 * @return string
163 public static function camelize($str)
165 $str = 'x'.strtolower(trim($str));
166 $str = ucwords(preg_replace('/[\s_]+/', ' ', $str));
168 return substr(str_replace(' ', '', $str), 1);
172 * Makes a phrase underscored instead of spaced.
174 * @param string phrase to underscore
175 * @return string
177 public static function underscore($str)
179 return preg_replace('/\s+/', '_', trim($str));
183 * Makes an underscored or dashed phrase human-reable.
185 * @param string phrase to make human-reable
186 * @return string
188 public static function humanize($str)
190 return preg_replace('/[_-]+/', ' ', trim($str));
193 } // End inflector