-
Notifications
You must be signed in to change notification settings - Fork 0
/
AddressValueObjectType.php
60 lines (51 loc) · 1.55 KB
/
AddressValueObjectType.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
<?php
/*
* Copyright (c) Fusonic GmbH. All rights reserved.
* Licensed under the MIT License. See LICENSE file in the project root for license information.
*/
declare(strict_types=1);
namespace Fusonic\DDDExtensions\Tests\Doctrine\Types;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Fusonic\DDDExtensions\Doctrine\Types\ValueObjectType;
use Fusonic\DDDExtensions\Tests\Domain\AddressValueObject;
/**
* @extends ValueObjectType<AddressValueObject>
*/
final class AddressValueObjectType extends ValueObjectType
{
public function getName(): string
{
return 'addressValueObject';
}
/**
* @throws \JsonException
*/
public function convertToDatabaseValue(mixed $value, AbstractPlatform $platform): ?string
{
return self::serialize($value, static fn (AddressValueObject $object) => self::fromObject($object));
}
/**
* @throws \JsonException
*/
public function convertToPHPValue(mixed $value, AbstractPlatform $platform): mixed
{
return self::deserialize($value, static fn (array $data) => self::toObject($data));
}
/**
* @param array<string, mixed> $data
*/
public static function toObject(array $data): AddressValueObject
{
return new AddressValueObject($data['street'], $data['number']);
}
/**
* @return array<string, mixed>
*/
public static function fromObject(AddressValueObject $object): array
{
return [
'number' => $object->getNumber(),
'street' => $object->getStreet(),
];
}
}