tapi/app/entity/gateway/ws/OutMessage.php

35 lines
803 B
PHP

<?php
namespace app\entity\gateway\ws;
use app\entity\gateway\el\ElNotification;
use app\enum\gateway\WsEventEnum;
use app\enum\gateway\WsTypeEnum;
class OutMessage
{
private string $type;
private string $event;
private array $data;
public function __construct(WsTypeEnum $type, WsEventEnum $event, mixed $data = [])
{
$this->type = $type->value;
$this->event = $event->value;
if($data instanceof ElNotification) {
$this->data = $data->toArray();
}else{
$this->data = (array)$data;
}
}
public function toJson(): string
{
return json_encode([
'type' => $this->type,
'event' => $this->event,
'data' => $this->data,
], JSON_UNESCAPED_UNICODE);
}
}