inflector.php 4.34 KB
Newer Older
Taylor Otwell committed
1
<?php namespace Laravel;
2 3 4

class Inflector {

Taylor Otwell committed
5 6 7 8 9 10 11 12 13 14 15 16 17
	/**
	 * The words that have been converted to singular.
	 *
	 * @var array
	 */
	private static $singular_cache = array();

	/**
	 * The words that have been converted to plural.
	 *
	 * @var array
	 */
	private static $plural_cache = array();
18 19 20 21 22 23 24

	/**
	 * Plural word forms.
	 *
	 * @var array
	 */
	private static $plural = array(
Taylor Otwell committed
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
		'/(quiz)$/i' => "$1zes",
		'/^(ox)$/i' => "$1en",
		'/([m|l])ouse$/i' => "$1ice",
		'/(matr|vert|ind)ix|ex$/i' => "$1ices",
		'/(x|ch|ss|sh)$/i' => "$1es",
		'/([^aeiouy]|qu)y$/i' => "$1ies",
		'/(hive)$/i' => "$1s",
		'/(?:([^f])fe|([lr])f)$/i' => "$1$2ves",
		'/(shea|lea|loa|thie)f$/i' => "$1ves",
		'/sis$/i' => "ses",
		'/([ti])um$/i' => "$1a",
		'/(tomat|potat|ech|her|vet)o$/i' => "$1oes",
		'/(bu)s$/i' => "$1ses",
		'/(alias)$/i' => "$1es",
		'/(octop)us$/i' => "$1i",
		'/(ax|test)is$/i' => "$1es",
		'/(us)$/i' => "$1es",
		'/s$/i' => "s",
		'/$/' => "s"
	);

	/**
	 * Singular word forms.
	 *
	 * @var array
	 */
	private static $singular = array(
		'/(quiz)zes$/i' => "$1",
		'/(matr)ices$/i' => "$1ix",
		'/(vert|ind)ices$/i' => "$1ex",
		'/^(ox)en$/i' => "$1",
		'/(alias)es$/i' => "$1",
		'/(octop|vir)i$/i' => "$1us",
		'/(cris|ax|test)es$/i' => "$1is",
		'/(shoe)s$/i' => "$1",
		'/(o)es$/i' => "$1",
		'/(bus)es$/i' => "$1",
		'/([m|l])ice$/i' => "$1ouse",
		'/(x|ch|ss|sh)es$/i' => "$1",
		'/(m)ovies$/i' => "$1ovie",
		'/(s)eries$/i' => "$1eries",
		'/([^aeiouy]|qu)ies$/i' => "$1y",
		'/([lr])ves$/i' => "$1f",
		'/(tive)s$/i' => "$1",
		'/(hive)s$/i' => "$1",
		'/(li|wi|kni)ves$/i' => "$1fe",
		'/(shea|loa|lea|thie)ves$/i' => "$1f",
		'/(^analy)ses$/i' => "$1sis",
		'/((a)naly|(b)a|(d)iagno|(p)arenthe|(p)rogno|(s)ynop|(t)he)ses$/i' => "$1$2sis",
		'/([ti])a$/i' => "$1um",
		'/(n)ews$/i' => "$1ews",
		'/(h|bl)ouses$/i' => "$1ouse",
		'/(corpse)s$/i' => "$1",
		'/(us)es$/i' => "$1",
		'/(us|ss)$/i' => "$1",
		'/s$/i' => "",
	);

	/**
	 * Irregular word forms.
	 *
	 * @var array
	 */
	private static $irregular = array(
89
		'child' => 'children',
Taylor Otwell committed
90 91 92
		'foot' => 'feet',
		'goose' => 'geese',
		'man' => 'men',
93
		'move' => 'moves',
Taylor Otwell committed
94
		'person' => 'people',
95 96
		'sex' => 'sexes',
		'tooth' => 'teeth',
Taylor Otwell committed
97 98 99 100 101 102 103 104
	);

	/**
	 * Uncountable word forms.
	 *
	 * @var array
	 */
	private static $uncountable = array(
105
		'audio',
106
		'equipment',
Taylor Otwell committed
107
		'deer',
108
		'fish',
109
		'gold',
110
		'information',
Taylor Otwell committed
111 112
		'money',
		'rice',
113
		'police',
114 115 116
		'series',
		'sheep',
		'species',
Taylor Otwell committed
117
	);
118 119 120 121

	/**
	 * Convert a word to its plural form.
	 *
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
	 * Optionally, a count argument may be provided. If the count is greater than
	 * one, the word will be pluralized, otherwise the word will be returned from
	 * the method unchanged.
	 *
	 * <code>
	 *		// Get the plural form of the word "child"
	 *		$children = Inflector::plural('child');
	 *
	 *		// Returns "comments"
	 *		$comments = Inflector::plural('comment', 10);
	 *
	 *		// Returns "comment"
	 *		$comment = Inflector::plural('comment', 1);
	 * </code>
	 *
137
	 * @param  string  $value
138
	 * @param  int     $count
139 140
	 * @return string
	 */
141
	public static function plural($value, $count = null)
142
	{
Phill Sparks committed
143
		if ( ! is_null($count) and $count == 1) return $value;
144

145 146
		$irregular = array_flip(static::$irregular);

Taylor Otwell committed
147 148 149
		$plural = static::inflect($value, static::$plural_cache, $irregular, static::$plural);

		return static::$plural_cache[$value] = $plural;
150 151 152 153 154 155 156 157 158 159
	}

	/**
	 * Convert a word to its singular form.
	 *
	 * @param  string  $value
	 * @return string
	 */
	public static function singular($value)
	{
Taylor Otwell committed
160 161 162
		$singular = static::inflect($value, static::$singular_cache, static::$irregular, static::$singular);

		return static::$singular_cache[$value] = $singular;
Taylor Otwell committed
163 164 165 166 167 168 169 170 171 172 173 174 175 176
	}

	/**
	 * Convert a word to its singular or plural form.
	 *
	 * @param  string  $value
	 * @param  array   $cache
	 * @param  array   $irregular
	 * @param  array   $source
	 * @return string
	 */
	private static function inflect($value, $cache, $irregular, $source)
	{
		if (array_key_exists($value, $cache))
Taylor Otwell committed
177
		{
Taylor Otwell committed
178
			return $cache[$value];
Taylor Otwell committed
179 180 181 182
		}

		if (in_array(strtolower($value), static::$uncountable))
		{
Taylor Otwell committed
183
			return $value;
Taylor Otwell committed
184 185
		}

Taylor Otwell committed
186
		foreach ($irregular as $irregular => $pattern)
Taylor Otwell committed
187
		{
Taylor Otwell committed
188
			if (preg_match($pattern = '/'.$pattern.'$/i', $value))
Taylor Otwell committed
189
			{
Taylor Otwell committed
190
				return preg_replace($pattern, $irregular, $value);
Taylor Otwell committed
191 192
			}
		}
193

Taylor Otwell committed
194
		foreach ($source as $pattern => $inflected)
Taylor Otwell committed
195 196 197
		{
			if (preg_match($pattern, $value))
			{
Taylor Otwell committed
198
				return preg_replace($pattern, $inflected, $value);
Taylor Otwell committed
199 200
			}
		}
201

Taylor Otwell committed
202
		return $value;
203 204
	}

Phill Sparks committed
205
}