8000 [String] improve `snake` method for unicode string. · symfony/symfony@9601b59 · GitHub
[go: up one dir, main page]

Skip to content

Commit 9601b59

Browse files
committed
[String] improve snake method for unicode string.
Better management of uppercase string and removal of special end-of-line characters.
1 parent de925bb commit 9601b59

File tree

1 file changed

+17
-4
lines changed

1 file changed

+17
-4
lines changed

src/Symfony/Component/String/AbstractUnicodeString.php

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -366,10 +366,23 @@ public function reverse(): parent
366366

367367
public function snake(): parent
368368
{
369-
$str = $this->camel();
370-
$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');
371-
372-
return $str;
369+
$str = clone $this;
370+
$matches = [];
371+
372+
preg_match_all(
373+
'/([\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',
374+
$str,
375+
$matches
376+
);
377+
378+
$strings = array_map(
379+
function (string $matchedString) {
380+
return mb_strtolower($matchedString);
381+
},
382+
$matches[0] ?? []
383+
);
384+
385+
return new static(implode('_', $strings));
373386
}
374387

375388
public function title(bool $allWords = false): parent

0 commit comments

Comments
 (0)
0