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

Breadcrumb

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

class SnakeCaseToCamelCaseNameConverter

Underscore to camelCase name converter.

@author Kévin Dunglas <kevin@dunglas.dev>

Hierarchy

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

Expanded class hierarchy of SnakeCaseToCamelCaseNameConverter

File

vendor/symfony/serializer/NameConverter/SnakeCaseToCamelCaseNameConverter.php, line 21

Namespace

Symfony\Component\Serializer\NameConverter
View source
final readonly class SnakeCaseToCamelCaseNameConverter implements NameConverterInterface {
    
    /**
     * Require all properties to be written in camelCase.
     */
    public const REQUIRE_CAMEL_CASE_PROPERTIES = 'require_camel_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 array<string, mixed> $context
     */
    public function normalize(string $propertyName, ?string $class = null, ?string $format = null, array $context = []) : string {
        if (null !== $this->attributes && !\in_array($propertyName, $this->attributes, true)) {
            return $propertyName;
        }
        $camelCasedName = preg_replace_callback('/(^|_|\\.)+(.)/', fn($match) => ('.' === $match[1] ? '_' : '') . strtoupper($match[2]), $propertyName);
        if ($this->lowerCamelCase) {
            $camelCasedName = lcfirst($camelCasedName);
        }
        return $camelCasedName;
    }
    
    /**
     * @param class-string|null    $class
     * @param array<string, mixed> $context
     */
    public function denormalize(string $propertyName, ?string $class = null, ?string $format = null, array $context = []) : string {
        if (($context[self::REQUIRE_CAMEL_CASE_PROPERTIES] ?? false) && $propertyName !== $this->normalize($propertyName, $class, $format, $context)) {
            throw new UnexpectedPropertyException($propertyName);
        }
        $snakeCased = strtolower(preg_replace('/[A-Z]/', '_\\0', lcfirst($propertyName)));
        if (null === $this->attributes || \in_array($snakeCased, $this->attributes, true)) {
            return $snakeCased;
        }
        return $propertyName;
    }

}

Members

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