Updated MessageOrigin objects

This commit is contained in:
netkas 2024-10-04 00:47:04 -04:00
parent 5426dd9e1c
commit 2405873510
5 changed files with 40 additions and 25 deletions

View file

@ -35,21 +35,20 @@ abstract class MessageOrigin implements ObjectTypeInterface
} }
/** /**
* Converts the object to an array representation. * @inheritDoc
*
* @return array The array representation of the object.
*/ */
public abstract function toArray(): array; public abstract function toArray(): array;
/** /**
* Converts an associative array to a MessageOrigin object. * @inheritDoc
*
* @param array $data The data representing the message origin.
* @return MessageOrigin The constructed MessageOrigin object.
* @throws InvalidArgumentException If the 'type' key is missing or an unknown type is provided.
*/ */
public static function fromArray(array $data): MessageOrigin public static function fromArray(?array $data): ?MessageOrigin
{ {
if($data === null)
{
return null;
}
if(!isset($data['type'])) if(!isset($data['type']))
{ {
throw new InvalidArgumentException('Message origin type is required'); throw new InvalidArgumentException('Message origin type is required');

View file

@ -49,8 +49,8 @@ class MessageOriginChannel extends MessageOrigin implements ObjectTypeInterface
public function toArray(): array public function toArray(): array
{ {
return [ return [
'type' => $this->getType()->value, 'type' => $this->type->value,
'date' => $this->getDate(), 'date' => $this->date,
'chat' => $this->chat->toArray(), 'chat' => $this->chat->toArray(),
'message_id' => $this->message_id, 'message_id' => $this->message_id,
'author_signature' => $this->author_signature, 'author_signature' => $this->author_signature,
@ -60,10 +60,14 @@ class MessageOriginChannel extends MessageOrigin implements ObjectTypeInterface
/** /**
* @inheritDoc * @inheritDoc
*/ */
public static function fromArray(array $data): MessageOrigin public static function fromArray(?array $data): ?MessageOrigin
{ {
$object = new self(); if($data === null)
{
return null;
}
$object = new self();
$object->type = MessageOriginType::CHANNEL; $object->type = MessageOriginType::CHANNEL;
$object->date = $data['date']; $object->date = $data['date'];
$object->chat = Chat::fromArray($data['chat']); $object->chat = Chat::fromArray($data['chat']);

View file

@ -38,8 +38,8 @@ class MessageOriginChat extends MessageOrigin implements ObjectTypeInterface
public function toArray(): array public function toArray(): array
{ {
return [ return [
'type' => $this->getType()->value, 'type' => $this->type->value,
'date' => $this->getDate(), 'date' => $this->date,
'sender_chat' => $this->sender_chat->toArray(), 'sender_chat' => $this->sender_chat->toArray(),
'author_signature' => $this->author_signature, 'author_signature' => $this->author_signature,
]; ];
@ -48,10 +48,14 @@ class MessageOriginChat extends MessageOrigin implements ObjectTypeInterface
/** /**
* @inheritDoc * @inheritDoc
*/ */
public static function fromArray(array $data): MessageOrigin public static function fromArray(?array $data): ?MessageOrigin
{ {
$object = new self(); if($data === null)
{
return null;
}
$object = new self();
$object->type = MessageOriginType::CHAT; $object->type = MessageOriginType::CHAT;
$object->date = $data['date']; $object->date = $data['date'];
$object->sender_chat = Chat::fromArray($data['sender_chat']); $object->sender_chat = Chat::fromArray($data['sender_chat']);

View file

@ -26,8 +26,8 @@ class MessageOriginHiddenUser extends MessageOrigin implements ObjectTypeInterfa
public function toArray(): array public function toArray(): array
{ {
return [ return [
'type' => $this->getType()->value, 'type' => $this->type->value,
'date' => $this->getDate(), 'date' => $this->date,
'sender_user_name' => $this->sender_user_name 'sender_user_name' => $this->sender_user_name
]; ];
} }
@ -35,10 +35,14 @@ class MessageOriginHiddenUser extends MessageOrigin implements ObjectTypeInterfa
/** /**
* @inheritDoc * @inheritDoc
*/ */
public static function fromArray(array $data): MessageOrigin public static function fromArray(?array $data): ?MessageOrigin
{ {
$object = new self(); if($data === null)
{
return null;
}
$object = new self();
$object->type = MessageOriginType::HIDDEN_USER; $object->type = MessageOriginType::HIDDEN_USER;
$object->date = $data['date']; $object->date = $data['date'];
$object->sender_user_name = $data['sender_user_name']; $object->sender_user_name = $data['sender_user_name'];

View file

@ -27,8 +27,8 @@ class MessageOriginUser extends MessageOrigin implements ObjectTypeInterface
public function toArray(): array public function toArray(): array
{ {
return [ return [
'type' => $this->getType()->value, 'type' => $this->type->value,
'date' => $this->getDate(), 'date' => $this->date,
'sender_user' => $this->sender_user->toArray() 'sender_user' => $this->sender_user->toArray()
]; ];
} }
@ -36,10 +36,14 @@ class MessageOriginUser extends MessageOrigin implements ObjectTypeInterface
/** /**
* @inheritDoc * @inheritDoc
*/ */
public static function fromArray(array $data): MessageOrigin public static function fromArray(?array $data): ?MessageOrigin
{ {
$object = new self(); if($data === null)
{
return null;
}
$object = new self();
$object->type = MessageOriginType::USER; $object->type = MessageOriginType::USER;
$object->date = $data['date']; $object->date = $data['date'];
$object->sender_user = User::fromArray($data['sender_user']); $object->sender_user = User::fromArray($data['sender_user']);