mirror of
https://github.com/danog/tg-dialog-id.git
synced 2024-11-26 12:04:55 +01:00
Add automatic toMTProtoId method
This commit is contained in:
parent
1bc37a5e59
commit
6118fe5ad1
@ -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;
|
||||
```
|
||||
|
||||
|
@ -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;
|
||||
|
@ -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)
|
||||
};
|
||||
}
|
||||
}
|
||||
|
@ -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));
|
||||
|
Loading…
Reference in New Issue
Block a user