| Current Path : /home/wuectly/www/03cbe/ |
| Current File : /home/wuectly/www/03cbe/Inflector.php.tar |
home/wuectly/www/libraries/fof30/Inflector/Inflector.php 0000604 00000031164 15245561532 0017301 0 ustar 00 <?php
/**
* @package FOF
* @copyright Copyright (c)2010-2020 Nicholas K. Dionysopoulos / Akeeba Ltd
* @license GNU General Public License version 2, or later
*/
namespace FOF30\Inflector;
defined('_JEXEC') || die;
/**
* An Inflector to pluralize and singularize English nouns.
*/
class Inflector
{
/**
* Rules for pluralizing and singularizing of nouns.
*
* @var array
*/
protected $rules = [
// Pluralization rules. The regex on the left transforms to the regex on the right.
'pluralization' => [
'/move$/i' => 'moves',
'/sex$/i' => 'sexes',
'/child$/i' => 'children',
'/children$/i' => 'children',
'/man$/i' => 'men',
'/men$/i' => 'men',
'/foot$/i' => 'feet',
'/feet$/i' => 'feet',
'/person$/i' => 'people',
'/people$/i' => 'people',
'/taxon$/i' => 'taxa',
'/taxa$/i' => 'taxa',
'/(quiz)$/i' => '$1zes',
'/^(ox)$/i' => '$1en',
'/oxen$/i' => 'oxen',
'/(m|l)ouse$/i' => '$1ice',
'/(m|l)ice$/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',
'/sis$/i' => 'ses',
'/([ti]|addend)um$/i' => '$1a',
'/([ti]|addend)a$/i' => '$1a',
'/(alumn|formul)a$/i' => '$1ae',
'/(alumn|formul)ae$/i' => '$1ae',
'/(buffal|tomat|her)o$/i' => '$1oes',
'/(bu)s$/i' => '$1ses',
'/(campu)s$/i' => '$1ses',
'/(alias|status)$/i' => '$1es',
'/(octop|vir)us$/i' => '$1i',
'/(octop|vir)i$/i' => '$1i',
'/(gen)us$/i' => '$1era',
'/(gen)era$/i' => '$1era',
'/(ax|test)is$/i' => '$1es',
'/s$/i' => 's',
'/$/' => 's',
],
// Singularization rules. The regex on the left transforms to the regex on the right.
'singularization' => [
'/cookies$/i' => 'cookie',
'/moves$/i' => 'move',
'/sexes$/i' => 'sex',
'/children$/i' => 'child',
'/men$/i' => 'man',
'/feet$/i' => 'foot',
'/people$/i' => 'person',
'/taxa$/i' => 'taxon',
'/databases$/i' => 'database',
'/menus$/i' => 'menu',
'/(quiz)zes$/i' => '\1',
'/(matr|suff)ices$/i' => '\1ix',
'/(vert|ind|cod)ices$/i' => '\1ex',
'/^(ox)en/i' => '\1',
'/(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',
'/is$/i' => 'is',
'/us$/i' => 'us',
'/ias$/i' => 'ias',
'/(shoe)s$/i' => '\1',
'/(o)es$/i' => '\1e',
'/(bus)es$/i' => '\1',
'/(campus)es$/i' => '\1',
'/([m|l])ice$/i' => '\1ouse',
'/(x|ch|ss|sh)es$/i' => '\1',
'/(m)ovies$/i' => '\1ovie',
'/(s)eries$/i' => '\1eries',
'/(v)ies$/i' => '\1ie',
'/([^aeiouy]|qu)ies$/i' => '\1y',
'/([lr])ves$/i' => '\1f',
'/(tive)s$/i' => '\1',
'/(hive)s$/i' => '\1',
'/([^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',
'/(n)ews$/i' => '\1ews',
'/(.*)ss$/i' => '\1ss',
'/(.*)s$/i' => '\1',
],
// Uncountable objects are always singular
'uncountable' => [
'aircraft',
'cannon',
'deer',
'equipment',
'fish',
'information',
'money',
'moose',
'news',
'rice',
'series',
'sheep',
'species',
'swine',
],
];
/**
* Cache of pluralized and singularized nouns.
*
* @var array
*/
protected $cache = [
'singularized' => [],
'pluralized' => [],
];
public function deleteCache()
{
$this->cache['pluralized'] = [];
$this->cache['singularized'] = [];
}
/**
* 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.
*
* @return void
*/
public function addWord($singular, $plural)
{
$this->cache['pluralized'][$singular] = $plural;
$this->cache['singularized'][$plural] = $singular;
}
/**
* Singular English word to plural.
*
* @param string $word word to pluralize.
*
* @return string Plural noun.
*/
public function pluralize($word)
{
// Get the cached noun of it exists
if (isset($this->cache['pluralized'][$word]))
{
return $this->cache['pluralized'][$word];
}
// Check if the noun is already in plural form, i.e. in the singularized cache
if (isset($this->cache['singularized'][$word]))
{
return $word;
}
// Create the plural noun
if (in_array($word, $this->rules['uncountable']))
{
$_cache['pluralized'][$word] = $word;
return $word;
}
foreach ($this->rules['pluralization'] as $regexp => $replacement)
{
$matches = null;
$plural = preg_replace($regexp, $replacement, $word, -1, $matches);
if ($matches > 0)
{
$_cache['pluralized'][$word] = $plural;
return $plural;
}
}
return $word;
}
/**
* Plural English word to singular.
*
* @param string $word Word to singularize.
*
* @return string Singular noun.
*/
public function singularize($word)
{
// Get the cached noun of it exists
if (isset($this->cache['singularized'][$word]))
{
return $this->cache['singularized'][$word];
}
// Check if the noun is already in singular form, i.e. in the pluralized cache
if (isset($this->cache['pluralized'][$word]))
{
return $word;
}
// Create the singular noun
if (in_array($word, $this->rules['uncountable']))
{
$_cache['singularized'][$word] = $word;
return $word;
}
foreach ($this->rules['singularization'] as $regexp => $replacement)
{
$matches = null;
$singular = preg_replace($regexp, $replacement, $word, -1, $matches);
if ($matches > 0)
{
$_cache['singularized'][$word] = $singular;
return $singular;
}
}
return $word;
}
/**
* 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
*/
public function camelize($word)
{
$word = preg_replace('/[^a-zA-Z0-9\s]/', ' ', $word);
$word = str_replace(' ', '', ucwords(strtolower(str_replace('_', ' ', $word))));
return $word;
}
/**
* 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
*/
public function underscore($word)
{
$word = preg_replace('/(\s)+/', '_', $word);
$word = strtolower(preg_replace('/(?<=\\w)([A-Z])/', '_\\1', $word));
return $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 function explode($word)
{
$result = explode('_', self::underscore($word));
return $result;
}
/**
* Convert an array of strings into a "CamelCased" word.
*
* @param array $words Array to implode
*
* @return string UpperCamelCasedWord
*/
public function implode($words)
{
$result = self::camelize(implode('_', $words));
return $result;
}
/**
* 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
* character by default.
*
* @param string $word String to "humanize"
*
* @return string Human-readable word
*/
public function humanize($word)
{
$result = ucwords(strtolower(str_replace("_", " ", $word)));
return $result;
}
/**
* Returns camelBacked version of a string. Same as camelize but first char is lowercased.
*
* @param string $string String to be camelBacked.
*
* @return string
*
* @see camelize
*/
public function variablize($string)
{
$string = self::camelize(self::underscore($string));
$result = strtolower(substr($string, 0, 1));
$variable = preg_replace('/\\w/', $result, $string, 1);
return $variable;
}
/**
* Check to see if an English word is singular
*
* @param string $string The word to check
*
* @return boolean
*/
public function isSingular($string)
{
// Check cache assuming the string is plural.
$singular = $this->cache['singularized'][$string] ?? null;
$plural = $singular && isset($this->cache['pluralized'][$singular]) ? $this->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 English word is plural.
*
* @param string $string String to be checked.
*
* @return boolean
*/
public function isPlural($string)
{
// Uncountable objects are always singular (e.g. information)
if (in_array($string, $this->rules['uncountable']))
{
return false;
}
// Check cache assuming the string is singular.
$plural = $this->cache['pluralized'][$string] ?? null;
$singular = $plural && isset($this->cache['singularized'][$plural]) ? $this->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
* last part)
*
* @param string $string Word
* @param integer $index Index of the part
* @param string $default Default value
*
* @return string
*/
public function getPart($string, $index, $default = null)
{
$parts = self::explode($string);
if ($index < 0)
{
$index = count($parts) + $index;
}
return $parts[$index] ?? $default;
}
}
home/wuectly/www/libraries/fof40/Inflector/Inflector.php 0000604 00000031553 15245627640 0017307 0 ustar 00 <?php
/**
* @package FOF
* @copyright Copyright (c)2010-2022 Nicholas K. Dionysopoulos / Akeeba Ltd
* @license GNU General Public License version 3, or later
*/
namespace FOF40\Inflector;
defined('_JEXEC') || die;
/**
* An Inflector to pluralize and singularize English nouns.
*/
class Inflector
{
/**
* Rules for pluralizing and singularizing of nouns.
*
* @var array
*/
protected $rules = [
// Pluralization rules. The regex on the left transforms to the regex on the right.
'pluralization' => [
'/move$/i' => 'moves',
'/sex$/i' => 'sexes',
'/child$/i' => 'children',
'/children$/i' => 'children',
'/man$/i' => 'men',
'/men$/i' => 'men',
'/foot$/i' => 'feet',
'/feet$/i' => 'feet',
'/person$/i' => 'people',
'/people$/i' => 'people',
'/taxon$/i' => 'taxa',
'/taxa$/i' => 'taxa',
'/(quiz)$/i' => '$1zes',
'/^(ox)$/i' => '$1en',
'/oxen$/i' => 'oxen',
'/(m|l)ouse$/i' => '$1ice',
'/(m|l)ice$/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',
'/sis$/i' => 'ses',
'/([ti]|addend)um$/i' => '$1a',
'/([ti]|addend)a$/i' => '$1a',
'/(alumn|formul)a$/i' => '$1ae',
'/(alumn|formul)ae$/i' => '$1ae',
'/(buffal|tomat|her)o$/i' => '$1oes',
'/(bu)s$/i' => '$1ses',
'/(campu)s$/i' => '$1ses',
'/(alias|status)$/i' => '$1es',
'/(octop|vir)us$/i' => '$1i',
'/(octop|vir)i$/i' => '$1i',
'/(gen)us$/i' => '$1era',
'/(gen)era$/i' => '$1era',
'/(ax|test)is$/i' => '$1es',
'/s$/i' => 's',
'/$/' => 's',
],
// Singularization rules. The regex on the left transforms to the regex on the right.
'singularization' => [
'/cookies$/i' => 'cookie',
'/moves$/i' => 'move',
'/sexes$/i' => 'sex',
'/children$/i' => 'child',
'/men$/i' => 'man',
'/feet$/i' => 'foot',
'/people$/i' => 'person',
'/taxa$/i' => 'taxon',
'/databases$/i' => 'database',
'/menus$/i' => 'menu',
'/(quiz)zes$/i' => '\1',
'/(matr|suff)ices$/i' => '\1ix',
'/(vert|ind|cod)ices$/i' => '\1ex',
'/^(ox)en/i' => '\1',
'/(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',
'/is$/i' => 'is',
'/us$/i' => 'us',
'/ias$/i' => 'ias',
'/(shoe)s$/i' => '\1',
'/(o)es$/i' => '\1e',
'/(bus)es$/i' => '\1',
'/(campus)es$/i' => '\1',
'/([m|l])ice$/i' => '\1ouse',
'/(x|ch|ss|sh)es$/i' => '\1',
'/(m)ovies$/i' => '\1ovie',
'/(s)eries$/i' => '\1eries',
'/(v)ies$/i' => '\1ie',
'/([^aeiouy]|qu)ies$/i' => '\1y',
'/([lr])ves$/i' => '\1f',
'/(tive)s$/i' => '\1',
'/(hive)s$/i' => '\1',
'/([^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',
'/(n)ews$/i' => '\1ews',
'/(.*)ss$/i' => '\1ss',
'/(.*)s$/i' => '\1',
],
// Uncountable objects are always singular
'uncountable' => [
'aircraft',
'cannon',
'deer',
'equipment',
'fish',
'information',
'money',
'moose',
'news',
'rice',
'series',
'sheep',
'species',
'swine',
],
];
/**
* Cache of pluralized and singularized nouns.
*
* @var array
*/
protected $cache = [
'singularized' => [],
'pluralized' => [],
];
/**
* Removes the cache of pluralised and singularised words. Useful when you want to replace word pairs.
*
* @return void
*/
public function deleteCache(): void
{
$this->cache['pluralized'] = [];
$this->cache['singularized'] = [];
}
/**
* 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.
*
* @return void
*/
public function addWord(string $singular, string $plural): void
{
$this->cache['pluralized'][$singular] = $plural;
$this->cache['singularized'][$plural] = $singular;
}
/**
* Singular English word to plural.
*
* @param string $word word to pluralize.
*
* @return string Plural noun.
*/
public function pluralize(string $word): string
{
// Get the cached noun of it exists
if (isset($this->cache['pluralized'][$word]))
{
return $this->cache['pluralized'][$word];
}
// Check if the noun is already in plural form, i.e. in the singularized cache
if (isset($this->cache['singularized'][$word]))
{
return $word;
}
// Create the plural noun
if (in_array($word, $this->rules['uncountable']))
{
$_cache['pluralized'][$word] = $word;
return $word;
}
foreach ($this->rules['pluralization'] as $regexp => $replacement)
{
$matches = null;
$plural = preg_replace($regexp, $replacement, $word, -1, $matches);
if ($matches > 0)
{
$_cache['pluralized'][$word] = $plural;
return $plural;
}
}
return $word;
}
/**
* Plural English word to singular.
*
* @param string $word Word to singularize.
*
* @return string Singular noun.
*/
public function singularize(string $word): string
{
// Get the cached noun of it exists
if (isset($this->cache['singularized'][$word]))
{
return $this->cache['singularized'][$word];
}
// Check if the noun is already in singular form, i.e. in the pluralized cache
if (isset($this->cache['pluralized'][$word]))
{
return $word;
}
// Create the singular noun
if (in_array($word, $this->rules['uncountable']))
{
$_cache['singularized'][$word] = $word;
return $word;
}
foreach ($this->rules['singularization'] as $regexp => $replacement)
{
$matches = null;
$singular = preg_replace($regexp, $replacement, $word, -1, $matches);
if ($matches > 0)
{
$_cache['singularized'][$word] = $singular;
return $singular;
}
}
return $word;
}
/**
* 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
*/
public function camelize(string $word): string
{
$word = preg_replace('/[^a-zA-Z0-9\s]/', ' ', $word);
return str_replace(' ', '', ucwords(strtolower(str_replace('_', ' ', $word))));
}
/**
* 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
*/
public function underscore(string $word): string
{
$word = preg_replace('/(\s)+/', '_', $word);
return strtolower(preg_replace('/(?<=\\w)([A-Z])/', '_\\1', $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 string[] Array of strings
*/
public function explode(string $word): array
{
return explode('_', self::underscore($word));
}
/**
* Convert an array of strings into a "CamelCased" word.
*
* @param string[] $words Array of words to implode
*
* @return string UpperCamelCasedWord
*/
public function implode(array $words): string
{
return 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
* character by default.
*
* @param string $word String to "humanize"
*
* @return string Human-readable word
*/
public function humanize(string $word): string
{
return ucwords(strtolower(str_replace("_", " ", $word)));
}
/**
* Returns camelBacked version of a string. Same as camelize but first char is lowercased.
*
* @param string $string String to be camelBacked.
*
* @return string
*
* @see self::camelize()
*/
public function variablize(string $string): string
{
$string = self::camelize(self::underscore($string));
$result = strtolower(substr($string, 0, 1));
return preg_replace('/\\w/', $result, $string, 1);
}
/**
* Check to see if an English word is singular
*
* @param string $string The word to check
*
* @return boolean
*/
public function isSingular(string $string): bool
{
// Check cache assuming the string is plural.
$singular = $this->cache['singularized'][$string] ?? null;
$plural = $singular && isset($this->cache['pluralized'][$singular]) ? $this->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 English word is plural.
*
* @param string $string String to be checked.
*
* @return boolean
*/
public function isPlural(string $string): bool
{
// Uncountable objects are always singular (e.g. information)
if (in_array($string, $this->rules['uncountable']))
{
return false;
}
// Check cache assuming the string is singular.
$plural = $this->cache['pluralized'][$string] ?? null;
$singular = $plural && isset($this->cache['singularized'][$plural]) ? $this->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
* last part)
*
* @param string $string Word
* @param integer $index Index of the part
* @param string|null $default Default value
*
* @return string|null
*/
public function getPart(string $string, int $index, ?string $default = null): ?string
{
$parts = self::explode($string);
if ($index < 0)
{
$index = count($parts) + $index;
}
return $parts[$index] ?? $default;
}
}
home/wuectly/www/libraries/vendor/joomla/string/src/Inflector.php 0000604 00000026632 15245705167 0021322 0 ustar 00 <?php
/**
* Part of the Joomla Framework String Package
*
* @copyright Copyright (C) 2005 - 2020 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE
*/
namespace Joomla\String;
use InvalidArgumentException;
/**
* Joomla Framework String Inflector Class
*
* The Inflector transforms words
*
* @since 1.0
*/
class Inflector
{
/**
* The singleton instance.
*
* @var Inflector
* @since 1.0
*/
private static $instance;
/**
* The inflector rules for singularisation, pluralisation and countability.
*
* @var array
* @since 1.0
*/
private $rules = array(
'singular' => array(
'/(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',
'/(o)es$/i' => '\1',
'/([^aeiouy]|qu)ies$/i' => '\1y',
'/$1ses$/i' => '\s',
'/ses$/i' => '\s',
'/eaus$/' => 'eau',
'/^(.*us)$/' => '\\1',
'/s$/i' => '',
),
'plural' => array(
'/([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',
'/sis$/i' => 'ses',
'/([ti])um$/i' => '\1a',
'/(buffal|tomat)o$/i' => '\1\2oes',
'/(alumn|bacill|cact|foc|fung|nucle|radi|stimul|syllab|termin|vir)us$/i' => '\1i',
'/us$/i' => 'uses',
'/(ax|cris|test)is$/i' => '\1es',
'/s$/i' => 's',
'/$/' => 's',
),
'countable' => array(
'id',
'hits',
'clicks',
),
);
/**
* Cached inflections.
*
* The array is in the form [singular => plural]
*
* @var string[]
* @since 1.0
*/
private $cache = array();
/**
* Protected constructor.
*
* @since 1.0
*/
protected function __construct()
{
// Pre=populate the irregual singular/plural.
$this
->addWord('deer')
->addWord('moose')
->addWord('sheep')
->addWord('bison')
->addWord('salmon')
->addWord('pike')
->addWord('trout')
->addWord('fish')
->addWord('swine')
->addWord('alias', 'aliases')
->addWord('bus', 'buses')
->addWord('foot', 'feet')
->addWord('goose', 'geese')
->addWord('hive', 'hives')
->addWord('louse', 'lice')
->addWord('man', 'men')
->addWord('mouse', 'mice')
->addWord('ox', 'oxen')
->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
*
* @return void
*
* @since 1.0
* @throws InvalidArgumentException
*/
private function addRule($data, $ruleType)
{
if (\is_string($data))
{
$data = array($data);
}
elseif (!\is_array($data))
{
// Do not translate.
throw new InvalidArgumentException('Invalid inflector rule data.');
}
foreach ($data as $rule)
{
// 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 string|boolean The cached inflection or false if none found.
*
* @since 1.0
*/
private function getCachedPlural($singular)
{
$singular = StringHelper::strtolower($singular);
// Check if the word is in cache.
if (isset($this->cache[$singular]))
{
return $this->cache[$singular];
}
return false;
}
/**
* Gets an inflected word from the cache where the plural form is supplied.
*
* @param string $plural A plural form of a word.
*
* @return string|boolean The cached inflection or false if none found.
*
* @since 1.0
*/
private function getCachedSingular($plural)
{
$plural = StringHelper::strtolower($plural);
return array_search($plural, $this->cache);
}
/**
* 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 string|boolean An inflected string, or false if no rule could be applied.
*
* @since 1.0
*/
private function matchRegexRule($word, $ruleType)
{
// Cycle through the regex rules.
foreach ($this->rules[$ruleType] as $regex => $replacement)
{
$matches = 0;
$matchedWord = preg_replace($regex, $replacement, $word, -1, $matches);
if ($matches > 0)
{
return $matchedWord;
}
}
return false;
}
/**
* 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.
*
* @return void
*
* @since 1.0
*/
private function setCache($singular, $plural = null)
{
$singular = StringHelper::strtolower($singular);
if ($plural === null)
{
$plural = $singular;
}
else
{
$plural = StringHelper::strtolower($plural);
}
$this->cache[$singular] = $plural;
}
/**
* Adds a countable word.
*
* @param mixed $data A string or an array of strings to add.
*
* @return Inflector Returns this object to support chaining.
*
* @since 1.0
*/
public function addCountableRule($data)
{
$this->addRule($data, 'countable');
return $this;
}
/**
* 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 Inflector Returns this object to support chaining.
*
* @since 1.0
*/
public function addWord($singular, $plural =null)
{
$this->setCache($singular, $plural);
return $this;
}
/**
* Adds a pluralisation rule.
*
* @param mixed $data A string or an array of regex rules to add.
*
* @return Inflector Returns this object to support chaining.
*
* @since 1.0
*/
public function addPluraliseRule($data)
{
$this->addRule($data, 'plural');
return $this;
}
/**
* Adds a singularisation rule.
*
* @param mixed $data A string or an array of regex rules to add.
*
* @return Inflector Returns this object to support chaining.
*
* @since 1.0
*/
public function addSingulariseRule($data)
{
$this->addRule($data, 'singular');
return $this;
}
/**
* 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 Inflector
*
* @since 1.0
*/
public static function getInstance($new = false)
{
if ($new)
{
return new static;
}
if (!\is_object(self::$instance))
{
self::$instance = new static;
}
return self::$instance;
}
/**
* Checks if a word is countable.
*
* @param string $word The string input.
*
* @return boolean True if word is countable, false otherwise.
*
* @since 1.0
*/
public function isCountable($word)
{
return \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.
*
* @since 1.0
*/
public function isPlural($word)
{
// Try the cache for a known inflection.
$inflection = $this->getCachedSingular($word);
if ($inflection !== false)
{
return true;
}
$singularWord = $this->toSingular($word);
if ($singularWord === false)
{
return false;
}
// Compute the inflection to cache the values, and compare.
return $this->toPlural($singularWord) == $word;
}
/**
* 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.
*
* @since 1.0
*/
public function isSingular($word)
{
// Try the cache for a known inflection.
$inflection = $this->getCachedPlural($word);
if ($inflection !== false)
{
return true;
}
$pluralWord = $this->toPlural($word);
if ($pluralWord === false)
{
return false;
}
// Compute the inflection to cache the values, and compare.
return $this->toSingular($pluralWord) == $word;
}
/**
* Converts a word into its plural form.
*
* @param string $word The singular word to pluralise.
*
* @return string|boolean An inflected string, or false if no rule could be applied.
*
* @since 1.0
*/
public function toPlural($word)
{
// Try to get the cached plural form from the singular.
$cache = $this->getCachedPlural($word);
if ($cache !== false)
{
return $cache;
}
// Check if the word is a known singular.
if ($this->getCachedSingular($word))
{
return false;
}
// Compute the inflection.
$inflected = $this->matchRegexRule($word, 'plural');
if ($inflected !== false)
{
$this->setCache($word, $inflected);
return $inflected;
}
// Dead code
return false;
}
/**
* Converts a word into its singular form.
*
* @param string $word The plural word to singularise.
*
* @return string|boolean An inflected string, or false if no rule could be applied.
*
* @since 1.0
*/
public function toSingular($word)
{
// Try to get the cached singular form from the plural.
$cache = $this->getCachedSingular($word);
if ($cache !== false)
{
return $cache;
}
// Check if the word is a known plural.
if ($this->getCachedPlural($word))
{
return false;
}
// Compute the inflection.
$inflected = $this->matchRegexRule($word, 'singular');
if ($inflected !== false)
{
$this->setCache($inflected, $word);
return $inflected;
}
return false;
}
}