Add automatic toMTProtoId method

This commit is contained in:
Daniil Gentili 2024-08-16 14:08:16 +02:00
parent 1bc37a5e59
commit 6118fe5ad1
4 changed files with 34 additions and 0 deletions

View File

@ -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;
```

View File

@ -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;

View File

@ -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)
};
}
}

View File

@ -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));