<?php
declare(strict_types=1);
namespace Bodymed\Webshop\Messenger\Messages\User;
use Bodymed\Webshop\Messenger\DistributedMessageInterface;
use Bodymed\Webshop\UserService\Struct\Center;
class CreateBodymedCenter implements DistributedMessageInterface
{
public const SUBJECT = '91e1c872-2877-4d14-ad7b-e8ecdaa43f83';
protected array $payload;
protected \DateTimeImmutable $when;
public function __construct(
array $payload,
\DateTimeImmutable $when = null
) {
$this->payload = $payload;
$this->when = $when ?? new \DateTimeImmutable();
}
public static function getSubject(): string
{
return self::SUBJECT;
}
public function toPayload(): array
{
return \array_merge($this->payload, [
'when' => $this->when,
'subject' => self::SUBJECT,
]);
}
public static function fromPayload(array $data): CreateBodymedCenter
{
$static = new CreateBodymedCenter($data);
$static->when = new \DateTimeImmutable(
$data['when']['date'],
new \DateTimeZone($data['when']['timezone'])
);
return $static;
}
public function jsonSerialize()
{
return $this->toPayload();
}
public function getPayload(): array
{
return $this->payload;
}
public function getWhen(): \DateTimeImmutable
{
return $this->when;
}
public function getCenterStruct(): Center
{
return Center::fromArray($this->payload);
}
}