headless_cms-1.0.3/modules/headless_cms_notify/src/NotifyMessage/HeadlessNotifyMessage.php
modules/headless_cms_notify/src/NotifyMessage/HeadlessNotifyMessage.php
<?php
declare(strict_types=1);
namespace Drupal\headless_cms_notify\NotifyMessage;
use Drupal\Component\Serialization\Json;
/**
* Defines a headless notification message.
*/
abstract class HeadlessNotifyMessage implements HeadlessNotifyMessageInterface {
public function __construct(
protected readonly string $type,
protected readonly ?string $subType = NULL,
protected readonly array $params = [],
) {}
/**
* {@inheritdoc}
*/
public function getType(): string {
return $this->type;
}
/**
* {@inheritdoc}
*/
public function getSubType(): ?string {
return $this->subType;
}
/**
* {@inheritdoc}
*/
public function getParams(): array {
return $this->params;
}
/**
* Get the message payload as array.
*/
final public function toArray(): array {
return [
'type' => $this->type,
'subType' => $this->subType,
'params' => $this->params,
];
}
/**
* Get the message payload as JSON.
*/
final public function toJson(): string {
return Json::encode($this->toArray());
}
/**
* Create a new message from an array.
*/
final public static function fromArray(array $data): static {
return new static(
$data['type'],
$data['subType'] ?? NULL,
$data['params'] ?? [],
);
}
}
