Source for file inflector.php
Documentation is available at inflector.php
* @package FrameworkOnFramework
* @copyright Copyright (C) 2010 - 2012 Akeeba Ltd. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
* The FOFInflector is an adaptation of the Akelos PHP Inflector which is a PHP
* port from a Ruby on Rails project.
* FOFInflector to pluralize and singularize English nouns.
* @package FrameworkOnFramework
* Rules for pluralizing and singularizing of nouns.
protected static $_rules =
array
'pluralization' =>
array(
'/child$/i' =>
'children',
'/person$/i' =>
'people',
'/(m|l)ouse$/i' =>
'$1ice',
'/(matr|vert|ind|suff)ix|ex$/i' =>
'$1ices',
'/(x|ch|ss|sh)$/i' =>
'$1es',
'/([^aeiouy]|qu)y$/i' =>
'$1ies',
'/(?:([^f])fe|([lr])f)$/i' =>
'$1$2ves',
'/([ti]|addend)um$/i' =>
'$1a',
'/(alumn|formul)a$/i' =>
'$1ae',
'/(buffal|tomat|her)o$/i' =>
'$1oes',
'/(alias|status)$/i' =>
'$1es',
'/(octop|vir)us$/i' =>
'$1i',
'/(gen)us$/i' =>
'$1era',
'/(ax|test)is$/i' =>
'$1es',
'singularization' =>
array(
'/cookies$/i' =>
'cookie',
'/children$/i' =>
'child',
'/people$/i' =>
'person',
'/databases$/i' =>
'database',
'/(matr|suff)ices$/i' =>
'\1ix',
'/(vert|ind)ices$/i' =>
'\1ex',
'/(alias|status)es$/i' =>
'\1',
'/(tomato|hero|buffalo)es$/i' =>
'\1',
'/([octop|vir])i$/i' =>
'\1us',
'/(gen)era$/i' =>
'\1us',
'/(cris|^ax|test)es$/i' =>
'\1is',
'/([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',
'/([^f])ves$/i' =>
'\1fe',
'/(^analy)ses$/i' =>
'\1sis',
'/((a)naly|(b)a|(d)iagno|(p)arenthe|(p)rogno|(s)ynop|(t)he)ses$/i' =>
'\1\2sis',
'/([ti]|addend)a$/i' =>
'\1um',
'/(alumn|formul)ae$/i' =>
'$1a',
* Cache of pluralized and singularized nouns.
protected static $_cache =
array(
'singularized' =>
array(),
* Prevent creating instances of this class by making the contructor private
private function __construct()
* Add a word to the cache, useful to make exceptions or to add words in other languages.
* @param string $singular word.
* @param string $plural word.
public static function addWord($singular, $plural)
self::$_cache['pluralized'][$singular] =
$plural;
self::$_cache['singularized'][$plural] =
$singular;
* Singular English word to plural.
* @param string $word word to pluralize.
* @return string Plural noun.
// Get the cached noun of it exists
if (isset
(self::$_cache['pluralized'][$word]))
return self::$_cache['pluralized'][$word];
// Create the plural noun
if (in_array($word, self::$_rules['countable']))
$_cache['pluralized'][$word] =
$word;
foreach (self::$_rules['pluralization'] as $regexp =>
$replacement)
$plural =
preg_replace($regexp, $replacement, $word, -
1, $matches);
$_cache['pluralized'][$word] =
$plural;
* Plural English word to singular.
* @param string $word Word to singularize.
* @return string Singular noun.
// Get the cached noun of it exists
if (isset
(self::$_cache['singularized'][$word]))
return self::$_cache['singularized'][$word];
// Create the singular noun
if (in_array($word, self::$_rules['countable']))
$_cache['singularized'][$word] =
$word;
foreach (self::$_rules['singularization'] as $regexp =>
$replacement)
$singular =
preg_replace($regexp, $replacement, $word, -
1, $matches);
$_cache['singularized'][$word] =
$singular;
* Returns given word as CamelCased.
* Converts a word like "foo_bar" or "foo bar" to "FooBar". It
* will remove non alphanumeric characters from the word, so
* "who's online" will be converted to "WhoSOnline"
* @param string $word Word to convert to camel case.
* @return string UpperCamelCasedWord
* Converts a word "into_it_s_underscored_version"
* Convert any "CamelCased" or "ordinary Word" into an "underscored_word".
* @param string $word Word to underscore
* @return string Underscored word
* Convert any "CamelCased" word into an array of strings
* Returns an array of strings each of which is a substring of string formed
* by splitting it at the camelcased letters.
* @param string $word Word to explode
* @return array Array of strings
public static function explode($word)
$result =
explode('_', self::underscore($word));
* Convert an array of strings into a "CamelCased" word.
* @param array $words Array to implode
* @return string UpperCamelCasedWord
public static function implode($words)
$result =
self::camelize(implode('_', $words));
* Returns a human-readable string from $word.
* Returns a human-readable string from $word, by replacing
* underscores with a space, and by upper-casing the initial
* @param string $word String to "humanize"
* @return string Human-readable word
* Converts a class name to its table name according to Koowa
* Converts "Person" to "people"
* @param string $className Class name for getting related table_name.
* @return string plural_table_name
public static function tableize($className)
$result =
self::underscore($className);
if (!self::isPlural($className))
$result =
self::pluralize($result);
* Converts a table name to its class name according to Koowa naming conventions.
* @param string $tableName Table name for getting related ClassName.
* @return string SingularClassName
* @example Converts "people" to "Person"
public static function classify($tableName)
$result =
self::camelize(self::singularize($tableName));
* Returns camelBacked version of a string. Same as camelize but first char is lowercased.
* @param string $string String to be camelBacked.
$string =
self::camelize(self::underscore($string));
* Check to see if an English word is singular
* @param string $string The word to check
// Check cache assuming the string is plural.
$singular = isset
(self::$_cache['singularized'][$string]) ?
self::$_cache['singularized'][$string] :
null;
$plural =
$singular && isset
(self::$_cache['pluralized'][$singular]) ?
self::$_cache['pluralized'][$singular] :
null;
if ($singular &&
$plural)
return $plural !=
$string;
// If string is not in the cache, try to pluralize and singularize it.
return self::singularize(self::pluralize($string)) ==
$string;
* Check to see if an Enlish word is plural.
* @param string $string String to be checked.
public static function isPlural($string)
// Check cache assuming the string is singular.
$plural = isset
(self::$_cache['pluralized'][$string]) ?
self::$_cache['pluralized'][$string] :
null;
$singular =
$plural && isset
(self::$_cache['singularized'][$plural]) ?
self::$_cache['singularized'][$plural] :
null;
if ($plural &&
$singular)
return $singular !=
$string;
// If string is not in the cache, try to singularize and pluralize it.
return self::pluralize(self::singularize($string)) ==
$string;
* Gets a part of a CamelCased word by index.
* Use a negative index to start at the last part of the word (-1 is the
* @param string $string Word
* @param integer $index Index of the part
* @param string $default Default value
public static function getPart($string, $index, $default =
null)
$parts =
self::explode($string);
$index =
count($parts) +
$index;
return isset
($parts[$index]) ?
$parts[$index] :
$default;
Documentation generated on Tue, 19 Nov 2013 15:05:33 +0100 by phpDocumentor 1.4.3