Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b9f10a1

Browse files
committed
[Serializer] Allow to access extra infos in name converters
1 parent 0e9ded3 commit b9f10a1

File tree

5 files changed

+61
-7
lines changed

5 files changed

+61
-7
lines changed

src/Symfony/Component/Serializer/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
4.2.0
5+
-----
6+
7+
* added `AdvancedNameConverterInterface` to access to the class, the format and the context in a name converter
8+
49
4.1.0
510
-----
611

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 <[email protected]>
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 format and the context in the property name converters.
16+
*
17+
* @author Kévin Dunglas <[email protected]>
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
@@ -341,7 +341,7 @@ protected function instantiateObject(array &$data, $class, array &$context, \Ref
341341
$params = array();
342342
foreach ($constructorParameters as $constructorParameter) {
343343
$paramName = $constructorParameter->name;
344-
$key = $this->nameConverter ? $this->nameConverter->normalize($paramName) : $paramName;
344+
$key = $this->nameConverter ? $this->nameConverter->normalize($paramName, $class, $format, $context) : $paramName;
345345

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

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ public function normalize($object, $format = null, array $context = array())
8585
$data = array();
8686
$stack = array();
8787
$attributes = $this->getAttributes($object, $format, $context);
88-
$class = get_class($object);
88+
$class = \get_class($object);
8989
$attributesMetadata = $this->classMetadataFactory ? $this->classMetadataFactory->getMetadataFor($class)->getAttributesMetadata() : null;
9090

9191
foreach ($attributes as $attribute) {
@@ -107,15 +107,15 @@ public function normalize($object, $format = null, array $context = array())
107107
$stack[$attribute] = $attributeValue;
108108
}
109109

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

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

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

121121
return $data;
@@ -245,7 +245,7 @@ public function denormalize($data, $class, $format = null, array $context = arra
245245

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

251251
if ((false !== $allowedAttributes && !in_array($attribute, $allowedAttributes)) || !$this->isAllowedAttribute($class, $attribute, $format, $context)) {
@@ -362,10 +362,10 @@ private function validateAndDenormalize(string $currentClass, string $attribute,
362362
*
363363
* @param mixed $attributeValue
364364
*/
365-
private function updateData(array $data, string $attribute, $attributeValue): array
365+
private function updateData(array $data, string $attribute, $attributeValue, string $class, ?string $format, array $context): array
366366
{
367367
if ($this->nameConverter) {
368-
$attribute = $this->nameConverter->normalize($attribute);
368+
$attribute = $this->nameConverter->normalize($attribute, $class, $format, $context);
369369
}
370370

371371
$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
@@ -16,6 +16,7 @@
1616
use Symfony\Component\PropertyInfo\Extractor\PhpDocExtractor;
1717
use Symfony\Component\PropertyInfo\Extractor\ReflectionExtractor;
1818
use Symfony\Component\PropertyInfo\PropertyInfoExtractor;
19+
use Symfony\Component\Serializer\NameConverter\AdvancedNameConverterInterface;
1920
use Symfony\Component\Serializer\NameConverter\CamelCaseToSnakeCaseNameConverter;
2021
use Symfony\Component\Serializer\Normalizer\ArrayDenormalizer;
2122
use Symfony\Component\Serializer\Normalizer\DateTimeNormalizer;
@@ -852,6 +853,24 @@ public function testNormalizeSameObjectWithDifferentAttributes()
852853
),
853854
)));
854855
}
856+
857+
public function testAdvancedNameConverter()
858+
{
859+
$nameConverter = new class() implements AdvancedNameConverterInterface {
860+
public function normalize($propertyName, string $class = null, string $format = null, array $context = array())
861+
{
862+
return sprintf('%s-%s-%s-%s', $propertyName, $class, $format, $context['foo']);
863+
}
864+
865+
public function denormalize($propertyName, string $class = null, string $format = null, array $context = array())
866+
{
867+
return sprintf('%s-%s-%s-%s', $propertyName, $class, $format, $context['foo']);
868+
}
869+
};
870+
871+
$normalizer = new ObjectNormalizer(null, $nameConverter);
872+
$this->assertArrayHasKey('foo-Symfony\Component\Serializer\Tests\Normalizer\ObjectDummy-json-bar', $normalizer->normalize(new ObjectDummy(), 'json', array('foo' => 'bar')));
873+
}
855874
}
856875

857876
class ObjectDummy

0 commit comments

Comments
 (0)