Added ChatBoostSource objects

This commit is contained in:
netkas 2024-10-06 15:45:44 -04:00
parent 3b02d5a5c2
commit e8a6e7a6e1
6 changed files with 275 additions and 0 deletions

View file

@ -0,0 +1,10 @@
<?php
namespace TgBotLib\Enums\Types;
enum ChatBoostSourceType : string
{
case PREMIUM = 'premium';
case GIFT_CODE = 'gift_code';
case GIVEAWAY = 'giveaway';
}

View file

@ -0,0 +1,23 @@
<?php
namespace TgBotLib\Objects;
use TgBotLib\Interfaces\ObjectTypeInterface;
class ChatBoostRemoved implements ObjectTypeInterface
{
private Chat $chat;
private string $boost_id;
private int $remove_date;
private ChatBoostSource $source;
public function toArray(): ?array
{
// TODO: Implement toArray() method.
}
public static function fromArray(?array $data): ?ChatBoostRemoved
{
// TODO: Implement fromArray() method.
}
}

View file

@ -0,0 +1,49 @@
<?php
namespace TgBotLib\Objects;
use InvalidArgumentException;
use TgBotLib\Enums\Types\ChatBoostSourceType;
use TgBotLib\Interfaces\ObjectTypeInterface;
use TgBotLib\Objects\ChatBoostSource\ChatBoostSourceGiftCode;
use TgBotLib\Objects\ChatBoostSource\ChatBoostSourceGiveaway;
use TgBotLib\Objects\ChatBoostSource\ChatBoostSourcePremium;
abstract class ChatBoostSource implements ObjectTypeInterface
{
protected ChatBoostSourceType $source;
/**
* Source of the boost
*
* @return ChatBoostSourceType
*/
public function getSource(): ChatBoostSourceType
{
return $this->source;
}
/**
* @inheritDoc
*/
public abstract function toArray(): ?array;
/**
* @inheritDoc
*/
public static function fromArray(?array $data): ?ChatBoostSource
{
if($data === null)
{
return null;
}
return match (ChatBoostSourceType::tryFrom($data['source']))
{
ChatBoostSourceType::GIFT_CODE => ChatBoostSourceGiftCode::fromArray($data),
ChatBoostSourceType::PREMIUM => ChatBoostSourcePremium::fromArray($data),
ChatBoostSourceType::GIVEAWAY => ChatBoostSourceGiveaway::fromArray($data),
default => throw new InvalidArgumentException('Unknown source type')
};
}
}

View file

@ -0,0 +1,51 @@
<?php
namespace TgBotLib\Objects\ChatBoostSource;
use TgBotLib\Enums\Types\ChatBoostSourceType;
use TgBotLib\Interfaces\ObjectTypeInterface;
use TgBotLib\Objects\ChatBoostSource;
use TgBotLib\Objects\User;
class ChatBoostSourceGiftCode extends ChatBoostSource implements ObjectTypeInterface
{
private User $user;
/**
* User that boosted the chat
*
* @return User
*/
public function getUser(): User
{
return $this->user;
}
/**
* @inheritDoc
*/
public function toArray(): array
{
return [
'source' => $this->source->value,
'user' => $this->user->toArray()
];
}
/**
* @inheritDoc
*/
public static function fromArray(?array $data): ?ChatBoostSourceGiftCode
{
if($data === null)
{
return null;
}
$object = new self();
$object->source = ChatBoostSourceType::PREMIUM;
$object->user = User::fromArray($data['user']);
return $object;
}
}

View file

@ -0,0 +1,91 @@
<?php
namespace TgBotLib\Objects\ChatBoostSource;
use TgBotLib\Enums\Types\ChatBoostSourceType;
use TgBotLib\Interfaces\ObjectTypeInterface;
use TgBotLib\Objects\ChatBoostSource;
use TgBotLib\Objects\User;
class ChatBoostSourceGiveaway extends ChatBoostSource implements ObjectTypeInterface
{
private int $giveaway_message_id;
private ?User $user;
private ?int $prize_star_count;
private bool $is_unclaimed;
/**
* Identifier of a message in the chat with the giveaway; the message could have been deleted already.
* May be 0 if the message isn't sent yet.
*
* @return int
*/
public function getGiveawayMessageId(): int
{
return $this->giveaway_message_id;
}
/**
* Optional. User that won the prize in the giveaway if any; for Telegram Premium giveaways only
*
* @return User|null
*/
public function getUser(): ?User
{
return $this->user;
}
/**
* Optional. The number of Telegram Stars to be split between giveaway winners; for Telegram Star giveaways only
*
* @return int|null
*/
public function getPrizeStarCount(): ?int
{
return $this->prize_star_count;
}
/**
* Optional. True, if the giveaway was completed, but there was no user to win the prize
*
* @return bool
*/
public function isIsUnclaimed(): bool
{
return $this->is_unclaimed;
}
/**
* @inheritDoc
*/
public function toArray(): ?array
{
return [
'source' => $this->source->value,
'giveaway_message_id' => $this->giveaway_message_id,
'user' => $this->user?->toArray(),
'prize_star_count' => $this->prize_star_count,
'is_unclaimed' => $this->is_unclaimed
];
}
/**
* @inheritDoc
*/
public static function fromArray(?array $data): ?ChatBoostSourceGiveaway
{
if($data === null)
{
return null;
}
$object = new self();
$object->source = ChatBoostSourceType::GIVEAWAY;
$object->giveaway_message_id = $data['giveaway_message_id'];
$object->user = User::fromArray($data['user']);
$object->prize_star_count = $data['prize_star_count'];
$object->is_unclaimed = $data['is_unclaimed'];
return $object;
}
}

View file

@ -0,0 +1,51 @@
<?php
namespace TgBotLib\Objects\ChatBoostSource;
use TgBotLib\Enums\Types\ChatBoostSourceType;
use TgBotLib\Interfaces\ObjectTypeInterface;
use TgBotLib\Objects\ChatBoostSource;
use TgBotLib\Objects\User;
class ChatBoostSourcePremium extends ChatBoostSource implements ObjectTypeInterface
{
private User $user;
/**
* User that boosted the chat
*
* @return User
*/
public function getUser(): User
{
return $this->user;
}
/**
* @inheritDoc
*/
public function toArray(): array
{
return [
'source' => $this->source->value,
'user' => $this->user?->toArray()
];
}
/**
* @inheritDoc
*/
public static function fromArray(?array $data): ?ChatBoostSourcePremium
{
if($data === null)
{
return null;
}
$object = new self();
$object->source = ChatBoostSourceType::PREMIUM;
$object->user = User::fromArray($data['user']);
return $object;
}
}