Updated MessageEntity

This commit is contained in:
netkas 2024-10-07 15:20:19 -04:00
parent 2b0309cd6d
commit b2fe2bdec3

View file

@ -2,11 +2,12 @@
namespace TgBotLib\Objects; namespace TgBotLib\Objects;
use TgBotLib\Enums\Types\MessageEntityType;
use TgBotLib\Interfaces\ObjectTypeInterface; use TgBotLib\Interfaces\ObjectTypeInterface;
class MessageEntity implements ObjectTypeInterface class MessageEntity implements ObjectTypeInterface
{ {
private string $type; private MessageEntityType $type;
private int $offset; private int $offset;
private int $length; private int $length;
private ?string $url; private ?string $url;
@ -24,11 +25,23 @@
* *
* @return string * @return string
*/ */
public function getType(): string public function getType(): MessageEntityType
{ {
return $this->type; return $this->type;
} }
/**
* Sets the type of the entity
*
* @param MessageEntityType $type
* @return MessageEntity
*/
public function setType(MessageEntityType $type): MessageEntity
{
$this->type = $type;
return $this;
}
/** /**
* Offset in UTF-16 code units to the start of the entity * Offset in UTF-16 code units to the start of the entity
* *
@ -40,6 +53,18 @@
return $this->offset; return $this->offset;
} }
/**
* Sets the offset of the entity
*
* @param int $offset
* @return MessageEntity
*/
public function setOffset(int $offset): MessageEntity
{
$this->offset = $offset;
return $this;
}
/** /**
* Offset in UTF-16 code units to the start of the entity * Offset in UTF-16 code units to the start of the entity
* *
@ -51,6 +76,18 @@
return $this->length; return $this->length;
} }
/**
* Sets the length of the entity
*
* @param int $length
* @return MessageEntity
*/
public function setLength(int $length): MessageEntity
{
$this->length = $length;
return $this;
}
/** /**
* Optional. For “text_link” only, URL that will be opened after user taps on the text * Optional. For “text_link” only, URL that will be opened after user taps on the text
* *
@ -61,6 +98,12 @@
return $this->url; return $this->url;
} }
public function setUrl(?string $url): MessageEntity
{
$this->url = $url;
return $this;
}
/** /**
* Optional. For “text_mention” only, the mentioned user * Optional. For “text_mention” only, the mentioned user
* *
@ -71,6 +114,18 @@
return $this->user; return $this->user;
} }
/**
* Sets the mentioned user
*
* @param User|null $user
* @return MessageEntity
*/
public function setUser(?User $user): MessageEntity
{
$this->user = $user;
return $this;
}
/** /**
* Optional. For “pre” only, the programming language of the entity text * Optional. For “pre” only, the programming language of the entity text
* *
@ -81,6 +136,18 @@
return $this->language; return $this->language;
} }
/**
* Sets the language of the entity text
*
* @param string|null $language
* @return MessageEntity
*/
public function setLanguage(?string $language): MessageEntity
{
$this->language = $language;
return $this;
}
/** /**
* Optional. For “custom_emoji” only, unique identifier of the custom emoji. Use getCustomEmojiStickers to get * Optional. For “custom_emoji” only, unique identifier of the custom emoji. Use getCustomEmojiStickers to get
* full information about the sticker * full information about the sticker
@ -93,20 +160,50 @@
return $this->custom_emoji_id; return $this->custom_emoji_id;
} }
/**
* Set the custom emoji id
*
* @param string|null $custom_emoji_id
* @return MessageEntity
*/
public function setCustomEmojiId(?string $custom_emoji_id): MessageEntity
{
$this->custom_emoji_id = $custom_emoji_id;
return $this;
}
/** /**
* @inheritDoc * @inheritDoc
*/ */
public function toArray(): array public function toArray(): array
{ {
return [ $array = [
'type' => $this->type, 'type' => $this->type->value,
'offset' => $this->offset, 'offset' => $this->offset,
'length' => $this->length, 'length' => $this->length
'url' => $this->url,
'user' => $this->user?->toArray(),
'language' => $this->language,
'custom_emoji_id' => $this->custom_emoji_id
]; ];
if($this->url !== null)
{
$array['url'] = $this->url;
}
if($this->user !== null)
{
$array['user'] = $this->user->toArray();
}
if($this->language !== null)
{
$array['language'] = $this->language;
}
if($this->custom_emoji_id !== null)
{
$array['custom_emoji_id'] = $this->custom_emoji_id;
}
return $array;
} }
/** /**
@ -120,7 +217,7 @@
} }
$object = new self(); $object = new self();
$object->type = $data['type'] ?? null; $object->type = MessageEntityType::tryFrom($data['type']);
$object->offset = $data['offset'] ?? null; $object->offset = $data['offset'] ?? null;
$object->length = $data['length'] ?? null; $object->length = $data['length'] ?? null;
$object->url = $data['url'] ?? null; $object->url = $data['url'] ?? null;