-
-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
47 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -496,11 +496,15 @@ public function toSentence(string $input, bool $uppercaseAll = false): string | |
*/ | ||
public function toWords(string $input): string | ||
{ | ||
return mb_strtolower(trim(str_replace([ | ||
'-', | ||
'_', | ||
'.', | ||
], ' ', preg_replace('/(?<!\p{Lu})(\p{Lu})|(\p{Lu})(?=\p{Ll})/u', ' \0', $input)))); | ||
/** | ||
* @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) | ||
) | ||
); | ||
} | ||
|
||
/** | ||
|
@@ -520,6 +524,10 @@ public function pascalCaseToId(string $input, string $separator = '-', bool $str | |
? '/(?<=\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 !== '_') { | ||
Check warning on line 533 in src/Inflector.php
|
||
|
@@ -544,10 +552,14 @@ public function pascalCaseToId(string $input, string $separator = '-', bool $str | |
*/ | ||
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(preg_replace('/[^\pL\pN]+/u', ' ', $input)), | ||
StringHelper::uppercaseFirstCharacterInEachWord($input), | ||
); | ||
} | ||
|
||
|
@@ -556,12 +568,14 @@ public function toPascalCase(string $input): string | |
* | ||
* @param string $input The string to humanize. | ||
* @param bool $uppercaseWords Whether to set all words to uppercase or not. | ||
* | ||
* @return string | ||
*/ | ||
public function toHumanReadable(string $input, bool $uppercaseWords = false): string | ||
{ | ||
$input = str_replace('_', ' ', preg_replace('/_id$/', '', $input)); | ||
/** | ||
* @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) | ||
|
@@ -600,7 +614,11 @@ public function toCamelCase(string $input): string | |
*/ | ||
public function toSnakeCase(string $input, bool $strict = true): string | ||
{ | ||
return $this->pascalCaseToId(preg_replace('/[^\pL\pN]+/u', '_', $input), '_', $strict); | ||
/** | ||
* @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); | ||
} | ||
|
||
/** | ||
|
@@ -646,9 +664,21 @@ public function tableToClass(string $tableName): string | |
public function toSlug(string $input, string $replacement = '-', bool $lowercase = true): string | ||
{ | ||
$quotedReplacement = preg_quote($replacement, '/'); | ||
Check warning on line 666 in src/Inflector.php
|
||
// replace all non words character | ||
|
||
/** | ||
* 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 | ||
|
||
/** | ||
* 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' : ''), | ||
Check warning on line 683 in src/Inflector.php
|
||
'', | ||
|
@@ -671,8 +701,6 @@ public function toSlug(string $input, string $replacement = '-', bool $lowercase | |
* @param string|Transliterator|null $transliterator either a {@see \Transliterator} or a string | ||
* from which a {@see \Transliterator} can be built. If null, value set with {@see withTransliterator()} | ||
* or {@see TRANSLITERATE_LOOSE} is used. | ||
* | ||
* @return string | ||
*/ | ||
public function toTransliterated(string $input, $transliterator = null): string | ||
{ | ||
|
@@ -681,7 +709,11 @@ public function toTransliterated(string $input, $transliterator = null): string | |
$transliterator = $this->transliterator; | ||
} | ||
|
||
/* @noinspection PhpComposerExtensionStubsInspection */ | ||
/** | ||
* @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); | ||
} | ||
|
||
|