Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions src/Symfony/Component/String/AbstractUnicodeString.php
Original file line number Diff line number Diff line change
Expand Up @@ -366,8 +366,23 @@ 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;
$matches = [];

preg_match_all(
'/([\p{Ll}0-9]+(?=\p{Lu}))|(\p{Lu}[\p{Ll}0-9]+)|(\p{Lu}+(?=\p{Lu}\p{Ll}))|((?<=\w)?[\p{Ll}\p{Lu}0-9]+(?=\w)?)/u',
$str->string,
$matches
);

$strings = array_map(
function (string $matchedString) {
return mb_strtolower($matchedString, 'UTF-8');
},
$matches[0] ?? []
);

$str->string = implode('_', $strings);

return $str;
}
Expand Down
19 changes: 17 additions & 2 deletions src/Symfony/Component/String/ByteString.php
Original file line number Diff line number Diff line change
Expand Up @@ -366,8 +366,23 @@ 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;
$matches = [];

preg_match_all(
'/([a-z0-9]+(?=[A-Z]))|([A-Z][a-z0-9]+)|([A-Z]+(?=[A-Z][a-z]))|((?<=\w)?[a-zA-Z0-9]+(?=\w)?)/',
$str->string,
$matches
);

$strings = array_map(
function (string $matchedString) {
return strtolower($matchedString);
},
$matches[0] ?? []
);

$str->string = implode('_', $strings);

return $str;
}
Expand Down
12 changes: 7 additions & 5 deletions src/Symfony/Component/String/Tests/AbstractAsciiTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -1080,12 +1080,14 @@ public static function provideSnake()
['symfony_is_great', 'symfonyIsGREAT'],
['symfony_is_really_great', 'symfonyIsREALLYGreat'],
['symfony', 'SYMFONY'],
['symfonyisgreat', 'SYMFONY IS GREAT'],
['symfonyisgreat', 'SYMFONY_IS_GREAT'],
['symfony_is_great', 'SYMFONY IS GREAT'],
['symfony_is_great', 'SYMFONY_IS_GREAT'],
['symfony_is_great', 'symfony is great'],
['symfonyisgreat', 'SYMFONY IS GREAT'],
['symfonyisgreat', 'SYMFONY _ IS _ GREAT'],
['symfony_isgreat', 'Symfony IS GREAT!'],
['symfony_is_great', 'SYMFONY IS GREAT'],
['symfony_is_great', 'SYMFONY _ IS _ GREAT'],
['symfony_is_great', 'Symfony IS GREAT!'],
['123_customer_with_special_name', '123-customer,with/special#name'],
['this_value_should_be_false', 'This value should be false.'],
];
}

Expand Down