You are here

public static function Inflector::singularize in Zircon Profile 8.0

Same name and namespace in other branches
  1. 8 vendor/doctrine/inflector/lib/Doctrine/Common/Inflector/Inflector.php \Doctrine\Common\Inflector\Inflector::singularize()

Returns a word in singular form.

Parameters

string $word The word in plural form.:

Return value

string The word in singular form.

4 calls to Inflector::singularize()
InflectorTest::testCustomRuleWithReset in vendor/doctrine/inflector/tests/Doctrine/Tests/Common/Inflector/InflectorTest.php
Test resetting inflection rules.
InflectorTest::testCustomSingularRule in vendor/doctrine/inflector/tests/Doctrine/Tests/Common/Inflector/InflectorTest.php
testCustomSingularRule method
InflectorTest::testInflectingSingulars in vendor/doctrine/inflector/tests/Doctrine/Tests/Common/Inflector/InflectorTest.php
testInflectingSingulars method
InflectorTest::testRulesClearsCaches in vendor/doctrine/inflector/tests/Doctrine/Tests/Common/Inflector/InflectorTest.php
test that setting new rules clears the inflector caches.

File

vendor/doctrine/inflector/lib/Doctrine/Common/Inflector/Inflector.php, line 366

Class

Inflector
Doctrine inflector has static methods for inflecting text.

Namespace

Doctrine\Common\Inflector

Code

public static function singularize($word) {
  if (isset(self::$cache['singularize'][$word])) {
    return self::$cache['singularize'][$word];
  }
  if (!isset(self::$singular['merged']['uninflected'])) {
    self::$singular['merged']['uninflected'] = array_merge(self::$singular['uninflected'], self::$uninflected);
  }
  if (!isset(self::$singular['merged']['irregular'])) {
    self::$singular['merged']['irregular'] = array_merge(self::$singular['irregular'], array_flip(self::$plural['irregular']));
  }
  if (!isset(self::$singular['cacheUninflected']) || !isset(self::$singular['cacheIrregular'])) {
    self::$singular['cacheUninflected'] = '(?:' . join('|', self::$singular['merged']['uninflected']) . ')';
    self::$singular['cacheIrregular'] = '(?:' . join('|', array_keys(self::$singular['merged']['irregular'])) . ')';
  }
  if (preg_match('/(.*)\\b(' . self::$singular['cacheIrregular'] . ')$/i', $word, $regs)) {
    self::$cache['singularize'][$word] = $regs[1] . substr($word, 0, 1) . substr(self::$singular['merged']['irregular'][strtolower($regs[2])], 1);
    return self::$cache['singularize'][$word];
  }
  if (preg_match('/^(' . self::$singular['cacheUninflected'] . ')$/i', $word, $regs)) {
    self::$cache['singularize'][$word] = $word;
    return $word;
  }
  foreach (self::$singular['rules'] as $rule => $replacement) {
    if (preg_match($rule, $word)) {
      self::$cache['singularize'][$word] = preg_replace($rule, $replacement, $word);
      return self::$cache['singularize'][$word];
    }
  }
  self::$cache['singularize'][$word] = $word;
  return $word;
}