Inflector
» Kohana_Inflector

Inflector helper class.

package
Kohana
author
Kohana Team
copyright
© 2007-2009 Kohana Team
license
http://kohanaphp.com/license.html

Methods

public static uncountable ( string $str )

Checks if a word is defined as uncountable.

Returns:
  • boolean
Source:
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 singular ( string $str, integer $count = NULL )

Makes a plural word singular.

Returns:
  • string
Source:
public static function singular($str, $count = NULL)
{
	// Remove garbage
	$str = strtolower(trim($str));

	if (is_string($count))
	{
		// Convert to integer when using a digit string
		$count = (int) $count;
	}

	// Do nothing with a single count
	if ($count === 0 OR $count > 1)
		return $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('/[sxz]es$/', $str) OR preg_match('/[^aeioudgkprt]hes$/', $str))
	{
		// Remove "es"
		$str = substr($str, 0, -2);
	}
	elseif (preg_match('/[^aeiou]ies$/', $str))
	{
		$str = substr($str, 0, -3).'y';
	}
	elseif (substr($str, -1) === 's' AND substr($str, -2) !== 'ss')
	{
		$str = substr($str, 0, -1);
	}

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

public static plural ( string $str )

Makes a singular word plural.

Returns:
  • string
Source:
public static function plural($str, $count = NULL)
{
	// Remove garbage
	$str = strtolower(trim($str));

	if (is_string($count))
	{
		// Convert to integer when using a digit string
		$count = (int) $count;
	}

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

	// Cache key name
	$key = 'plural_'.$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 (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';
	}

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

public static camelize ( string $str )

Makes a phrase camel case.

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

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

public static underscore ( string $str )

Makes a phrase underscored instead of spaced.

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

public static humanize ( string $str )

Makes an underscored or dashed phrase human-readable.

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

final private __construct ( )

Source:
final private function __construct()
{
	// This is a static class
}