Refactored InputMessageContent types to it's own namespace so InputMessageContent can always return the correct InputMessageContent object type when calling fromArray()

This commit is contained in:
Netkas 2023-04-23 16:22:15 -04:00
parent 9ffc0a2105
commit 269baf7d26
7 changed files with 80 additions and 12 deletions

View file

@ -10,11 +10,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
This update accompanies the release of the [Telegram Bot API 6.7](https://core.telegram.org/bots/api#april-21-2023).
### Added
* Added object `\TgBotLib\Objects\Telegram > InputTextMessageContent` to represent the content of a text message to be sent as the result of an inline query.
* Added object `\TgBotLib\Objects\Telegram > InputLocationMessageContent` to represent the content of a location message to be sent as the result of an inline query.
* Added object `\TgBotLib\Objects\Telegram > InputVenueMessageContent` to represent the content of a venue message to be sent as the result of an inline query.
* Added object `\TgBotLib\Objects\Telegram > InputContactMessageContent` to represent the content of a contact message to be sent as the result of an inline query.
* Added object `\TgBotLib\Objects\Telegram > InputInvoiceMessageContent` to represent the content of an invoice message to be sent as the result of an inline query.
* Added object `\TgBotLib\Objects\Telegram\InputMessageContent > InputTextMessageContent` to represent the content of a text message to be sent as the result of an inline query.
* Added object `\TgBotLib\Objects\Telegram\InputMessageContent > InputLocationMessageContent` to represent the content of a location message to be sent as the result of an inline query.
* Added object `\TgBotLib\Objects\Telegram\InputMessageContent > InputVenueMessageContent` to represent the content of a venue message to be sent as the result of an inline query.
* Added object `\TgBotLib\Objects\Telegram\InputMessageContent > InputContactMessageContent` to represent the content of a contact message to be sent as the result of an inline query.
* Added object `\TgBotLib\Objects\Telegram\InputMessageContent > InputInvoiceMessageContent` to represent the content of an invoice message to be sent as the result of an inline query.
* Added new exception class `NotImplementedException` to represent the case when a method is not implemented yet or the method is not applicable to the current object.
## [6.6.0] - 2023-04-10

View file

@ -0,0 +1,64 @@
<?php
namespace TgBotLib\Objects\Telegram;
use LogLib\Log;
use TgBotLib\Exceptions\NotImplementedException;
use TgBotLib\Interfaces\ObjectTypeInterface;
use TgBotLib\Objects\Telegram\InputMessageContent\InputContactMessageContent;
use TgBotLib\Objects\Telegram\InputMessageContent\InputInvoiceMessageContent;
use TgBotLib\Objects\Telegram\InputMessageContent\InputLocationMessageContent;
use TgBotLib\Objects\Telegram\InputMessageContent\InputTextMessageContent;
use TgBotLib\Objects\Telegram\InputMessageContent\InputVenueMessageContent;
class InputMessageContent implements ObjectTypeInterface
{
/**
* @inheritDoc
*/
public function toArray(): array
{
throw new NotImplementedException('This object is abstract, you can\'t use it directly, try constructing one of the child classes with fromArray()');
}
/**
* @inheritDoc
*/
public static function fromArray(array $data): ObjectTypeInterface
{
// You may be wondering why this is needed, it's because Telegram Developers can't
// actually write good software, they tend to forget the little things.
// Like for example, providing the type of the object in the JSON response.
// So this little code snippet is needed to determine the type of the object. :(
//
// Thanks Telegram!
if(isset($data['provider_token']))
{
return InputInvoiceMessageContent::fromArray($data);
}
if(isset($data['phone_number']))
{
return InputContactMessageContent::fromArray($data);
}
if(isset($data['address']))
{
return InputVenueMessageContent::fromArray($data);
}
if(isset($data['longitude']) && isset($data['latitude']))
{
return InputLocationMessageContent::fromArray($data);
}
if(isset($data['message_text']))
{
return InputTextMessageContent::fromArray($data);
}
Log::warning('net.nosial.tgbotlib', 'InputMessageContent::fromArray() - Unknown type of InputMessageContent, returning InputTextMessageContent (go complain to Telegram)');
return InputTextMessageContent::fromArray($data);
}
}

View file

@ -2,7 +2,7 @@
/** @noinspection PhpMissingFieldTypeInspection */
namespace TgBotLib\Objects\Telegram;
namespace TgBotLib\Objects\Telegram\InputMessageContent;
use TgBotLib\Interfaces\ObjectTypeInterface;

View file

@ -3,9 +3,10 @@
/** @noinspection PhpMissingFieldTypeInspection */
/** @noinspection PhpUnused */
namespace TgBotLib\Objects\Telegram;
namespace TgBotLib\Objects\Telegram\InputMessageContent;
use TgBotLib\Interfaces\ObjectTypeInterface;
use TgBotLib\Objects\Telegram\LabeledPrice;
class InputInvoiceMessageContent implements \TgBotLib\Interfaces\ObjectTypeInterface
{

View file

@ -2,11 +2,12 @@
/** @noinspection PhpMissingFieldTypeInspection */
namespace TgBotLib\Objects\Telegram;
namespace TgBotLib\Objects\Telegram\InputMessageContent;
use TgBotLib\Interfaces\ObjectTypeInterface;
class InputLocationMessageContent implements ObjectTypeInterface
class
InputLocationMessageContent implements ObjectTypeInterface
{
/**
* @var float

View file

@ -2,9 +2,10 @@
/** @noinspection PhpMissingFieldTypeInspection */
namespace TgBotLib\Objects\Telegram;
namespace TgBotLib\Objects\Telegram\InputMessageContent;
use TgBotLib\Interfaces\ObjectTypeInterface;
use TgBotLib\Objects\Telegram\MessageEntity;
class InputTextMessageContent implements ObjectTypeInterface
{

View file

@ -3,11 +3,12 @@
/** @noinspection PhpUnused */
/** @noinspection PhpMissingFieldTypeInspection */
namespace TgBotLib\Objects\Telegram;
namespace TgBotLib\Objects\Telegram\InputMessageContent;
use TgBotLib\Interfaces\ObjectTypeInterface;
class InputVenueMessageContent implements ObjectTypeInterface
class
InputVenueMessageContent implements ObjectTypeInterface
{
/**
* @var float