Skip to content

Commit

Permalink
minor #54531 [Serializer] add $class, $format and $context arguments …
Browse files Browse the repository at this point in the history
…to NameConverterInterface methods (xabbuh)

This PR was merged into the 7.1 branch.

Discussion
----------

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

| Q             | A
| ------------- | ---
| Branch?       | 7.1
| Bug fix?      | no
| New feature?  | no
| Deprecations? | no
| Issues        | Fix symfony/symfony#53898 (comment)
| License       | MIT

Commits
-------

d088047dfa add $class, $format and $context arguments to NameConverterInterface methods
  • Loading branch information
fabpot committed Apr 17, 2024
2 parents 11fd887 + 260e685 commit 309af65
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
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
10 changes: 7 additions & 3 deletions NameConverter/CamelCaseToSnakeCaseNameConverter.php
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
14 changes: 11 additions & 3 deletions NameConverter/MetadataAwareNameConverter.php
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
12 changes: 10 additions & 2 deletions NameConverter/NameConverterInterface.php
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;
}

0 comments on commit 309af65

Please sign in to comment.