tgbotlib/src/TgBotLib/Objects/BotCommandScope.php

63 lines
2.5 KiB
PHP
Raw Normal View History

<?php
2024-10-02 00:18:12 -04:00
namespace TgBotLib\Objects;
2024-10-02 00:32:13 -04:00
use InvalidArgumentException;
use TgBotLib\Enums\Types\BotCommandScopeType;
use TgBotLib\Interfaces\ObjectTypeInterface;
2024-10-02 00:32:13 -04:00
use TgBotLib\Objects\BotCommandScope\BotCommandScopeAllChatAdministrators;
use TgBotLib\Objects\BotCommandScope\BotCommandScopeAllGroupChats;
use TgBotLib\Objects\BotCommandScope\BotCommandScopeAllPrivateChats;
2024-10-02 00:18:12 -04:00
use TgBotLib\Objects\BotCommandScope\BotCommandScopeChat;
use TgBotLib\Objects\BotCommandScope\BotCommandScopeChatAdministrators;
use TgBotLib\Objects\BotCommandScope\BotCommandScopeChatMember;
2024-10-02 00:32:13 -04:00
use TgBotLib\Objects\BotCommandScope\BotCommandScopeDefault;
2024-10-02 00:32:13 -04:00
abstract class BotCommandScope implements ObjectTypeInterface
{
2024-10-02 00:33:20 -04:00
protected BotCommandScopeType $type;
/**
* Scope type, one of “default”, “all_private_chats”, “all_group_chats”, “all_chat_administrators”, “chat”,
* “chat_administrators”, “chat_member”
*
2024-10-02 00:32:13 -04:00
* @return BotCommandScopeType
*/
2024-10-02 00:32:13 -04:00
public function getType(): BotCommandScopeType
{
return $this->type;
}
/**
2024-10-02 00:32:13 -04:00
* @inheritDoc
*/
2024-10-02 00:32:13 -04:00
public abstract function toArray(): array;
/**
2024-10-02 00:32:13 -04:00
* @inheritDoc
*/
public static function fromArray(?array $data): ?BotCommandScope
{
if($data === null)
{
return null;
}
2024-10-02 00:32:13 -04:00
if(!isset($data['type']))
2023-02-14 17:35:16 -05:00
{
2024-10-02 00:32:13 -04:00
throw new InvalidArgumentException('BotCommandScope expected type');
2023-02-14 17:35:16 -05:00
}
2024-10-02 00:32:13 -04:00
return match(BotCommandScopeType::tryFrom($data['type']))
{
BotCommandScopeType::DEFAULT => BotCommandScopeDefault::fromArray($data),
BotCommandScopeType::ALL_PRIVATE_CHATS => BotCommandScopeAllPrivateChats::fromArray($data),
BotCommandScopeType::ALL_CHAT_GROUPS => BotCommandScopeAllGroupChats::fromArray($data),
BotCommandScopeType::ALL_CHAT_ADMINISTRATORS => BotCommandScopeChatAdministrators::fromArray($data),
BotCommandScopeType::CHAT => BotCommandScopeChat::fromArray($data),
BotCommandScopeType::CHAT_ADMINISTRATORS => BotCommandScopeAllChatAdministrators::fromArray($data),
BotCommandScopeType::CHAT_MEMBER => BotCommandScopeChatMember::fromArray($data),
default => throw new InvalidArgumentException('Unexpected match value')
};
}
}