Skip to content

Commit 50ca944

Browse files
authored
[Serializer] Reduce complexity of NameConverter
Cleaner and faster implementation of camelcase normalization. Speed improvement is particularly visible in long string input.
1 parent 64ec5c5 commit 50ca944

File tree

1 file changed

+1
-13
lines changed

1 file changed

+1
-13
lines changed

src/Symfony/Component/Serializer/NameConverter/CamelCaseToSnakeCaseNameConverter.php

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -44,19 +44,7 @@ public function __construct(array $attributes = null, $lowerCamelCase = true)
4444
public function normalize($propertyName)
4545
{
4646
if (null === $this->attributes || in_array($propertyName, $this->attributes)) {
47-
$lcPropertyName = lcfirst($propertyName);
48-
$snakeCasedName = '';
49-
50-
$len = strlen($lcPropertyName);
51-
for ($i = 0; $i < $len; ++$i) {
52-
if (ctype_upper($lcPropertyName[$i])) {
53-
$snakeCasedName .= '_'.strtolower($lcPropertyName[$i]);
54-
} else {
55-
$snakeCasedName .= strtolower($lcPropertyName[$i]);
56-
}
57-
}
58-
59-
return $snakeCasedName;
47+
return strtolower(preg_replace('/[A-Z]/', '_\\0', lcfirst($propertyName)));
6048
}
6149

6250
return $propertyName;

0 commit comments

Comments
 (0)