Skip to content

[Serializer] add $class, $format and $context arguments to NameConverterInterface methods #54531

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Symfony/Component/Serializer/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
* @author Kévin Dunglas <dunglas@gmail.com>
* @author Aurélien Pillevesse <aurelienpillevesse@hotmail.fr>
*/
class CamelCaseToSnakeCaseNameConverter implements AdvancedNameConverterInterface
class CamelCaseToSnakeCaseNameConverter implements NameConverterInterface
{
/**
* Require all properties to be written in snake_case.
Expand All @@ -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)));
Expand All @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
/**
* @author Fabien Bourigault <bourigaultfabien@gmail.com>
*/
final class MetadataAwareNameConverter implements AdvancedNameConverterInterface
final class MetadataAwareNameConverter implements NameConverterInterface
{
/**
* @var array<string, array<string, string|null>>
Expand All @@ -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);
}
Expand All @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, mixed> $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<string, mixed> $context
*/
public function denormalize(string $propertyName): string;
public function denormalize(string $propertyName/* , ?string $class = null, ?string $format = null, array $context = [] */): string;
}
Loading