Skip to content

Commit

Permalink
bug #47423 [String] CamelCase/SnakeCase on uppercase word (mpiot)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
fabpot committed Sep 1, 2022
2 parents 1864eaa + c3cae1f commit c61dfca
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/Symfony/Component/String/AbstractUnicodeString.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ public function ascii(array $rules = []): self
public function camel(): parent
{
$str = clone $this;
$str->string = str_replace(' ', '', preg_replace_callback('/\b./u', static function ($m) use (&$i) {
$str->string = str_replace(' ', '', preg_replace_callback('/\b.(?![A-Z]{2,})/u', static function ($m) use (&$i) {
return 1 === ++$i ? ('İ' === $m[0] ? 'i̇' : mb_strtolower($m[0], 'UTF-8')) : mb_convert_case($m[0], \MB_CASE_TITLE, 'UTF-8');
}, preg_replace('/[^\pL0-9]++/u', ' ', $this->string)));

Expand Down
5 changes: 4 additions & 1 deletion src/Symfony/Component/String/ByteString.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,10 @@ public function append(string ...$suffix): parent
public function camel(): parent
{
$str = clone $this;
$str->string = lcfirst(str_replace(' ', '', ucwords(preg_replace('/[^a-zA-Z0-9\x7f-\xff]++/', ' ', $this->string))));

$parts = explode(' ', trim(ucwords(preg_replace('/[^a-zA-Z0-9\x7f-\xff]++/', ' ', $this->string))));
$parts[0] = 1 !== \strlen($parts[0]) && ctype_upper($parts[0]) ? $parts[0] : lcfirst($parts[0]);
$str->string = implode('', $parts);

return $str;
}
Expand Down
4 changes: 4 additions & 0 deletions src/Symfony/Component/String/Tests/AbstractAsciiTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -1042,11 +1042,13 @@ public static function provideCamel()
return [
['', ''],
['xY', 'x_y'],
['xuYo', 'xu_yo'],
['symfonyIsGreat', 'symfony_is_great'],
['symfony5IsGreat', 'symfony_5_is_great'],
['symfonyIsGreat', 'Symfony is great'],
['symfonyIsAGreatFramework', 'Symfony is a great framework'],
['symfonyIsGREAT', '*Symfony* is GREAT!!'],
['SYMFONY', 'SYMFONY'],
];
}

Expand All @@ -1066,13 +1068,15 @@ public static function provideSnake()
['', ''],
['x_y', 'x_y'],
['x_y', 'X_Y'],
['xu_yo', 'xu_yo'],
['symfony_is_great', 'symfonyIsGreat'],
['symfony5_is_great', 'symfony5IsGreat'],
['symfony5is_great', 'symfony5isGreat'],
['symfony_is_great', 'Symfony is great'],
['symfony_is_a_great_framework', 'symfonyIsAGreatFramework'],
['symfony_is_great', 'symfonyIsGREAT'],
['symfony_is_really_great', 'symfonyIsREALLYGreat'],
['symfony', 'SYMFONY'],
];
}

Expand Down

0 comments on commit c61dfca

Please sign in to comment.