GemberPHP /
event-sourcing
| 1 | <?php |
||
| 2 | |||
| 3 | declare(strict_types=1); |
||
| 4 | |||
| 5 | namespace Gember\EventSourcing\Resolver\DomainEvent; |
||
| 6 | |||
| 7 | use Gember\EventSourcing\Resolver\Common\DomainTag\DomainTagDefinition; |
||
| 8 | use Gember\EventSourcing\Resolver\Common\SagaId\SagaIdDefinition; |
||
| 9 | use Gember\EventSourcing\Util\Serialization\Serializable; |
||
| 10 | |||
| 11 | /** |
||
| 12 | * @phpstan-import-type DomainTagDefinitionPayload from DomainTagDefinition |
||
| 13 | * @phpstan-import-type SagaIdDefinitionPayload from SagaIdDefinition |
||
| 14 | * |
||
| 15 | * @phpstan-type DomainEventDefinitionPayload array{ |
||
| 16 | * eventClassName: class-string, |
||
| 17 | * eventName: string, |
||
| 18 | * domainTags: list<DomainTagDefinitionPayload>, |
||
| 19 | * sagaIds: list<SagaIdDefinitionPayload> |
||
| 20 | * } |
||
| 21 | * |
||
| 22 | * @implements Serializable<DomainEventDefinitionPayload, DomainEventDefinition> |
||
| 23 | */ |
||
| 24 | final readonly class DomainEventDefinition implements Serializable |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 25 | { |
||
| 26 | /** |
||
| 27 | * @param class-string $eventClassName |
||
| 28 | * @param list<DomainTagDefinition> $domainTags |
||
| 29 | * @param list<SagaIdDefinition> $sagaIds |
||
| 30 | */ |
||
| 31 | 20 | public function __construct( |
|
| 32 | public string $eventClassName, |
||
| 33 | public string $eventName, |
||
| 34 | public array $domainTags, |
||
| 35 | public array $sagaIds, |
||
| 36 | 20 | ) {} |
|
| 37 | |||
| 38 | 3 | public function toPayload(): array |
|
| 39 | { |
||
| 40 | 3 | return [ |
|
| 41 | 3 | 'eventClassName' => $this->eventClassName, |
|
| 42 | 3 | 'eventName' => $this->eventName, |
|
| 43 | 3 | 'domainTags' => array_map(fn($domainTag) => $domainTag->toPayload(), $this->domainTags), |
|
| 44 | 3 | 'sagaIds' => array_map(fn($sagaId) => $sagaId->toPayload(), $this->sagaIds), |
|
| 45 | 3 | ]; |
|
| 46 | } |
||
| 47 | |||
| 48 | 3 | public static function fromPayload(array $payload): self |
|
| 49 | { |
||
| 50 | 3 | return new self( |
|
| 51 | 3 | $payload['eventClassName'], |
|
| 52 | 3 | $payload['eventName'], |
|
| 53 | 3 | array_map(fn($domainTagPayload) => DomainTagDefinition::fromPayload($domainTagPayload), $payload['domainTags']), |
|
| 54 | 3 | array_map(fn($sagaIdPayload) => SagaIdDefinition::fromPayload($sagaIdPayload), $payload['sagaIds']), |
|
| 55 | 3 | ); |
|
| 56 | } |
||
| 57 | } |
||
| 58 |