Add message origin classes
This commit is contained in:
parent
1e3824a87a
commit
c4c307fcf1
7 changed files with 343 additions and 0 deletions
11
src/TgBotLib/Enums/Types/MessageOriginType.php
Normal file
11
src/TgBotLib/Enums/Types/MessageOriginType.php
Normal file
|
@ -0,0 +1,11 @@
|
|||
<?php
|
||||
|
||||
namespace TgBotLib\Enums\Types;
|
||||
|
||||
enum MessageOriginType : string
|
||||
{
|
||||
case USER = 'user';
|
||||
case HIDDEN_USER = 'hidden_user';
|
||||
case CHAT = 'chat';
|
||||
case CHANNEL = 'channel';
|
||||
}
|
30
src/TgBotLib/Methods/SendMessage.php
Normal file
30
src/TgBotLib/Methods/SendMessage.php
Normal file
|
@ -0,0 +1,30 @@
|
|||
<?php
|
||||
|
||||
namespace TgBotLib\Methods;
|
||||
|
||||
use InvalidArgumentException;
|
||||
use TgBotLib\Abstracts\Method;
|
||||
use TgBotLib\Bot;
|
||||
use TgBotLib\Enums\Methods;
|
||||
use TgBotLib\Objects\Telegram\Message;
|
||||
|
||||
class SendMessage extends Method
|
||||
{
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public static function execute(Bot $bot, array $parameters = []): Message
|
||||
{
|
||||
if(!isset($parameters['chat_id']))
|
||||
{
|
||||
throw new InvalidArgumentException('chat_id is required');
|
||||
}
|
||||
|
||||
if(!isset($parameters['text']))
|
||||
{
|
||||
throw new InvalidArgumentException('text is required');
|
||||
}
|
||||
|
||||
return Message::fromArray(self::executeCurl(self::buildPost($bot, Methods::SEND_MESSAGE->value, $parameters)));
|
||||
}
|
||||
}
|
67
src/TgBotLib/Objects/Telegram/MessageOrigin.php
Normal file
67
src/TgBotLib/Objects/Telegram/MessageOrigin.php
Normal file
|
@ -0,0 +1,67 @@
|
|||
<?php
|
||||
|
||||
namespace TgBotLib\Objects\Telegram;
|
||||
|
||||
use InvalidArgumentException;
|
||||
use TgBotLib\Enums\Types\MessageOriginType;
|
||||
use TgBotLib\Interfaces\ObjectTypeInterface;
|
||||
use TgBotLib\Objects\Telegram\MessageOrigin\MessageOriginChannel;
|
||||
use TgBotLib\Objects\Telegram\MessageOrigin\MessageOriginChat;
|
||||
use TgBotLib\Objects\Telegram\MessageOrigin\MessageOriginHiddenUser;
|
||||
use TgBotLib\Objects\Telegram\MessageOrigin\MessageOriginUser;
|
||||
|
||||
abstract class MessageOrigin implements ObjectTypeInterface
|
||||
{
|
||||
protected MessageOriginType $type;
|
||||
protected int $date;
|
||||
|
||||
/**
|
||||
*
|
||||
* @return MessageOriginType
|
||||
*/
|
||||
public function getType(): MessageOriginType
|
||||
{
|
||||
return $this->type;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves the date.
|
||||
*
|
||||
* @return int The date value.
|
||||
*/
|
||||
public function getDate(): int
|
||||
{
|
||||
return $this->date;
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts the object to an array representation.
|
||||
*
|
||||
* @return array The array representation of the object.
|
||||
*/
|
||||
public abstract function toArray(): array;
|
||||
|
||||
/**
|
||||
* Converts an associative array to a MessageOrigin object.
|
||||
*
|
||||
* @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
|
||||
{
|
||||
if(!isset($data['type']))
|
||||
{
|
||||
throw new InvalidArgumentException('Message origin type is required');
|
||||
}
|
||||
|
||||
return match (MessageOriginType::tryFrom($data['type']))
|
||||
{
|
||||
MessageOriginType::USER => MessageOriginUser::fromArray($data),
|
||||
MessageOriginType::CHAT => MessageOriginChat::fromArray($data),
|
||||
MessageOriginType::HIDDEN_USER => MessageOriginHiddenUser::fromArray($data),
|
||||
MessageOriginType::CHANNEL => MessageOriginChannel::fromArray($data),
|
||||
default => throw new InvalidArgumentException('Unknown message origin type'),
|
||||
};
|
||||
}
|
||||
}
|
|
@ -0,0 +1,75 @@
|
|||
<?php
|
||||
|
||||
namespace TgBotLib\Objects\Telegram\MessageOrigin;
|
||||
|
||||
use TgBotLib\Enums\Types\MessageOriginType;
|
||||
use TgBotLib\Interfaces\ObjectTypeInterface;
|
||||
use TgBotLib\Objects\Telegram\Chat;
|
||||
use TgBotLib\Objects\Telegram\MessageOrigin;
|
||||
|
||||
class MessageOriginChannel extends MessageOrigin implements ObjectTypeInterface
|
||||
{
|
||||
private Chat $chat;
|
||||
private int $message_id;
|
||||
private ?string $author_signature;
|
||||
|
||||
/**
|
||||
* Channel chat to which the message was originally sent
|
||||
*
|
||||
* @return Chat
|
||||
*/
|
||||
public function getChat(): Chat
|
||||
{
|
||||
return $this->chat;
|
||||
}
|
||||
|
||||
/**
|
||||
* Unique message identifier inside the chat
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function getMessageId(): int
|
||||
{
|
||||
return $this->message_id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Optional. Signature of the original post author
|
||||
*
|
||||
* @return string|null
|
||||
*/
|
||||
public function getAuthorSignature(): ?string
|
||||
{
|
||||
return $this->author_signature;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function toArray(): array
|
||||
{
|
||||
return [
|
||||
'type' => $this->getType()->value,
|
||||
'date' => $this->getDate(),
|
||||
'chat' => $this->chat->toArray(),
|
||||
'message_id' => $this->message_id,
|
||||
'author_signature' => $this->author_signature,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public static function fromArray(array $data): MessageOrigin
|
||||
{
|
||||
$object = new self();
|
||||
|
||||
$object->type = MessageOriginType::CHANNEL;
|
||||
$object->date = $data['date'];
|
||||
$object->chat = Chat::fromArray($data['chat']);
|
||||
$object->message_id = $data['message_id'];
|
||||
$object->author_signature = $data['author_signature'] ?? null;
|
||||
|
||||
return $object;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,62 @@
|
|||
<?php
|
||||
|
||||
namespace TgBotLib\Objects\Telegram\MessageOrigin;
|
||||
|
||||
use TgBotLib\Enums\Types\MessageOriginType;
|
||||
use TgBotLib\Interfaces\ObjectTypeInterface;
|
||||
use TgBotLib\Objects\Telegram\Chat;
|
||||
use TgBotLib\Objects\Telegram\MessageOrigin;
|
||||
|
||||
class MessageOriginChat extends MessageOrigin implements ObjectTypeInterface
|
||||
{
|
||||
private Chat $sender_chat;
|
||||
private ?string $author_signature;
|
||||
|
||||
/**
|
||||
* Unique message identifier inside the chat
|
||||
*
|
||||
* @return Chat
|
||||
*/
|
||||
public function getSenderChat(): Chat
|
||||
{
|
||||
return $this->sender_chat;
|
||||
}
|
||||
|
||||
/**
|
||||
* Optional. Signature of the original post author
|
||||
*
|
||||
* @return string|null
|
||||
*/
|
||||
public function getAuthorSignature(): ?string
|
||||
{
|
||||
return $this->author_signature;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function toArray(): array
|
||||
{
|
||||
return [
|
||||
'type' => $this->getType()->value,
|
||||
'date' => $this->getDate(),
|
||||
'sender_chat' => $this->sender_chat->toArray(),
|
||||
'author_signature' => $this->author_signature,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public static function fromArray(array $data): MessageOrigin
|
||||
{
|
||||
$object = new self();
|
||||
|
||||
$object->type = MessageOriginType::CHAT;
|
||||
$object->date = $data['date'];
|
||||
$object->sender_chat = Chat::fromArray($data['sender_chat']);
|
||||
$object->author_signature = $data['author_signature'] ?? null;
|
||||
|
||||
return $object;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,49 @@
|
|||
<?php
|
||||
|
||||
namespace TgBotLib\Objects\Telegram\MessageOrigin;
|
||||
|
||||
use TgBotLib\Enums\Types\MessageOriginType;
|
||||
use TgBotLib\Interfaces\ObjectTypeInterface;
|
||||
use TgBotLib\Objects\Telegram\MessageOrigin;
|
||||
use TgBotLib\Objects\Telegram\User;
|
||||
|
||||
class MessageOriginHiddenUser extends MessageOrigin implements ObjectTypeInterface
|
||||
{
|
||||
private string $sender_user_name;
|
||||
|
||||
/**
|
||||
* Name of the user that sent the message originally
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getSenderUserName(): string
|
||||
{
|
||||
return $this->sender_user_name;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function toArray(): array
|
||||
{
|
||||
return [
|
||||
'type' => $this->getType()->value,
|
||||
'date' => $this->getDate(),
|
||||
'sender_user_name' => $this->sender_user_name
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public static function fromArray(array $data): MessageOrigin
|
||||
{
|
||||
$object = new self();
|
||||
|
||||
$object->type = MessageOriginType::HIDDEN_USER;
|
||||
$object->date = $data['date'];
|
||||
$object->sender_user_name = $data['sender_user_name'];
|
||||
|
||||
return $object;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,49 @@
|
|||
<?php
|
||||
|
||||
namespace TgBotLib\Objects\Telegram\MessageOrigin;
|
||||
|
||||
use TgBotLib\Enums\Types\MessageOriginType;
|
||||
use TgBotLib\Interfaces\ObjectTypeInterface;
|
||||
use TgBotLib\Objects\Telegram\MessageOrigin;
|
||||
use TgBotLib\Objects\Telegram\User;
|
||||
|
||||
class MessageOriginUser extends MessageOrigin implements ObjectTypeInterface
|
||||
{
|
||||
private User $sender_user;
|
||||
|
||||
/**
|
||||
* User that sent the message originally
|
||||
*
|
||||
* @return User
|
||||
*/
|
||||
public function getSenderUser(): User
|
||||
{
|
||||
return $this->sender_user;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function toArray(): array
|
||||
{
|
||||
return [
|
||||
'type' => $this->getType()->value,
|
||||
'date' => $this->getDate(),
|
||||
'sender_user' => $this->sender_user->toArray()
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public static function fromArray(array $data): MessageOrigin
|
||||
{
|
||||
$object = new self();
|
||||
|
||||
$object->type = MessageOriginType::USER;
|
||||
$object->date = $data['date'];
|
||||
$object->sender_user = User::fromArray($data['sender_user']);
|
||||
|
||||
return $object;
|
||||
}
|
||||
}
|
Loading…
Add table
Reference in a new issue