From 703568e1e0e155bbf93c759f741951ac86081497 Mon Sep 17 00:00:00 2001 From: Daniil Gentili Date: Mon, 1 Apr 2024 18:20:14 +0200 Subject: [PATCH] Cleanup --- README.md | 42 ++--------- docs/danog/Decoder/FileIdType.md | 21 +----- src/FileId.php | 10 +-- src/FileIdType.php | 125 ++++++++++++++++--------------- tests/IntegrationTest.php | 11 ++- 5 files changed, 83 insertions(+), 126 deletions(-) diff --git a/README.md b/README.md index 772a419..6121074 100644 --- a/README.md +++ b/README.md @@ -120,50 +120,18 @@ $encoded = (string) $fileId; // CAACAgQAAxkDAAJEsl44nl3yxPZ8biI8uhaA7rbQceOSAAKt ### 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 name is a string version of the file type, typically the one used in bot API file objects. +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 `TYPES` array contains a `file type` => `file type name` map. -The `TYPES_IDS` array contains a `file type name` => `file type` 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. -`const CONSTANTNAME = value` - Description (`type name`) - -* `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` - +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. ### 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 name is a string version of the unique file type, typically the one used in bot API file objects. +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). +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 diff --git a/docs/danog/Decoder/FileIdType.md b/docs/danog/Decoder/FileIdType.md index facaccc..73792a5 100644 --- a/docs/danog/Decoder/FileIdType.md +++ b/docs/danog/Decoder/FileIdType.md @@ -55,34 +55,15 @@ Represents decoded bot API file ID type. ## Properties * `$name`: `string` -* `$value`: `int` +* `$value`: `string` ## Method list: -* [`fromBotApiType(string $type): self`](#frombotapitype-string-type-self) -* [`toBotApiType(): string`](#tobotapitype-string) * [`toUnique(): \danog\Decoder\UniqueFileIdType`](#tounique-danog-decoder-uniquefileidtype) * [`cases(): array`](#cases-array) * [`from(string|int $value): static`](#from-string-int-value-static) * [`tryFrom(string|int $value): ?static`](#tryfrom-string-int-value-static) ## 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` Convert file ID type to unique file ID type. diff --git a/src/FileId.php b/src/FileId.php index 8e127f4..16d2091 100644 --- a/src/FileId.php +++ b/src/FileId.php @@ -138,7 +138,7 @@ final class FileId $access_hash = Tools::unpackLong($read(8)); return new self( $dc_id, - FileIdType::from($typeId), + FileIdType::fromInnerId($typeId), null, $access_hash, fileReference: $fileReference, @@ -153,7 +153,7 @@ final class FileId $volume_id = null; $local_id = null; $photoSizeSource = null; - if ($typeId <= FileIdType::PHOTO->value) { + if ($typeId <= FileIdType::PHOTO->toInnerID()) { if ($subVersion < 32) { $volume_id = Tools::unpackLong($read(8)); $local_id = Tools::unpackInt($read(4)); @@ -174,7 +174,7 @@ final class FileId /** @var array{file_type: int, thumbnail_type: string} */ $result = \unpack('Vfile_type/athumbnail_type', $read(8)); $photoSizeSource = new PhotoSizeSourceThumbnail( - FileIdType::from($result['file_type']), + FileIdType::fromInnerId($result['file_type']), $result['thumbnail_type'] ); break; @@ -233,7 +233,7 @@ final class FileId return new self( dcId: $dc_id, - type: FileIdType::from($typeId), + type: FileIdType::fromInnerId($typeId), id: $id, accessHash: $access_hash, volumeId: $volume_id, @@ -251,7 +251,7 @@ final class FileId */ public function getBotAPI(): string { - $type = $this->type->value; + $type = $this->type->toInnerID(); if ($this->fileReference !== null) { $type |= Tools::FILE_REFERENCE_FLAG; } diff --git a/src/FileIdType.php b/src/FileIdType.php index 798791b..9dbbc57 100644 --- a/src/FileIdType.php +++ b/src/FileIdType.php @@ -25,134 +25,139 @@ use AssertionError; * * @api */ -enum FileIdType: int +enum FileIdType: string { /** * Thumbnail. */ - case THUMBNAIL = 0; + case THUMBNAIL = 'thumbnail'; /** * Profile photo. * Used for users and channels, chat photos are normal PHOTOs. */ - case PROFILE_PHOTO = 1; + case PROFILE_PHOTO = 'profile_photo'; /** * Normal photos. */ - case PHOTO = 2; + case PHOTO = 'photo'; /** * Voice messages. */ - case VOICE = 3; + case VOICE = 'voice'; /** * Video. */ - case VIDEO = 4; + case VIDEO = 'video'; /** * Document. */ - case DOCUMENT = 5; + case DOCUMENT = 'document'; /** * Secret chat document. */ - case ENCRYPTED = 6; + case ENCRYPTED = 'encrypted'; /** * Temporary document. */ - case TEMP = 7; + case TEMP = 'temp'; /** * Sticker. */ - case STICKER = 8; + case STICKER = 'sticker'; /** * Music. */ - case AUDIO = 9; + case AUDIO = 'audio'; /** * GIF. */ - case ANIMATION = 10; + case ANIMATION = 'animation'; /** * Encrypted thumbnail. */ - case ENCRYPTED_THUMBNAIL = 11; + case ENCRYPTED_THUMBNAIL = 'encrypted_thumbnail'; /** * Wallpaper. */ - case WALLPAPER = 12; + case WALLPAPER = 'wallpaper'; /** * Round video. */ - case VIDEO_NOTE = 13; + case VIDEO_NOTE = 'video_note'; /** * Passport raw file. */ - case SECURE_RAW = 14; + case SECURE_RAW = 'secure_raw'; /** * Passport file. */ - case SECURE = 15; + case SECURE = 'secure'; /** * Background. */ - case BACKGROUND = 16; + case BACKGROUND = 'background'; /** * 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) { - 'thumbnail' => self::THUMBNAIL, - 'profile_photo' => self::PROFILE_PHOTO, - 'photo' => self::PHOTO, - 'voice' => self::VOICE, - 'video' => self::VIDEO, - 'document' => self::DOCUMENT, - 'encrypted' => self::ENCRYPTED, - 'temp' => self::TEMP, - 'sticker' => self::STICKER, - 'audio' => self::AUDIO, - 'animation' => self::ANIMATION, - 'encrypted_thumbnail' => self::ENCRYPTED_THUMBNAIL, - 'wallpaper' => self::WALLPAPER, - 'video_note' => self::VIDEO_NOTE, - 'secure_raw' => self::SECURE_RAW, - 'secure' => self::SECURE, - 'background' => self::BACKGROUND, - 'size' => self::SIZE, + return match ($id) { + 0 => self::THUMBNAIL, + 1 => self::PROFILE_PHOTO, + 2 => self::PHOTO, + 3 => self::VOICE, + 4 => self::VIDEO, + 5 => self::DOCUMENT, + 6 => self::ENCRYPTED, + 7 => self::TEMP, + 8 => self::STICKER, + 9 => self::AUDIO, + 10 => self::ANIMATION, + 11 => self::ENCRYPTED_THUMBNAIL, + 12 => self::WALLPAPER, + 13 => self::VIDEO_NOTE, + 14 => self::SECURE_RAW, + 15 => self::SECURE, + 16 => self::BACKGROUND, + 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) { - self::THUMBNAIL => 'thumbnail' , - self::PROFILE_PHOTO => 'profile_photo', - self::PHOTO =>'photo' , - self::VOICE=>'voice' , - self::VIDEO =>'video' , - self::DOCUMENT=> 'document' , - self::ENCRYPTED =>'encrypted' , - self::TEMP =>'temp', - self::STICKER =>'sticker', - self::AUDIO =>'audio', - self::ANIMATION =>'animation', - self::ENCRYPTED_THUMBNAIL =>'encrypted_thumbnail', - self::WALLPAPER =>'wallpaper', - self::VIDEO_NOTE =>'video_note', - self::SECURE_RAW =>'secure_raw', - self::SECURE =>'secure', - self::BACKGROUND=>'background', - self::SIZE=>'size', + self::THUMBNAIL => 0, + self::PROFILE_PHOTO => 1, + self::PHOTO => 2, + self::VOICE=> 3, + self::VIDEO => 4, + self::DOCUMENT=> 5, + self::ENCRYPTED => 6, + self::TEMP => 7, + self::STICKER => 8, + self::AUDIO => 9, + self::ANIMATION => 10, + self::ENCRYPTED_THUMBNAIL => 11, + self::WALLPAPER => 12, + self::VIDEO_NOTE => 13, + self::SECURE_RAW => 14, + self::SECURE => 15, + self::BACKGROUND=> 16, + self::SIZE=>17, }; } diff --git a/tests/IntegrationTest.php b/tests/IntegrationTest.php index 72ac13f..85fc6ce 100644 --- a/tests/IntegrationTest.php +++ b/tests/IntegrationTest.php @@ -9,7 +9,10 @@ use danog\Decoder\UniqueFileId; use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\TestCase; -/** @internal */ +/** + * @api + * @internal + */ class IntegrationTest extends TestCase { #[DataProvider('provideFileIdsAndType')] @@ -56,7 +59,7 @@ class IntegrationTest extends TestCase $result['small_file_unique_id'], ]; yield [ - FileIdType::fromBotApiType('profile_photo'), + FileIdType::from('profile_photo'), $result['big_file_id'], $result['big_file_unique_id'], ]; @@ -85,13 +88,13 @@ class IntegrationTest extends TestCase foreach ($botResult as $subResult) { /** @var string $type */ yield [ - FileIdType::fromBotApiType($type), + FileIdType::from($type), $subResult['file_id'], $subResult['file_unique_id'] ]; if (isset($subResult['thumb'])) { yield [ - FileIdType::fromBotApiType('thumbnail'), + FileIdType::from('thumbnail'), $subResult['thumb']['file_id'], $subResult['thumb']['file_unique_id'] ];