Skip to content

Commit

Permalink
[#21] Simplified based methods for quick copy/paste.
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexSkrypnyk committed Jan 20, 2025
1 parent 5bed281 commit bb1c047
Showing 1 changed file with 52 additions and 24 deletions.
76 changes: 52 additions & 24 deletions Str2Name.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,19 @@ public static function snake(string $string): string {
* @to iAmAStringWithSp@ce¥s14And😀UnicodeÉlève
*/
public static function camel(string $string): string {
return static::mbLcfirst(static::pascal($string));
$string = str_replace(['-', '_'], ' ', $string);

$result = '';
$upper = '';
for ($i = 0; $i < mb_strlen($string); $i++) {
$letter = mb_substr($string, $i, 1);
$result .= $upper || $i == 0 ? mb_convert_case($letter, MB_CASE_TITLE) : $letter;
$upper = ($i + 1) < mb_strlen($string) && mb_strpos(" \t\r\n\f\v", $letter) !== FALSE ? 1 : 0;
}

$string = str_replace(' ', '', $result);

return mb_convert_case(mb_substr($string, 0, 1), MB_CASE_LOWER) . mb_substr($string, 1);
}

/**
Expand All @@ -42,9 +54,16 @@ public static function camel(string $string): string {
*/
public static function pascal(string $string): string {
$string = str_replace(['-', '_'], ' ', $string);
$string = static::mbUcwords($string);

return str_replace(' ', '', $string);
$result = '';
$upper = '';
for ($i = 0; $i < mb_strlen($string); $i++) {
$letter = mb_substr($string, $i, 1);
$result .= $upper || $i == 0 ? mb_convert_case($letter, MB_CASE_TITLE) : $letter;
$upper = ($i + 1) < mb_strlen($string) && mb_strpos(" \t\r\n\f\v", $letter) !== FALSE ? 1 : 0;
}

return str_replace(' ', '', $result);
}

/**
Expand All @@ -60,26 +79,43 @@ public static function kebab(string $string): string {
* @to I-Am-A--String-With-Sp@ce¥s-14-And-😀-Unicode-Élève
*/
public static function train(string $string): string {
return static::mbUcwords(static::kebab($string), '-');
$string = mb_strtolower(str_replace([' ', '_'], '-', $string));

$result = '';
$upper = '';
for ($i = 0; $i < mb_strlen($string); $i++) {
$letter = mb_substr($string, $i, 1);
$result .= $upper || $i == 0 ? mb_convert_case($letter, MB_CASE_TITLE) : $letter;
$upper = ($i + 1) < mb_strlen($string) && mb_strpos('-', $letter) !== FALSE ? 1 : 0;
}

return $result;
}

/**
* @from I am a__string-With sp@ce¥s 14 and 😀 unicode élève
* @to iamastringwithsp@ce¥s14and😀unicodeélève
*/
public static function flat(string $string): string {
return str_replace('_', '', mb_strtolower(static::snake($string)));
return mb_strtolower(str_replace([' ', '-', '_'], '', $string));
}

/**
* @from I am a__string-With sp@ce¥s 14 and 😀 unicode élève
* @to I-AM-A--STRING-WITH-SP@CE¥S-14-AND-😀-UNICODE-ÉLÈVE
*/
public static function cobol(string $string): string {
return mb_strtoupper(str_replace([' ', '_'], '-', $string));
}

/**
* @from I am a__string-With sp@ce¥s 14 and 😀 unicode élève
* @to I am a string-with sp@ce¥s 14 and 😀 unicode élève
*/
public static function sentence(string $string): string {
$string = str_replace(['_'], ' ', $string);
$string = (string) preg_replace('/[\s]{2,}/', ' ', $string);
$string = (string) preg_replace('/[\s]{2,}/', ' ', str_replace(['_'], ' ', mb_strtolower($string)));

return static::mbUcfirst(mb_strtolower($string));
return mb_convert_case(mb_substr($string, 0, 1), MB_CASE_TITLE) . mb_substr($string, 1);
}

/**
Expand Down Expand Up @@ -126,14 +162,6 @@ public static function constantRaw(string $string): string {
return mb_strtoupper(static::snake($string));
}

/**
* @from I am a__string-With sp@ce¥s 14 and 😀 unicode élève
* @to I-AM-A--STRING-WITH-SP@CE¥S-14-AND-😀-UNICODE-ÉLÈVE
*/
public static function cobol(string $string): string {
return mb_strtoupper(static::kebab($string));
}

/**
* @from I am a__string-With sp@ce¥s 14 and 😀 unicode élève
* @to i_am_a__string_with_spces_14_and__unicode_eleve
Expand Down Expand Up @@ -690,20 +718,20 @@ public static function idUpperRaw(string $string): string {
* Multibyte ucfirst.
*/
protected static function mbUcfirst(string $string, ?string $encoding = NULL): string {
$firstChar = mb_substr($string, 0, 1, $encoding);
$firstChar = mb_convert_case($firstChar, MB_CASE_TITLE, $encoding);
$first_char = mb_substr($string, 0, 1, $encoding);
$first_char = mb_convert_case($first_char, MB_CASE_TITLE, $encoding);

return $firstChar . mb_substr($string, 1, NULL, $encoding);
return $first_char . mb_substr($string, 1, NULL, $encoding);
}

/**
* Multibyte lcfirst.
*/
protected static function mbLcfirst(string $string, ?string $encoding = NULL): string {
$firstChar = mb_substr($string, 0, 1, $encoding);
$firstChar = mb_convert_case($firstChar, MB_CASE_LOWER, $encoding);
$first_char = mb_substr($string, 0, 1, $encoding);
$first_char = mb_convert_case($first_char, MB_CASE_LOWER, $encoding);

return $firstChar . mb_substr($string, 1, NULL, $encoding);
return $first_char . mb_substr($string, 1, NULL, $encoding);
}

/**
Expand Down Expand Up @@ -748,8 +776,8 @@ protected static function strict(string $string): string {
* Add separator before an upper case char in string.
*/
protected static function mbAddSeparatorBeforeUpperCaseChar(string $string, string $separator = '_'): string {
$string = preg_replace_callback('/([^0-9])(\d+)/', static function (array $matches) use ($separator) : string {
return $matches[1] . $separator . $matches[2];
$string = preg_replace_callback('/([^0-9])(\d+)/', static function (array $matches) use ($separator): string {
return $matches[1] . $separator . $matches[2];
}, $string);
$replacements = [];
foreach (mb_str_split((string) $string) as $key => $char) {
Expand Down

0 comments on commit bb1c047

Please sign in to comment.