src/Messenger/Messages/User/CreateBodymedCenter.php line 52

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Bodymed\Webshop\Messenger\Messages\User;
  4. use Bodymed\Webshop\Messenger\DistributedMessageInterface;
  5. use Bodymed\Webshop\UserService\Struct\Center;
  6. class CreateBodymedCenter implements DistributedMessageInterface
  7. {
  8.     public const SUBJECT '91e1c872-2877-4d14-ad7b-e8ecdaa43f83';
  9.     protected array $payload;
  10.     protected \DateTimeImmutable $when;
  11.     public function __construct(
  12.         array $payload,
  13.         \DateTimeImmutable $when null
  14.     ) {
  15.         $this->payload $payload;
  16.         $this->when $when ?? new \DateTimeImmutable();
  17.     }
  18.     public static function getSubject(): string
  19.     {
  20.         return self::SUBJECT;
  21.     }
  22.     public function toPayload(): array
  23.     {
  24.         return \array_merge($this->payload, [
  25.             'when' => $this->when,
  26.             'subject' => self::SUBJECT,
  27.         ]);
  28.     }
  29.     public static function fromPayload(array $data): CreateBodymedCenter
  30.     {
  31.         $static = new CreateBodymedCenter($data);
  32.         $static->when = new \DateTimeImmutable(
  33.             $data['when']['date'],
  34.             new \DateTimeZone($data['when']['timezone'])
  35.         );
  36.         return $static;
  37.     }
  38.     public function jsonSerialize()
  39.     {
  40.         return $this->toPayload();
  41.     }
  42.     public function getPayload(): array
  43.     {
  44.         return $this->payload;
  45.     }
  46.     public function getWhen(): \DateTimeImmutable
  47.     {
  48.         return $this->when;
  49.     }
  50.     public function getCenterStruct(): Center
  51.     {
  52.         return Center::fromArray($this->payload);
  53.     }
  54. }