2010年2月24日水曜日

cakephp inflector.php 修正


function word_pluralize($word) {
$_this =& Inflector::getInstance();
if (!isset($_this->pluralRules) || empty($_this->pluralRules)) {
$_this->__initPluralRules();
}
if (isset($_this->pluralized[$word])) {
return $_this->pluralized[$word];
}
extract($_this->pluralRules);
if (!isset($regexUninflected) || !isset($regexIrregular)) {
$regexUninflected = __enclose(implode( '|', $uninflected));
$regexIrregular = __enclose(implode( '|', array_keys($irregular)));
$_this->pluralRules['regexUninflected'] = $regexUninflected;
$_this->pluralRules['regexIrregular'] = $regexIrregular;
}
if (preg_match('/^(' . $regexUninflected . ')$/i', $word, $regs)) {
$_this->pluralized[$word] = $word;
return $word;
}
if (preg_match('/(.*)\\b(' . $regexIrregular . ')$/i', $word, $regs)) {
$_this->pluralized[$word] = $regs[1] . substr($word, 0, 1) . substr($irregular[strtolower($regs[2])], 1);
return $_this->pluralized[$word];
}
foreach ($pluralRules as $rule => $replacement) {
if (preg_match($rule, $word)) {
$_this->pluralized[$word] = preg_replace($rule, $replacement, $word);
return $_this->pluralized[$word];
}
}
}

function pluralize($words) {
$_this =& Inflector::getInstance();
$camelize = false;
if( preg_match('/[A-Z]/', $words, $regs) ) {
$words = $_this->underscore($words);
$camelize = true;
}
$word_array = explode('_',$words);
$cnt = count($word_array);
if( $cnt ) {
$word_array[$cnt-1] = $_this->word_pluralize($word_array[$cnt-1]);
}
$result = implode('_',$word_array);
unset($word_array);
if( $camelize ) {
$result = $_this->camelize($result);
}
return $result;
}


参考:CPA-LABテクニカル

ついでに、information を $coreUninflectedSingular と $coreIrregularSingular に追加した。ちゃんと入ってました。
修正報告入れた方が良いのだろうか?微妙な感じもする。

追記:あかん、他が動かなくなる…。もうちょいテストしないと…
追記:バグ修正。+いろんな場所から、多様な使われ方をしているようで、それに対応できるよう修正を入れ直した。

0 件のコメント: