Chris@0: Chris@0: * Chris@0: * For the full copyright and license information, please view the LICENSE Chris@0: * file that was distributed with this source code. Chris@0: */ Chris@0: Chris@0: namespace Symfony\Component\Serializer\NameConverter; Chris@0: Chris@0: /** Chris@0: * CamelCase to Underscore name converter. Chris@0: * Chris@0: * @author Kévin Dunglas Chris@0: */ Chris@0: class CamelCaseToSnakeCaseNameConverter implements NameConverterInterface Chris@0: { Chris@0: private $attributes; Chris@0: private $lowerCamelCase; Chris@0: Chris@0: /** Chris@17: * @param array|null $attributes The list of attributes to rename or null for all attributes Chris@0: * @param bool $lowerCamelCase Use lowerCamelCase style Chris@0: */ Chris@0: public function __construct(array $attributes = null, $lowerCamelCase = true) Chris@0: { Chris@0: $this->attributes = $attributes; Chris@0: $this->lowerCamelCase = $lowerCamelCase; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function normalize($propertyName) Chris@0: { Chris@14: if (null === $this->attributes || \in_array($propertyName, $this->attributes)) { Chris@14: return strtolower(preg_replace('/[A-Z]/', '_\\0', lcfirst($propertyName))); Chris@0: } Chris@0: Chris@0: return $propertyName; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function denormalize($propertyName) Chris@0: { Chris@0: $camelCasedName = preg_replace_callback('/(^|_|\.)+(.)/', function ($match) { Chris@0: return ('.' === $match[1] ? '_' : '').strtoupper($match[2]); Chris@0: }, $propertyName); Chris@0: Chris@0: if ($this->lowerCamelCase) { Chris@0: $camelCasedName = lcfirst($camelCasedName); Chris@0: } Chris@0: Chris@14: if (null === $this->attributes || \in_array($camelCasedName, $this->attributes)) { Chris@0: return $camelCasedName; Chris@0: } Chris@0: Chris@0: return $propertyName; Chris@0: } Chris@0: }