8000 [Serializer] Allow to access extra infos in name converters · symfony/symfony@57fe017 · GitHub
[go: up one dir, main page]

Skip to content

Commit 57fe017

Browse files
dunglasfabpot
authored andcommitted
[Serializer] Allow to access extra infos in name converters
1 parent 9ad492f commit 57fe017

File tree

5 files changed

+56
-6
lines changed

5 files changed

+56
-6
lines changed

src/Symfony/Component/Serializer/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ CHANGELOG
88
* added support for XML comment encoding (encoding `['#comment' => ' foo ']` results `<!-- foo -->`)
99
* added optional `int[] $encoderIgnoredNodeTypes` argument to `XmlEncoder::__construct` to configure node types to be
1010
ignored during encoding.
11+
* added `AdvancedNameConverterInterface` to access the class, the format and the context in a name converter
1112

1213
4.1.0
1314
-----
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Serializer\NameConverter;
13+
14+
/**
15+
* Gives access to the class, the format and the context in the property name converters.
16+
*
17+
* @author Kévin Dunglas <dunglas@gmail.com>
18+
*/
19+
interface AdvancedNameConverterInterface extends NameConverterInterface
20+
{
21+
/**
22+
* {@inheritdoc}
23+
*/
24+
public function normalize($propertyName, string $class = null, string $format = null, array $context = array());
25+
26+
/**
27+
* {@inheritdoc}
28+
*/
29+
public function denormalize($propertyName, string $class = null, string $format = null, array $context = array());
30+
}

src/Symfony/Component/Serializer/Normalizer/AbstractNormalizer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,7 @@ protected function instantiateObject(array &$data, $class, array &$context, \Ref
348348
$params = array();
349349
foreach ($constructorParameters as $constructorParameter) {
350350
$paramName = $constructorParameter->name;
351-
$key = $this->nameConverter ? $this->nameConverter->normalize($paramName) : $paramName;
351+
$key = $this->nameConverter ? $this->nameConverter->normalize($paramName, $class, $format, $context) : $paramName;
352352

353353
$allowed = false === $allowedAttributes || \in_array($paramName, $allowedAttributes);
354354
$ignored = !$this->isAllowedAttribute($class, $paramName, $format, $context);

src/Symfony/Component/Serializer/Normalizer/AbstractObjectNormalizer.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -108,15 +108,15 @@ public function normalize($object, $format = null, array $context = array())
108108
$stack[$attribute] = $attributeValue;
109109
}
110110

111-
$data = $this->updateData($data, $attribute, $attributeValue);
111+
$data = $this->updateData($data, $attribute, $attributeValue, $class, $format, $context);
112112
}
113113

114114
foreach ($stack as $attribute => $attributeValue) {
115115
if (!$this->serializer instanceof NormalizerInterface) {
116116
throw new LogicException(sprintf('Cannot normalize attribute "%s" because the injected serializer is not a normalizer', $attribute));
117117
}
118118

119-
$data = $this->updateData($data, $attribute, $this->serializer->normalize($attributeValue, $format, $this->createChildContext($context, $attribute)));
119+
$data = $this->updateData($data, $attribute, $this->serializer->normalize($attributeValue, $format, $this->createChildContext($context, $attribute)), $class, $format, $context);
120120
}
121121

122122
return $data;
@@ -246,7 +246,7 @@ public function denormalize($data, $class, $format = null, array $context = arra
246246

247247
foreach ($normalizedData as $attribute => $value) {
248248
if ($this->nameConverter) {
249-
$attribute = $this->nameConverter->denormalize($attribute);
249+
$attribute = $this->nameConverter->denormalize($attribute, $class, $format, $context);
250250
}
251251

252252
if ((false !== $allowedAttributes && !\in_array($attribute, $allowedAttributes)) || !$this->isAllowedAttribute($class, $attribute, $format, $context)) {
@@ -400,10 +400,10 @@ private function getTypes(string $currentClass, string $attribute)
400400
*
401401
* @param mixed $attributeValue
402402
*/
403-
private function updateData(array $data, string $attribute, $attributeValue): array
403+
private function updateData(array $data, string $attribute, $attributeValue, string $class, ?string $format, array $context): array
404404
{
405405
if ($this->nameConverter) {
406-
$attribute = $this->nameConverter->normalize($attribute);
406+
$attribute = $this->nameConverter->normalize($attribute, $class, $format, $context);
407407
}
408408

409409
$data[$attribute] = $attributeValue;

src/Symfony/Component/Serializer/Tests/Normalizer/ObjectNormalizerTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use Symfony\Component\PropertyInfo\PropertyInfoExtractor;
1919
use Symfony\Compo F438 nent\Serializer\Mapping\Factory\ClassMetadataFactory;
2020
use Symfony\Component\Serializer\Mapping\Loader\AnnotationLoader;
21+
use Symfony\Component\Serializer\NameConverter\AdvancedNameConverterInterface;
2122
use Symfony\Component\Serializer\NameConverter\CamelCaseToSnakeCaseNameConverter;
2223
use Symfony\Component\Serializer\Normalizer\ArrayDenormalizer;
2324
use Symfony\Component\Serializer\Normalizer\DateTimeNormalizer;
@@ -861,6 +862,24 @@ public function testNormalizeSameObjectWithDifferentAttributes()
861862
),
862863
)));
863864
}
865+
866+
public function testAdvancedNameConverter()
867+
{
868+
$nameConverter = new class() implements AdvancedNameConverterInterface {
869+
public function normalize($propertyName, string $class = null, string $format = null, array $context = array())
870+
{
871+
return sprintf('%s-%s-%s-%s', $propertyName, $class, $format, $context['foo']);
872+
}
873+
874+
public function denormalize($propertyName, string $class = null, string $format = null, array $context = array())
875+
{
876+
return sprintf('%s-%s-%s-%s', $propertyName, $class, $format, $context['foo']);
877+
}
878+
};
879+
880+
$normalizer = new ObjectNormalizer(null, $nameConverter);
881+
$this->assertArrayHasKey('foo-Symfony\Component\Serializer\Tests\Normalizer\ObjectDummy-json-bar', $normalizer->normalize(new ObjectDummy(), 'json', array('foo' => 'bar')));
882+
}
864883
}
865884

866885
class ObjectDummy

0 commit comments

Comments
 (0)
0