This commit is contained in:
Daniil Gentili 2024-04-01 18:20:14 +02:00
parent 49f8b8256a
commit 703568e1e0
5 changed files with 83 additions and 126 deletions

View File

@ -120,50 +120,18 @@ $encoded = (string) $fileId; // CAACAgQAAxkDAAJEsl44nl3yxPZ8biI8uhaA7rbQceOSAAKt
### Bot API file ID types ### Bot API file ID types
The file type is a numeric constant indicating the type of file, (the constant is always in the `danog\Decoder` namespace). The file type is a PHP enum indicating the type of file, [danog\Decoder\FileIdType](https://github.com/danog/tg-file-decoder/blob/master/docs/danog/Decoder/FileIdType.md).
The file type name is a string version of the file type, typically the one used in bot API file objects.
The `TYPES` array contains a `file type` => `file type name` map. Click [here »](https://github.com/danog/tg-file-decoder/blob/master/docs/danog/Decoder/FileIdType.md) to view the full list of file ID types.
The `TYPES_IDS` array contains a `file type name` => `file type` map.
`const CONSTANTNAME = value` - Description (`type name`) The enum also offers a `FileIdType::from` method that can be used to obtain the correct case, from a string version of the file type, typically the one used in bot API file objects.
* `const THUMBNAIL = 0` - Thumbnail (`thumbnail`)
* `const PROFILE_PHOTO = 1` - Profile photo; used for users, supergroups and channels, chat photos are normal PHOTOs (`profile_photo`)
* `const PHOTO = 2` - Photo (`photo`)
* `const VOICE = 3` - Voice message (`voice`)
* `const VIDEO = 4` - Video (`video`)
* `const DOCUMENT = 5` - Document (`document`)
* `const ENCRYPTED = 6` - Secret chat document (`encrypted`)
* `const TEMP = 7` - Temp document (`temp`)
* `const STICKER = 8` - Sticker (`sticker`)
* `const AUDIO = 9` - Music (`audio`)
* `const ANIMATION = 10` - GIF (`animation`)
* `const ENCRYPTED_THUMBNAIL = 11` - Thumbnail of secret chat document (`encrypted_thumbnail`)
* `const WALLPAPER = 12` - Wallpaper (`wallpaper`)
* `const VIDEO_NOTE = 13` - Round video (`video_note`)
* `const SECURE_RAW = 14` - Passport raw file (`secure_raw`)
* `const SECURE = 15` - Passport file (`secure`)
* `const WALLPAPER = 16` - Background (`background`)
* `const WALLPAPER = 17` - Size (`size`)
* `const NONE = 18` -
### Bot API unique file ID types ### Bot API unique file ID types
The unique file type is a numeric constant indicating the type of the unique file ID, (the constant is always in the `danog\Decoder` namespace). The unique file type is a PHP enum uniquely indicating the unique file, [danog\Decoder\UniqueFileIdType](https://github.com/danog/tg-file-decoder/blob/master/docs/danog/Decoder/UniqueFileIdType.md).
The unique file type name is a string version of the unique file type, typically the one used in bot API file objects.
Click [here »](https://github.com/danog/tg-file-decoder/blob/master/docs/danog/Decoder/UniqueFileIdType.md) to view the full list of file ID types.
The `UNIQUE_TYPES` array contains a `unique file type` => `unique file type name` map.
The `UNIQUE_TYPES_IDS` array contains a `unique file type name` => `unique file type` map.
The `FULL_UNIQUE_MAP` array contains a `full file type` => `unique file type` map.
* `const UNIQUE_WEB = 0` - Used for web files (all file types that have a URL (`hasUrl`))
* `const UNIQUE_PHOTO = 1` - Used for photos and similar (`getType() <= PHOTO`)
* `const UNIQUE_DOCUMENT = 2` - Used for all other types of files (documents, audio, video, voice, sticker, animation, video note)
* `const UNIQUE_SECURE = 3` - Used for passport files
* `const UNIQUE_ENCRYPTED = 4` - Used for secret chat files
* `const UNIQUE_TEMP = 5` - Used for temp files
## Full API documentation ## Full API documentation

View File

@ -55,34 +55,15 @@ Represents decoded bot API file ID type.
## Properties ## Properties
* `$name`: `string` * `$name`: `string`
* `$value`: `int` * `$value`: `string`
## Method list: ## Method list:
* [`fromBotApiType(string $type): self`](#frombotapitype-string-type-self)
* [`toBotApiType(): string`](#tobotapitype-string)
* [`toUnique(): \danog\Decoder\UniqueFileIdType`](#tounique-danog-decoder-uniquefileidtype) * [`toUnique(): \danog\Decoder\UniqueFileIdType`](#tounique-danog-decoder-uniquefileidtype)
* [`cases(): array`](#cases-array) * [`cases(): array`](#cases-array)
* [`from(string|int $value): static`](#from-string-int-value-static) * [`from(string|int $value): static`](#from-string-int-value-static)
* [`tryFrom(string|int $value): ?static`](#tryfrom-string-int-value-static) * [`tryFrom(string|int $value): ?static`](#tryfrom-string-int-value-static)
## Methods: ## Methods:
### `fromBotApiType(string $type): self`
Obtain a FileId enum variant from a bot API type name.
Parameters:
* `$type`: `string`
### `toBotApiType(): string`
Obtain a bot API type name.
### `toUnique(): \danog\Decoder\UniqueFileIdType` ### `toUnique(): \danog\Decoder\UniqueFileIdType`
Convert file ID type to unique file ID type. Convert file ID type to unique file ID type.

View File

@ -138,7 +138,7 @@ final class FileId
$access_hash = Tools::unpackLong($read(8)); $access_hash = Tools::unpackLong($read(8));
return new self( return new self(
$dc_id, $dc_id,
FileIdType::from($typeId), FileIdType::fromInnerId($typeId),
null, null,
$access_hash, $access_hash,
fileReference: $fileReference, fileReference: $fileReference,
@ -153,7 +153,7 @@ final class FileId
$volume_id = null; $volume_id = null;
$local_id = null; $local_id = null;
$photoSizeSource = null; $photoSizeSource = null;
if ($typeId <= FileIdType::PHOTO->value) { if ($typeId <= FileIdType::PHOTO->toInnerID()) {
if ($subVersion < 32) { if ($subVersion < 32) {
$volume_id = Tools::unpackLong($read(8)); $volume_id = Tools::unpackLong($read(8));
$local_id = Tools::unpackInt($read(4)); $local_id = Tools::unpackInt($read(4));
@ -174,7 +174,7 @@ final class FileId
/** @var array{file_type: int, thumbnail_type: string} */ /** @var array{file_type: int, thumbnail_type: string} */
$result = \unpack('Vfile_type/athumbnail_type', $read(8)); $result = \unpack('Vfile_type/athumbnail_type', $read(8));
$photoSizeSource = new PhotoSizeSourceThumbnail( $photoSizeSource = new PhotoSizeSourceThumbnail(
FileIdType::from($result['file_type']), FileIdType::fromInnerId($result['file_type']),
$result['thumbnail_type'] $result['thumbnail_type']
); );
break; break;
@ -233,7 +233,7 @@ final class FileId
return new self( return new self(
dcId: $dc_id, dcId: $dc_id,
type: FileIdType::from($typeId), type: FileIdType::fromInnerId($typeId),
id: $id, id: $id,
accessHash: $access_hash, accessHash: $access_hash,
volumeId: $volume_id, volumeId: $volume_id,
@ -251,7 +251,7 @@ final class FileId
*/ */
public function getBotAPI(): string public function getBotAPI(): string
{ {
$type = $this->type->value; $type = $this->type->toInnerID();
if ($this->fileReference !== null) { if ($this->fileReference !== null) {
$type |= Tools::FILE_REFERENCE_FLAG; $type |= Tools::FILE_REFERENCE_FLAG;
} }

View File

@ -25,134 +25,139 @@ use AssertionError;
* *
* @api * @api
*/ */
enum FileIdType: int enum FileIdType: string
{ {
/** /**
* Thumbnail. * Thumbnail.
*/ */
case THUMBNAIL = 0; case THUMBNAIL = 'thumbnail';
/** /**
* Profile photo. * Profile photo.
* Used for users and channels, chat photos are normal PHOTOs. * Used for users and channels, chat photos are normal PHOTOs.
*/ */
case PROFILE_PHOTO = 1; case PROFILE_PHOTO = 'profile_photo';
/** /**
* Normal photos. * Normal photos.
*/ */
case PHOTO = 2; case PHOTO = 'photo';
/** /**
* Voice messages. * Voice messages.
*/ */
case VOICE = 3; case VOICE = 'voice';
/** /**
* Video. * Video.
*/ */
case VIDEO = 4; case VIDEO = 'video';
/** /**
* Document. * Document.
*/ */
case DOCUMENT = 5; case DOCUMENT = 'document';
/** /**
* Secret chat document. * Secret chat document.
*/ */
case ENCRYPTED = 6; case ENCRYPTED = 'encrypted';
/** /**
* Temporary document. * Temporary document.
*/ */
case TEMP = 7; case TEMP = 'temp';
/** /**
* Sticker. * Sticker.
*/ */
case STICKER = 8; case STICKER = 'sticker';
/** /**
* Music. * Music.
*/ */
case AUDIO = 9; case AUDIO = 'audio';
/** /**
* GIF. * GIF.
*/ */
case ANIMATION = 10; case ANIMATION = 'animation';
/** /**
* Encrypted thumbnail. * Encrypted thumbnail.
*/ */
case ENCRYPTED_THUMBNAIL = 11; case ENCRYPTED_THUMBNAIL = 'encrypted_thumbnail';
/** /**
* Wallpaper. * Wallpaper.
*/ */
case WALLPAPER = 12; case WALLPAPER = 'wallpaper';
/** /**
* Round video. * Round video.
*/ */
case VIDEO_NOTE = 13; case VIDEO_NOTE = 'video_note';
/** /**
* Passport raw file. * Passport raw file.
*/ */
case SECURE_RAW = 14; case SECURE_RAW = 'secure_raw';
/** /**
* Passport file. * Passport file.
*/ */
case SECURE = 15; case SECURE = 'secure';
/** /**
* Background. * Background.
*/ */
case BACKGROUND = 16; case BACKGROUND = 'background';
/** /**
* Size. * Size.
*/ */
case SIZE = 17; case SIZE = 'size';
/** @internal Should not be used manually. */
/** /**
* Obtain a FileId enum variant from a bot API type name. * Obtain a bot API type ID.
*
* @internal Should not be used manually.
*/ */
public static function fromBotApiType(string $type): self public static function fromInnerID(int $id): self
{ {
return match ($type) { return match ($id) {
'thumbnail' => self::THUMBNAIL, 0 => self::THUMBNAIL,
'profile_photo' => self::PROFILE_PHOTO, 1 => self::PROFILE_PHOTO,
'photo' => self::PHOTO, 2 => self::PHOTO,
'voice' => self::VOICE, 3 => self::VOICE,
'video' => self::VIDEO, 4 => self::VIDEO,
'document' => self::DOCUMENT, 5 => self::DOCUMENT,
'encrypted' => self::ENCRYPTED, 6 => self::ENCRYPTED,
'temp' => self::TEMP, 7 => self::TEMP,
'sticker' => self::STICKER, 8 => self::STICKER,
'audio' => self::AUDIO, 9 => self::AUDIO,
'animation' => self::ANIMATION, 10 => self::ANIMATION,
'encrypted_thumbnail' => self::ENCRYPTED_THUMBNAIL, 11 => self::ENCRYPTED_THUMBNAIL,
'wallpaper' => self::WALLPAPER, 12 => self::WALLPAPER,
'video_note' => self::VIDEO_NOTE, 13 => self::VIDEO_NOTE,
'secure_raw' => self::SECURE_RAW, 14 => self::SECURE_RAW,
'secure' => self::SECURE, 15 => self::SECURE,
'background' => self::BACKGROUND, 16 => self::BACKGROUND,
'size' => self::SIZE, 17 => self::SIZE,
}; };
} }
/** /**
* Obtain a bot API type name. * Obtain a bot API type ID.
*
* @internal Should not be used manually.
*/ */
public function toBotApiType(): string public function toInnerID(): int
{ {
return match ($this) { return match ($this) {
self::THUMBNAIL => 'thumbnail' , self::THUMBNAIL => 0,
self::PROFILE_PHOTO => 'profile_photo', self::PROFILE_PHOTO => 1,
self::PHOTO =>'photo' , self::PHOTO => 2,
self::VOICE=>'voice' , self::VOICE=> 3,
self::VIDEO =>'video' , self::VIDEO => 4,
self::DOCUMENT=> 'document' , self::DOCUMENT=> 5,
self::ENCRYPTED =>'encrypted' , self::ENCRYPTED => 6,
self::TEMP =>'temp', self::TEMP => 7,
self::STICKER =>'sticker', self::STICKER => 8,
self::AUDIO =>'audio', self::AUDIO => 9,
self::ANIMATION =>'animation', self::ANIMATION => 10,
self::ENCRYPTED_THUMBNAIL =>'encrypted_thumbnail', self::ENCRYPTED_THUMBNAIL => 11,
self::WALLPAPER =>'wallpaper', self::WALLPAPER => 12,
self::VIDEO_NOTE =>'video_note', self::VIDEO_NOTE => 13,
self::SECURE_RAW =>'secure_raw', self::SECURE_RAW => 14,
self::SECURE =>'secure', self::SECURE => 15,
self::BACKGROUND=>'background', self::BACKGROUND=> 16,
self::SIZE=>'size', self::SIZE=>17,
}; };
} }

View File

@ -9,7 +9,10 @@ use danog\Decoder\UniqueFileId;
use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
/** @internal */ /**
* @api
* @internal
*/
class IntegrationTest extends TestCase class IntegrationTest extends TestCase
{ {
#[DataProvider('provideFileIdsAndType')] #[DataProvider('provideFileIdsAndType')]
@ -56,7 +59,7 @@ class IntegrationTest extends TestCase
$result['small_file_unique_id'], $result['small_file_unique_id'],
]; ];
yield [ yield [
FileIdType::fromBotApiType('profile_photo'), FileIdType::from('profile_photo'),
$result['big_file_id'], $result['big_file_id'],
$result['big_file_unique_id'], $result['big_file_unique_id'],
]; ];
@ -85,13 +88,13 @@ class IntegrationTest extends TestCase
foreach ($botResult as $subResult) { foreach ($botResult as $subResult) {
/** @var string $type */ /** @var string $type */
yield [ yield [
FileIdType::fromBotApiType($type), FileIdType::from($type),
$subResult['file_id'], $subResult['file_id'],
$subResult['file_unique_id'] $subResult['file_unique_id']
]; ];
if (isset($subResult['thumb'])) { if (isset($subResult['thumb'])) {
yield [ yield [
FileIdType::fromBotApiType('thumbnail'), FileIdType::from('thumbnail'),
$subResult['thumb']['file_id'], $subResult['thumb']['file_id'],
$subResult['thumb']['file_unique_id'] $subResult['thumb']['file_unique_id']
]; ];