Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f9129e6

Browse files
committed
[String] CamelCase/SnakeCase on uppercase word
1 parent f841389 commit f9129e6

File tree

3 files changed

+9
-2
lines changed

3 files changed

+9
-2
lines changed

src/Symfony/Component/String/AbstractUnicodeString.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ public function ascii(array $rules = []): self
159159
public function camel(): static
160160
{
161161
$str = clone $this;
162-
$str->string = str_replace(' ', '', preg_replace_callback('/\b./u', static function ($m) use (&$i) {
162+
$str->string = str_replace(' ', '', preg_replace_callback('/\b.(?![A-Z]{2,})/u', static function ($m) use (&$i) {
163163
return 1 === ++$i ? ('İ' === $m[0] ? '' : mb_strtolower($m[0], 'UTF-8')) : mb_convert_case($m[0], \MB_CASE_TITLE, 'UTF-8');
164164
}, preg_replace('/[^\pL0-9]++/u', ' ', $this->string)));
165165

src/Symfony/Component/String/ByteString.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,10 @@ public function append(string ...$suffix): static
103103
public function camel(): static
104104
{
105105
$str = clone $this;
106-
$str->string = lcfirst(str_replace(' ', '', ucwords(preg_replace('/[^a-zA-Z0-9\x7f-\xff]++/', ' ', $this->string))));
106+
107+
$parts = explode(' ', trim(ucwords(preg_replace('/[^a-zA-Z0-9\x7f-\xff]++/', ' ', $this->string))));
108+
$parts[0] = 1 !== \strlen($parts[0]) && ctype_upper($parts[0]) ? $parts[0] : lcfirst($parts[0]);
109+
$str->string = implode('', $parts);
107110

108111
return $str;
109112
}

src/Symfony/Component/String/Tests/AbstractAsciiTestCase.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1042,11 +1042,13 @@ public static function provideCamel()
10421042
return [
10431043
['', ''],
10441044
['xY', 'x_y'],
1045+
['xuYo', 'xu_yo'],
10451046
['symfonyIsGreat', 'symfony_is_great'],
10461047
['symfony5IsGreat', 'symfony_5_is_great'],
10471048
['symfonyIsGreat', 'Symfony is great'],
10481049
['symfonyIsAGreatFramework', 'Symfony is a great framework'],
10491050
['symfonyIsGREAT', '*Symfony* is GREAT!!'],
1051+
['SYMFONY', 'SYMFONY'],
10501052
];
10511053
}
10521054

@@ -1066,13 +1068,15 @@ public static function provideSnake()
10661068
['', ''],
10671069
['x_y', 'x_y'],
10681070
['x_y', 'X_Y'],
1071+
['xu_yo', 'xu_yo'],
10691072
['symfony_is_great', 'symfonyIsGreat'],
10701073
['symfony5_is_great', 'symfony5IsGreat'],
10711074
['symfony5is_great', 'symfony5isGreat'],
10721075
['symfony_is_great', 'Symfony is great'],
10731076
['symfony_is_a_great_framework', 'symfonyIsAGreatFramework'],
10741077
['symfony_is_great', 'symfonyIsGREAT'],
10751078
['symfony_is_really_great', 'symfonyIsREALLYGreat'],
1079+
['symfony', 'SYMFONY'],
10761080
];
10771081
}
10781082

0 commit comments

Comments
 (0)