Skip to content

Commit

Permalink
bug #57497 [String] Fixed u()->snake(), b()->snake() and s()->snake()…
Browse files Browse the repository at this point in the history
… methods (arczinosek)

This PR was merged into the 5.4 branch.

Discussion
----------

[String] Fixed u()->snake(), b()->snake() and s()->snake() methods

| Q             | A
| ------------- | ---
| Branch?       | 5.4
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Issues        | Fix #57464
| License       | MIT

[String] ByteString::snake and AbstractUnitcodeString::snake methods doesn't convert all uppercase words in the right way.

For example, string "GREAT SYMFONY" is converted to "greatsymfony" instead of "great_symfony".

Commits
-------

f93113e80e [String] Fix *String::snake methods
  • Loading branch information
nicolas-grekas committed Jun 28, 2024
2 parents a8b1c35 + 5fe083e commit 065a961
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 4 deletions.
4 changes: 2 additions & 2 deletions AbstractUnicodeString.php
Original file line number Diff line number Diff line change
Expand Up @@ -366,8 +366,8 @@ public function reverse(): parent

public function snake(): parent
{
$str = $this->camel();
$str->string = mb_strtolower(preg_replace(['/(\p{Lu}+)(\p{Lu}\p{Ll})/u', '/([\p{Ll}0-9])(\p{Lu})/u'], '\1_\2', $str->string), 'UTF-8');
$str = clone $this;
$str->string = str_replace(' ', '_', mb_strtolower(preg_replace(['/(\p{Lu}+)(\p{Lu}\p{Ll})/u', '/([\p{Ll}0-9])(\p{Lu})/u'], '\1 \2', $str->string), 'UTF-8'));

return $str;
}
Expand Down
4 changes: 2 additions & 2 deletions ByteString.php
Original file line number Diff line number Diff line change
Expand Up @@ -366,8 +366,8 @@ public function slice(int $start = 0, ?int $length = null): parent

public function snake(): parent
{
$str = $this->camel();
$str->string = strtolower(preg_replace(['/([A-Z]+)([A-Z][a-z])/', '/([a-z\d])([A-Z])/'], '\1_\2', $str->string));
$str = clone $this;
$str->string = str_replace(' ', '_', strtolower(preg_replace(['/([A-Z]+)([A-Z][a-z])/', '/([a-z\d])([A-Z])/'], '\1 \2', $str->string)));

return $str;
}
Expand Down
2 changes: 2 additions & 0 deletions Tests/AbstractAsciiTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -1077,6 +1077,8 @@ public static function provideSnake()
['symfony_is_great', 'symfonyIsGREAT'],
['symfony_is_really_great', 'symfonyIsREALLYGreat'],
['symfony', 'SYMFONY'],
['symfony_is_great', 'SYMFONY IS GREAT'],
['symfony_is_great', 'SYMFONY_IS_GREAT'],
];
}

Expand Down

0 comments on commit 065a961

Please sign in to comment.