4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Serializer\NameConverter;
15 * CamelCase to Underscore name converter.
17 * @author Kévin Dunglas <dunglas@gmail.com>
19 class CamelCaseToSnakeCaseNameConverter implements NameConverterInterface
22 private $lowerCamelCase;
25 * @param array|null $attributes The list of attributes to rename or null for all attributes
26 * @param bool $lowerCamelCase Use lowerCamelCase style
28 public function __construct(array $attributes = null, $lowerCamelCase = true)
30 $this->attributes = $attributes;
31 $this->lowerCamelCase = $lowerCamelCase;
37 public function normalize($propertyName)
39 if (null === $this->attributes || \in_array($propertyName, $this->attributes)) {
40 return strtolower(preg_replace('/[A-Z]/', '_\\0', lcfirst($propertyName)));
49 public function denormalize($propertyName)
51 $camelCasedName = preg_replace_callback('/(^|_|\.)+(.)/', function ($match) {
52 return ('.' === $match[1] ? '_' : '').strtoupper($match[2]);
55 if ($this->lowerCamelCase) {
56 $camelCasedName = lcfirst($camelCasedName);
59 if (null === $this->attributes || \in_array($camelCasedName, $this->attributes)) {
60 return $camelCasedName;