1
0
mirror of https://github.com/danog/MadelineProto.git synced 2024-11-30 07:18:57 +01:00

Add missing file

This commit is contained in:
Daniil Gentili 2023-09-05 15:34:53 +02:00
parent 26823cc46d
commit 9ea91c23ff
Signed by: danog
GPG Key ID: 8C1BE3B34B230CA7

View File

@ -0,0 +1,87 @@
<?php declare(strict_types=1);
namespace danog\MadelineProto\MTProtoTools;
use AssertionError;
use danog\MadelineProto\Magic;
use Webmozart\Assert\Assert;
/**
* Represents the type of a bot API dialog ID.
*/
enum DialogId
{
case USER;
case CHAT;
case CHANNEL_OR_SUPERGROUP;
case SECRET_CHAT;
/**
* Get the type of a dialog using just its bot API dialog ID.
*
* For more detailed types, use API::getType, instead.
*
* @param integer $id Bot API ID.
*/
public static function getType(int $id): self
{
if ($id < 0) {
if (-Magic::MAX_CHAT_ID <= $id) {
return DialogId::CHAT;
}
if (Magic::ZERO_CHANNEL_ID - Magic::MAX_CHANNEL_ID <= $id && $id !== Magic::ZERO_CHANNEL_ID) {
return DialogId::CHANNEL_OR_SUPERGROUP;
}
if (Magic::ZERO_SECRET_CHAT_ID + Magic::MIN_INT32 <= $id && $id !== Magic::ZERO_SECRET_CHAT_ID) {
return DialogId::SECRET_CHAT;
}
} elseif (0 < $id && $id <= Magic::MAX_USER_ID) {
return DialogId::USER;
}
throw new AssertionError("Invalid ID $id provided!");
}
/**
* Convert MTProto secret chat ID to bot API secret chat ID.
*
* @param int $id MTProto secret chat ID
*
* @return int Bot API secret chat ID
*/
public static function fromSecretChatId(int $id): int
{
return Magic::ZERO_SECRET_CHAT_ID + $id;
}
/**
* Convert bot API secret chat ID to MTProto secret chat ID.
*
* @param int $id Bot API secret chat ID
*
* @return int MTProto secret chat ID
*/
public static function toSecretChatId(int $id): int
{
Assert::eq(self::getType($id), self::SECRET_CHAT);
return $id - Magic::ZERO_SECRET_CHAT_ID;
}
/**
* Convert MTProto channel ID to bot API channel ID.
*
* @param int $id MTProto channel ID
*/
public static function toSupergroupOrChannel(int $id): int
{
return Magic::ZERO_CHANNEL_ID - $id;
}
/**
* Convert bot API channel ID to MTProto channel ID.
*
* @param int $id Bot API channel ID
*/
public static function fromSupergroupOrChannel(int $id): int
{
Assert::eq(self::getType($id), self::SECRET_CHAT);
return (-$id) + Magic::ZERO_CHANNEL_ID;
}
}