/** * Gets or adds inflection and transliteration rules. * * @param string $type Either `'transliteration'`, `'uninflected'`, `'singular'` or `'plural'`. * @param array $config * @return array|void If `$config` is empty, returns the rules list specified * by `$type`, otherwise returns `null`. */ public static function rules($type, $config = array()) { $var = '_' . $type; if (!isset(static::${$var})) { return null; } if (empty($config)) { return static::${$var}; } switch ($type) { case 'transliteration': $_config = array(); foreach ($config as $key => $val) { if ($key[0] != '/') { $key = '/' . join('|', array_filter(preg_split('//u', $key))) . '/'; } $_config[$key] = $val; } static::$_transliteration = array_merge($_config, static::$_transliteration, $_config); break; case 'uninflected': static::$_uninflected = array_merge(static::$_uninflected, (array) $config); static::$_plural['regexUninflected'] = null; static::$_singular['regexUninflected'] = null; foreach ((array) $config as $word) { unset(static::$_singularized[$word], static::$_pluralized[$word]); } break; case 'singular': case 'plural': if (isset(static::${$var}[key($config)])) { foreach ($config as $rType => $set) { static::${$var}[$rType] = array_merge($set, static::${$var}[$rType], $set); if ($rType == 'irregular') { $swap = $type == 'singular' ? '_plural' : '_singular'; static::${$swap}[$rType] = array_flip(static::${$var}[$rType]); } } } else { static::${$var}['rules'] = array_merge($config, static::${$var}['rules'], $config); } break; } }
/** * Clears local in-memory caches. Can be used to force a full-cache clear when updating * inflection rules mid-way through request execution. */ public static function reset() { static::$_singularized = static::$_pluralized = array(); static::$_camelized = static::$_underscored = array(); static::$_humanized = array(); static::$_plural['regexUninflected'] = static::$_singular['regexUninflected'] = null; static::$_plural['regexIrregular'] = static::$_singular['regexIrregular'] = null; static::$_transliteration = array('/à|á|å|â/' => 'a', '/è|é|ê|ẽ|ë/' => 'e', '/ì|í|î/' => 'i', '/ò|ó|ô|ø/' => 'o', '/ù|ú|ů|û/' => 'u', '/ç|ć|č/' => 'c', '/đ/' => 'dj', '/š/' => 's', '/ž/' => 'z', '/ñ/' => 'n', '/ä|æ/' => 'ae', '/ö/' => 'oe', '/ü/' => 'ue', '/Ä/' => 'Ae', '/Ü/' => 'Ue', '/Ö/' => 'Oe', '/ß/' => 'ss', '/Č|Ć/' => 'C', '/DŽ/' => 'Dz', '/Đ/' => 'Dj', '/Š/' => 'S', '/Ž/' => 'Z'); }
/** * Adds custom inflection $rules, of either 'plural', 'singular' or 'transliteration' $type. * * ### Usage: * * ``` * Inflector::rules('plural', array('/^(inflect)or$/i' => '\1ables')); * Inflector::rules('plural', array( * 'rules' => array('/^(inflect)ors$/i' => '\1ables'), * 'uninflected' => array('dontinflectme'), * 'irregular' => array('red' => 'redlings') * )); * Inflector::rules('transliteration', array('/å/' => 'aa')); * ``` * * @param string $type The type of inflection, either 'plural', 'singular' or 'transliteration' * @param array $rules Array of rules to be added. * @param bool $reset If true, will unset default inflections for all * new rules that are being defined in $rules. * @return void */ public static function rules($type, $rules, $reset = false) { $var = '_' . $type; switch ($type) { case 'transliteration': if ($reset) { static::$_transliteration = $rules; } else { static::$_transliteration = $rules + static::$_transliteration; } break; default: foreach ($rules as $rule => $pattern) { if (is_array($pattern)) { if ($reset) { static::${$var}[$rule] = $pattern; } else { if ($rule === 'uninflected') { static::${$var}[$rule] = array_merge($pattern, static::${$var}[$rule]); } else { static::${$var}[$rule] = $pattern + static::${$var}[$rule]; } } unset($rules[$rule], static::${$var}['cache' . ucfirst($rule)]); if (isset(static::${$var}['merged'][$rule])) { unset(static::${$var}['merged'][$rule]); } if ($type === 'plural') { static::$_cache['pluralize'] = static::$_cache['tableize'] = array(); } elseif ($type === 'singular') { static::$_cache['singularize'] = array(); } } } static::${$var}['rules'] = $rules + static::${$var}['rules']; } }
/** * Clears local in-memory caches. Can be used to force a full-cache clear when updating * inflection rules mid-way through request execution. * * @return void */ public static function reset() { static::$_singularized = static::$_pluralized = array(); static::$_camelized = static::$_underscored = array(); static::$_plural['regexUninflected'] = static::$_singular['regexUninflected'] = null; static::$_plural['regexIrregular'] = static::$_singular['regexIrregular'] = null; static::$_transliteration = array('/à|á|å|â/' => 'a', '/è|é|ê|ẽ|ë/' => 'e', '/ì|í|î/' => 'i', '/ò|ó|ô|ø/' => 'o', '/ù|ú|ů|û/' => 'u', '/ç/' => 'c', '/ñ/' => 'n', '/ä|æ/' => 'ae', '/ö/' => 'oe', '/ü/' => 'ue', '/Ä/' => 'Ae', '/Ü/' => 'Ue', '/Ö/' => 'Oe', '/ß/' => 'ss'); }