Skip to content

Commit c61dfca

Browse files
committed
bug #47423 [String] CamelCase/SnakeCase on uppercase word (mpiot)
This PR was submitted for the 6.2 branch but it was merged into the 5.4 branch instead. Discussion ---------- [String] CamelCase/SnakeCase on uppercase word | Q | A | ------------- | --- | Branch? | 6.1 | Bug fix? | yes | New feature? | no | Deprecations? | ~ | Tickets | Fix #47421 | License | MIT | Doc PR | The behavior of `->snake()` method from String component has changed in 6.1.4, now, using `u('SYMFONY')->snake()` return `s_ymfony` instead of `symfony` (#47185). After, some investigations, it seams the `->camel()` behavior is not exactly the expected one: `u('SYMFONY')->camel()` return `sYMFONY` instead of `SYMFONY`. This PR give theses behaviors: - CamelCase: - '' => '' - x_y => xY - xu_yo => xuYo - symfony_is_great => symfonyIsGreat - symfony_5_is_great => symfony5IsGreat - Symfony is great => symfonyIsGreat - Symfony is a great framework => symfonyIsAGreatFramework - \*Symfony\* is GREAT!! => symfonyIsGREAT - **SYMFONY => SYMFONY** - SnakeCase: - '' => '' - x_y => x_y - X_Y => x_y - xu_yo => xu_yo - symfonyIsGreat => symfony_is_great - symfony5IsGreat => symfony5_is_great - symfony5isGreat => symfony5is_great - Symfony is great => symfony_is_great - symfonyIsAGreatFramework => symfony_is_a_great_framework - symfonyIsGREAT => symfony_is_great - symfonyIsREALLYGreat => symfony_is_really_great - **SYMFONY => symfony** Commits ------- c3cae1f [String] CamelCase/SnakeCase on uppercase word
2 parents 1864eaa + c3cae1f commit c61dfca

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
@@ -162,7 +162,7 @@ public function ascii(array $rules = []): self
162162
public function camel(): parent
163163
{
164164
$str = clone $this;
165-
$str->string = str_replace(' ', '', preg_replace_callback('/\b./u', static function ($m) use (&$i) {
165+
$str->string = str_replace(' ', '', preg_replace_callback('/\b.(?![A-Z]{2,})/u', static function ($m) use (&$i) {
166166
return 1 === ++$i ? ('İ' === $m[0] ? '' : mb_strtolower($m[0], 'UTF-8')) : mb_convert_case($m[0], \MB_CASE_TITLE, 'UTF-8');
167167
}, preg_replace('/[^\pL0-9]++/u', ' ', $this->string)));
168168

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): parent
103103
public function camel(): parent
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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy