PHP Class yii\helpers\BaseInflector

Do not use BaseInflector. Use Inflector instead.
Since: 2.0
Author: Antonio Ramirez ([email protected])
Author: Alexander Makarov ([email protected])
显示文件 Open project: yiisoft/yii2 Class Usage Examples

Public Properties

Property Type Description
$plurals the rules for converting a word into its plural form. The keys are the regular expressions and the values are the corresponding replacements.
$singulars the rules for converting a word into its singular form. The keys are the regular expressions and the values are the corresponding replacements.
$specials the special rules for converting a word between its plural form and singular form. The keys are the special words in singular form, and the values are the corresponding plural form.
$transliteration fallback map for transliteration used by BaseInflector::transliterate when intl isn't available.
$transliterator Either a [[\Transliterator]], or a string from which a [[\Transliterator]] can be built for transliteration. Used by BaseInflector::transliterate when intl is available. Defaults to [[TRANSLITERATE_LOOSE]]

Public Methods

Method Description
camel2id ( string $name, string $separator = '-', boolean | string $strict = false ) : string Converts a CamelCase name into an ID in lowercase.
camel2words ( string $name, boolean $ucwords = true ) : string Converts a CamelCase name into space-separated words.
camelize ( string $word ) : string Returns given word as CamelCased Converts a word like "send_email" to "SendEmail". It will remove non alphanumeric character from the word, so "who's online" will be converted to "WhoSOnline"
classify ( string $tableName ) : string Converts a table name to its class name. For example, converts "people" to "Person"
humanize ( string $word, boolean $ucAll = false ) : string Returns a human-readable string from $word
id2camel ( string $id, string $separator = '-' ) : string Converts an ID into a CamelCase name.
ordinalize ( integer $number ) : string Converts number to its ordinal English form. For example, converts 13 to 13th, 2 to 2nd .
pluralize ( string $word ) : string Converts a word to its plural form.
sentence ( array $words, string $twoWordsConnector = ' and ', string $lastWordConnector = null, string $connector = ', ' ) : string Converts a list of words into a sentence.
singularize ( string $word ) : string Returns the singular of the $word
slug ( string $string, string $replacement = '-', boolean $lowercase = true ) : string Returns a string with all spaces converted to given replacement, non word characters removed and the rest of characters transliterated.
tableize ( string $className ) : string Converts a class name to its table name (pluralized) naming conventions. For example, converts "Person" to "people"
titleize ( string $words, boolean $ucAll = false ) : string Converts an underscored or CamelCase word into a English sentence.
transliterate ( string $string, string | Transliterator $transliterator = null ) : string Returns transliterated version of a string.
underscore ( string $words ) : string Converts any "CamelCased" into an "underscored_word".
variablize ( string $word ) : string Same as camelize but first char is in lowercase.

Protected Methods

Method Description
hasIntl ( ) : boolean

Method Details

camel2id() public static method

Words in the ID may be concatenated using the specified character (defaults to '-'). For example, 'PostTag' will be converted to 'post-tag'.
public static camel2id ( string $name, string $separator = '-', boolean | string $strict = false ) : string
$name string the string to be converted
$separator string the character used to concatenate the words in the ID
$strict boolean | string whether to insert a separator between two consecutive uppercase chars, defaults to false
return string the resulting ID

camel2words() public static method

For example, 'PostTag' will be converted to 'Post Tag'.
public static camel2words ( string $name, boolean $ucwords = true ) : string
$name string the string to be converted
$ucwords boolean whether to capitalize the first letter in each word
return string the resulting words

camelize() public static method

Returns given word as CamelCased Converts a word like "send_email" to "SendEmail". It will remove non alphanumeric character from the word, so "who's online" will be converted to "WhoSOnline"
See also: variablize()
public static camelize ( string $word ) : string
$word string the word to CamelCase
return string

classify() public static method

Converts a table name to its class name. For example, converts "people" to "Person"
public static classify ( string $tableName ) : string
$tableName string
return string

hasIntl() protected static method

protected static hasIntl ( ) : boolean
return boolean if intl extension is loaded

humanize() public static method

Returns a human-readable string from $word
public static humanize ( string $word, boolean $ucAll = false ) : string
$word string the string to humanize
$ucAll boolean whether to set all words to uppercase or not
return string

id2camel() public static method

Words in the ID separated by $separator (defaults to '-') will be concatenated into a CamelCase name. For example, 'post-tag' is converted to 'PostTag'.
public static id2camel ( string $id, string $separator = '-' ) : string
$id string the ID to be converted
$separator string the character used to separate the words in the ID
return string the resulting CamelCase name

ordinalize() public static method

..
public static ordinalize ( integer $number ) : string
$number integer the number to get its ordinal value
return string

pluralize() public static method

Note that this is for English only! For example, 'apple' will become 'apples', and 'child' will become 'children'.
public static pluralize ( string $word ) : string
$word string the word to be pluralized
return string the pluralized word

sentence() public static method

Special treatment is done for the last few words. For example, php $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
Since: 2.0.1
public static sentence ( array $words, string $twoWordsConnector = ' and ', string $lastWordConnector = null, string $connector = ', ' ) : string
$words array the words to be converted into an string
$twoWordsConnector string the string connecting words when there are only two
$lastWordConnector string the string connecting the last two words. If this is null, it will take the value of `$twoWordsConnector`.
$connector string the string connecting words other than those connected by $lastWordConnector and $twoWordsConnector
return string the generated sentence

singularize() public static method

Returns the singular of the $word
public static singularize ( string $word ) : string
$word string the english word to singularize
return string Singular noun.

slug() public static method

If intl extension isn't available uses fallback that converts latin characters only and removes the rest. You may customize characters map via $transliteration property of the helper.
public static slug ( string $string, string $replacement = '-', boolean $lowercase = true ) : string
$string string An arbitrary string to convert
$replacement string The replacement to use for spaces
$lowercase boolean whether to return the string in lowercase or not. Defaults to `true`.
return string The converted string.

tableize() public static method

Converts a class name to its table name (pluralized) naming conventions. For example, converts "Person" to "people"
public static tableize ( string $className ) : string
$className string the class name for getting related table_name
return string

titleize() public static method

Converts an underscored or CamelCase word into a English sentence.
public static titleize ( string $words, boolean $ucAll = false ) : string
$words string
$ucAll boolean whether to set all words to uppercase
return string

transliterate() public static method

If intl extension isn't available uses fallback that converts latin characters only and removes the rest. You may customize characters map via $transliteration property of the helper.
Since: 2.0.7 this method is public.
public static transliterate ( string $string, string | Transliterator $transliterator = null ) : string
$string string input string
$transliterator string | Transliterator either a [[\Transliterator]] or a string from which a [[\Transliterator]] can be built.
return string

underscore() public static method

Converts any "CamelCased" into an "underscored_word".
public static underscore ( string $words ) : string
$words string the word(s) to underscore
return string

variablize() public static method

Converts a word like "send_email" to "sendEmail". It will remove non alphanumeric character from the word, so "who's online" will be converted to "whoSOnline"
public static variablize ( string $word ) : string
$word string to lowerCamelCase
return string

Property Details

$plurals public_oe static_oe property

the rules for converting a word into its plural form. The keys are the regular expressions and the values are the corresponding replacements.
public static $plurals

$singulars public_oe static_oe property

the rules for converting a word into its singular form. The keys are the regular expressions and the values are the corresponding replacements.
public static $singulars

$specials public_oe static_oe property

the special rules for converting a word between its plural form and singular form. The keys are the special words in singular form, and the values are the corresponding plural form.
public static $specials

$transliteration public_oe static_oe property

fallback map for transliteration used by BaseInflector::transliterate when intl isn't available.
public static $transliteration

$transliterator public_oe static_oe property

Either a [[\Transliterator]], or a string from which a [[\Transliterator]] can be built for transliteration. Used by BaseInflector::transliterate when intl is available. Defaults to [[TRANSLITERATE_LOOSE]]
See also: http://php.net/manual/en/transliterator.transliterate.php
public static $transliterator