function _inflect($word, $new_value, $type, $dictionary = null) { static $_cached; static $_loaded; if ($dictionary == null || $dictionary == 'inflector') { $dictionary = 'inflector'; } else { $dictionary = 'inflector/' . $dictionary; } if (!isset($_loaded[$dictionary])) { $_loaded[$dictionary] = true; $_cached[$dictionary] = array('singularize' => array(), 'pluralize' => array()); } $config = AkInflector::_loadConfig($dictionary); if (!in_array($type, array('singularize', 'pluralize'))) { return $word; } if (isset($new_value)) { $_cached[$dictionary][$type][$word] = $new_value; return; } $_original_word = $word; if (!isset($_cached[$dictionary][$type][$_original_word])) { $lowercased_word = strtolower($word); if (in_array($lowercased_word, $config[$type]['uncountable'])) { return $word; } foreach ($config[$type]['irregular'] as $_plural => $_singular) { if ($type == 'singularize') { if (preg_match('/(' . $_singular . ')$/iu', $word, $arr)) { $_cached[$dictionary][$type][$_original_word] = preg_replace('/(' . $_singular . ')$/i', substr($arr[0], 0, 1) . substr($_plural, 1), $word); return $_cached[$dictionary][$type][$_original_word]; } } else { if (preg_match('/(' . $_plural . ')$/iu', $word, $arr)) { $_cached[$dictionary][$type][$_original_word] = preg_replace('/(' . $_plural . ')$/i', substr($arr[0], 0, 1) . substr($_singular, 1), $word); return $_cached[$dictionary][$type][$_original_word]; } } } $replacements = isset($config[$type]['replacements']) ? $config[$type]['replacements'] : false; if ($replacements !== false) { $replacements_keys = array_keys($replacements); foreach ($replacements_keys as $idx => $key) { $replacements_keys[$idx] = '/' . $key . '/u'; } $replacements_values = array_values($replacements); } foreach ($config[$type]['rules'] as $rule => $replacement) { if (preg_match($rule . 'u', $word, $match)) { if (strstr($replacement, '@') && $replacements) { foreach ($match as $k => $v) { $replacement = preg_replace("/(@{$k})/u", preg_replace($replacements_keys, $replacements_values, $v), $replacement); } } $_cached[$dictionary][$type][$_original_word] = preg_replace($rule . 'u', $replacement, $word); return $_cached[$dictionary][$type][$_original_word]; } } $_cached[$dictionary][$type][$_original_word] = $word; return $_cached[$dictionary][$type][$_original_word]; } return $_cached[$dictionary][$type][$_original_word]; }