Merge pull request #1 from danog/custom-serializers

Feat: support of custom serializers in OrmMappedArray
This commit is contained in:
Daniil Gentili 2024-09-24 18:49:13 +02:00 committed by GitHub
commit 1a5129adea
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 24 additions and 1 deletions

View File

@ -18,6 +18,7 @@ namespace danog\AsyncOrm\Annotations;
use Attribute; use Attribute;
use danog\AsyncOrm\KeyType; use danog\AsyncOrm\KeyType;
use danog\AsyncOrm\Serializer;
use danog\AsyncOrm\ValueType; use danog\AsyncOrm\ValueType;
/** /**
@ -54,7 +55,11 @@ final class OrmMappedArray
/** /**
* Table name postfix, if null defaults to the property name. * 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,
) { ) {
} }
} }

View File

@ -78,6 +78,13 @@ trait DbAutoProperties
['cacheTtl' => $ttl] ['cacheTtl' => $ttl]
)); ));
} }
$serializer = $attr->serializer ?? $settings->serializer;
if ($serializer !== $settings->serializer) {
$settings = new $settings(...\array_merge(
(array) $settings,
['serializer' => $serializer]
));
}
if ($settings instanceof MysqlSettings) { if ($settings instanceof MysqlSettings) {
$optimize = $attr->optimizeIfWastedMb ?? $settings->optimizeIfWastedMb; $optimize = $attr->optimizeIfWastedMb ?? $settings->optimizeIfWastedMb;

View File

@ -433,8 +433,10 @@ final class OrmTest extends TestCase
$obj->arr[12345] = 54321; $obj->arr[12345] = 54321;
$obj->arr2[123456] = 654321; $obj->arr2[123456] = 654321;
$obj->arr5[123] = "7734";
$this->assertSame(54321, $obj->arr[12345]); $this->assertSame(54321, $obj->arr[12345]);
$this->assertSame(654321, $obj->arr2[123456]); $this->assertSame(654321, $obj->arr2[123456]);
$this->assertSame("7734", $obj->arr5[123]);
$this->assertCount(1, $obj->arr); $this->assertCount(1, $obj->arr);
$this->assertCount(1, $obj->arr2); $this->assertCount(1, $obj->arr2);

View File

@ -29,6 +29,7 @@ use danog\AsyncOrm\DbArrayBuilder;
use danog\AsyncOrm\DbAutoProperties; use danog\AsyncOrm\DbAutoProperties;
use danog\AsyncOrm\DbObject; use danog\AsyncOrm\DbObject;
use danog\AsyncOrm\KeyType; use danog\AsyncOrm\KeyType;
use danog\AsyncOrm\Serializer\Json;
use danog\AsyncOrm\ValueType; use danog\AsyncOrm\ValueType;
final class TestObject extends DbObject final class TestObject extends DbObject
@ -67,6 +68,14 @@ final class TestObject extends DbObject
)] )]
public DbArray $arr4; public DbArray $arr4;
#[OrmMappedArray(
keyType: KeyType::INT,
valueType: ValueType::SCALAR,
cacheTtl: 0,
serializer: new Json(),
)]
public DbArray $arr5;
public function __sleep() public function __sleep()
{ {
return ['savedProp', 'arr', 'arr2', 'arr3', 'arr4']; return ['savedProp', 'arr', 'arr2', 'arr3', 'arr4'];