Source for file inflector.php
Documentation is available at inflector.php
* @package Joomla.Platform
* @copyright Copyright (C) 2005 - 2011 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE
* Joomla Platform String Inflector Class
* The Inflector transforms words
* @package Joomla.Platform
* The singleton instance.
private static $_instance;
* The inflector rules for singularisation, pluralisation and countability.
'/(matr)ices$/i' =>
'\1ix',
'/(vert|ind)ices$/i' =>
'\1ex',
'/(alumn|bacill|cact|foc|fung|nucle|radi|stimul|syllab|termin|viri?)i$/i' =>
'\1us',
'/([ftw]ax)es/i' =>
'\1',
'/(cris|ax|test)es$/i' =>
'\1is',
'/(shoe|slave)s$/i' =>
'\1',
'/([^aeiouy]|qu)ies$/i' =>
'\1y',
'/([m|l])ouse$/i' =>
'\1ice',
'/(matr|vert|ind)(ix|ex)$/i' =>
'\1ices',
'/(x|ch|ss|sh)$/i' =>
'\1es',
'/([^aeiouy]|qu)y$/i' =>
'\1ies',
'/([^aeiouy]|qu)ies$/i' =>
'\1y',
'/(?:([^f])fe|([lr])f)$/i' =>
'\1\2ves',
'/(buffal|tomat)o$/i' =>
'\1\2oes',
'/(alumn|bacill|cact|foc|fung|nucle|radi|stimul|syllab|termin|vir)us$/i' =>
'\1i',
'/(ax|cris|test)is$/i' =>
'\1es',
* The array is in the form [singular => plural]
private $_cache =
array();
// Pre=populate the irregual singular/plural.
->addWord('alias', 'aliases')
->addWord('bus', 'buses')
->addWord('foot', 'feet')
->addWord('goose', 'geese')
->addWord('hive', 'hives')
->addWord('louse', 'lice')
->addWord('mouse', 'mice')
->addWord('quiz', 'quizes')
->addWord('status', 'statuses')
->addWord('tooth', 'teeth')
->addWord('woman', 'women');
* Adds inflection regex rules to the inflector.
* @param mixed $data A string or an array of strings or regex rules to add.
* @param string $ruleType The rule type: singular | plural | countable
* @throws InvalidArgumentException
private function _addRule($data, $ruleType)
throw
new InvalidArgumentException('Invalid inflector rule data.');
// Ensure a string is pushed.
array_push($this->_rules[$ruleType], (string)
$rule);
* Gets an inflected word from the cache where the singular form is supplied.
* @param string $singular A singular form of a word.
* @return mixed The cached inflection or false if none found.
private function _getCachedPlural($singular)
// Check if the word is in cache.
if (isset
($this->_cache[$singular]))
return $this->_cache[$singular];
* Gets an inflected word from the cache where the plural form is supplied.
* @param string $plural A plural form of a word.
* @return mixed The cached inflection or false if none found.
private function _getCachedSingular($plural)
* Execute a regex from rules.
* The 'plural' rule type expects a singular word.
* The 'singular' rule type expects a plural word.
* @param string $word The string input.
* @param string $ruleType String (eg, singular|plural)
* @return mixed An inflected string, or false if no rule could be applied.
private function _matchRegexRule($word, $ruleType)
// Cycle through the regex rules.
foreach ($this->_rules[$ruleType] as $regex =>
$replacement)
$matchedWord =
preg_replace($regex, $replacement, $word, -
1, $matches);
* Sets an inflected word in the cache.
* @param string $singular The singular form of the word.
* @param string $plural The plural form of the word. If omitted, it is assumed the singular and plural are identical.
private function _setCache($singular, $plural =
null)
$this->_cache[$singular] =
$plural;
* @param mixed $data A string or an array of strings to add.
* @return JStringInflector Returns this object to support chaining.
$this->_addRule($data, 'countable');
* Adds a specific singular-plural pair for a word.
* @param string $singular The singular form of the word.
* @param string $plural The plural form of the word. If omitted, it is assumed the singular and plural are identical.
* @return JStringInflector Returns this object to support chaining.
public function addWord($singular, $plural =
null)
$this->_setCache($singular, $plural);
* Adds a pluralisation rule.
* @param mixed $data A string or an array of regex rules to add.
* @return JStringInflector Returns this object to support chaining.
$this->_addRule($data, 'plural');
* Adds a singularisation rule.
* @param mixed $data A string or an array of regex rules to add.
* @return JStringInflector Returns this object to support chaining.
$this->_addRule($data, 'singular');
* Gets an instance of the JStringInflector singleton.
* @param boolean $new If true (default is false), returns a new instance regardless if one exists.
* This argument is mainly used for testing.
* @return JStringInflector
self::$_instance =
new static;
* Checks if a word is countable.
* @param string $word The string input.
* @return boolean True if word is countable, false otherwise.
return (boolean)
in_array($word, $this->_rules['countable']);
* Checks if a word is in a plural form.
* @param string $word The string input.
* @return boolean True if word is plural, false if not.
// Try the cache for an known inflection.
$inflection =
$this->_getCachedSingular($word);
if ($inflection !==
false)
// Compute the inflection to cache the values, and compare.
* Checks if a word is in a singular form.
* @param string $word The string input.
* @return boolean True if word is singular, false if not.
// Try the cache for an known inflection.
$inflection =
$this->_getCachedPlural($word);
if ($inflection !==
false)
// Compute the inflection to cache the values, and compare.
* Converts a word into its plural form.
* @param string $word The singular word to pluralise.
* @return mixed An inflected string, or false if no rule could be applied.
// Try to get the cached plural form from the singular.
$cache =
$this->_getCachedPlural($word);
// Check if the word is a known singular.
if ($this->_getCachedSingular($word))
// Compute the inflection.
$inflected =
$this->_matchRegexRule($word, 'plural');
if ($inflected !==
false)
$this->_setCache($word, $inflected);
* Converts a word into its singular form.
* @param string $word The plural word to singularise.
* @return mixed An inflected string, or false if no rule could be applied.
// Try to get the cached singular form from the plural.
$cache =
$this->_getCachedSingular($word);
// Check if the word is a known plural.
if ($this->_getCachedPlural($word))
// Compute the inflection.
$inflected =
$this->_matchRegexRule($word, 'singular');
if ($inflected !==
false)
$this->_setCache($inflected, $word);
Documentation generated on Tue, 19 Nov 2013 15:05:32 +0100 by phpDocumentor 1.4.3