Skip to content

Commit

Permalink
Modernize
Browse files Browse the repository at this point in the history
  • Loading branch information
danog committed Apr 1, 2024
1 parent 500f131 commit 464800a
Show file tree
Hide file tree
Showing 16 changed files with 313 additions and 314 deletions.
1 change: 1 addition & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,6 @@ jobs:
TOKEN: ${{ secrets.TOKEN }}
DEST: ${{ secrets.DEST }}
run: |
vendor/bin/psalm --no-cache
vendor/bin/phpunit --coverage-text --coverage-clover build/logs/clover.xml
vendor/bin/php-cs-fixer --diff --dry-run -v fix
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,15 @@
},
"require-dev": {
"phpunit/phpunit": "^9",
"amphp/php-cs-fixer-config": "^2"
"amphp/php-cs-fixer-config": "^2",
"vimeo/psalm": "dev-master"
},
"autoload": {
"psr-4": {
"danog\\Decoder\\": "src/"
},
"files": [
"src/type.php"
"src/functions.php"
]
},
"autoload-dev": {
Expand Down
8 changes: 8 additions & 0 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<files psalm-version="dev-master@ef3b018e89c4ffc157332c13e2ebf9cf22320d17">
<file src="src/functions.php">
<UnusedVariable>
<code><![CDATA[$BIG_ENDIAN]]></code>
</UnusedVariable>
</file>
</files>
1 change: 1 addition & 0 deletions psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://getpsalm.org/schema/config"
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
errorBaseline="psalm-baseline.xml"
>
<projectFiles>
<directory name="src" />
Expand Down
75 changes: 37 additions & 38 deletions src/FileId.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@

/**
* Represents decoded bot API file ID.
*
* @api
*/
class FileId
final class FileId
{
/**
* Bot API file ID version.
Expand All @@ -51,7 +53,7 @@ class FileId
* File type.
*
*/
private int $type = 0;
private FileIdType $type = FileIdType::NONE;

/**
* File reference.
Expand Down Expand Up @@ -123,39 +125,39 @@ public static function fromBotAPI(string $fileId): self
}
$result->setId($resultArray['id']);

if ($result->getType() <= PHOTO) {
if ($result->getType()->value <= FileIdType::PHOTO->value) {
if (isset($resultArray['volume_id'])) {
$result->setVolumeId($resultArray['volume_id']);
}
if (isset($resultArray['local_id'])) {
$result->setLocalId($resultArray['local_id']);
}
switch ($resultArray['photosize_source']) {
case PHOTOSIZE_SOURCE_LEGACY:
case PHOTOSIZE_SOURCE_FULL_LEGACY:
case PhotoSizeSourceType::LEGACY:
case PhotoSizeSourceType::FULL_LEGACY:
$photoSizeSource = new PhotoSizeSourceLegacy($resultArray['photosize_source']);
$photoSizeSource->setSecret($resultArray['secret']);
break;
case PHOTOSIZE_SOURCE_THUMBNAIL:
case PhotoSizeSourceType::THUMBNAIL:
$photoSizeSource = new PhotoSizeSourceThumbnail($resultArray['photosize_source']);
$photoSizeSource->setThumbType($resultArray['thumbnail_type']);
$photoSizeSource->setThumbFileType($resultArray['file_type']);
break;
case PHOTOSIZE_SOURCE_DIALOGPHOTO_BIG_LEGACY:
case PHOTOSIZE_SOURCE_DIALOGPHOTO_SMALL_LEGACY:
case PHOTOSIZE_SOURCE_DIALOGPHOTO_BIG:
case PHOTOSIZE_SOURCE_DIALOGPHOTO_SMALL:
case PhotoSizeSourceType::DIALOGPHOTO_BIG_LEGACY:
case PhotoSizeSourceType::DIALOGPHOTO_SMALL_LEGACY:
case PhotoSizeSourceType::DIALOGPHOTO_BIG:
case PhotoSizeSourceType::DIALOGPHOTO_SMALL:
$photoSizeSource = new PhotoSizeSourceDialogPhoto($resultArray['photosize_source']);
$photoSizeSource->setDialogId($resultArray['dialog_id']);
$photoSizeSource->setDialogAccessHash($resultArray['dialog_access_hash']);
break;
case PHOTOSIZE_SOURCE_STICKERSET_THUMBNAIL:
case PHOTOSIZE_SOURCE_STICKERSET_THUMBNAIL_LEGACY:
case PhotoSizeSourceType::STICKERSET_THUMBNAIL:
case PhotoSizeSourceType::STICKERSET_THUMBNAIL_LEGACY:
$photoSizeSource = new PhotoSizeSourceStickersetThumbnail($resultArray['photosize_source']);
$photoSizeSource->setStickerSetId($resultArray['sticker_set_id']);
$photoSizeSource->setStickerSetAccessHash($resultArray['sticker_set_access_hash']);
break;
case PHOTOSIZE_SOURCE_STICKERSET_THUMBNAIL_VERSION:
case PhotoSizeSourceType::STICKERSET_THUMBNAIL_VERSION:
$photoSizeSource = new PhotoSizeSourceStickersetThumbnailVersion($resultArray['photosize_source']);
$photoSizeSource->setStickerSetId($resultArray['sticker_set_id']);
$photoSizeSource->setStickerSetAccessHash($resultArray['sticker_set_access_hash']);
Expand All @@ -174,7 +176,7 @@ public static function fromBotAPI(string $fileId): self
*/
public function getBotAPI(): string
{
$type = $this->getType();
$type = $this->getType()->value;
if ($this->hasFileReference()) {
$type |= FILE_REFERENCE_FLAG;
}
Expand All @@ -195,40 +197,46 @@ public function getBotAPI(): string
$fileId .= packLong($this->getId());
$fileId .= packLong($this->getAccessHash());

if ($this->getType() <= PHOTO) {
if ($this->getType()->value <= FileIdType::PHOTO->value) {
$photoSize = $this->getPhotoSizeSource();
$fileId .= \pack('V', $photoSize->getType());
switch ($photoSize->getType()) {
case PHOTOSIZE_SOURCE_LEGACY:
case PhotoSizeSourceType::LEGACY:
assert($photoSize instanceof PhotoSizeSourceLegacy);
$fileId .= packLong($photoSize->getSecret());
break;
case PHOTOSIZE_SOURCE_FULL_LEGACY:
case PhotoSizeSourceType::FULL_LEGACY:
assert($photoSize instanceof PhotoSizeSourceLegacy);
$fileId .= packLong($this->getVolumeId());
$fileId .= packLong($photoSize->getSecret());
$fileId .= \pack('l', $this->getLocalId());
break;
case PHOTOSIZE_SOURCE_THUMBNAIL:
case PhotoSizeSourceType::THUMBNAIL:
assert($photoSize instanceof PhotoSizeSourceThumbnail);
$fileId .= \pack('Va4', $photoSize->getThumbFileType(), $photoSize->getThumbType());
break;
case PHOTOSIZE_SOURCE_DIALOGPHOTO_BIG:
case PHOTOSIZE_SOURCE_DIALOGPHOTO_SMALL:
case PHOTOSIZE_SOURCE_DIALOGPHOTO_BIG_LEGACY:
case PHOTOSIZE_SOURCE_DIALOGPHOTO_SMALL_LEGACY:
case PhotoSizeSourceType::DIALOGPHOTO_BIG:
case PhotoSizeSourceType::DIALOGPHOTO_SMALL:
case PhotoSizeSourceType::DIALOGPHOTO_BIG_LEGACY:
case PhotoSizeSourceType::DIALOGPHOTO_SMALL_LEGACY:
assert($photoSize instanceof PhotoSizeSourceDialogPhoto);
$fileId .= packLongBig($photoSize->getDialogId());
$fileId .= packLong($photoSize->getDialogAccessHash());
break;
case PHOTOSIZE_SOURCE_STICKERSET_THUMBNAIL:
case PHOTOSIZE_SOURCE_STICKERSET_THUMBNAIL_LEGACY:
case PhotoSizeSourceType::STICKERSET_THUMBNAIL:
case PhotoSizeSourceType::STICKERSET_THUMBNAIL_LEGACY:
assert($photoSize instanceof PhotoSizeSourceStickersetThumbnail);
$fileId .= packLong($photoSize->getStickerSetId());
$fileId .= packLong($photoSize->getStickerSetAccessHash());
break;
case PHOTOSIZE_SOURCE_STICKERSET_THUMBNAIL_VERSION:
case PhotoSizeSourceType::STICKERSET_THUMBNAIL_VERSION:
assert($photoSize instanceof PhotoSizeSourceStickersetThumbnailVersion);
$fileId .= packLong($photoSize->getStickerSetId());
$fileId .= packLong($photoSize->getStickerSetAccessHash());
$fileId .= \pack('l', $photoSize->getStickerSetVersion());
break;
}
if ($photoSize->getType() >= PHOTOSIZE_SOURCE_DIALOGPHOTO_SMALL_LEGACY && $photoSize->getType() <= PHOTOSIZE_SOURCE_STICKERSET_THUMBNAIL_LEGACY) {
if ($photoSize->getType() >= PhotoSizeSourceType::DIALOGPHOTO_SMALL_LEGACY && $photoSize->getType()->value <= PhotoSizeSourceType::STICKERSET_THUMBNAIL_LEGACY->value) {
$fileId .= packLong($this->getVolumeId());
$fileId .= \pack('l', $this->getLocalId());
}
Expand Down Expand Up @@ -314,27 +322,18 @@ public function setSubVersion(int $subVersion): self
* Get file type.
*
*/
public function getType(): int
public function getType(): FileIdType
{
return $this->type;
}

/**
* Get file type as string.
*
*/
public function getTypeName(): string
{
return TYPES[$this->type];
}

/**
* Set file type.
*
* @param int $type File type.
* @param FileIdType $type File type.
*
*/
public function setType(int $type): self
public function setType(FileIdType $type): self
{
$this->type = $type;

Expand Down
132 changes: 132 additions & 0 deletions src/FileIdType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
<?php declare(strict_types=1);
/**
* Type enum + helper functions.
*
* This file is part of tg-file-decoder.
* tg-file-decoder is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
* tg-file-decoder is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Affero General Public License for more details.
* You should have received a copy of the GNU General Public License along with tg-file-decoder.
* If not, see <http://www.gnu.org/licenses/>.
*
* @author Daniil Gentili <[email protected]>
* @copyright 2016-2019 Daniil Gentili <[email protected]>
* @license https://opensource.org/licenses/AGPL-3.0 AGPLv3
*
* @link https://github.com/tg-file-decoder Documentation
*/

namespace danog\Decoder;

use AssertionError;

enum FileIdType: int
{
/**
* Thumbnail.
*/
case THUMBNAIL = 0;
/**
* Profile photo.
* Used for users and channels, chat photos are normal PHOTOs.
*/
case PROFILE_PHOTO = 1;
/**
* Normal photos.
*/
case PHOTO = 2;

/**
* Voice messages.
*/
case VOICE = 3;
/**
* Video.
*/
case VIDEO = 4;
/**
* Document.
*/
case DOCUMENT = 5;
/**
* Secret chat document.
*/
case ENCRYPTED = 6;
/**
* Temporary document.
*/
case TEMP = 7;
/**
* Sticker.
*/
case STICKER = 8;
/**
* Music.
*/
case AUDIO = 9;
/**
* GIF.
*/
case ANIMATION = 10;
/**
* Encrypted thumbnail.
*/
case ENCRYPTED_THUMBNAIL = 11;
/**
* Wallpaper.
*/
case WALLPAPER = 12;
/**
* Round video.
*/
case VIDEO_NOTE = 13;
/**
* Passport raw file.
*/
case SECURE_RAW = 14;
/**
* Passport file.
*/
case SECURE = 15;
/**
* Background.
*/
case BACKGROUND = 16;
/**
* Size.
*/
case SIZE = 17;
case NONE = 18;

/**
* Convert file ID type to unique file ID type.
*/
public function toUnique(): UniqueFileIdType
{
return match ($this) {
self::PHOTO => UniqueFileIdType::PHOTO,
self::PROFILE_PHOTO => UniqueFileIdType::PHOTO,
self::THUMBNAIL => UniqueFileIdType::PHOTO,
self::ENCRYPTED_THUMBNAIL => UniqueFileIdType::PHOTO,
self::WALLPAPER => UniqueFileIdType::PHOTO,

self::VIDEO => UniqueFileIdType::DOCUMENT,
self::VOICE => UniqueFileIdType::DOCUMENT,
self::DOCUMENT => UniqueFileIdType::DOCUMENT,
self::STICKER => UniqueFileIdType::DOCUMENT,
self::AUDIO => UniqueFileIdType::DOCUMENT,
self::ANIMATION => UniqueFileIdType::DOCUMENT,
self::VIDEO_NOTE => UniqueFileIdType::DOCUMENT,
self::BACKGROUND => UniqueFileIdType::DOCUMENT,

self::SECURE => UniqueFileIdType::SECURE,
self::SECURE_RAW => UniqueFileIdType::SECURE,

self::ENCRYPTED => UniqueFileIdType::ENCRYPTED,

self::TEMP => UniqueFileIdType::TEMP,

default => throw new AssertionError("Cannot convert file ID of type ".$this->name." to a unique file ID!")
};
}
}
18 changes: 8 additions & 10 deletions src/PhotoSizeSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,16 @@ abstract class PhotoSizeSource
* Source type.
*
*/
private int $type;
private PhotoSizeSourceType $type;

/**
* Set photosize source type.
*
* @param integer $type Type
* @param PhotoSizeSourceType $type Type
*/
public function __construct(int $type)
public function __construct(PhotoSizeSourceType $type)
{
$this->type = $type;

return $this;
}
/**
* Get photosize source type.
Expand All @@ -51,18 +49,18 @@ public function __construct(int $type)
*
* @psalm-return (
* T is PhotoSizeSourceLegacy ?
* ? \danog\Decoder\PHOTOSIZE_SOURCE_LEGACY
* ? \danog\Decoder\PhotoSizeSourceType::LEGACY
* : (T is PhotoSizeSourceDialogPhoto
* ? \danog\Decoder\PHOTOSIZE_SOURCE_DIALOGPHOTO_*
* ? \danog\Decoder\PhotoSizeSourceType::DIALOGPHOTO_*
* (T is PhotoSizeSourceStickersetThumbnail
* ? \danog\Decoder\PHOTOSIZE_SOURCE_STICKERSET_THUMBNAIL
* : \danog\Decoder\PHOTOSIZE_SOURCE_THUMBNAIL
* ? \danog\Decoder\PhotoSizeSourceType::STICKERSET_THUMBNAIL
* : \danog\Decoder\PhotoSizeSourceType::THUMBNAIL
* )
* )
*
* @internal Internal use
*/
public function getType(): int
public function getType(): PhotoSizeSourceType
{
return $this->type;
}
Expand Down
Loading

0 comments on commit 464800a

Please sign in to comment.