クラス yii\helpers\BaseInflector
継承 | yii\helpers\BaseInflector |
---|---|
サブクラス | yii\helpers\Inflector |
利用可能バージョン | 2.0 |
ソースコード | https://github.com/yiisoft/yii2/blob/master/framework/helpers/BaseInflector.php |
BaseInflector は yii\helpers\Inflector の具体的な実装を提供します。
BaseInflector は使用しないでください。代わりに yii\helpers\Inflector を使用してください。
公開プロパティ
プロパティ | 型 | 説明 | 定義元 |
---|---|---|---|
$plurals | array | 単語を複数形に変換するためのルールです。 | yii\helpers\BaseInflector |
$singulars | array | 単語を単数形に変換するためのルールです。 | yii\helpers\BaseInflector |
$specials | array | 単語を複数形と単数形の間で変換するための特別なルールです。 | yii\helpers\BaseInflector |
$transliteration | array | intl が利用できない場合、transliterate() によって使用される、フォールバックの転写マップです。 | yii\helpers\BaseInflector |
$transliterator | mixed | Transliterator、または Transliterator を構築できる文字列です。 | yii\helpers\BaseInflector |
公開メソッド
定数
定数 | 値 | 説明 | 定義元 |
---|---|---|---|
TRANSLITERATE_LOOSE | 'Any-Latin; Latin-ASCII; [\u0080-\uffff] remove' | Any-Latin; Latin-ASCII; [\u0080-\uffff] remove 転写ルールのショートカットです。このルールは緩やかで、文字は基本ラテンUnicodeブロックの文字で転写されます。例:「获取到 どちら Українська: ґ,є, Српска: ђ, њ, џ! ¿Español?」は「huo qu dao dochira Ukrainska: g,e, Srpska: d, n, d! Espanol?」に変換されます。transliterate()で使用されます。詳細については、unicode normalization formsを参照してください。 |
yii\helpers\BaseInflector |
TRANSLITERATE_MEDIUM | 'Any-Latin; Latin-ASCII' | Any-Latin; Latin-ASCII 転写ルールのショートカットです。このルールは中程度で、文字はLatin-1 (ISO 8859-1) ASCIIテーブルの文字に変換されます。例:「获取到 どちら Українська: ґ,є, Српска: ђ, њ, џ! ¿Español?」は「huo qu dao dochira Ukrainsʹka: g,e, Srpska: d, n, d! ¿Espanol?」に変換されます。transliterate()で使用されます。詳細については、unicode normalization formsを参照してください。 |
yii\helpers\BaseInflector |
TRANSLITERATE_STRICT | 'Any-Latin; NFKD' | Any-Latin; NFKD 転写ルールのショートカットです。このルールは厳密で、文字は最も近い音声表現文字で転写されます。結果は任意のUTF-8文字を含む可能性があります。例:「获取到 どちら Українська: ґ,є, Српска: ђ, њ, џ! ¿Español?」は「huò qǔ dào dochira Ukraí̈nsʹka: g̀,ê, Srpska: đ, n̂, d̂! ¿Español?」に変換されます。transliterate()で使用されます。詳細については、unicode normalization formsを参照してください。 |
yii\helpers\BaseInflector |
プロパティの詳細
単語を複数形に変換するためのルールです。キーは正規表現で、値は対応する置換です。
'/([nrlm]ese|deer|fish|sheep|measles|ois|pox|media)$/i' => '\1',
'/^(sea[- ]bass)$/i' => '\1',
'/(m)ove$/i' => '\1oves',
'/(f)oot$/i' => '\1eet',
'/(h)uman$/i' => '\1umans',
'/(s)tatus$/i' => '\1tatuses',
'/(s)taff$/i' => '\1taff',
'/(t)ooth$/i' => '\1eeth',
'/(quiz)$/i' => '\1zes',
'/^(ox)$/i' => '\1\2en',
'/([m|l])ouse$/i' => '\1ice',
'/(matr|vert|ind)(ix|ex)$/i' => '\1ices',
'/(x|ch|ss|sh)$/i' => '\1es',
'/([^aeiouy]|qu)y$/i' => '\1ies',
'/(hive)$/i' => '\1s',
'/(?:([^f])fe|([lr])f)$/i' => '\1\2ves',
'/sis$/i' => 'ses',
'/([ti])um$/i' => '\1a',
'/(p)erson$/i' => '\1eople',
'/(m)an$/i' => '\1en',
'/(c)hild$/i' => '\1hildren',
'/(buffal|tomat|potat|ech|her|vet)o$/i' => '\1oes',
'/(alumn|bacill|cact|foc|fung|nucle|radi|stimul|syllab|termin|vir)us$/i' => '\1i',
'/us$/i' => 'uses',
'/(alias)$/i' => '\1es',
'/(ax|cris|test)is$/i' => '\1es',
'/(currenc)y$/' => '\1ies',
'/s$/' => 's',
'/^$/' => '',
'/$/' => 's',
]
単語を単数形に変換するためのルールです。キーは正規表現、値は対応する置換文字列です。
'/([nrlm]ese|deer|fish|sheep|measles|ois|pox|media|ss)$/i' => '\1',
'/^(sea[- ]bass)$/i' => '\1',
'/(s)tatuses$/i' => '\1tatus',
'/(f)eet$/i' => '\1oot',
'/(t)eeth$/i' => '\1ooth',
'/^(.*)(menu)s$/i' => '\1\2',
'/(quiz)zes$/i' => '\1',
'/(matr)ices$/i' => '\1ix',
'/(vert|ind)ices$/i' => '\1ex',
'/^(ox)en/i' => '\1',
'/(alias)(es)*$/i' => '\1',
'/(alumn|bacill|cact|foc|fung|nucle|radi|stimul|syllab|termin|viri?)i$/i' => '\1us',
'/([ftw]ax)es/i' => '\1',
'/(cris|ax|test)es$/i' => '\1is',
'/(shoe|slave)s$/i' => '\1',
'/(o)es$/i' => '\1',
'/ouses$/' => 'ouse',
'/([^a])uses$/' => '\1us',
'/([m|l])ice$/i' => '\1ouse',
'/(x|ch|ss|sh)es$/i' => '\1',
'/(m)ovies$/i' => '\1\2ovie',
'/(s)eries$/i' => '\1\2eries',
'/([^aeiouy]|qu)ies$/i' => '\1y',
'/([lr])ves$/i' => '\1f',
'/(tive)s$/i' => '\1',
'/(hive)s$/i' => '\1',
'/(drive)s$/i' => '\1',
'/([^fo])ves$/i' => '\1fe',
'/(^analy)ses$/i' => '\1sis',
'/(analy|diagno|^ba|(p)arenthe|(p)rogno|(s)ynop|(t)he)ses$/i' => '\1\2sis',
'/([ti])a$/i' => '\1um',
'/(p)eople$/i' => '\1\2erson',
'/(m)en$/i' => '\1an',
'/(c)hildren$/i' => '\1\2hild',
'/(n)ews$/i' => '\1\2ews',
'/(n)etherlands$/i' => '\1\2etherlands',
'/eaus$/' => 'eau',
'/(currenc)ies$/' => '\1y',
'/^(.*us)$/' => '\1',
'/s$/i' => '',
]
単語を複数形と単数形の間で変換するための特別なルールです。キーは単数形の特別な単語で、値は対応する複数形です。
'atlas' => 'atlases',
'beef' => 'beefs',
'brother' => 'brothers',
'cafe' => 'cafes',
'child' => 'children',
'cookie' => 'cookies',
'corpus' => 'corpuses',
'cow' => 'cows',
'curve' => 'curves',
'foe' => 'foes',
'ganglion' => 'ganglions',
'genie' => 'genies',
'genus' => 'genera',
'graffito' => 'graffiti',
'hoof' => 'hoofs',
'loaf' => 'loaves',
'man' => 'men',
'money' => 'monies',
'mongoose' => 'mongooses',
'move' => 'moves',
'mythos' => 'mythoi',
'niche' => 'niches',
'numen' => 'numina',
'occiput' => 'occiputs',
'octopus' => 'octopuses',
'opus' => 'opuses',
'ox' => 'oxen',
'pasta' => 'pasta',
'penis' => 'penises',
'sex' => 'sexes',
'soliloquy' => 'soliloquies',
'testis' => 'testes',
'trilby' => 'trilbys',
'turf' => 'turfs',
'wave' => 'waves',
'Amoyese' => 'Amoyese',
'bison' => 'bison',
'Borghese' => 'Borghese',
'bream' => 'bream',
'breeches' => 'breeches',
'britches' => 'britches',
'buffalo' => 'buffalo',
'cantus' => 'cantus',
'carp' => 'carp',
'chassis' => 'chassis',
'clippers' => 'clippers',
'cod' => 'cod',
'coitus' => 'coitus',
'Congoese' => 'Congoese',
'contretemps' => 'contretemps',
'corps' => 'corps',
'debris' => 'debris',
'diabetes' => 'diabetes',
'djinn' => 'djinn',
'eland' => 'eland',
'elk' => 'elk',
'equipment' => 'equipment',
'Faroese' => 'Faroese',
'flounder' => 'flounder',
'Foochowese' => 'Foochowese',
'gallows' => 'gallows',
'Genevese' => 'Genevese',
'Genoese' => 'Genoese',
'Gilbertese' => 'Gilbertese',
'graffiti' => 'graffiti',
'headquarters' => 'headquarters',
'herpes' => 'herpes',
'hijinks' => 'hijinks',
'Hottentotese' => 'Hottentotese',
'information' => 'information',
'innings' => 'innings',
'jackanapes' => 'jackanapes',
'Kiplingese' => 'Kiplingese',
'Kongoese' => 'Kongoese',
'Lucchese' => 'Lucchese',
'mackerel' => 'mackerel',
'Maltese' => 'Maltese',
'mews' => 'mews',
'moose' => 'moose',
'mumps' => 'mumps',
'Nankingese' => 'Nankingese',
'news' => 'news',
'nexus' => 'nexus',
'Niasese' => 'Niasese',
'Pekingese' => 'Pekingese',
'Piedmontese' => 'Piedmontese',
'pincers' => 'pincers',
'Pistoiese' => 'Pistoiese',
'pliers' => 'pliers',
'Portuguese' => 'Portuguese',
'proceedings' => 'proceedings',
'rabies' => 'rabies',
'rice' => 'rice',
'rhinoceros' => 'rhinoceros',
'salmon' => 'salmon',
'Sarawakese' => 'Sarawakese',
'scissors' => 'scissors',
'series' => 'series',
'Shavese' => 'Shavese',
'shears' => 'shears',
'siemens' => 'siemens',
'species' => 'species',
'swine' => 'swine',
'testes' => 'testes',
'trousers' => 'trousers',
'trout' => 'trout',
'tuna' => 'tuna',
'Vermontese' => 'Vermontese',
'Wenchowese' => 'Wenchowese',
'whiting' => 'whiting',
'wildebeest' => 'wildebeest',
'Yengeese' => 'Yengeese',
'software' => 'software',
'hardware' => 'hardware',
]
intl が利用できない場合、transliterate() によって使用される、フォールバックの転写マップです。
'À' => 'A',
'Á' => 'A',
'Â' => 'A',
'Ã' => 'A',
'Ä' => 'A',
'Å' => 'A',
'Æ' => 'AE',
'Ç' => 'C',
'È' => 'E',
'É' => 'E',
'Ê' => 'E',
'Ë' => 'E',
'Ì' => 'I',
'Í' => 'I',
'Î' => 'I',
'Ï' => 'I',
'Ð' => 'D',
'Ñ' => 'N',
'Ò' => 'O',
'Ó' => 'O',
'Ô' => 'O',
'Õ' => 'O',
'Ö' => 'O',
'Ő' => 'O',
'Ø' => 'O',
'Ù' => 'U',
'Ú' => 'U',
'Û' => 'U',
'Ü' => 'U',
'Ű' => 'U',
'Ý' => 'Y',
'Þ' => 'TH',
'ß' => 'ss',
'à' => 'a',
'á' => 'a',
'â' => 'a',
'ã' => 'a',
'ä' => 'a',
'å' => 'a',
'æ' => 'ae',
'ç' => 'c',
'è' => 'e',
'é' => 'e',
'ê' => 'e',
'ë' => 'e',
'ì' => 'i',
'í' => 'i',
'î' => 'i',
'ï' => 'i',
'ð' => 'd',
'ñ' => 'n',
'ò' => 'o',
'ó' => 'o',
'ô' => 'o',
'õ' => 'o',
'ö' => 'o',
'ő' => 'o',
'ø' => 'o',
'ù' => 'u',
'ú' => 'u',
'û' => 'u',
'ü' => 'u',
'ű' => 'u',
'ý' => 'y',
'þ' => 'th',
'ÿ' => 'y',
]
Transliterator、またはTransliteratorを構築できる文字列のいずれかです。intlが利用可能な場合、transliterate()で使用されます。TRANSLITERATE_LOOSEがデフォルトです。
https://www.php.net/manual/en/transliterator.transliterate.phpも参照してください。
メソッドの詳細
CamelCase 名を小文字の ID に変換します。
ID内の単語は、指定された文字(デフォルトは '-')を使用して連結できます。たとえば、'PostTag'は'post-tag'に変換されます。
public static string camel2id ( $name, $separator = '-', $strict = false ) | ||
$name | 文字列 |
変換する文字列 |
$separator | 文字列 |
ID内の単語を連結するために使用される文字 |
$strict | boolean|string |
連続する2つの大文字文字間にセパレータを挿入するかどうか、デフォルトはfalse |
戻り値 | 文字列 |
結果のID |
---|
public static function camel2id($name, $separator = '-', $strict = false)
{
if (empty($name)) {
return (string) $name;
}
$regex = $strict ? '/\p{Lu}/u' : '/(?<!\p{Lu})\p{Lu}/u';
if ($separator === '_') {
return mb_strtolower(trim(preg_replace($regex, '_\0', $name), '_'), self::encoding());
}
return mb_strtolower(trim(str_replace('_', $separator, preg_replace($regex, $separator . '\0', $name)), $separator), self::encoding());
}
CamelCase 名をスペースで区切られた単語に変換します。
例えば、'PostTag' は 'Post Tag' に変換されます。
public static string camel2words ( $name, $ucwords = true ) | ||
$name | 文字列 |
変換する文字列 |
$ucwords | 論理値 |
各単語の先頭文字を大文字にするかどうか |
戻り値 | 文字列 |
結果の単語 |
---|
public static function camel2words($name, $ucwords = true)
{
if (empty($name)) {
return (string) $name;
}
// Add a space before any uppercase letter preceded by a lowercase letter (xY => x Y)
// and any uppercase letter preceded by an uppercase letter and followed by a lowercase letter (XYz => X Yz)
$label = preg_replace('/(?<=\p{Ll})\p{Lu}|(?<=\p{L})\p{Lu}(?=\p{Ll})/u', ' \0', $name);
$label = mb_strtolower(trim(str_replace(['-', '_', '.'], ' ', $label)), self::encoding());
return $ucwords ? StringHelper::mb_ucwords($label, self::encoding()) : $label;
}
指定された単語を CamelCase にします。
"send_email" のような単語を "SendEmail" に変換します。英数字以外の文字は削除されるため、"who's online" は "WhoSOnline" に変換されます。
variablize() も参照してください。
public static string camelize ( $word ) | ||
$word | 文字列 |
CamelCaseに変換する単語 |
public static function camelize($word)
{
if (empty($word)) {
return (string) $word;
}
return str_replace(' ', '', StringHelper::mb_ucwords(preg_replace('/[^\pL\pN]+/u', ' ', $word), self::encoding()));
}
テーブル名をクラス名に変換します。
例えば、"people" を "Person" に変換します。
public static string classify ( $tableName ) | ||
$tableName | 文字列 |
public static function classify($tableName)
{
if (empty($tableName)) {
return (string) $tableName;
}
return static::camelize(static::singularize($tableName));
}
protected static boolean hasIntl ( ) | ||
戻り値 | 論理値 |
intl拡張機能がロードされている場合 |
---|
protected static function hasIntl()
{
return extension_loaded('intl');
}
$word から人間が読める文字列を返します。
public static string humanize ( $word, $ucAll = false ) | ||
$word | 文字列 |
人間化すべき文字列 |
$ucAll | 論理値 |
すべての単語を大文字にするかどうか |
public static function humanize($word, $ucAll = false)
{
if (empty($word)) {
return (string) $word;
}
$word = str_replace('_', ' ', preg_replace('/_id$/', '', $word));
$encoding = self::encoding();
return $ucAll ? StringHelper::mb_ucwords($word, $encoding) : StringHelper::mb_ucfirst($word, $encoding);
}
ID を CamelCase 名に変換します。
$separator
(デフォルトは'-')で区切られたID内の単語は、CamelCaseの名前に連結されます。例えば、'post-tag' は 'PostTag' に変換されます。
public static string id2camel ( $id, $separator = '-' ) | ||
$id | 文字列 |
変換するID |
$separator | 文字列 |
ID内の単語を区切るために使用される文字 |
戻り値 | 文字列 |
結果のCamelCase名 |
---|
public static function id2camel($id, $separator = '-')
{
if (empty($id)) {
return (string) $id;
}
return str_replace(' ', '', StringHelper::mb_ucwords(str_replace($separator, ' ', $id), self::encoding()));
}
数字を序数(英語)に変換します。例:13 を 13th に、2 を 2nd に変換します。
..
public static string ordinalize ( $number ) | ||
$number | 整数 |
序数値を取得する数値 |
public static function ordinalize($number)
{
if (in_array($number % 100, range(11, 13))) {
return $number . 'th';
}
switch ($number % 10) {
case 1:
return $number . 'st';
case 2:
return $number . 'nd';
case 3:
return $number . 'rd';
default:
return $number . 'th';
}
}
単語を複数形に変換します。
これは英語のみです!例えば、'apple' は 'apples' になり、'child' は 'children' になります。
public static string pluralize ( $word ) | ||
$word | 文字列 |
複数形にする単語 |
戻り値 | 文字列 |
複数形にした単語 |
---|
public static function pluralize($word)
{
if (empty($word)) {
return (string) $word;
}
if (isset(static::$specials[$word])) {
return static::$specials[$word];
}
foreach (static::$plurals as $rule => $replacement) {
if (preg_match($rule, $word)) {
return preg_replace($rule, $replacement, $word);
}
}
return $word;
}
単語のリストを文に変換します。
最後のいくつかの単語に対して特別な処理が行われます。例えば、
$words = ['Spain', 'France'];
echo Inflector::sentence($words);
// output: Spain and France
$words = ['Spain', 'France', 'Italy'];
echo Inflector::sentence($words);
// output: Spain, France and Italy
$words = ['Spain', 'France', 'Italy'];
echo Inflector::sentence($words, ' & ');
// output: Spain, France & Italy
public static string sentence ( array $words, $twoWordsConnector = null, $lastWordConnector = null, $connector = ', ' ) | ||
$words | array |
文字列に変換する単語 |
$twoWordsConnector | string|null |
単語が2つの場合に単語を接続する文字列 |
$lastWordConnector | string|null |
最後の2つの単語を接続する文字列。これがnullの場合、 |
$connector | 文字列 |
$lastWordConnectorと$twoWordsConnectorで接続された単語以外の単語を接続する文字列 |
戻り値 | 文字列 |
生成された文章 |
---|
public static function sentence(array $words, $twoWordsConnector = null, $lastWordConnector = null, $connector = ', ')
{
if ($twoWordsConnector === null) {
$twoWordsConnector = Yii::t('yii', ' and ');
}
if ($lastWordConnector === null) {
$lastWordConnector = $twoWordsConnector;
}
switch (count($words)) {
case 0:
return '';
case 1:
return reset($words);
case 2:
return implode($twoWordsConnector, $words);
default:
return implode($connector, array_slice($words, 0, -1)) . $lastWordConnector . end($words);
}
}
$word の単数形を返します。
public static string singularize ( $word ) | ||
$word | 文字列 |
単数形にする英単語 |
戻り値 | 文字列 |
単数名詞。 |
---|
public static function singularize($word)
{
if (empty($word)) {
return (string) $word;
}
$result = array_search($word, static::$specials, true);
if ($result !== false) {
return $result;
}
foreach (static::$singulars as $rule => $replacement) {
if (preg_match($rule, $word)) {
return preg_replace($rule, $replacement, $word);
}
}
return $word;
}
すべてのスペースを指定した置換文字に変換し、単語以外の文字を削除し、残りの文字を転写した文字列を返します。
intl拡張機能が利用できない場合は、ラテン文字のみを変換し、残りを削除するフォールバックを使用します。ヘルパーの$transliterationプロパティで文字マップをカスタマイズできます。
public static string slug ( $string, $replacement = '-', $lowercase = true ) | ||
$string | 文字列 |
変換する任意の文字列 |
$replacement | 文字列 |
スペースに使用される置換文字 |
$lowercase | 論理値 |
小文字で文字列を返すかどうか。デフォルトは |
戻り値 | 文字列 |
変換された文字列。 |
---|
public static function slug($string, $replacement = '-', $lowercase = true)
{
if (empty($string)) {
return (string) $string;
}
if ((string)$replacement !== '') {
$parts = explode($replacement, static::transliterate($string));
} else {
$parts = [static::transliterate($string)];
}
$replaced = array_map(function ($element) use ($replacement) {
$element = preg_replace('/[^a-zA-Z0-9=\s—–-]+/u', '', $element);
return preg_replace('/[=\s—–-]+/u', $replacement, $element);
}, $parts);
$string = trim(implode($replacement, $replaced), $replacement);
if ((string)$replacement !== '') {
$string = preg_replace('#' . preg_quote($replacement, '#') . '+#', $replacement, $string);
}
return $lowercase ? strtolower($string) : $string;
}
クラス名をテーブル名(複数形)の命名規則に変換します。
例えば、「Person」を「people」に変換します。
public static string tableize ( $className ) | ||
$className | 文字列 |
関連するtable_nameを取得するためのクラス名 |
public static function tableize($className)
{
if (empty($className)) {
return (string) $className;
}
return static::pluralize(static::underscore($className));
}
アンダースコアまたは CamelCase の単語を英語の文に変換します。
public static string titleize ( $words, $ucAll = false ) | ||
$words | 文字列 | |
$ucAll | 論理値 |
すべての単語を大文字にするかどうか |
public static function titleize($words, $ucAll = false)
{
if (empty($words)) {
return (string) $words;
}
$words = static::humanize(static::underscore($words), $ucAll);
return $ucAll ? StringHelper::mb_ucwords($words, self::encoding()) : StringHelper::mb_ucfirst($words, self::encoding());
}
文字列の転写されたバージョンを返します。
intl拡張機能が利用できない場合は、ラテン文字のみを変換し、残りを削除するフォールバックを使用します。ヘルパーの$transliterationプロパティで文字マップをカスタマイズできます。
public static string transliterate ( $string, $transliterator = null ) | ||
$string | 文字列 |
入力文字列 |
$transliterator | string|Transliterator|null |
Transliteratorオブジェクト、またはTransliteratorオブジェクトを作成できる文字列。 |
バージョン | 説明 |
---|---|
2.0.7 | このメソッドはpublicです。 |
public static function transliterate($string, $transliterator = null)
{
if (empty($string)) {
return (string) $string;
}
if (static::hasIntl()) {
if ($transliterator === null) {
$transliterator = static::$transliterator;
}
return transliterator_transliterate($transliterator, $string);
}
return strtr($string, static::$transliteration);
}
任意の "CamelCased" を "underscored_word" に変換します。
public static string underscore ( $words ) | ||
$words | 文字列 |
アンダースコアにする単語 |
public static function underscore($words)
{
if (empty($words)) {
return (string) $words;
}
return mb_strtolower(preg_replace('/(?<=\\pL)(\\p{Lu})/u', '_\\1', $words), self::encoding());
}
camelize と同じですが、最初の文字は小文字です。
「send_email」のような単語を「sendEmail」に変換します。英数字以外の文字は削除されるため、「who's online」は「whoSOnline」に変換されます。
public static string variablize ( $word ) | ||
$word | 文字列 |
lowerCamelCaseに変換 |
public static function variablize($word)
{
if (empty($word)) {
return (string) $word;
}
$word = static::camelize($word);
return mb_strtolower(mb_substr($word, 0, 1, self::encoding())) . mb_substr($word, 1, null, self::encoding());
}
サインアップ または ログイン してコメントしてください。