10 defined(
'JPATH_PLATFORM') or die;
37 private $_rules = array(
39 '/(matr)ices$/i' =>
'\1ix',
40 '/(vert|ind)ices$/i' =>
'\1ex',
41 '/(alumn|bacill|cact|foc|fung|nucle|radi|stimul|syllab|termin|viri?)i$/i' =>
'\1us',
42 '/([ftw]ax)es/i' =>
'\1',
43 '/(cris|ax|test)es$/i' =>
'\1is',
44 '/(shoe|slave)s$/i' =>
'\1',
46 '/([^aeiouy]|qu)ies$/i' =>
'\1y',
50 '/^(.*us)$/' =>
'\\1',
54 '/([m|l])ouse$/i' =>
'\1ice',
55 '/(matr|vert|ind)(ix|ex)$/i' =>
'\1ices',
56 '/(x|ch|ss|sh)$/i' =>
'\1es',
57 '/([^aeiouy]|qu)y$/i' =>
'\1ies',
58 '/([^aeiouy]|qu)ies$/i' =>
'\1y',
59 '/(?:([^f])fe|([lr])f)$/i' =>
'\1\2ves',
61 '/([ti])um$/i' =>
'\1a',
62 '/(buffal|tomat)o$/i' =>
'\1\2oes',
63 '/(alumn|bacill|cact|foc|fung|nucle|radi|stimul|syllab|termin|vir)us$/i' =>
'\1i',
65 '/(ax|cris|test)is$/i' =>
'\1es',
84 private $_cache = array();
91 protected function __construct()
105 ->addWord(
'alias',
'aliases')
106 ->addWord(
'bus',
'buses')
107 ->addWord(
'foot',
'feet')
108 ->addWord(
'goose',
'geese')
109 ->addWord(
'hive',
'hives')
110 ->addWord(
'louse',
'lice')
111 ->addWord(
'man',
'men')
112 ->addWord(
'mouse',
'mice')
113 ->addWord(
'ox',
'oxen')
114 ->addWord(
'quiz',
'quizes')
115 ->addWord(
'status',
'statuses')
116 ->addWord(
'tooth',
'teeth')
117 ->addWord(
'woman',
'women');
131 private function _addRule($data, $ruleType)
133 if (is_string($data))
135 $data = array($data);
137 elseif (!is_array($data))
140 throw new InvalidArgumentException(
'Invalid inflector rule data.');
143 foreach ($data as $rule)
146 array_push($this->_rules[$ruleType], (
string) $rule);
159 private function _getCachedPlural($singular)
164 if (isset($this->_cache[$singular]))
166 return $this->_cache[$singular];
181 private function _getCachedSingular($plural)
185 return array_search($plural, $this->_cache);
201 private function _matchRegexRule($word, $ruleType)
204 foreach ($this->_rules[$ruleType] as $regex => $replacement)
207 $matchedWord = preg_replace($regex, $replacement, $word, -1, $matches);
228 private function _setCache($singular, $plural = null)
232 if ($plural === null)
241 $this->_cache[$singular] = $plural;
253 public function addCountableRule($data)
255 $this->_addRule($data,
'countable');
270 public function addWord($singular, $plural =null)
272 $this->_setCache($singular, $plural);
286 public function addPluraliseRule($data)
288 $this->_addRule($data,
'plural');
302 public function addSingulariseRule($data)
304 $this->_addRule($data,
'singular');
319 public static function getInstance($new =
false)
325 elseif (!is_object(self::$_instance))
327 self::$_instance =
new static;
330 return self::$_instance;
342 public function isCountable($word)
344 return (
boolean) in_array($word, $this->_rules[
'countable']);
356 public function isPlural($word)
359 $inflection = $this->_getCachedSingular($word);
361 if ($inflection !==
false)
367 return $this->toPlural($this->toSingular($word)) == $word;
379 public function isSingular($word)
382 $inflection = $this->_getCachedPlural($word);
384 if ($inflection !==
false)
390 return $this->toSingular($this->toPlural($word)) == $word;
402 public function toPlural($word)
405 $cache = $this->_getCachedPlural($word);
407 if ($cache !==
false)
413 if ($this->_getCachedSingular($word))
419 $inflected = $this->_matchRegexRule($word,
'plural');
421 if ($inflected !==
false)
423 $this->_setCache($word, $inflected);
440 public function toSingular($word)
443 $cache = $this->_getCachedSingular($word);
445 if ($cache !==
false)
451 if ($this->_getCachedPlural($word))
457 $inflected = $this->_matchRegexRule($word,
'singular');
459 if ($inflected !==
false)
461 $this->_setCache($inflected, $word);