Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. CamelCaseToSnakeCaseNameConverter.php

class CamelCaseToSnakeCaseNameConverter

CamelCase to Underscore name converter.

@author Kévin Dunglas <dunglas@gmail.com> @author Aurélien Pillevesse <aurelienpillevesse@hotmail.fr>

Hierarchy

  • class \Symfony\Component\Serializer\NameConverter\CamelCaseToSnakeCaseNameConverter implements \Symfony\Component\Serializer\NameConverter\NameConverterInterface

Expanded class hierarchy of CamelCaseToSnakeCaseNameConverter

File

vendor/symfony/serializer/NameConverter/CamelCaseToSnakeCaseNameConverter.php, line 22

Namespace

Symfony\Component\Serializer\NameConverter
View source
class CamelCaseToSnakeCaseNameConverter implements NameConverterInterface {
    
    /**
     * Require all properties to be written in snake_case.
     */
    public const REQUIRE_SNAKE_CASE_PROPERTIES = 'require_snake_case_properties';
    
    /**
     * @param string[]|null $attributes     The list of attributes to rename or null for all attributes
     * @param bool          $lowerCamelCase Use lowerCamelCase style
     */
    public function __construct(?array $attributes = null, bool $lowerCamelCase = true) {
    }
    
    /**
     * @param class-string|null    $class
     * @param string|null          $format
     * @param array<string, mixed> $context
     */
    public function normalize(string $propertyName) : string {
        if (null === $this->attributes || \in_array($propertyName, $this->attributes, true)) {
            return strtolower(preg_replace('/[A-Z]/', '_\\0', lcfirst($propertyName)));
        }
        return $propertyName;
    }
    
    /**
     * @param class-string|null    $class
     * @param string|null          $format
     * @param array<string, mixed> $context
     */
    public function denormalize(string $propertyName) : string {
        $class = 1 < \func_num_args() ? func_get_arg(1) : null;
        $format = 2 < \func_num_args() ? func_get_arg(2) : null;
        $context = 3 < \func_num_args() ? func_get_arg(3) : [];
        if (($context[self::REQUIRE_SNAKE_CASE_PROPERTIES] ?? false) && $propertyName !== $this->normalize($propertyName, $class, $format, $context)) {
            throw new UnexpectedPropertyException($propertyName);
        }
        $camelCasedName = preg_replace_callback('/(^|_|\\.)+(.)/', fn($match) => ('.' === $match[1] ? '_' : '') . strtoupper($match[2]), $propertyName);
        if ($this->lowerCamelCase) {
            $camelCasedName = lcfirst($camelCasedName);
        }
        if (null === $this->attributes || \in_array($camelCasedName, $this->attributes, true)) {
            return $camelCasedName;
        }
        return $propertyName;
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title
CamelCaseToSnakeCaseNameConverter::denormalize public function Overrides NameConverterInterface::denormalize
CamelCaseToSnakeCaseNameConverter::normalize public function Overrides NameConverterInterface::normalize
CamelCaseToSnakeCaseNameConverter::REQUIRE_SNAKE_CASE_PROPERTIES public constant Require all properties to be written in snake_case.
CamelCaseToSnakeCaseNameConverter::__construct public function
RSS feed
Powered by Drupal