tgbotlib/src/TgBotLib/Objects/Chat.php

130 lines
3.6 KiB
PHP
Raw Normal View History

2023-02-12 14:58:38 -05:00
<?php
2024-10-02 00:18:12 -04:00
namespace TgBotLib\Objects;
2023-02-12 14:58:38 -05:00
2024-09-29 21:49:10 -04:00
use TgBotLib\Enums\Types\ChatType;
2023-02-12 14:58:38 -05:00
use TgBotLib\Interfaces\ObjectTypeInterface;
class Chat implements ObjectTypeInterface
{
2024-10-04 12:29:29 -04:00
private int $id;
private ?ChatType $type;
2024-10-04 12:29:29 -04:00
private ?string $title;
private ?string $username;
private ?string $first_name;
private ?string $last_name;
private bool $is_forum;
2023-02-12 14:58:38 -05:00
/**
* Unique identifier for this chat. This number may have more than 32 significant bits and some programming
* languages may have difficulty/silent defects in interpreting it. But it has at most 52 significant bits,
* so a signed 64-bit integer or double-precision float type are safe for storing this identifier.
*
* @return int
*/
public function getId(): int
{
return $this->id;
}
/**
* Type of chat, can be either “private”, “group”, “supergroup” or “channel”
*
2024-10-04 12:29:29 -04:00
* @return ChatType
2023-02-12 14:58:38 -05:00
* @see ChatType
*/
2024-10-04 12:29:29 -04:00
public function getType(): ChatType
2023-02-12 14:58:38 -05:00
{
return $this->type;
}
/**
* Optional. Title, for supergroups, channels and group chats
*
* @return string|null
*/
public function getTitle(): ?string
{
return $this->title;
}
/**
* Optional. Username, for private chats, supergroups and channels if available
*
* @return string|null
*/
public function getUsername(): ?string
{
return $this->username;
}
/**
* Optional. First name of the other party in a private chat
*
* @return string|null
*/
public function getFirstName(): ?string
{
return $this->first_name;
}
/**
* Optional. Last name of the other party in a private chat
*
* @return string|null
*/
public function getLastName(): ?string
{
return $this->last_name;
}
/**
* Optional. True, if the supergroup chat is a forum (has topics enabled)
*
* @see https://telegram.org/blog/topics-in-groups-collectible-usernames#topics-in-groups
* @return bool
*/
2023-02-14 17:35:16 -05:00
public function isForum(): bool
2023-02-12 14:58:38 -05:00
{
return $this->is_forum;
}
/**
2024-10-04 12:29:29 -04:00
* @inheritDoc
2023-02-12 14:58:38 -05:00
*/
public function toArray(): array
{
return [
'id' => $this->id,
2024-10-04 12:29:29 -04:00
'type' => $this->type->value,
2023-02-12 14:58:38 -05:00
'title' => $this->title,
'username' => $this->username,
'first_name' => $this->first_name,
'last_name' => $this->last_name,
2024-10-04 12:29:29 -04:00
'is_forum' => $this->is_forum
2023-02-12 14:58:38 -05:00
];
}
/**
2024-10-04 12:29:29 -04:00
* @inheritDoc
2023-02-12 14:58:38 -05:00
*/
2024-10-04 12:29:29 -04:00
public static function fromArray(?array $data): ?Chat
2023-02-12 14:58:38 -05:00
{
2024-10-04 12:29:29 -04:00
if($data === null)
{
return null;
}
2023-02-12 14:58:38 -05:00
2024-10-04 12:29:29 -04:00
$object = new self();
2023-02-14 17:35:16 -05:00
$object->id = $data['id'] ?? null;
2024-10-04 12:29:29 -04:00
$object->type = ChatType::tryFrom($data['type']) ?? null;
2023-02-14 17:35:16 -05:00
$object->title = $data['title'] ?? null;
$object->username = $data['username'] ?? null;
$object->first_name = $data['first_name'] ?? null;
$object->last_name = $data['last_name'] ?? null;
$object->is_forum = $data['is_forum'] ?? false;
2023-02-12 14:58:38 -05:00
return $object;
}
}