Updated classes fromArray signatures (in-progress)

This commit is contained in:
netkas 2024-10-03 21:14:27 -04:00
parent 59f6875edf
commit dfb812237b
43 changed files with 288 additions and 75 deletions

View file

@ -14,8 +14,8 @@
/**
* Reconstruction of the object from an array
*
* @param array $data
* @return ObjectTypeInterface
* @param array|null $data
* @return ObjectTypeInterface|null
*/
public static function fromArray(array $data): ObjectTypeInterface;
public static function fromArray(?array $data): ?ObjectTypeInterface;
}

View file

@ -31,8 +31,13 @@ abstract class BackgroundFill implements ObjectTypeInterface
/**
* @inheritDoc
*/
public static function fromArray(array $data): BackgroundFill
public static function fromArray(?array $data): ?BackgroundFill
{
if($data === null)
{
return null;
}
if(!isset($data['type']))
{
throw new InvalidArgumentException('BackgroundFill expected type');

View file

@ -37,8 +37,13 @@ class BackgroundFillFreeformGradient extends BackgroundFill implements ObjectTyp
/**
* @inheritDoc
*/
public static function fromArray(array $data): BackgroundFill
public static function fromArray(?array $data): ?BackgroundFillFreeformGradient
{
if($data === null)
{
return null;
}
$object = new self();
$object->type = BackgroundFillType::FREEFORM_GRADIENT;

View file

@ -57,8 +57,13 @@ class BackgroundFillGradient extends BackgroundFill implements ObjectTypeInterfa
/**
* @inheritDoc
*/
public static function fromArray(array $data): BackgroundFillGradient
public static function fromArray(?array $data): ?BackgroundFillGradient
{
if($data === null)
{
return null;
}
$object = new self();
$object->type = $data['type'] ?? null;
$object->top_color = $data['top_color'] ?? 0;

View file

@ -34,10 +34,14 @@ class BackgroundFillSolid extends BackgroundFill implements ObjectTypeInterface
/**
* @inheritDoc
*/
public static function fromArray(array $data): BackgroundFill
public static function fromArray(?array $data): ?BackgroundFillSolid
{
$object = new self();
if($data === null)
{
return null;
}
$object = new self();
$object->type = BackgroundFillType::SOLID;
$object->color = $data['color'];

View file

@ -7,6 +7,7 @@ use TgBotLib\Enums\Types\BackgroundType as type;
use TgBotLib\Interfaces\ObjectTypeInterface;
use TgBotLib\Objects\BackgroundType\BackgroundTypeChatTheme;
use TgBotLib\Objects\BackgroundType\BackgroundTypeFill;
use TgBotLib\Objects\BackgroundType\BackgroundTypePattern;
use TgBotLib\Objects\BackgroundType\BackgroundTypeWallpaper;
abstract class BackgroundType implements ObjectTypeInterface
@ -31,8 +32,13 @@ abstract class BackgroundType implements ObjectTypeInterface
/**
* @inheritDoc
*/
public static function fromArray(array $data): BackgroundType
public static function fromArray(?array $data): ?BackgroundType
{
if($data === null)
{
return null;
}
if (!isset($data['type']))
{
throw new InvalidArgumentException('BackgroundType expected type');
@ -42,6 +48,7 @@ abstract class BackgroundType implements ObjectTypeInterface
{
type::WALLPAPER => BackgroundTypeWallpaper::fromArray($data),
type::FILL => BackgroundTypeFill::fromArray($data),
type::PATTERN => BackgroundTypePattern::fromArray($data),
type::CHAT_THEME => BackgroundTypeChatTheme::fromArray($data),
default => throw new InvalidArgumentException("Invalid BackgroundType Type")
};

View file

@ -34,10 +34,14 @@ class BackgroundTypeChatTheme extends BackgroundType implements ObjectTypeInterf
/**
* @inheritDoc
*/
public static function fromArray(array $data): BackgroundTypeChatTheme
public static function fromArray(?array $data): ?BackgroundTypeChatTheme
{
$object = new self();
if($data === null)
{
return null;
}
$object = new self();
$object->type = type::CHAT_THEME;
$object->theme_name = $data['theme_name'];

View file

@ -47,10 +47,11 @@ class BackgroundTypeFill extends BackgroundType implements ObjectTypeInterface
/**
* @inheritDoc
*/
public static function fromArray(array $data): BackgroundTypeFill
public static function fromArray(?array $data): ?BackgroundTypeFill
{
$object = new self();
if()
$object = new self();
$object->type = type::FILL;
$object->fill = BackgroundFill::fromArray($data['fill']);
$object->dark_theme_dimming = $data['dark_theme_dimming'];

View file

@ -85,10 +85,14 @@ class BackgroundTypePattern extends BackgroundType implements ObjectTypeInterfac
/**
* @inheritDoc
*/
public static function fromArray(array $data): BackgroundTypePattern
public static function fromArray(?array $data): ?BackgroundTypePattern
{
$object = new self();
if($data === null)
{
return null;
}
$object = new self();
$object->type = type::PATTERN;
$object->document = Document::fromArray($data['document']);
$object->fill = BackgroundFill::fromArray($data['fill']);

View file

@ -71,10 +71,14 @@ class BackgroundTypeWallpaper extends BackgroundType implements ObjectTypeInterf
/**
* @inheritDoc
*/
public static function fromArray(array $data): BackgroundTypeWallpaper
public static function fromArray(?array $data): ?BackgroundTypeWallpaper
{
$object = new self();
if($data === null)
{
return null;
}
$object = new self();
$object->type = type::WALLPAPER;
$object->document = Document::fromArray($data['document']);
$object->dark_theme_dimming = $data['dark_theme_dimming'];

View file

@ -41,8 +41,13 @@
/**
* @inheritDoc
*/
public static function fromArray(array $data): ObjectTypeInterface
public static function fromArray(?array $data): ?BotCommandScope
{
if($data === null)
{
return null;
}
if(!isset($data['type']))
{
throw new InvalidArgumentException('BotCommandScope expected type');

View file

@ -26,9 +26,7 @@
}
/**
* Returns an array representation of the object
*
* @return array
* @inheritDoc
*/
public function toArray(): array
{
@ -39,13 +37,15 @@
}
/**
* Constructs object from an array representation
*
* @param array $data
* @return BotCommandScopeAllChatAdministrators
* @inheritDoc
*/
public static function fromArray(array $data): BotCommandScopeAllChatAdministrators
public static function fromArray(?array $data): ?BotCommandScopeAllChatAdministrators
{
if($data === null)
{
return null;
}
$object = new self();
$object->type = BotCommandScopeType::ALL_CHAT_ADMINISTRATORS;
$object->chat_id = $data['chat_id'] ?? null;

View file

@ -23,8 +23,13 @@
/**
* @inheritDoc
*/
public static function fromArray(array $data): self
public static function fromArray(?array $data): ?BotCommandScopeAllGroupChats
{
if($data === null)
{
return null;
}
$object = new self();
$object->type = BotCommandScopeType::ALL_CHAT_GROUPS;

View file

@ -23,8 +23,13 @@
/**
* @inheritDoc
*/
public static function fromArray(array $data): BotCommandScopeAllPrivateChats
public static function fromArray(?array $data): ?BotCommandScopeAllPrivateChats
{
if($data === null)
{
return null;
}
$object = new self();
$object->type = BotCommandScopeType::ALL_PRIVATE_CHATS;

View file

@ -39,8 +39,13 @@
/**
* @inheritDoc
*/
public static function fromArray(array $data): self
public static function fromArray(?array $data): ?BotCommandScopeChat
{
if($data === null)
{
return null;
}
$object = new self();
$object->type = BotCommandScopeType::CHAT;
$object->chat_id = $data['chat_id'] ?? null;

View file

@ -24,8 +24,13 @@
/**
* @inheritDoc
*/
public static function fromArray(array $data): self
public static function fromArray(?array $data): ?BotCommandScopeChatAdministrators
{
if($data === null)
{
return null;
}
$object = new self();
$object->type = BotCommandScopeType::CHAT_ADMINISTRATORS;

View file

@ -48,8 +48,13 @@
/**
* @inheritDoc
*/
public static function fromArray(array $data): self
public static function fromArray(?array $data): ?BotCommandScopeChatMember
{
if($data === null)
{
return null;
}
$object = new self();
$object->type = BotCommandScopeType::CHAT_MEMBER;
$object->chat_id = $data['chat_id'] ?? null;

View file

@ -23,8 +23,13 @@
/**
* @inheritDoc
*/
public static function fromArray(array $data): BotCommandScopeDefault
public static function fromArray(?array $data): ?BotCommandScopeDefault
{
if($data === null)
{
return null;
}
$object = new self();
$object->type = BotCommandScopeType::DEFAULT;

View file

@ -39,8 +39,13 @@
/**
* @inheritDoc
*/
public static function fromArray(array $data): ChatMember
public static function fromArray(?array $data): ?ChatMember
{
if($data === null)
{
return null;
}
if(!isset($data['status']))
{
throw new InvalidArgumentException('ChatMember expected status');

View file

@ -210,8 +210,13 @@
/**
* @inheritDoc
*/
public static function fromArray(array $data): ChatMemberAdministrator
public static function fromArray(?array $data): ?ChatMemberAdministrator
{
if($data === null)
{
return null;
}
$object = new self();
$object->status = ChatMemberStatus::ADMINISTRATOR;
$object->user = isset($data['user']) ? User::fromArray($data['user']) : null;

View file

@ -49,8 +49,13 @@
/**
* @inheritDoc
*/
public static function fromArray(array $data): ChatMemberBanned
public static function fromArray(?array $data): ?ChatMemberBanned
{
if($data === null)
{
return null;
}
$object = new self();
$object->status = ChatMemberStatus::KICKED;
$object->user = isset($data['user']) ? User::fromArray($data['user']) : null;

View file

@ -37,8 +37,13 @@
/**
* @inheritDoc
*/
public static function fromArray(array $data): ChatMemberLeft
public static function fromArray(?array $data): ?ChatMemberLeft
{
if($data === null)
{
return null;
}
$object = new self();
$object->status = ChatMemberStatus::LEFT;
$object->user = isset($data['user']) ? User::fromArray($data['user']) : null;

View file

@ -37,8 +37,13 @@
/**
* @inheritDoc
*/
public static function fromArray(array $data): ChatMemberMember
public static function fromArray(?array $data): ?ChatMemberMember
{
if($data === null)
{
return null;
}
$object = new self();
$object->status = ChatMemberStatus::MEMBER;
$object->user = isset($data['user']) ? User::fromArray($data['user']) : null;

View file

@ -61,8 +61,13 @@
/**
* @inheritDoc
*/
public static function fromArray(array $data): ChatMemberOwner
public static function fromArray(?array $data): ?ChatMemberOwner
{
if($data === null)
{
return null;
}
$object = new ChatMemberOwner();
$object->status = ChatMemberStatus::CREATOR;
$object->user = isset($data['user']) ? User::fromArray($data['user']) : null;

View file

@ -229,9 +229,14 @@
/**
* @inheritDoc
*/
public static function fromArray(array $data): ChatMemberRestricted
public static function fromArray(?array $data): ?ChatMemberRestricted
{
$object = new static();
if($data === null)
{
return null;
}
$object = new self();
$object->status = ChatMemberStatus::RESTRICTED;
$object->user = isset($data['user']) ? User::fromArray($data['user']) : null;
$object->is_member = $data['is_member'] ?? false;

View file

@ -108,13 +108,17 @@
/**
* Constructs object from an array representation
*
* @param array $data
* @return ChosenInlineResult
* @param array|null $data
* @return ChosenInlineResult|null
*/
public static function fromArray(array $data): self
public static function fromArray(?array $data): ?ChosenInlineResult
{
$object = new self();
if($data === null)
{
return null;
}
$object = new self();
$object->result_id = $data['result_id'] ?? null;
$object->from = $data['from'] ?? null;
$object->location = $data['location'] ?? null;

View file

@ -334,13 +334,17 @@
/**
* Constructs a new InlineKeyboardButton object from an array
*
* @param array $data
* @return InlineKeyboardButton
* @param array|null $data
* @return InlineKeyboardButton|null
*/
public static function fromArray(array $data): self
public static function fromArray(?array $data): ?InlineKeyboardButton
{
$object = new self();
if($data === null)
{
return null;
}
$object = new self();
$object->text = $data['text'] ?? null;
$object->url = $data['url'] ?? null;
$object->callback_data = $data['callback_data'] ?? null;

View file

@ -72,13 +72,17 @@
/**
* Constructs the object from an array representation
*
* @param array $data
* @return InlineKeyboardMarkup
* @param array|null $data
* @return InlineKeyboardMarkup|null
*/
public static function fromArray(array $data): self
public static function fromArray(?array $data): ?InlineKeyboardMarkup
{
$object = new self();
if($data === null)
{
return null;
}
$object = new self();
$object->inline_keyboard = [];
foreach($data as $item)

View file

@ -123,13 +123,17 @@
/**
* Constructs object from an array representation
*
* @param array $data
* @return InlineQuery
* @param array|null $data
* @return InlineQuery|null
*/
public static function fromArray(array $data): self
public static function fromArray(?array $data): ?InlineQuery
{
$object = new self();
if($data === null)
{
return null;
}
$object = new self();
$object->id = $data['id'];
$object->from = isset($data['from']) && is_array($data['from']) ? User::fromArray($data['from']) : $data['from'];
$object->query = $data['query'];

View file

@ -5,7 +5,6 @@
use InvalidArgumentException;
use TgBotLib\Classes\Validate;
use TgBotLib\Enums\Types\InlineQueryResultType;
use TgBotLib\Exceptions\NotImplementedException;
use TgBotLib\Interfaces\ObjectTypeInterface;
use TgBotLib\Objects\Inline\InlineQueryResult\InlineQueryResultArticle;
use TgBotLib\Objects\Inline\InlineQueryResult\InlineQueryResultAudio;
@ -71,8 +70,33 @@
/**
* @inheritDoc
*/
public static function fromArray(array $data): ObjectTypeInterface
public static function fromArray(?array $data): ?InlineQueryResult
{
// TODO: Implement this
if($data === null)
{
return null;
}
if(!isset($data['type']))
{
throw new InvalidArgumentException('InlineQueryResult expected type');
}
return match(InlineQueryResultType::tryFrom($data['type']))
{
InlineQueryResultType::ARTICLE => InlineQueryResultArticle::fromArray($data),
InlineQueryResultType::AUDIO => InlineQueryResultAudio::fromArray($data),
InlineQueryResultType::CONTACT => InlineQueryResultContact::fromArray($data),
InlineQueryResultType::DOCUMENT => InlineQueryResultDocument::fromArray($data),
InlineQueryResultType::GAME => InlineQueryResultGame::fromArray($data),
InlineQueryResultType::GIF => InlineQueryResultGif::fromArray($data),
InlineQueryResultType::LOCATION => InlineQueryResultLocation::fromArray($data),
InlineQueryResultType::MPEG_4_GIF => InlineQueryResultMpeg4Gif::fromArray($data),
InlineQueryResultType::PHOTO => InlineQueryResultPhoto::fromArray($data),
InlineQueryResultType::VENUE => InlineQueryResultVenue::fromArray($data),
InlineQueryResultType::VIDEO => InlineQueryResultVideo::fromArray($data),
InlineQueryResultType::VOICE => InlineQueryResultVoice::fromArray($data),
default => throw new InvalidArgumentException('Unexpected type for InlineQueryResult')
};
}
}

View file

@ -262,8 +262,13 @@
/**
* @inheritDoc
*/
public static function fromArray(array $data): InlineQueryResultArticle
public static function fromArray(?array $data): ?InlineQueryResultArticle
{
if($data === null)
{
return null;
}
$object = new self();
$object->type = InlineQueryResultType::ARTICLE;
$object->id = $data['id'] ?? null;

View file

@ -312,8 +312,13 @@
/**
* @inheritDoc
*/
public static function fromArray(array $data): InlineQueryResultAudio
public static function fromArray(?array $data): ?InlineQueryResultAudio
{
if($data === null)
{
return null;
}
$object = new self();
$object->type = InlineQueryResultType::AUDIO;
$object->id = $data['id'] ?? null;

View file

@ -260,8 +260,13 @@
/**
* @inheritDoc
*/
public static function fromArray(array $data): InlineQueryResultContact
public static function fromArray(?array $data): ?InlineQueryResultContact
{
if($data === null)
{
return null;
}
$object = new self();
$object->type = InlineQueryResultType::CONTACT;
$object->id = $data['id'] ?? null;

View file

@ -381,8 +381,13 @@
/**
* @inheritDoc
*/
public static function fromArray(array $data): InlineQueryResultDocument
public static function fromArray(?array $data): ?InlineQueryResultDocument
{
if($data === null)
{
return null;
}
$object = new self();
$object->type = InlineQueryResultType::DOCUMENT;
$object->id = $data['id'] ?? null;

View file

@ -89,8 +89,13 @@
/**
* @inheritDoc
*/
public static function fromArray(array $data): InlineQueryResultGame
public static function fromArray(?array $data): ?InlineQueryResultGame
{
if($data === null)
{
return null;
}
$object = new self();
$object->type = InlineQueryResultType::GAME;
$object->id = $data['id'] ?? null;

View file

@ -390,8 +390,13 @@
/**
* @inheritDoc
*/
public static function fromArray(array $data): InlineQueryResultGif
public static function fromArray(?array $data): ?InlineQueryResultGif
{
if($data === null)
{
return null;
}
$object = new self();
$object->type = InlineQueryResultType::GIF;
$object->id = $data['id'] ?? null;

View file

@ -339,8 +339,13 @@
/**
* @inheritDoc
*/
public static function fromArray(array $data): InlineQueryResultLocation
public static function fromArray(?array $data): ?InlineQueryResultLocation
{
if($data === null)
{
return null;
}
$object = new self();
$object->type = InlineQueryResultType::LOCATION;
$object->id = $data['id'] ?? null;

View file

@ -322,8 +322,13 @@
/**
* @inheritDoc
*/
public static function fromArray(array $data): InlineQueryResultMpeg4Gif
public static function fromArray(?array $data): ?InlineQueryResultMpeg4Gif
{
if($data === null)
{
return null;
}
$object = new self();
$object->type = InlineQueryResultType::MPEG_4_GIF;
$object->id = $data['id'] ?? null;

View file

@ -303,8 +303,13 @@
/**
* @inheritDoc
*/
public static function fromArray(array $data): InlineQueryResultPhoto
public static function fromArray(?array $data): ?InlineQueryResultPhoto
{
if($data === null)
{
return null;
}
$object = new self();
$object->type = InlineQueryResultType::PHOTO;
$object->id = $data['id'] ?? null;

View file

@ -342,8 +342,13 @@
/**
* @inheritDoc
*/
public static function fromArray(array $data): InlineQueryResultVenue
public static function fromArray(?array $data): ?InlineQueryResultVenue
{
if($data === null)
{
return null;
}
$object = new self();
$object->type = InlineQueryResultType::VENUE;
$object->id = $data['id'] ?? null;

View file

@ -356,8 +356,13 @@
/**
* @inheritDoc
*/
public static function fromArray(array $data): ObjectTypeInterface
public static function fromArray(?array $data): ?InlineQueryResultVideo
{
if($data === null)
{
return null;
}
$object = new self();
$object->type = InlineQueryResultType::VIDEO;
$object->id = $data['id'] ?? null;

View file

@ -232,8 +232,13 @@
/**
* @inheritDoc
*/
public static function fromArray(array $data): InlineQueryResultVoice
public static function fromArray(?array $data): ?InlineQueryResultVoice
{
if($data === null)
{
return null;
}
$object = new self();
$object->type = InlineQueryResultType::VOICE;
$object->id = $data['id'] ?? null;

View file

@ -37,13 +37,17 @@
/**
* Constructs object from an array representation
*
* @param array $data
* @return SentWebAppMessage
* @param array|null $data
* @return SentWebAppMessage|null
*/
public static function fromArray(array $data): SentWebAppMessage
public static function fromArray(?array $data): ?SentWebAppMessage
{
$object = new self();
if($data === null)
{
return null;
}
$object = new self();
$object->inline_message_id = $data['inline_message_id'] ?? null;
return $object;