Final Class Yiisoft\Strings\Inflector
| Inheritance | Yiisoft\Strings\Inflector |
|---|
Inflector provides methods such as pluralize() or slug() that derive a new string based on the string given.
Public Methods
Constants
| Constant | Value | Description | Defined By |
|---|---|---|---|
| TRANSLITERATE_LOOSE | 'Any-Latin; Latin-ASCII; [\u0080-\uffff] remove' |
Shortcut for Any-Latin; Latin-ASCII; [\u0080-\uffff] remove transliteration rule.
The rule is loose,
letters will be transliterated with the characters of Basic Latin Unicode Block.
For example:
获取到 どちら Українська: ґ,є, Српска: ђ, њ, џ! ¿Español? will be transliterated to
huo qu dao dochira Ukrainska: g,e, Srpska: d, n, d! Espanol?. |
Yiisoft\Strings\Inflector |
| TRANSLITERATE_MEDIUM | 'Any-Latin; Latin-ASCII' |
Shortcut for Any-Latin; Latin-ASCII transliteration rule.
The rule is medium, letters will be
transliterated to characters of Latin-1 (ISO 8859-1) ASCII table. For example:
获取到 どちら Українська: ґ,є, Српска: ђ, њ, џ! ¿Español? will be transliterated to
huo qu dao dochira Ukrainsʹka: g,e, Srpska: d, n, d! ¿Espanol?. |
Yiisoft\Strings\Inflector |
| TRANSLITERATE_STRICT | 'Any-Latin; NFKD' |
Shortcut for Any-Latin; NFKD transliteration rule.
The rule is strict, letters will be transliterated with
the closest sound-representation chars. The result may contain any UTF-8 chars. For example:
获取到 どちら Українська: ґ,є, Српска: ђ, њ, џ! ¿Español? will be transliterated to
huò qǔ dào dochira Ukraí̈nsʹka: g̀,ê, Srpska: đ, n̂, d̂! ¿Español?.
For detailed information see unicode normalization forms |
Yiisoft\Strings\Inflector |
Method Details
Converts a class name to its table name (pluralized) naming conventions.
For example, converts "Car" to "cars", "Person" to "people", and "ActionLog" to "action_log".
| public classToTable( string $className ): string | ||
| $className | string |
The class name for getting related table_name. |
public function classToTable(string $className): string
{
return $this->toPlural($this->pascalCaseToId($className, '_'));
}
| public getPluralizeRules( ): string[] | ||
| return | string[] |
The rules for converting a word into its plural form. The keys are the regular expressions and the values are the corresponding replacements. |
|---|---|---|
public function getPluralizeRules(): array
{
return $this->pluralizeRules;
}
| public getSingularizeRules( ): string[] | ||
| return | string[] |
The rules for converting a word into its singular form. The keys are the regular expressions and the values are the corresponding replacements. |
|---|---|---|
public function getSingularizeRules(): array
{
return $this->singularizeRules;
}
| public getSpecialRules( ): string[] | ||
| return | string[] |
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 function getSpecialRules(): array
{
return $this->specialRules;
}
Converts a PascalCase name into an ID in lowercase.
Words in the ID may be concatenated using the specified character (defaults to '-'). For example, 'PostTag' will be converted to 'post-tag'.
| public pascalCaseToId( string $input, string $separator = '-', boolean $strict = false ): string | ||
| $input | string |
The string to be converted. |
| $separator | string |
The character used to concatenate the words in the ID. It must be valid UTF-8 string. |
| $strict | boolean |
Whether to insert a separator between two consecutive uppercase chars, defaults to false. |
| return | string |
The resulting ID. |
|---|---|---|
public function pascalCaseToId(string $input, string $separator = '-', bool $strict = false): string
{
$regex = $strict
? '/(?<=\p{L})(\p{Lu})/u'
: '/(?<=\p{L})(?<!\p{Lu})(\p{Lu})/u';
/**
* @var string $result We assume that `$separator` and `$input` are valid UTF-8 strings, so `preg_replace()`
* never returns `false`.
*/
$result = preg_replace($regex, addslashes($separator) . '\1', $input);
if ($separator !== '_') {
$result = str_replace('_', $separator, $result);
}
return mb_strtolower(trim($result, $separator));
}
Converts a table name to its class name.
For example, converts "cars" to "Car", "people" to "Person", and "action_log" to "ActionLog".
| public tableToClass( string $tableName ): string | ||
| $tableName | string | |
public function tableToClass(string $tableName): string
{
return $this->toPascalCase($this->toSingular($tableName));
}
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".
| public toCamelCase( string $input ): string | ||
| $input | string |
The word to convert. |
public function toCamelCase(string $input): string
{
$input = $this->toPascalCase($input);
return mb_strtolower(mb_substr($input, 0, 1)) . mb_substr($input, 1);
}
Returns a human-readable string.
| public toHumanReadable( string $input, boolean $uppercaseWords = false ): string | ||
| $input | string |
The valid UTF-8 string to humanize. |
| $uppercaseWords | boolean |
Whether to set all words to uppercase or not. |
public function toHumanReadable(string $input, bool $uppercaseWords = false): string
{
/**
* @var string $input We assume that `$input` is valid UTF-8 string, so `preg_replace()` never returns `false`.
*/
$input = preg_replace('/_id$/', '', $input);
$input = str_replace('_', ' ', $input);
return $uppercaseWords
? StringHelper::uppercaseFirstCharacterInEachWord($input)
: StringHelper::uppercaseFirstCharacter($input);
}
Returns given word as PascalCased.
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 toCamelCase().
| public toPascalCase( string $input ): string | ||
| $input | string |
The word to PascalCase. It must be valid UTF-8 string. |
| return | string |
PascalCased string. |
|---|---|---|
public function toPascalCase(string $input): string
{
/**
* @var string $input We assume that `$input` is valid UTF-8 string, so `preg_replace()` never returns `false`.
*/
$input = preg_replace('/[^\pL\pN]+/u', ' ', $input);
return str_replace(
' ',
'',
StringHelper::uppercaseFirstCharacterInEachWord($input),
);
}
Converts a word to its plural form.
Note that this is for English only! For example, "apple" will become "apples", and "child" will become "children".
| public toPlural( string $input ): string | ||
| $input | string |
The word to be pluralized. |
| return | string |
The pluralized word. |
|---|---|---|
public function toPlural(string $input): string
{
if (isset($this->specialRules[$input])) {
return $this->specialRules[$input];
}
foreach ($this->pluralizeRules as $rule => $replacement) {
if (preg_match($rule, $input)) {
/** @var string `$rule` and `$replacement` always correct, so `preg_replace` always returns string */
return preg_replace($rule, $replacement, $input);
}
}
return $input;
}
Converts an underscored or PascalCase word into a English sentence.
| public toSentence( string $input, boolean $uppercaseAll = false ): string | ||
| $input | string |
The string to titleize. |
| $uppercaseAll | boolean |
Whether to set all words to uppercase. |
public function toSentence(string $input, bool $uppercaseAll = false): string
{
$input = $this->toHumanReadable($this->pascalCaseToId($input, '_'), $uppercaseAll);
return $uppercaseAll
? StringHelper::uppercaseFirstCharacterInEachWord($input)
: StringHelper::uppercaseFirstCharacter($input);
}
Returns the singular of the $word.
| public toSingular( string $input ): string | ||
| $input | string |
The english word to singularize. |
| return | string |
Singular noun. |
|---|---|---|
public function toSingular(string $input): string
{
$result = array_search($input, $this->specialRules, true);
if ($result !== false) {
return $result;
}
foreach ($this->singularizeRules as $rule => $replacement) {
if (preg_match($rule, $input)) {
/** @var string `$rule` and `$replacement` always correct, so `preg_replace` always returns string */
return preg_replace($rule, $replacement, $input);
}
}
return $input;
}
Returns a string with all spaces converted to given replacement, non word characters removed and the rest of characters transliterated.
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 toSlug( string $input, string $replacement = '-', boolean $lowercase = true ): string | ||
| $input | string |
An arbitrary valid UTF-8 string to convert. |
| $replacement | string |
The replacement to use for spaces. It must be valid UTF-8 string. |
| $lowercase | boolean |
Whether to return the string in lowercase or not. Defaults to |
| return | string |
The converted string. |
|---|---|---|
public function toSlug(string $input, string $replacement = '-', bool $lowercase = true): string
{
$quotedReplacement = preg_quote($replacement, '/');
/**
* Replace all non-words character
*
* @var string $input We assume that `$input` and `$replacement` are valid UTF-8 strings, so `preg_replace()`
* never returns `false`.
*/
$input = preg_replace('/[^a-zA-Z0-9]+/u', $replacement, $this->toTransliterated($input));
/**
* Remove first and last replacements
*
* @var string $input We assume that `$input` and `$quotedReplacement` are valid UTF-8 strings, so
* `preg_replace()` never returns `false`.
*/
$input = preg_replace(
"/^(?:$quotedReplacement)+|(?:$quotedReplacement)+$/u" . ($lowercase ? 'i' : ''),
'',
$input,
);
return $lowercase ? strtolower($input) : $input;
}
Returns given word as "snake_cased".
Converts a word like "userName" to "user_name". It will remove non-alphanumeric character from the word, so "who's online" will be converted to "who_s_online".
| public toSnakeCase( string $input, boolean $strict = true ): string | ||
| $input | string |
The word to convert. It must be valid UTF-8 string. |
| $strict | boolean |
Whether to insert a separator between two consecutive uppercase chars, defaults to true. |
| return | string |
The "snake_cased" string. |
|---|---|---|
public function toSnakeCase(string $input, bool $strict = true): string
{
/**
* @var string $input We assume that `$input` is valid UTF-8 string, so `preg_replace()` never returns `false`.
*/
$input = preg_replace('/[^\pL\pN]+/u', '_', $input);
return $this->pascalCaseToId($input, '_', $strict);
}
Returns transliterated version of a string.
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 toTransliterated( string $input, string|Transliterator|null $transliterator = null ): string | ||
| $input | string |
Input string. It must be valid UTF-8 string. |
| $transliterator | string|Transliterator|null |
Either a Transliterator or a string from which a Transliterator can be built. If null, value set with withTransliterator() or \Yiisoft\Strings\TRANSLITERATE_LOOSE is used. |
public function toTransliterated(string $input, $transliterator = null): string
{
if ($this->useIntl()) {
if ($transliterator === null) {
$transliterator = $this->transliterator;
}
/**
* @noinspection PhpComposerExtensionStubsInspection
* @var string We assume that `$input` are valid UTF-8 strings and `$transliterator` is valid, so
* `preg_replace()` never returns `false`.
*/
return transliterator_transliterate($transliterator, $input);
}
return strtr($input, $this->transliterationMap);
}
Converts a string into space-separated words.
For example, 'PostTag' will be converted to 'Post Tag'.
| public toWords( string $input ): string | ||
| $input | string |
The valid UTF-8 string to be converted. |
| return | string |
The resulting words. |
|---|---|---|
public function toWords(string $input): string
{
/**
* @var string $words We assume that `$input` is valid UTF-8 string, so `preg_replace()` never returns `false`.
*/
$words = preg_replace('/(?<!\p{Lu})(\p{Lu})|(\p{Lu})(?=\p{Ll})/u', ' \0', $input);
return mb_strtolower(
trim(
str_replace(['-', '_', '.'], ' ', $words)
)
);
}
| public withPluralizeRules( string[] $rules ): self | ||
| $rules | string[] |
The rules for converting a word into its plural form. The keys are the regular expressions and the values are the corresponding replacements. |
public function withPluralizeRules(array $rules): self
{
$new = clone $this;
$new->pluralizeRules = $rules;
return $new;
}
| public withSingularizeRules( string[] $rules ): self | ||
| $rules | string[] |
The rules for converting a word into its singular form. The keys are the regular expressions and the values are the corresponding replacements. |
public function withSingularizeRules(array $rules): self
{
$new = clone $this;
$new->singularizeRules = $rules;
return $new;
}
| public withSpecialRules( string[] $rules ): self | ||
| $rules | string[] |
The special rules for converting a word between its plural form and singular form. |
public function withSpecialRules(array $rules): self
{
$new = clone $this;
$new->specialRules = $rules;
return $new;
}
| public withTransliterationMap( string[] $transliterationMap ): $this | ||
| $transliterationMap | string[] |
Fallback map for transliteration used by toTransliterated() when intl isn't available or turned off with withoutIntl(). |
public function withTransliterationMap(array $transliterationMap): self
{
$new = clone $this;
$new->transliterationMap = $transliterationMap;
return $new;
}
| public withTransliterator( string|Transliterator $transliterator ): self | ||
| $transliterator | string|Transliterator |
Either a Transliterator, or a string from which a Transliterator can be built for transliteration. Used by toTransliterated() when intl is available. Defaults to \Yiisoft\Strings\TRANSLITERATE_LOOSE. |
public function withTransliterator($transliterator): self
{
$new = clone $this;
$new->transliterator = $transliterator;
return $new;
}
Disables usage of intl for toTransliterated().
| public withoutIntl( ): self |
public function withoutIntl(): self
{
$new = clone $this;
$new->withoutIntl = true;
return $new;
}
Signup or Login in order to comment.