Files
kupshop/bundles/KupShop/PreordersBundle/Entity/UserPreorderMessage.php
2025-08-02 16:30:27 +02:00

63 lines
1.6 KiB
PHP

<?php
declare(strict_types=1);
namespace KupShop\PreordersBundle\Entity;
use KupShop\PreordersBundle\Util\CustomDataArrayAccess;
enum NotifyState: int
{
case NotNotified = 0;
case EmailNotified = 1;
public function getMessage(): string
{
return translate('notification'.$this->name, 'preordersUsers');
}
}
class UserPreorderMessage implements \ArrayAccess, \JsonSerializable
{
use CustomDataArrayAccess;
public function __construct(
public string $text,
public ?string $admin = null,
public \DateTimeImmutable $date = new \DateTimeImmutable(),
public NotifyState $notified = NotifyState::NotNotified,
) {
}
public static function createFromArray(array $array): UserPreorderMessage
{
$date = is_string($array['date'] ?? false) ?
\DateTimeImmutable::createFromFormat(\DateTimeInterface::ATOM, $array['date'])
: new \DateTimeImmutable();
$instance = new self(
text: $array['text'],
admin: $array['admin'] ?? null,
date: $date,
notified: NotifyState::from(
(int) ($array['notified'] ?? NotifyState::NotNotified->value),
),
);
$instance->customData = $array['custom_data'] ?? [];
return $instance;
}
public function jsonSerialize(): array
{
return [
'text' => $this->text,
'admin' => $this->admin,
'custom_data' => $this->customData,
'date' => $this->date->format(\DateTimeInterface::ATOM),
'notified' => $this->notified->value,
];
}
}