diff --git a/src/TgBotLib/Objects/ChatMember.php b/src/TgBotLib/Objects/ChatMember.php index 1fe7143..6fa8921 100644 --- a/src/TgBotLib/Objects/ChatMember.php +++ b/src/TgBotLib/Objects/ChatMember.php @@ -4,513 +4,57 @@ namespace TgBotLib\Objects; + use InvalidArgumentException; + use TgBotLib\Enums\Types\ChatMemberStatus; use TgBotLib\Interfaces\ObjectTypeInterface; + use TgBotLib\Objects\ChatMember\ChatMemberAdministrator; + use TgBotLib\Objects\ChatMember\ChatMemberBanned; + use TgBotLib\Objects\ChatMember\ChatMemberLeft; + use TgBotLib\Objects\ChatMember\ChatMemberMember; + use TgBotLib\Objects\ChatMember\ChatMemberOwner; + use TgBotLib\Objects\ChatMember\ChatMemberRestricted; - class ChatMember implements ObjectTypeInterface + abstract class ChatMember implements ObjectTypeInterface { /** - * @var string + * @var ChatMemberStatus */ - private $status; - - /** - * @var User - */ - private $user; - - /** - * @var string|null - */ - private $custom_title; - - /** - * @var bool - */ - private $is_member; - - /** - * @var bool - */ - private $is_anonymous; - - /** - * @var bool - */ - private $can_send_messages; - - /** - * @var bool - */ - private $can_send_audios; - - /** - * @var bool - */ - private $can_send_documents; - - /** - * @var bool - */ - private $can_send_photos; - - /** - * @var bool - */ - private $can_send_videos; - - /** - * @var bool - */ - private $can_send_video_notes; - - /** - * @var bool - */ - private $can_send_voice_notes; - - /** - * @var bool - */ - private $can_send_polls; - - /** - * @var bool - */ - private $can_send_other_messages; - - /** - * @var bool - */ - private $can_add_web_page_previews; - - /** - * @var bool - */ - private $can_change_info; - - /** - * @var bool - */ - private $can_manage_chat; - - /** - * @var bool - */ - private $can_delete_messages; - - /** - * @var bool - */ - private $can_manage_video_chats; - - /** - * @var bool - */ - private $can_restrict_members; - - /** - * @var bool - */ - private $can_promote_members; - - /** - * @var bool - */ - private $can_invite_users; - - /** - * @var bool - */ - private $can_post_messages; - - /** - * @var bool - */ - private $can_edit_messages; - - /** - * @var bool - */ - private $can_pin_messages; - - /** - * @var bool - */ - private $can_manage_topics; - - /** - * @var bool - */ - private $can_be_edited; - - /** - * @var int|null - */ - private $until_date; + protected ChatMemberStatus $status; /** * The member's status in the chat, can be “creator”, “administrator”, “member”, “restricted”, “left” or “kicked” * - * @return string + * @return ChatMemberStatus */ - public function getStatus(): string + public function getStatus(): ChatMemberStatus { return $this->status; } /** - * Information about the user - * - * @return User + * @inheritDoc */ - public function getUser(): User - { - return $this->user; - } + public abstract function toArray(): array; /** - * Optional. Custom title for this user - * - * @return string|null + * @inheritDoc */ - public function getCustomTitle(): ?string + public static function fromArray(array $data): ChatMember { - return $this->custom_title; - } + if(!isset($data['status'])) + { + throw new InvalidArgumentException('ChatMember expected status'); + } - /** - * True, if the user is a member of the chat at the moment of the request - * - * @return bool - */ - public function isMember(): bool - { - return $this->is_member; - } - - /** - * True, if the user's presence in the chat is hidden - * - * @return bool - */ - public function isAnonymous(): bool - { - return $this->is_anonymous; - } - - /** - * True, if the user is allowed to send text messages, contacts, invoices, locations and venues - * - * @return bool - */ - public function canSendMessages(): bool - { - return $this->can_send_messages; - } - - /** - * True, if the user is allowed to send audios - * - * @return bool - */ - public function canSendAudios(): bool - { - return $this->can_send_audios; - } - - /** - * True, if the user is allowed to send documents - * - * @return bool - */ - public function canSendDocuments(): bool - { - return $this->can_send_documents; - } - - /** - * True, if the user is allowed to send photos - * - * @return bool - */ - public function canSendPhotos(): bool - { - return $this->can_send_photos; - } - - /** - * True, if the user is allowed to send videos - * - * @return bool - */ - public function canSendVideos(): bool - { - return $this->can_send_videos; - } - - /** - * True, if the user is allowed to send video notes - * - * @return bool - */ - public function canSendVideoNotes(): bool - { - return $this->can_send_video_notes; - } - - /** - * True, if the user is allowed to send voice notes - * - * @return bool - */ - public function canSendVoiceNotes(): bool - { - return $this->can_send_voice_notes; - } - - /** - * True, if the user is allowed to send polls - * - * @return bool - */ - public function canSendPolls(): bool - { - return $this->can_send_polls; - } - - /** - * True, if the user is allowed to send animations, games, stickers and use inline bots - * - * @return bool - */ - public function canSendOtherMessages(): bool - { - return $this->can_send_other_messages; - } - - /** - * True, if the user is allowed to add web page previews to their messages - * - * @return bool - */ - public function canAddWebPagePreviews(): bool - { - return $this->can_add_web_page_previews; - } - - /** - * True, if the user is allowed to change the chat title, photo and other settings - * - * @return bool - */ - public function canChangeInfo(): bool - { - return $this->can_change_info; - } - - /** - * True, if the administrator can access the chat event log, chat statistics, message statistics in channels, - * see channel members, see anonymous administrators in supergroups and ignore slow mode. Implied by any - * other administrator privilege - * - * @return bool - */ - public function canManageChat(): bool - { - return $this->can_manage_chat; - } - - /** - * True, if the administrator can delete messages of other users - * - * @return bool - */ - public function canDeleteMessages(): bool - { - return $this->can_delete_messages; - } - - /** - * True, if the administrator can manage video chats - * - * @return bool - */ - public function canManageVideoChats(): bool - { - return $this->can_manage_video_chats; - } - - /** - * True, if the administrator can restrict, ban or unban chat members - * - * @return bool - */ - public function canRestrictMembers(): bool - { - return $this->can_restrict_members; - } - - /** - * True, if the administrator can add new administrators with a subset of their own privileges or demote - * administrators that they have promoted, directly or indirectly (promoted by administrators that - * were appointed by the user) - * - * @return bool - */ - public function canPromoteMembers(): bool - { - return $this->can_promote_members; - } - - /** - * True, if the user is allowed to invite new users to the chat - * - * @return bool - */ - public function canInviteUsers(): bool - { - return $this->can_invite_users; - } - - /** - * Optional. True, if the administrator can post in the channel; channels only - * - * @return bool - */ - public function canPostMessages(): bool - { - return $this->can_post_messages; - } - - /** - * Optional. True, if the administrator can edit messages of other users and can pin messages; channels only - * - * @return bool - */ - public function canEditMessages(): bool - { - return $this->can_edit_messages; - } - - /** - * Optional. True, if the user is allowed to pin messages; groups and supergroups only - * - * @return bool - */ - public function canPinMessages(): bool - { - return $this->can_pin_messages; - } - - /** - * Optional. True, if the user is allowed to create, rename, close, and reopen forum topics; supergroups only - * - * @return bool - */ - public function canManageTopics(): bool - { - return $this->can_manage_topics; - } - - /** - * True, if the bot is allowed to edit administrator privileges of that user - * - * @return bool - */ - public function canBeEdited(): bool - { - return $this->can_be_edited; - } - - /** - * Date when restrictions will be lifted for this user; unix time. If 0, then the user is restricted forever - * - * @return int|null - */ - public function getUntilDate(): ?int - { - return $this->until_date; - } - - /** - * Returns an array representation of the object - * - * @return array - */ - public function toArray(): array - { - return [ - 'status' => $this->status, - 'user' => ($this->user instanceof ObjectTypeInterface) ? $this->user->toArray() : $this->user, - 'custom_title' => $this->custom_title, - 'is_member' => $this->is_member, - 'is_anonymous' => $this->is_anonymous, - 'can_send_messages' => $this->can_send_messages, - 'can_send_audios' => $this->can_send_audios, - 'can_send_documents' => $this->can_send_documents, - 'can_send_photos' => $this->can_send_photos, - 'can_send_videos' => $this->can_send_videos, - 'can_send_video_notes' => $this->can_send_video_notes, - 'can_send_voice_notes' => $this->can_send_voice_notes, - 'can_send_polls' => $this->can_send_polls, - 'can_send_other_messages' => $this->can_send_other_messages, - 'can_add_web_page_previews' => $this->can_add_web_page_previews, - 'can_change_info' => $this->can_change_info, - 'can_manage_chat' => $this->can_manage_chat, - 'can_delete_messages' => $this->can_delete_messages, - 'can_manage_video_chats' => $this->can_manage_video_chats, - 'can_restrict_members' => $this->can_restrict_members, - 'can_promote_members' => $this->can_promote_members, - 'can_invite_users' => $this->can_invite_users, - 'can_post_messages' => $this->can_post_messages, - 'can_edit_messages' => $this->can_edit_messages, - 'can_pin_messages' => $this->can_pin_messages, - 'can_manage_topics' => $this->can_manage_topics, - 'can_be_edited' => $this->can_be_edited, - 'until_date' => $this->until_date, - ]; - } - - /** - * Constructs an object from an array representation - * - * @param array $data - * @return ChatMember - * @noinspection DuplicatedCode - */ - public static function fromArray(array $data): self - { - $object = new static(); - - $object->status = $data['status'] ?? null; - $object->user = isset($data['user']) ? User::fromArray($data['user']) : null; - $object->custom_title = $data['custom_title'] ?? null; - $object->is_member = $data['is_member'] ?? false; - $object->is_anonymous = $data['is_anonymous'] ?? false; - $object->can_send_messages = $data['can_send_messages'] ?? false; - $object->can_send_audios = $data['can_send_audios'] ?? false; - $object->can_send_documents = $data['can_send_documents'] ?? false; - $object->can_send_photos = $data['can_send_photos'] ?? false; - $object->can_send_videos = $data['can_send_videos'] ?? false; - $object->can_send_video_notes = $data['can_send_video_notes'] ?? false; - $object->can_send_voice_notes = $data['can_send_voice_notes'] ?? false; - $object->can_send_polls = $data['can_send_polls'] ?? false; - $object->can_send_other_messages = $data['can_send_other_messages'] ?? false; - $object->can_add_web_page_previews = $data['can_add_web_page_previews'] ?? false; - $object->can_change_info = $data['can_change_info'] ?? false; - $object->can_manage_chat = $data['can_manage_chat'] ?? false; - $object->can_delete_messages = $data['can_delete_messages'] ?? false; - $object->can_manage_video_chats = $data['can_manage_video_chats'] ?? false; - $object->can_restrict_members = $data['can_restrict_members'] ?? false; - $object->can_promote_members = $data['can_promote_members'] ?? false; - $object->can_invite_users = $data['can_invite_users'] ?? false; - $object->can_post_messages = $data['can_post_messages'] ?? false; - $object->can_edit_messages = $data['can_edit_messages'] ?? false; - $object->can_pin_messages = $data['can_pin_messages'] ?? false; - $object->can_manage_topics = $data['can_manage_topics'] ?? false; - $object->can_be_edited = $data['can_be_edited'] ?? false; - $object->until_date = $data['until_date'] ?? null; - - return $object; + return match(ChatMemberStatus::tryFrom($data['status'])) + { + ChatMemberStatus::ADMINISTRATOR => ChatMemberAdministrator::fromArray($data), + ChatMemberStatus::KICKED => ChatMemberBanned::fromArray($data), + ChatMemberStatus::LEFT => ChatMemberLeft::fromArray($data), + ChatMemberStatus::MEMBER => ChatMemberMember::fromArray($data), + ChatMemberStatus::CREATOR => ChatMemberOwner::fromArray($data), + ChatMemberStatus::RESTRICTED => ChatMemberRestricted::fromArray($data), + default => throw new InvalidArgumentException('Unexpected status for ChatMember') + }; } } \ No newline at end of file diff --git a/src/TgBotLib/Objects/ChatMember/ChatMemberAdministrator.php b/src/TgBotLib/Objects/ChatMember/ChatMemberAdministrator.php index daea8b5..00e18b7 100644 --- a/src/TgBotLib/Objects/ChatMember/ChatMemberAdministrator.php +++ b/src/TgBotLib/Objects/ChatMember/ChatMemberAdministrator.php @@ -9,97 +9,23 @@ use TgBotLib\Objects\ChatMember; use TgBotLib\Objects\User; - class ChatMemberAdministrator implements ObjectTypeInterface + class ChatMemberAdministrator extends ChatMember implements ObjectTypeInterface { - /** - * @var string - */ - private $status; - - /** - * @var User - */ - private $user; - - /** - * @var bool - */ - private $can_be_edited; - - /** - * @var bool - */ - private $is_anonymous; - - /** - * @var bool - */ - private $can_manage_chat; - - /** - * @var bool - */ - private $can_delete_messages; - - /** - * @var bool - */ - private $can_manage_video_chats; - - /** - * @var bool - */ - private $can_restrict_members; - - /** - * @var bool - */ - private $can_promote_members; - - /** - * @var bool - */ - private $can_change_info; - - /** - * @var bool - */ - private $can_invite_users; - - /** - * @var bool - */ - private $can_post_messages; - - /** - * @var bool - */ - private $can_edit_messages; - - /** - * @var bool - */ - private $can_pin_messages; - - /** - * @var bool - */ - private $can_manage_topics; - - /** - * @var string|null - */ - private $custom_title; - - /** - * The member's status in the chat, always “administrator” - * - * @return string - */ - public function getStatus(): string - { - return $this->status; - } + private User $user; + private bool $can_be_edited; + private bool $is_anonymous; + private bool $can_manage_chat; + private bool $can_delete_messages; + private bool $can_manage_video_chats; + private bool $can_restrict_members; + private bool $can_promote_members; + private bool $can_change_info; + private bool $can_invite_users; + private bool $can_post_messages; + private bool $can_edit_messages; + private bool $can_pin_messages; + private bool $can_manage_topics; + private ?string $custom_title; /** * Information about the user @@ -263,8 +189,8 @@ public function toArray(): array { return [ - 'status' => $this->status, - 'user' => ($this->user instanceof ObjectTypeInterface) ? $this->user->toArray() : $this->user, + 'status' => $this->status->value, + 'user' => $this->user?->toArray(), 'can_be_edited' => $this->can_be_edited, 'is_anonymous' => $this->is_anonymous, 'can_manage_chat' => $this->can_manage_chat, @@ -282,17 +208,12 @@ } /** - * Constructs object from an array representation - * - * @param array $data - * @return ChatMemberAdministrator - * @noinspection DuplicatedCode + * @inheritDoc */ - public static function fromArray(array $data): self + public static function fromArray(array $data): ChatMemberAdministrator { $object = new self(); - - $object->status = $data['status'] ?? ChatMemberStatus::ADMINISTRATOR; + $object->status = ChatMemberStatus::ADMINISTRATOR; $object->user = isset($data['user']) ? User::fromArray($data['user']) : null; $object->can_be_edited = $data['can_be_edited'] ?? false; $object->is_anonymous = $data['is_anonymous'] ?? false; @@ -311,34 +232,4 @@ return $object; } - - /** - * Constructs object from ChatMember object - * - * @param ChatMember $chatMember - * @return static - */ - public static function fromChatMember(ChatMember $chatMember): self - { - $object = new self(); - - $object->status = $chatMember->getStatus(); - $object->user = $chatMember->getUser(); - $object->can_be_edited = $chatMember->canBeEdited(); - $object->is_anonymous = $chatMember->isAnonymous(); - $object->can_manage_chat = $chatMember->canManageChat(); - $object->can_delete_messages = $chatMember->canDeleteMessages(); - $object->can_manage_video_chats = $chatMember->canManageVideoChats(); - $object->can_restrict_members = $chatMember->canRestrictMembers(); - $object->can_promote_members = $chatMember->canPromoteMembers(); - $object->can_change_info = $chatMember->canChangeInfo(); - $object->can_invite_users = $chatMember->canInviteUsers(); - $object->can_post_messages = $chatMember->canPostMessages(); - $object->can_edit_messages = $chatMember->canEditMessages(); - $object->can_pin_messages = $chatMember->canPinMessages(); - $object->can_manage_topics = $chatMember->canManageTopics(); - $object->custom_title = $chatMember->getCustomTitle(); - - return $object; - } } \ No newline at end of file diff --git a/src/TgBotLib/Objects/ChatMember/ChatMemberBanned.php b/src/TgBotLib/Objects/ChatMember/ChatMemberBanned.php index cbc95e0..8fd2a6f 100644 --- a/src/TgBotLib/Objects/ChatMember/ChatMemberBanned.php +++ b/src/TgBotLib/Objects/ChatMember/ChatMemberBanned.php @@ -9,32 +9,10 @@ use TgBotLib\Objects\ChatMember; use TgBotLib\Objects\User; - class ChatMemberBanned implements ObjectTypeInterface + class ChatMemberBanned extends ChatMember implements ObjectTypeInterface { - /** - * @var string - */ - private $status; - - /** - * @var User - */ - private $user; - - /** - * @var int - */ - private $until_date; - - /** - * The member's status in the chat, always “kicked” - * - * @return string - */ - public function getStatus(): string - { - return $this->status; - } + private User $user; + private int $until_date; /** * Information about the user @@ -57,50 +35,27 @@ } /** - * Returns an array representation of the object - * - * @return array + * @inheritDoc */ public function toArray(): array { return [ - 'status' => $this->status, - 'user' => ($this->user instanceof ObjectTypeInterface) ? $this->user->toArray() : $this->user, + 'status' => $this->status->value, + 'user' => $this->user?->toArray(), 'until_date' => $this->until_date ]; } /** - * Constructs object from an array representation - * - * @param array $data - * @return ChatMemberBanned + * @inheritDoc */ - public static function fromArray(array $data): self + public static function fromArray(array $data): ChatMemberBanned { $object = new self(); - - $object->status = $data['status'] ?? ChatMemberStatus::KICKED; + $object->status = ChatMemberStatus::KICKED; $object->user = isset($data['user']) ? User::fromArray($data['user']) : null; $object->until_date = $data['until_date'] ?? null; return $object; } - - /** - * Constructs object from a ChatMember object - * - * @param ChatMember $chatMember - * @return ChatMemberBanned - */ - public static function fromChatMember(ChatMember $chatMember): ChatMemberBanned - { - $object = new self(); - - $object->status = $chatMember->getStatus(); - $object->user = $chatMember->getUser(); - $object->until_date = $chatMember->getUntilDate(); - - return $object; - } } \ No newline at end of file diff --git a/src/TgBotLib/Objects/ChatMember/ChatMemberLeft.php b/src/TgBotLib/Objects/ChatMember/ChatMemberLeft.php index 09b4217..cec1c65 100644 --- a/src/TgBotLib/Objects/ChatMember/ChatMemberLeft.php +++ b/src/TgBotLib/Objects/ChatMember/ChatMemberLeft.php @@ -9,27 +9,9 @@ use TgBotLib\Objects\ChatMember; use TgBotLib\Objects\User; - class ChatMemberLeft implements ObjectTypeInterface + class ChatMemberLeft extends ChatMember implements ObjectTypeInterface { - /** - * @var string - */ - private $status; - - /** - * @var User - */ - private $user; - - /** - * The member's status in the chat, always “left” - * - * @return string - */ - public function getStatus(): string - { - return $this->status; - } + private User $user; /** * Information about the user @@ -42,47 +24,25 @@ } /** - * Returns an array representation of the object - * - * @return array + * @inheritDoc */ public function toArray(): array { return [ - 'status' => $this->status, - 'user' => ($this->user instanceof ObjectTypeInterface) ? $this->user->toArray() : $this->user, + 'status' => $this->status->value, + 'user' => $this->user?->toArray() ]; } /** - * Constructs the object from an array representation - * - * @param array $data - * @return ChatMemberLeft + * @inheritDoc */ - public static function fromArray(array $data): self + public static function fromArray(array $data): ChatMemberLeft { $object = new self(); - - $object->status = $data['status'] ?? ChatMemberStatus::LEFT; + $object->status = ChatMemberStatus::LEFT; $object->user = isset($data['user']) ? User::fromArray($data['user']) : null; return $object; } - - /** - * Constructs the object from ChatMember object - * - * @param ChatMember $chatMember - * @return static - */ - public static function fromChatMember(ChatMember $chatMember): self - { - $object = new self(); - - $object->status = $chatMember->getStatus(); - $object->user = $chatMember->getUser(); - - return $object; - } } \ No newline at end of file diff --git a/src/TgBotLib/Objects/ChatMember/ChatMemberMember.php b/src/TgBotLib/Objects/ChatMember/ChatMemberMember.php index 4c3f35e..8628497 100644 --- a/src/TgBotLib/Objects/ChatMember/ChatMemberMember.php +++ b/src/TgBotLib/Objects/ChatMember/ChatMemberMember.php @@ -9,27 +9,9 @@ use TgBotLib\Objects\ChatMember; use TgBotLib\Objects\User; - class ChatMemberMember implements ObjectTypeInterface + class ChatMemberMember extends ChatMember implements ObjectTypeInterface { - /** - * @var string - */ - private $status; - - /** - * @var User - */ - private $user; - - /** - * The member's status in the chat, always “member” - * - * @return string - */ - public function getStatus(): string - { - return $this->status; - } + private User $user; /** * Information about the user @@ -42,47 +24,25 @@ } /** - * Returns an array representation of the object - * - * @return array + * @inheritDoc */ public function toArray(): array { return [ - 'status' => $this->getStatus(), - 'user' => ($this->user instanceof ObjectTypeInterface) ? $this->user->toArray() : $this->user, + 'status' => $this->status->value, + 'user' => $this->user?->toArray() ]; } /** - * Constructs object from an array representation - * - * @param array $data - * @return ChatMemberMember + * @inheritDoc */ - public static function fromArray(array $data): self + public static function fromArray(array $data): ChatMemberMember { $object = new self(); - - $object->status = $data['status'] ?? ChatMemberStatus::MEMBER; + $object->status = ChatMemberStatus::MEMBER; $object->user = isset($data['user']) ? User::fromArray($data['user']) : null; return $object; } - - /** - * Constructs object from ChatMember object - * - * @param ChatMember $chatMember - * @return static - */ - public static function fromChatMember(ChatMember $chatMember): self - { - $object = new self(); - - $object->status = $chatMember->getStatus(); - $object->user = $chatMember->getUser(); - - return $object; - } } \ No newline at end of file diff --git a/src/TgBotLib/Objects/ChatMember/ChatMemberOwner.php b/src/TgBotLib/Objects/ChatMember/ChatMemberOwner.php index 69d23dd..ce11f96 100644 --- a/src/TgBotLib/Objects/ChatMember/ChatMemberOwner.php +++ b/src/TgBotLib/Objects/ChatMember/ChatMemberOwner.php @@ -9,37 +9,11 @@ use TgBotLib\Objects\ChatMember; use TgBotLib\Objects\User; - class ChatMemberOwner implements ObjectTypeInterface + class ChatMemberOwner extends ChatMember implements ObjectTypeInterface { - /** - * @var string - */ - private $status; - - /** - * @var User - */ - private $user; - - /** - * @var bool - */ - private $is_anonymous; - - /** - * @var string|null - */ - private $custom_title; - - /** - * The member's status in the chat, always “creator” - * - * @return string - */ - public function getStatus(): string - { - return $this->status; - } + private User $user; + private bool $is_anonymous; + private ?string $custom_title; /** * Information about the user @@ -72,53 +46,29 @@ } /** - * Returns an array representation of this object - * - * @return array + * @inheritDoc */ public function toArray(): array { return [ - 'status' => $this->status, - 'user' => ($this->user instanceof ObjectTypeInterface) ? $this->user->toArray() : $this->user, + 'status' => $this->status->value, + 'user' => $this->user?->toArray(), 'is_anonymous' => $this->is_anonymous, 'custom_title' => $this->custom_title ]; } /** - * Constructs ChatMemberOwner object from an array representation - * - * @param array $data - * @return ChatMemberOwner + * @inheritDoc */ - public static function fromArray(array $data): self + public static function fromArray(array $data): ChatMemberOwner { $object = new ChatMemberOwner(); - - $object->status = $data['status'] ?? ChatMemberStatus::CREATOR; + $object->status = ChatMemberStatus::CREATOR; $object->user = isset($data['user']) ? User::fromArray($data['user']) : null; $object->is_anonymous = $data['is_anonymous'] ?? false; $object->custom_title = $data['custom_title'] ?? null; return $object; } - - /** - * Constructs ChatMemberOwner object from ChatMember object - * - * @param ChatMember $chatMember - * @return ChatMemberOwner - */ - public static function fromChatMember(ChatMember $chatMember): ChatMemberOwner - { - $object = new ChatMemberOwner(); - - $object->status = $chatMember->getStatus(); - $object->user = $chatMember->getUser(); - $object->is_anonymous = $chatMember->isAnonymous(); - $object->custom_title = $chatMember->getCustomTitle(); - - return $object; - } } \ No newline at end of file diff --git a/src/TgBotLib/Objects/ChatMember/ChatMemberRestricted.php b/src/TgBotLib/Objects/ChatMember/ChatMemberRestricted.php index e77920f..27f9bee 100644 --- a/src/TgBotLib/Objects/ChatMember/ChatMemberRestricted.php +++ b/src/TgBotLib/Objects/ChatMember/ChatMemberRestricted.php @@ -9,107 +9,25 @@ use TgBotLib\Objects\ChatMember; use TgBotLib\Objects\User; - class ChatMemberRestricted implements ObjectTypeInterface + class ChatMemberRestricted extends ChatMember implements ObjectTypeInterface { - /** - * @var string - */ - private $status; - - /** - * @var User - */ - private $user; - - /** - * @var bool - */ - private $is_member; - - /** - * @var bool - */ - private $can_send_messages; - - /** - * @var bool - */ - private $can_send_audios; - - /** - * @var bool - */ - private $can_send_documents; - - /** - * @var bool - */ - private $can_send_photos; - - /** - * @var bool - */ - private $can_send_videos; - - /** - * @var bool - */ - private $can_send_video_notes; - - /** - * @var bool - */ - private $can_send_voice_notes; - - /** - * @var bool - */ - private $can_send_polls; - - /** - * @var bool - */ - private $can_send_other_messages; - - /** - * @var bool - */ - private $can_add_web_page_previews; - - /** - * @var bool - */ - private $can_change_info; - - /** - * @var bool - */ - private $can_invite_users; - - /** - * @var bool - */ - private $can_pin_messages; - - /** - * @var bool - */ - private $can_manage_topics; - - /** - * @var int - */ - private $until_date; - - /** - * The member's status in the chat, always “restricted” - * - * @return string - */ - public function getStatus(): string - { - return $this->status; - } + private User $user; + private bool $is_member; + private bool $can_send_messages; + private bool $can_send_audios; + private bool $can_send_documents; + private bool $can_send_photos; + private bool $can_send_videos; + private bool $can_send_video_notes; + private bool $can_send_voice_notes; + private bool $can_send_polls; + private bool $can_send_other_messages; + private bool $can_add_web_page_previews; + private bool $can_change_info; + private bool $can_invite_users; + private bool $can_pin_messages; + private bool $can_manage_topics; + private int $until_date; /** * Information about the user @@ -282,15 +200,13 @@ } /** - * Returns an array representation of the object. - * - * @return array + * @inheritDoc */ public function toArray(): array { return [ - 'status' => $this->status, - 'user' => ($this->user instanceof ObjectTypeInterface) ? $this->user->toArray() : $this->user, + 'status' => $this->status->value, + 'user' => $this->user?->toArray(), 'is_member' => $this->is_member, 'can_send_messages' => $this->can_send_messages, 'can_send_audios' => $this->can_send_audios, @@ -311,17 +227,12 @@ } /** - * Constructs object from an array representation - * - * @param array $data - * @return ChatMemberRestricted - * @noinspection DuplicatedCode + * @inheritDoc */ - public static function fromArray(array $data): self + public static function fromArray(array $data): ChatMemberRestricted { $object = new static(); - - $object->status = $data['status'] ?? ChatMemberStatus::RESTRICTED; + $object->status = ChatMemberStatus::RESTRICTED; $object->user = isset($data['user']) ? User::fromArray($data['user']) : null; $object->is_member = $data['is_member'] ?? false; $object->can_send_messages = $data['can_send_messages'] ?? false; @@ -342,36 +253,4 @@ return $object; } - - /** - * Constructs object from ChatMember - * - * @param ChatMember $chatMember - * @return static - */ - public static function fromChatMember(ChatMember $chatMember): self - { - $object = new static(); - - $object->status = $chatMember->getStatus(); - $object->user = $chatMember->getUser(); - $object->is_member = $chatMember->isMember(); - $object->can_send_messages = $chatMember->canSendMessages(); - $object->can_send_audios = $chatMember->canSendAudios(); - $object->can_send_documents = $chatMember->canSendDocuments(); - $object->can_send_photos = $chatMember->canSendPhotos(); - $object->can_send_videos = $chatMember->canSendVideos(); - $object->can_send_video_notes = $chatMember->canSendVideoNotes(); - $object->can_send_voice_notes = $chatMember->canSendVoiceNotes(); - $object->can_send_polls = $chatMember->canSendPolls(); - $object->can_send_other_messages = $chatMember->canSendOtherMessages(); - $object->can_add_web_page_previews = $chatMember->canAddWebPagePreviews(); - $object->can_change_info = $chatMember->canChangeInfo(); - $object->can_invite_users = $chatMember->canInviteUsers(); - $object->can_pin_messages = $chatMember->canPinMessages(); - $object->can_manage_topics = $chatMember->canManageTopics(); - $object->until_date = $chatMember->getUntilDate(); - - return $object; - } } \ No newline at end of file