[Serializer] Reduce complexity of NameConverter

Cleaner and faster implementation of camelcase normalization.
Speed improvement is particularly visible in long string input.
This commit is contained in:
Gabriel Ostrolucký 2017-02-19 23:03:20 +01:00 committed by GitHub
parent 64ec5c5faf
commit 50ca944278

View File

@ -44,19 +44,7 @@ class CamelCaseToSnakeCaseNameConverter implements NameConverterInterface
public function normalize($propertyName)
{
if (null === $this->attributes || in_array($propertyName, $this->attributes)) {
$lcPropertyName = lcfirst($propertyName);
$snakeCasedName = '';
$len = strlen($lcPropertyName);
for ($i = 0; $i < $len; ++$i) {
if (ctype_upper($lcPropertyName[$i])) {
$snakeCasedName .= '_'.strtolower($lcPropertyName[$i]);
} else {
$snakeCasedName .= strtolower($lcPropertyName[$i]);
}
}
return $snakeCasedName;
return strtolower(preg_replace('/[A-Z]/', '_\\0', lcfirst($propertyName)));
}
return $propertyName;