From 5a221978ed0f66182caaf9401fcedfc6a25a8a07 Mon Sep 17 00:00:00 2001 From: Aleksei Lebedev <1329824+LastDragon-ru@users.noreply.github.com> Date: Thu, 2 Nov 2023 11:51:44 +0400 Subject: [PATCH] feat(serializer): `VersionMap` attribute to add version for `Serializable`. --- .../serializer/src/Attributes/VersionMap.php | 16 +++++++++++ .../src/Metadata/MetadataFactoryTest.php | 28 +++++++++++++++++++ 2 files changed, 44 insertions(+) create mode 100644 packages/serializer/src/Attributes/VersionMap.php diff --git a/packages/serializer/src/Attributes/VersionMap.php b/packages/serializer/src/Attributes/VersionMap.php new file mode 100644 index 000000000..08bbd2cbf --- /dev/null +++ b/packages/serializer/src/Attributes/VersionMap.php @@ -0,0 +1,16 @@ + $mapping + */ + public function __construct(array $mapping) { + parent::__construct('$v', $mapping); + } +} diff --git a/packages/serializer/src/Metadata/MetadataFactoryTest.php b/packages/serializer/src/Metadata/MetadataFactoryTest.php index 0ca7d8b59..ffdb323be 100644 --- a/packages/serializer/src/Metadata/MetadataFactoryTest.php +++ b/packages/serializer/src/Metadata/MetadataFactoryTest.php @@ -3,6 +3,7 @@ namespace LastDragon_ru\LaraASP\Serializer\Metadata; use JsonSerializable; +use LastDragon_ru\LaraASP\Serializer\Attributes\VersionMap; use LastDragon_ru\LaraASP\Serializer\Testing\Package\TestCase; use PHPUnit\Framework\Attributes\CoversClass; use Symfony\Component\PropertyInfo\Type; @@ -27,6 +28,7 @@ public function testGetMetadataFor(): void { $factory = new MetadataFactory(); $a = $factory->getMetadataFor(MetadataFactoryTest_A::class); $b = $factory->getMetadataFor(MetadataFactoryTest_B::class); + $c = $factory->getMetadataFor(MetadataFactoryTest_C::class); self::assertEquals( [ @@ -60,6 +62,19 @@ public function testGetMetadataFor(): void { 'mapping' => $b->getClassDiscriminatorMapping()?->getTypesMapping(), ], ); + self::assertEquals( + [ + 'property' => '$v', + 'mapping' => [ + 'a' => MetadataFactoryTest_A::class, + 'b' => MetadataFactoryTest_B::class, + ], + ], + [ + 'property' => $c->getClassDiscriminatorMapping()?->getTypeProperty(), + 'mapping' => $c->getClassDiscriminatorMapping()?->getTypesMapping(), + ], + ); } public function testGetTypes(): void { @@ -138,6 +153,10 @@ public function jsonSerialize(): mixed { } } +/** + * @internal + * @noinspection PhpMultipleClassesDeclarationsInOneFile + */ class MetadataFactoryTest_B extends MetadataFactoryTest_A { /** * @phpstan-ignore-next-line required for tests @@ -155,3 +174,12 @@ public function __construct( // empty } } + +/** + * @internal + * @noinspection PhpMultipleClassesDeclarationsInOneFile + */ +#[VersionMap(['b' => MetadataFactoryTest_B::class, 'a' => MetadataFactoryTest_A::class])] +class MetadataFactoryTest_C { + // empty +}