8000 [Serializer] Allow to access extra infos in name converters by dunglas · Pull Request #27021 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Serializer] Allow to access extra infos in name converters #27021

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
Sep 4, 2018
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 @@ -8,6 +8,7 @@ CHANGELOG
* added support for XML comment encoding (encoding `['#comment' => ' foo ']` results `<!-- foo -->`)
* added optional `int[] $encoderIgnoredNodeTypes` argument to `XmlEncoder::__construct` to configure node types to be
ignored during encoding.
* added `AdvancedNameConverterInterface` to access the class, the format and the context in a name converter

4.1.0
-----
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Serializer\NameConverter;

/**
* Gives access to the class, the format and the context in the property name converters.
*
* @author Kévin Dunglas <dunglas@gmail.com>
*/
interface AdvancedNameConverterInterface extends NameConverterInterface
{
/**
* {@inheritdoc}
*/
public function normalize($propertyName, string $class = null, string $format = null, array $context = array());

/**
* {@inheritdoc}
*/
public function denormalize($propertyName, string $class = null, string $format = null, array $context = array());
}
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ protected function instantiateObject(array &$data, $class, array &$context, \Ref
$params = array();
foreach ($constructorParameters as $constructorParameter) {
$paramName = $constructorParameter->name;
$key = $this->nameConverter ? $this->nameConverter->normalize($paramName) : $paramName;
$key = $this->nameConverter ? $this->nameConverter->normalize($paramName, $class, $format, $context) : $paramName;

$allowed = false === $allowedAttributes || \in_array($paramName, $allowedAttributes);
$ignored = !$this->isAllowedAttribute($class, $paramName, $format, $context);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,15 +108,15 @@ public function normalize($object, $format = null, array $context = array())
$stack[$attribute] = $attributeValue;
}

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

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

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

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

foreach ($normalizedData as $attribute => $value) {
if ($this->nameConverter) {
$attribute = $this->nameConverter->denormalize($attribute);
$attribute = $this->nameConverter->denormalize($attribute, $class, $format, $context);
}

if ((false !== $allowedAttributes && !\in_array($attribute, $allowedAttributes)) || !$this->isAllowedAttribute($class, $attribute, $format, $context)) {
Expand Down Expand Up @@ -400,10 +400,10 @@ private function getTypes(string $currentClass, string $attribute)
*
* @param mixed $attributeValue
*/
private function updateData(array $data, string $attribute, $attributeValue): array
private function updateData(array $data, string $attribute, $attributeValue, string $class, ?string $format, array $context): array
{
if ($this->nameConverter) {
$attribute = $this->nameConverter->normalize($attribute);
$attribute = $this->nameConverter->normalize($attribute, $class, $format, $context);
}

$data[$attribute] = $attributeValue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Symfony\Component\PropertyInfo\PropertyInfoExtractor;
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactory;
use Symfony\Component\Serializer\Mapping\Loader\AnnotationLoader;
use Symfony\Component\Serializer\NameConverter\AdvancedNameConverterInterface;
use Symfony\Component\Serializer\NameConverter\CamelCaseToSnakeCaseNameConverter;
use Symfony\Component\Serializer\Normalizer\ArrayDenormalizer;
use Symfony\Component\Serializer\Normalizer\DateTimeNormalizer;
Expand Down Expand Up @@ -861,6 +862,24 @@ public function testNormalizeSameObjectWithDifferentAttributes()
),
)));
}

public function testAdvancedNameConverter()
{
$nameConverter = new class() implements AdvancedNameConverterInterface {
public function normalize($propertyName, string $class = null, string $format = null, array $context = array())
{
return sprintf('%s-%s-%s-%s', $propertyName, $class, $format, $context['foo']);
}

public function denormalize($propertyName, string $class = null, string $format = null, array $context = array())
{
return sprintf('%s-%s-%s-%s', $propertyName, $class, $format, $context['foo']);
}
};

$normalizer = new ObjectNormalizer(null, $nameConverter);
$this->assertArrayHasKey('foo-Symfony\Component\Serializer\Tests\Normalizer\ObjectDummy-json-bar', $normalizer->normalize(new ObjectDummy(), 'json', array('foo' => 'bar')));
}
}

class ObjectDummy
Expand Down
0