From 6118fe5ad17b068237a43262d459664869affcc0 Mon Sep 17 00:00:00 2001 From: Daniil Gentili Date: Fri, 16 Aug 2024 14:08:16 +0200 Subject: [PATCH] Add automatic toMTProtoId method --- README.md | 6 ++++++ examples/1-type.php | 6 ++++++ src/DialogId.php | 17 +++++++++++++++++ test/DialogIdTest.php | 5 +++++ 4 files changed, 34 insertions(+) diff --git a/README.md b/README.md index 38bc3a0..03d4e7a 100644 --- a/README.md +++ b/README.md @@ -79,6 +79,12 @@ expect(DialogId::isChat(-101374607) === true); expect(DialogId::isSupergroupOrChannel(-1001234567890) === true); expect(DialogId::isSecretChat(-1999898625393) === true); +// Converts a bot API dialog ID => MTProto ID automatically depending on type +expect(DialogId::toMTProtoId(-1001234567890) === 1234567890); +expect(DialogId::toMTProtoId(-123456789) === 123456789); +expect(DialogId::toMTProtoId(123456789) === 123456789); +expect(DialogId::toMTProtoId(-1999876543211) === 123456789); + echo "OK!".PHP_EOL; ``` diff --git a/examples/1-type.php b/examples/1-type.php index 21f344c..ae3add1 100644 --- a/examples/1-type.php +++ b/examples/1-type.php @@ -56,4 +56,10 @@ expect(DialogId::isChat(-101374607) === true); expect(DialogId::isSupergroupOrChannel(-1001234567890) === true); expect(DialogId::isSecretChat(-1999898625393) === true); +// Converts a bot API dialog ID => MTProto ID automatically depending on type +expect(DialogId::toMTProtoId(-1001234567890) === 1234567890); +expect(DialogId::toMTProtoId(-123456789) === 123456789); +expect(DialogId::toMTProtoId(123456789) === 123456789); +expect(DialogId::toMTProtoId(-1999876543211) === 123456789); + echo "OK!".PHP_EOL; diff --git a/src/DialogId.php b/src/DialogId.php index 48514d1..e8d1690 100644 --- a/src/DialogId.php +++ b/src/DialogId.php @@ -231,4 +231,21 @@ enum DialogId } return $id; } + + /** + * Convert bot API ID to MTProto ID (automatically detecting the correct type). + * + * @psalm-pure + * + * @param int $id Bot API dialog ID + */ + public static function toMTProtoId(int $id): int + { + return match (self::getType($id)) { + self::USER => self::toUserId($id), + self::CHAT => self::toChatId($id), + self::CHANNEL_OR_SUPERGROUP => self::toSupergroupOrChannelId($id), + self::SECRET_CHAT => self::toSecretChatId($id) + }; + } } diff --git a/test/DialogIdTest.php b/test/DialogIdTest.php index 443bb17..f9071e7 100644 --- a/test/DialogIdTest.php +++ b/test/DialogIdTest.php @@ -30,6 +30,11 @@ final class DialogIdTest extends TestCase $this->assertSame(1234567890, DialogId::toSupergroupOrChannelId(-1001234567890)); $this->assertSame(101374607, DialogId::toSecretChatId(-1999898625393)); + $this->assertSame(101374607, DialogId::toMTProtoId(101374607)); + $this->assertSame(101374607, DialogId::toMTProtoId(-101374607)); + $this->assertSame(1234567890, DialogId::toMTProtoId(-1001234567890)); + $this->assertSame(101374607, DialogId::toMTProtoId(-1999898625393)); + $this->assertSame(101374607, DialogId::fromUserId(101374607)); $this->assertSame(-101374607, DialogId::fromChatId(101374607)); $this->assertSame(-1001234567890, DialogId::fromSupergroupOrChannelId(1234567890));