0 follower

クラス yii\helpers\Inflector

継承yii\helpers\Inflector » yii\helpers\BaseInflector
利用可能なバージョン2.0
ソースコード https://github.com/yiisoft/yii2/blob/master/framework/helpers/Inflector.php

Inflector は英語の名詞を複数形および単数形にします。また、いくつかの他の便利なメソッドも含まれています。

パブリックプロパティ

継承されたプロパティを隠す

プロパティ 説明 定義元
$plurals 配列 単語を複数形に変換するための規則。 yii\helpers\BaseInflector
$singulars 配列 単語を単数形に変換するための規則。 yii\helpers\BaseInflector
$specials 配列 単語を複数形と単数形の間で変換するための特別な規則。 yii\helpers\BaseInflector
$transliteration 配列 intl が利用できない場合に transliterate() によって使用される、翻字用のフォールバックマップ。 yii\helpers\BaseInflector
$transliterator 混合 Transliterator、または Transliterator を翻字のために構築できる文字列。 yii\helpers\BaseInflector

パブリックメソッド

継承されたメソッドを隠す

メソッド 説明 定義元
camel2id() キャメルケースの名前を小文字の ID に変換します。 yii\helpers\BaseInflector
camel2words() キャメルケースの名前をスペースで区切られた単語に変換します。 yii\helpers\BaseInflector
camelize() 指定された単語をキャメルケースとして返します。 yii\helpers\BaseInflector
classify() テーブル名をそのクラス名に変換します。 yii\helpers\BaseInflector
humanize() $word から人間が読める文字列を返します。 yii\helpers\BaseInflector
id2camel() ID をキャメルケースの名前に変換します。 yii\helpers\BaseInflector
ordinalize() 数字をその序数の英語形式に変換します。たとえば、13 を 13th、2 を 2nd に変換します。 yii\helpers\BaseInflector
pluralize() 単語を複数形に変換します。 yii\helpers\BaseInflector
sentence() 単語のリストを文に変換します。 yii\helpers\BaseInflector
singularize() $word の単数形を返します。 yii\helpers\BaseInflector
slug() すべてのスペースを与えられた置換文字に変換し、単語以外の文字を削除し、残りの文字を翻字した文字列を返します。 yii\helpers\BaseInflector
tableize() クラス名をそのテーブル名(複数形)の命名規則に変換します。 yii\helpers\BaseInflector
titleize() アンダースコアまたはキャメルケースの単語を英文に変換します。 yii\helpers\BaseInflector
transliterate() 文字列の翻字されたバージョンを返します。 yii\helpers\BaseInflector
underscore() 任意の "キャメルケース" を "アンダースコア付き_単語" に変換します。 yii\helpers\BaseInflector
variablize() camelize と同じですが、最初の文字は小文字です。 yii\helpers\BaseInflector

保護されたメソッド

継承されたメソッドを隠す

メソッド 説明 定義元
hasIntl() yii\helpers\BaseInflector

定数

継承された定数を隠す

定数 説明 定義元
TRANSLITERATE_LOOSE 'Any-Latin; Latin-ASCII; [\u0080-\uffff] remove' Any-Latin; Latin-ASCII; [\u0080-\uffff] remove の翻字規則のショートカットです。この規則は緩く、文字は Basic Latin Unicode Block の文字で翻字されます。例:获取到 どちら Українська: ґ,є, Српска: ђ, њ, џ! ¿Español?huo qu dao dochira Ukrainska: g,e, Srpska: d, n, d! Espanol? に翻字されます。transliterate() で使用されます。詳細については、ユニコード正規化形式 を参照してください。 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() で使用されます。詳細については、ユニコード正規化形式 を参照してください。 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() で使用されます。詳細については、ユニコード正規化形式 を参照してください。 yii\helpers\BaseInflector

メソッド詳細

継承されたメソッドを隠す

camel2id() public static メソッド

定義元: yii\helpers\BaseInflector::camel2id()

キャメルケースの名前を小文字の ID に変換します。

ID 内の単語は、指定された文字(デフォルトは '-')を使用して連結できます。たとえば、'PostTag' は 'post-tag' に変換されます。

public static string camel2id ( $name, $separator '-', $strict false )
$name string

変換する文字列

$separator string

ID 内の単語を連結するために使用される文字

$strict boolean|string

2 つの連続した大文字の間にセパレーターを挿入するかどうか、デフォルトは false

return string

結果の 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());
}

            
camel2words() public static メソッド

定義元: yii\helpers\BaseInflector::camel2words()

キャメルケースの名前をスペースで区切られた単語に変換します。

たとえば、'PostTag' は 'Post Tag' に変換されます。

public static string camel2words ( $name, $ucwords true )
$name string

変換する文字列

$ucwords boolean

各単語の最初の文字を大文字にするかどうか

return string

結果の単語

                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;
}

            
camelize() public static メソッド

定義元: yii\helpers\BaseInflector::camelize()

指定された単語をキャメルケースとして返します。

"send_email" のような単語を "SendEmail" に変換します。単語から英数字以外の文字を削除するため、"who's online" は "WhoSOnline" に変換されます。

variablize() も参照してください。

public static string camelize ( $word )
$word string

キャメルケースにする単語

                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()));
}

            
classify() public static メソッド

定義元: yii\helpers\BaseInflector::classify()

テーブル名をそのクラス名に変換します。

たとえば、"people" を "Person" に変換します。

public static string classify ( $tableName )
$tableName string

                public static function classify($tableName)
{
    if (empty($tableName)) {
        return (string) $tableName;
    }
    return static::camelize(static::singularize($tableName));
}

            
hasIntl() protected static メソッド
protected static boolean hasIntl ( )
return boolean

intl 拡張機能がロードされている場合

                protected static function hasIntl()
{
    return extension_loaded('intl');
}

            
humanize() public static メソッド

定義元: yii\helpers\BaseInflector::humanize()

$word から人間が読める文字列を返します。

public static string humanize ( $word, $ucAll false )
$word string

ヒューマナイズする文字列

$ucAll boolean

すべての単語を大文字にするかどうか

                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);
}

            
id2camel() public static メソッド

定義元: yii\helpers\BaseInflector::id2camel()

ID をキャメルケースの名前に変換します。

$separator(デフォルトは '-')で区切られた ID 内の単語は、キャメルケース名に連結されます。たとえば、'post-tag' は 'PostTag' に変換されます。

public static string id2camel ( $id, $separator '-' )
$id string

変換する ID

$separator string

ID 内の単語を区切るために使用される文字

return string

結果のキャメルケース名

                public static function id2camel($id, $separator = '-')
{
    if (empty($id)) {
        return (string) $id;
    }
    return str_replace(' ', '', StringHelper::mb_ucwords(str_replace($separator, ' ', $id), self::encoding()));
}

            
ordinalize() public static メソッド

定義元: yii\helpers\BaseInflector::ordinalize()

数字をその序数の英語形式に変換します。たとえば、13 を 13th、2 を 2nd に変換します。

..

public static string ordinalize ( $number )
$number integer

序数値を取得する数値

                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';
    }
}

            
pluralize() public static メソッド

定義元: yii\helpers\BaseInflector::pluralize()

単語を複数形に変換します。

これは英語専用であることに注意してください。たとえば、'apple' は 'apples' になり、'child' は 'children' になります。

public static string pluralize ( $word )
$word string

複数形にする単語

return string

複数形になった単語

                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;
}

            
sentence() public static メソッド (バージョン 2.0.1 以降で利用可能)

定義元: yii\helpers\BaseInflector::sentence()

単語のリストを文に変換します。

最後の数個の単語には特別な処理が適用されます。例えば、

$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 配列

文字列に変換される単語

$twoWordsConnector string|null

単語が2つしかない場合に単語を接続する文字列

$lastWordConnector string|null

最後の2つの単語を接続する文字列。これが null の場合、$twoWordsConnector の値が使用されます。

$connector string

$lastWordConnector および $twoWordsConnector で接続された単語以外の単語を接続する文字列

return string

生成された文章

                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);
    }
}

            
singularize() public static メソッド

定義元: yii\helpers\BaseInflector::singularize()

$word の単数形を返します。

public static string singularize ( $word )
$word string

単数形にする英語の単語

return string

単数名詞。

                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;
}

            
slug() public static メソッド

定義元: yii\helpers\BaseInflector::slug()

すべてのスペースを与えられた置換文字に変換し、単語以外の文字を削除し、残りの文字を翻字した文字列を返します。

intl拡張機能が利用できない場合、ラテン文字のみを変換し、残りを削除するフォールバックを使用します。ヘルパーの $transliteration プロパティを使用して文字マップをカスタマイズできます。

public static string slug ( $string, $replacement '-', $lowercase true )
$string string

変換する任意の文字列

$replacement string

スペースに使用する置換文字列

$lowercase boolean

文字列を小文字で返すかどうか。デフォルトは true です。

return string

変換された文字列。

                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;
}

            
tableize() public static メソッド

定義元: yii\helpers\BaseInflector::tableize()

クラス名をそのテーブル名(複数形)の命名規則に変換します。

例えば、"Person" を "people" に変換します。

public static string tableize ( $className )
$className string

関連する table_name を取得するためのクラス名

                public static function tableize($className)
{
    if (empty($className)) {
        return (string) $className;
    }
    return static::pluralize(static::underscore($className));
}

            
titleize() public static メソッド

定義元: yii\helpers\BaseInflector::titleize()

アンダースコアまたはキャメルケースの単語を英文に変換します。

public static string titleize ( $words, $ucAll false )
$words string
$ucAll boolean

すべての単語を大文字にするかどうか

                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());
}

            
transliterate() public static メソッド

定義元: yii\helpers\BaseInflector::transliterate()

文字列の翻字されたバージョンを返します。

intl拡張機能が利用できない場合、ラテン文字のみを変換し、残りを削除するフォールバックを使用します。ヘルパーの $transliteration プロパティを使用して文字マップをカスタマイズできます。

public static string transliterate ( $string, $transliterator null )
$string 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);
}

            
underscore() public static メソッド

定義元: yii\helpers\BaseInflector::underscore()

任意の "キャメルケース" を "アンダースコア付き_単語" に変換します。

public static string underscore ( $words )
$words string

アンダースコアにする単語

                public static function underscore($words)
{
    if (empty($words)) {
        return (string) $words;
    }
    return mb_strtolower(preg_replace('/(?<=\\pL)(\\p{Lu})/u', '_\\1', $words), self::encoding());
}

            
variablize() public static メソッド

定義元: yii\helpers\BaseInflector::variablize()

camelize と同じですが、最初の文字は小文字です。

"send_email" のような単語を "sendEmail" に変換します。単語から英数字以外の文字を削除するため、"who's online" は "whoSOnline" に変換されます。

public static string variablize ( $word )
$word string

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());
}