Inflector
Kohana_Inflector

Class Contents

Class declared in SYSPATH/classes/inflector.php on line 3.

Methods

public static camelize( )
Kohana_Inflector

Source Code
public static function camelize($str)
{
	$str = 'x'.strtolower(trim($str));
	$str = ucwords(preg_replace('/[\s_]+/', ' ', $str));

	return substr(str_replace(' ', '', $str), 1);
}

public static decamelize( )
Kohana_Inflector

Source Code
public static function decamelize($str, $sep = ' ')
{
	return strtolower(preg_replace('/([a-z])([A-Z])/', '$1'.$sep.'$2', trim($str)));
}

public static humanize( )
Kohana_Inflector

Source Code
public static function humanize($str)
{
	return preg_replace('/[_-]+/', ' ', trim($str));
}

public static plural( )
Kohana_Inflector

Source Code
public static function plural($str, $count = NULL)
{
	// $count should always be a float
	$count = ($count === NULL) ? 0.0 : (float) $count;

	// Do nothing with singular
	if ($count == 1)
		return $str;

	// Remove garbage
	$str = trim($str);

	// Cache key name
	$key = 'plural_'.$str.$count;

	// Check uppercase
	$is_uppercase = ctype_upper($str);

	if (isset(Inflector::$cache[$key]))
		return Inflector::$cache[$key];

	if (Inflector::uncountable($str))
		return Inflector::$cache[$key] = $str;

	if (empty(Inflector::$irregular))
	{
		// Cache irregular words
		Inflector::$irregular = Kohana::config('inflector')->irregular;
	}

	if (isset(Inflector::$irregular[$str]))
	{
		$str = Inflector::$irregular[$str];
	}
	elseif (preg_match('/[sxz]$/', $str) OR preg_match('/[^aeioudgkprt]h$/', $str))
	{
		$str .= 'es';
	}
	elseif (preg_match('/[^aeiou]y$/', $str))
	{
		// Change "y" to "ies"
		$str = substr_replace($str, 'ies', -1);
	}
	else
	{
		$str .= 's';
	}

	// Convert to uppsecase if nessasary
	if ($is_uppercase)
	{
		$str = strtoupper($str);
	}

	// Set the cache and return
	return Inflector::$cache[$key] = $str;
}

public static singular( )
Kohana_Inflector

Source Code
public static function singular($str, $count = NULL)
{
	// $count should always be a float
	$count = ($count === NULL) ? 1.0 : (float) $count;

	// Do nothing when $count is not 1
	if ($count != 1)
		return $str;

	// Remove garbage
	$str = strtolower(trim($str));

	// Cache key name
	$key = 'singular_'.$str.$count;

	if (isset(Inflector::$cache[$key]))
		return Inflector::$cache[$key];

	if (Inflector::uncountable($str))
		return Inflector::$cache[$key] = $str;

	if (empty(Inflector::$irregular))
	{
		// Cache irregular words
		Inflector::$irregular = Kohana::config('inflector')->irregular;
	}

	if ($irregular = array_search($str, Inflector::$irregular))
	{
		$str = $irregular;
	}
	elseif (preg_match('/us$/', $str))
	{
		// http://en.wikipedia.org/wiki/Plural_form_of_words_ending_in_-us
		// Already singular, do nothing
	}
	elseif (preg_match('/[sxz]es$/', $str) OR preg_match('/[^aeioudgkprt]hes$/', $str))
	{
		// Remove "es"
		$str = substr($str, 0, -2);
	}
	elseif (preg_match('/[^aeiou]ies$/', $str))
	{
		// Replace "ies" with "y"
		$str = substr($str, 0, -3).'y';
	}
	elseif (substr($str, -1) === 's' AND substr($str, -2) !== 'ss')
	{
		// Remove singular "s"
		$str = substr($str, 0, -1);
	}

	return Inflector::$cache[$key] = $str;
}

public static uncountable( )
Kohana_Inflector

Source Code
public static function uncountable($str)
{
	if (Inflector::$uncountable === NULL)
	{
		// Cache uncountables
		Inflector::$uncountable = Kohana::config('inflector')->uncountable;

		// Make uncountables mirrored
		Inflector::$uncountable = array_combine(Inflector::$uncountable, Inflector::$uncountable);
	}

	return isset(Inflector::$uncountable[strtolower($str)]);
}

public static underscore( )
Kohana_Inflector

Source Code
public static function underscore($str)
{
	return preg_replace('/\s+/', '_', trim($str));
}