Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature #28505 [Serialized] allow configuring the serialized name of …
…properties through metadata (fbourigault) This PR was squashed before being merged into the 4.2-dev branch (closes #28505). Discussion ---------- [Serialized] allow configuring the serialized name of properties through metadata | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #15171 | License | MIT | Doc PR | symfony/symfony-docs#10422 This leverage the new `AdvancedNameConverterInterface` interface (#27021) to implement a name converter that relies on metadata. The name to use is configured per property using a `@SerializedName` annotation or the `serialized-name` XML attribute or the `serialized_name` key for YAML. This was exposed by @dunglas in #19374 (comment). # Framework integration For FramworkBundle integration, a ChainNameConverter could be added to allow users to use this name converter with a custom one. # To do - [x] add a CHANGELOG.md entry. - [x] add a fallback. - [x] add framework integration. - [x] add local caching to `MetadataAwareNameConverter`. - [x] add a doc PR. Commits ------- d1d1ceb [Serialized] allow configuring the serialized name of properties through metadata
- Loading branch information
Showing
22 changed files
with
516 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
src/Symfony/Component/Serializer/Annotation/SerializedName.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?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\Annotation; | ||
|
||
use Symfony\Component\Serializer\Exception\InvalidArgumentException; | ||
|
||
/** | ||
* Annotation class for @SerializedName(). | ||
* | ||
* @Annotation | ||
* @Target({"PROPERTY", "METHOD"}) | ||
* | ||
* @author Fabien Bourigault <bourigaultfabien@gmail.com> | ||
*/ | ||
final class SerializedName | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
private $serializedName; | ||
|
||
public function __construct(array $data) | ||
{ | ||
if (!isset($data['value'])) { | ||
throw new InvalidArgumentException(sprintf('Parameter of annotation "%s" should be set.', \get_class($this))); | ||
} | ||
|
||
if (!\is_string($data['value']) || empty($data['value'])) { | ||
throw new InvalidArgumentException(sprintf('Parameter of annotation "%s" must be a non-empty string.', \get_class($this))); | ||
} | ||
|
||
$this->serializedName = $data['value']; | ||
} | ||
|
||
public function getSerializedName(): string | ||
{ | ||
return $this->serializedName; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
119 changes: 119 additions & 0 deletions
119
src/Symfony/Component/Serializer/NameConverter/MetadataAwareNameConverter.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
<?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; | ||
|
||
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactoryInterface; | ||
|
||
/** | ||
* @author Fabien Bourigault <bourigaultfabien@gmail.com> | ||
*/ | ||
final class MetadataAwareNameConverter implements AdvancedNameConverterInterface | ||
{ | ||
private $metadataFactory; | ||
|
||
/** | ||
* @var NameConverterInterface|AdvancedNameConverterInterface|null | ||
*/ | ||
private $fallbackNameConverter; | ||
|
||
private static $normalizeCache = array(); | ||
|
||
private static $denormalizeCache = array(); | ||
|
||
private static $attributesMetadataCache = array(); | ||
|
||
public function __construct(ClassMetadataFactoryInterface $metadataFactory, NameConverterInterface $fallbackNameConverter = null) | ||
{ | ||
$this->metadataFactory = $metadataFactory; | ||
$this->fallbackNameConverter = $fallbackNameConverter; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function normalize($propertyName, string $class = null, string $format = null, array $context = array()) | ||
{ | ||
if (null === $class) { | ||
return $this->normalizeFallback($propertyName, $class, $format, $context); | ||
} | ||
|
||
if (!isset(self::$normalizeCache[$class][$propertyName])) { | ||
self::$normalizeCache[$class][$propertyName] = $this->getCacheValueForNormalization($propertyName, $class); | ||
} | ||
|
||
return self::$normalizeCache[$class][$propertyName] ?? $this->normalizeFallback($propertyName, $class, $format, $context); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function denormalize($propertyName, string $class = null, string $format = null, array $context = array()) | ||
{ | ||
if (null === $class) { | ||
return $this->denormalizeFallback($propertyName, $class, $format, $context); | ||
} | ||
|
||
if (!isset(self::$denormalizeCache[$class][$propertyName])) { | ||
self::$denormalizeCache[$class][$propertyName] = $this->getCacheValueForDenormalization($propertyName, $class); | ||
} | ||
|
||
return self::$denormalizeCache[$class][$propertyName] ?? $this->denormalizeFallback($propertyName, $class, $format, $context); | ||
} | ||
|
||
private function getCacheValueForNormalization(string $propertyName, string $class): ?string | ||
{ | ||
if (!$this->metadataFactory->hasMetadataFor($class)) { | ||
return null; | ||
} | ||
|
||
return $this->metadataFactory->getMetadataFor($class)->getAttributesMetadata()[$propertyName]->getSerializedName() ?? null; | ||
} | ||
|
||
private function normalizeFallback(string $propertyName, string $class = null, string $format = null, array $context = array()): string | ||
{ | ||
return $this->fallbackNameConverter ? $this->fallbackNameConverter->normalize($propertyName, $class, $format, $context) : $propertyName; | ||
} | ||
|
||
private function getCacheValueForDenormalization(string $propertyName, string $class): ?string | ||
{ | ||
if (!isset(self::$attributesMetadataCache[$class])) { | ||
self::$attributesMetadataCache[$class] = $this->getCacheValueForAttributesMetadata($class); | ||
} | ||
|
||
return self::$attributesMetadataCache[$class][$propertyName] ?? null; | ||
} | ||
|
||
private function denormalizeFallback(string $propertyName, string $class = null, string $format = null, array $context = array()): string | ||
{ | ||
return $this->fallbackNameConverter ? $this->fallbackNameConverter->denormalize($propertyName, $class, $format, $context) : $propertyName; | ||
} | ||
|
||
private function getCacheValueForAttributesMetadata(string $class): array | ||
{ | ||
if (!$this->metadataFactory->hasMetadataFor($class)) { | ||
return array(); | ||
} | ||
|
||
$classMetadata = $this->metadataFactory->getMetadataFor($class); | ||
|
||
$cache = array(); | ||
foreach ($classMetadata->getAttributesMetadata() as $name => $metadata) { | ||
if (null === $metadata->getSerializedName()) { | ||
continue; | ||
} | ||
|
||
$cache[$metadata->getSerializedName()] = $name; | ||
} | ||
|
||
return $cache; | ||
} | ||
} |
Oops, something went wrong.