From fe292890455abcd2aeccd35de142326c5fc59cef Mon Sep 17 00:00:00 2001 From: Alexander Pankratov Date: Sat, 17 Aug 2024 21:36:54 +0200 Subject: [PATCH] Feat: support of custom serializers --- src/Annotations/OrmMappedArray.php | 7 ++++++- src/DbAutoProperties.php | 7 +++++++ tests/OrmTest.php | 2 ++ tests/TestObject.php | 9 +++++++++ 4 files changed, 24 insertions(+), 1 deletion(-) diff --git a/src/Annotations/OrmMappedArray.php b/src/Annotations/OrmMappedArray.php index fb57502..1238fa0 100644 --- a/src/Annotations/OrmMappedArray.php +++ b/src/Annotations/OrmMappedArray.php @@ -18,6 +18,7 @@ namespace danog\AsyncOrm\Annotations; use Attribute; use danog\AsyncOrm\KeyType; +use danog\AsyncOrm\Serializer; use danog\AsyncOrm\ValueType; /** @@ -54,7 +55,11 @@ final class OrmMappedArray /** * Table name postfix, if null defaults to the property name. */ - public readonly ?string $tablePostfix = null + public readonly ?string $tablePostfix = null, + /** + * Provide custom serializer for table. + */ + public readonly ?Serializer $serializer = null, ) { } } diff --git a/src/DbAutoProperties.php b/src/DbAutoProperties.php index 24284f0..3dd9685 100644 --- a/src/DbAutoProperties.php +++ b/src/DbAutoProperties.php @@ -78,6 +78,13 @@ trait DbAutoProperties ['cacheTtl' => $ttl] )); } + $serializer = $attr->serializer ?? $settings->serializer; + if ($serializer !== $settings->serializer) { + $settings = new $settings(...\array_merge( + (array) $settings, + ['serializer' => $serializer] + )); + } if ($settings instanceof MysqlSettings) { $optimize = $attr->optimizeIfWastedMb ?? $settings->optimizeIfWastedMb; diff --git a/tests/OrmTest.php b/tests/OrmTest.php index c609cbb..8800e06 100644 --- a/tests/OrmTest.php +++ b/tests/OrmTest.php @@ -433,8 +433,10 @@ final class OrmTest extends TestCase $obj->arr[12345] = 54321; $obj->arr2[123456] = 654321; + $obj->arr5[123] = "7734"; $this->assertSame(54321, $obj->arr[12345]); $this->assertSame(654321, $obj->arr2[123456]); + $this->assertSame("7734", $obj->arr5[123]); $this->assertCount(1, $obj->arr); $this->assertCount(1, $obj->arr2); diff --git a/tests/TestObject.php b/tests/TestObject.php index a6bb9ad..631b3d5 100644 --- a/tests/TestObject.php +++ b/tests/TestObject.php @@ -29,6 +29,7 @@ use danog\AsyncOrm\DbArrayBuilder; use danog\AsyncOrm\DbAutoProperties; use danog\AsyncOrm\DbObject; use danog\AsyncOrm\KeyType; +use danog\AsyncOrm\Serializer\Json; use danog\AsyncOrm\ValueType; final class TestObject extends DbObject @@ -67,6 +68,14 @@ final class TestObject extends DbObject )] public DbArray $arr4; + #[OrmMappedArray( + keyType: KeyType::INT, + valueType: ValueType::SCALAR, + cacheTtl: 0, + serializer: new Json(), + )] + public DbArray $arr5; + public function __sleep() { return ['savedProp', 'arr', 'arr2', 'arr3', 'arr4'];