From d088047dfa7d417a25348aee961fc96ff00201d8 Mon Sep 17 00:00:00 2001 From: Christian Flothmann Date: Tue, 9 Apr 2024 10:33:09 +0200 Subject: [PATCH] add $class, $format and $context arguments to NameConverterInterface methods --- src/Symfony/Component/Serializer/CHANGELOG.md | 1 + .../CamelCaseToSnakeCaseNameConverter.php | 10 +++++++--- .../NameConverter/MetadataAwareNameConverter.php | 14 +++++++++++--- .../NameConverter/NameConverterInterface.php | 12 ++++++++++-- 4 files changed, 29 insertions(+), 8 deletions(-) diff --git a/src/Symfony/Component/Serializer/CHANGELOG.md b/src/Symfony/Component/Serializer/CHANGELOG.md index 9e495db1c8b47..4a84fa2c7719d 100644 --- a/src/Symfony/Component/Serializer/CHANGELOG.md +++ b/src/Symfony/Component/Serializer/CHANGELOG.md @@ -4,6 +4,7 @@ CHANGELOG 7.1 --- + * Add arguments `$class`, `$format` and `$context` to `NameConverterInterface::normalize()` and `NameConverterInterface::denormalize()` * Add `DateTimeNormalizer::CAST_KEY` context option * Add `Default` and "class name" default groups * Add `AbstractNormalizer::FILTER_BOOL` context option diff --git a/src/Symfony/Component/Serializer/NameConverter/CamelCaseToSnakeCaseNameConverter.php b/src/Symfony/Component/Serializer/NameConverter/CamelCaseToSnakeCaseNameConverter.php index 8c0b53157f4cd..ad1e3cc9d7b76 100644 --- a/src/Symfony/Component/Serializer/NameConverter/CamelCaseToSnakeCaseNameConverter.php +++ b/src/Symfony/Component/Serializer/NameConverter/CamelCaseToSnakeCaseNameConverter.php @@ -19,7 +19,7 @@ * @author Kévin Dunglas * @author Aurélien Pillevesse */ -class CamelCaseToSnakeCaseNameConverter implements AdvancedNameConverterInterface +class CamelCaseToSnakeCaseNameConverter implements NameConverterInterface { /** * Require all properties to be written in snake_case. @@ -36,7 +36,7 @@ public function __construct( ) { } - public function normalize(string $propertyName, ?string $class = null, ?string $format = null, array $context = []): string + 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 strtolower(preg_replace('/[A-Z]/', '_\\0', lcfirst($propertyName))); @@ -45,8 +45,12 @@ public function normalize(string $propertyName, ?string $class = null, ?string $ return $propertyName; } - public function denormalize(string $propertyName, ?string $class = null, ?string $format = null, array $context = []): string + public function denormalize(string $propertyName/* , ?string $class = null, ?string $format = null, array $context = [] */): 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); } diff --git a/src/Symfony/Component/Serializer/NameConverter/MetadataAwareNameConverter.php b/src/Symfony/Component/Serializer/NameConverter/MetadataAwareNameConverter.php index 327d92dc1b1c3..5366b57b9ea23 100644 --- a/src/Symfony/Component/Serializer/NameConverter/MetadataAwareNameConverter.php +++ b/src/Symfony/Component/Serializer/NameConverter/MetadataAwareNameConverter.php @@ -18,7 +18,7 @@ /** * @author Fabien Bourigault */ -final class MetadataAwareNameConverter implements AdvancedNameConverterInterface +final class MetadataAwareNameConverter implements NameConverterInterface { /** * @var array> @@ -41,8 +41,12 @@ public function __construct( ) { } - public function normalize(string $propertyName, ?string $class = null, ?string $format = null, array $context = []): string + public function normalize(string $propertyName/* , ?string $class = null, ?string $format = null, array $context = [] */): 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 (null === $class) { return $this->normalizeFallback($propertyName, $class, $format, $context); } @@ -54,8 +58,12 @@ public function normalize(string $propertyName, ?string $class = null, ?string $ return self::$normalizeCache[$class][$propertyName] ?? $this->normalizeFallback($propertyName, $class, $format, $context); } - public function denormalize(string $propertyName, ?string $class = null, ?string $format = null, array $context = []): string + public function denormalize(string $propertyName/* , ?string $class = null, ?string $format = null, array $context = [] */): 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 (null === $class) { return $this->denormalizeFallback($propertyName, $class, $format, $context); } diff --git a/src/Symfony/Component/Serializer/NameConverter/NameConverterInterface.php b/src/Symfony/Component/Serializer/NameConverter/NameConverterInterface.php index aba69a49e6294..d6bfeceb46c6d 100644 --- a/src/Symfony/Component/Serializer/NameConverter/NameConverterInterface.php +++ b/src/Symfony/Component/Serializer/NameConverter/NameConverterInterface.php @@ -20,11 +20,19 @@ interface NameConverterInterface { /** * Converts a property name to its normalized value. + * + * @param class-string|null $class + * @param string|null $format + * @param array $context */ - public function normalize(string $propertyName): string; + public function normalize(string $propertyName/* , ?string $class = null, ?string $format = null, array $context = [] */): string; /** * Converts a property name to its denormalized value. + * + * @param class-string|null $class + * @param string|null $format + * @param array $context */ - public function denormalize(string $propertyName): string; + public function denormalize(string $propertyName/* , ?string $class = null, ?string $format = null, array $context = [] */): string; }