lithium\util\Inflector::pluralize PHP Method

pluralize() public static method

Changes the form of a word from singular to plural.
public static pluralize ( string $word ) : string
$word string Word in singular form.
return string Word in plural form.
    public static function pluralize($word)
    {
        if (isset(static::$_pluralized[$word])) {
            return static::$_pluralized[$word];
        }
        extract(static::$_plural);
        if (!isset($regexUninflected) || !isset($regexIrregular)) {
            $regexUninflected = static::_enclose(join('|', $uninflected + static::$_uninflected));
            $regexIrregular = static::_enclose(join('|', array_keys($irregular)));
            static::$_plural += compact('regexUninflected', 'regexIrregular');
        }
        if (preg_match('/(' . $regexUninflected . ')$/i', $word, $regs)) {
            return static::$_pluralized[$word] = $word;
        }
        if (preg_match('/(.*)\\b(' . $regexIrregular . ')$/i', $word, $regs)) {
            $plural = substr($word, 0, 1) . substr($irregular[strtolower($regs[2])], 1);
            return static::$_pluralized[$word] = $regs[1] . $plural;
        }
        foreach ($rules as $rule => $replacement) {
            if (preg_match($rule, $word)) {
                return static::$_pluralized[$word] = preg_replace($rule, $replacement, $word);
            }
        }
        return static::$_pluralized[$word] = $word;
    }

Usage Example

Beispiel #1
0
 protected function _autoSelects($name, array $options = array())
 {
     $model = $this->_binding->model();
     $method = Inflector::pluralize($name);
     $rules = $this->instance->validates;
     if (method_exists($model, $method)) {
         $list = $model::$method();
         if (!empty($list)) {
             $options['list'] = $list;
             return $options;
         }
     }
     if (isset($rules[$name])) {
         if (is_array($rules[$name][0])) {
             $rule_list = $rules[$name];
         } else {
             $rule_list = array($rules[$name]);
         }
         foreach ($rule_list as $rule) {
             if ($rule[0] === 'inList' and isset($rule['list'])) {
                 foreach ($rule['list'] as $optval) {
                     $options['list'][$optval] = Inflector::humanize($optval);
                 }
             }
         }
     }
     return $options;
 }
All Usage Examples Of lithium\util\Inflector::pluralize