Updated InputMessageContent objects

This commit is contained in:
netkas 2024-10-03 00:45:23 -04:00
parent 6a7668c427
commit 58849813e9
7 changed files with 148 additions and 278 deletions

View file

@ -0,0 +1,12 @@
<?php
namespace TgBotLib\Enums\Types;
enum InputMessageContentType : string
{
case TEXT = 'text';
case LOCATION = 'location';
case VENUE = 'venue';
case CONTACT = 'contact';
case INVOICE = 'invoice';
}

View file

@ -2,63 +2,58 @@
namespace TgBotLib\Objects;
use LogLib\Log;
use TgBotLib\Exceptions\NotImplementedException;
use InvalidArgumentException;
use TgBotLib\Enums\Types\InputMessageContentType;
use TgBotLib\Interfaces\ObjectTypeInterface;
use TgBotLib\Objects\InputMessageContent\InputContactMessageContent;
use TgBotLib\Objects\InputMessageContent\InputInvoiceMessageContent;
use TgBotLib\Objects\InputMessageContent\InputLocationMessageContent;
use TgBotLib\Objects\InputMessageContent\InputTextMessageContent;
use TgBotLib\Objects\InputMessageContent\InputVenueMessageContent;
class InputMessageContent implements ObjectTypeInterface
abstract class InputMessageContent implements ObjectTypeInterface
{
protected InputMessageContentType $type;
/**
* @inheritDoc
* Returns the type for the Input message content
*
* @return InputMessageContentType
*/
public function toArray(): array
public function getType(): InputMessageContentType
{
throw new NotImplementedException('This object is abstract, you can\'t use it directly, try constructing one of the child classes with fromArray()');
return $this->type;
}
/**
* @inheritDoc
*/
public static function fromArray(array $data): ObjectTypeInterface
public abstract function toArray(): array;
/**
* @inheritDoc
*/
public static function fromArray(array $data): InputMessageContent
{
// 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);
if(isset($data['latitude']) && isset($data['longitude']) && isset($data['tile']) && isset($data['address']))
{
return InputVenueMessageContent::fromArray($data);
}
if(isset($data['latitude']) && isset($data['longitude']))
{
return InputLocationMessageContent::fromArray($data);
}
if(isset($data['title']) && isset($data['description']) && isset($data['payload']))
{
return InputInvoiceMessageContent::fromArray($data);
}
throw new InvalidArgumentException("Invalid object type, unexpected type");
}
}

View file

@ -7,29 +7,16 @@
use InvalidArgumentException;
use TgBotLib\Classes\Validate;
use TgBotLib\Enums\Types\InputMessageContentType;
use TgBotLib\Interfaces\ObjectTypeInterface;
use TgBotLib\Objects\InputMessageContent;
class InputContactMessageContent implements ObjectTypeInterface
class InputContactMessageContent extends InputMessageContent implements ObjectTypeInterface
{
/**
* @var string
*/
private $phone_number;
/**
* @var string
*/
private $first_name;
/**
* @var string|null
*/
private $last_name;
/**
* @var string|null
*/
private $vcard;
private string $phone_number;
private string $first_name;
private ?string $last_name;
private ?string $vcard;
/**
* Contact's phone number
@ -51,7 +38,9 @@
public function setPhoneNumber(string $phone_number): self
{
if(!Validate::length($phone_number, 1, 255))
{
throw new InvalidArgumentException('phone_number should be between 1-255 characters');
}
$this->phone_number = $phone_number;
return $this;
@ -77,7 +66,9 @@
public function setFirstName(string $first_name): self
{
if(!Validate::length($first_name, 1, 255))
{
throw new InvalidArgumentException('first_name should be between 1-255 characters');
}
$this->first_name = $first_name;
return $this;
@ -109,7 +100,9 @@
}
if(!Validate::length($last_name, 1, 255))
{
throw new InvalidArgumentException('last_name should be between 1-255 characters (or null)');
}
$this->last_name = $last_name;
return $this;
@ -142,16 +135,16 @@
}
if(!Validate::length($vcard, 1, 2048))
{
throw new InvalidArgumentException('vcard should be between 1-2048 characters (or null)');
}
$this->vcard = $vcard;
return $this;
}
/**
* Returns an array representation of the object
*
* @return array
* @inheritDoc
*/
public function toArray(): array
{
@ -164,15 +157,13 @@
}
/**
* Constructs object from an array representation
*
* @param array $data
* @return ObjectTypeInterface
* @inheritDoc
*/
public static function fromArray(array $data): ObjectTypeInterface
public static function fromArray(array $data): InputContactMessageContent
{
$object = new self();
$object->type = InputMessageContentType::CONTACT;
$object->phone_number = $data['phone_number'] ?? null;
$object->first_name = $data['first_name'] ?? null;
$object->last_name = $data['last_name'] ?? null;

View file

@ -7,110 +7,39 @@
use InvalidArgumentException;
use TgBotLib\Classes\Validate;
use TgBotLib\Enums\Types\InputMessageContentType;
use TgBotLib\Interfaces\ObjectTypeInterface;
use TgBotLib\Objects\InputMessageContent;
use TgBotLib\Objects\LabeledPrice;
class InputInvoiceMessageContent implements ObjectTypeInterface
class InputInvoiceMessageContent extends InputMessageContent implements ObjectTypeInterface
{
/**
* @var string
*/
private $title;
/**
* @var string
*/
private $description;
/**
* @var string
*/
private $payload;
/**
* @var string
*/
private $provider_token;
/**
* @var string
*/
private $currency;
private string $title;
private string $description;
private string $payload;
private string $provider_token;
private string $currency;
/**
* @var LabeledPrice[]
*/
private $prices;
/**
* @var int|null
*/
private $max_tip_amount;
private array $prices;
private ?int $max_tip_amount;
/**
* @var int[]|null
*/
private $suggested_tip_amounts;
/**
* @var string|null
*/
private $provider_data;
/**
* @var string|null
*/
private $photo_url;
/**
* @var int|null
*/
private $photo_size;
/**
* @var int|null
*/
private $photo_width;
/**
* @var int|null
*/
private $photo_height;
/**
* @var bool
*/
private $need_name;
/**
* @var bool
*/
private $need_phone_number;
/**
* @var bool
*/
private $need_email;
/**
* @var bool
*/
private $need_shipping_address;
/**
* @var bool
*/
private $send_phone_number_to_provider;
/**
* @var bool
*/
private $send_email_to_provider;
/**
* @var bool
*/
private $is_flexible;
private ?array $suggested_tip_amounts;
private ?string $provider_data;
private ?string $photo_url;
private ?int $photo_size;
private ?int $photo_width;
private ?int $photo_height;
private bool $need_name;
private bool $need_phone_number;
private bool $need_email;
private bool $need_shipping_address;
private bool $send_phone_number_to_provider;
private bool $send_email_to_provider;
private bool $is_flexible;
/**
* Product name, 1-32 characters
@ -132,7 +61,9 @@
public function setTitle(string $title): self
{
if(!Validate::length($title, 1, 32))
{
throw new InvalidArgumentException('title should be between 1-32 characters');
}
$this->title = $title;
return $this;
@ -158,7 +89,9 @@
public function setDescription(string $description): self
{
if(!Validate::length($description, 1, 255))
{
throw new InvalidArgumentException('description should be between 1-255 characters');
}
$this->description = $description;
return $this;
@ -185,7 +118,9 @@
public function setPayload(string $payload): self
{
if(!Validate::length($payload, 1, 128))
{
throw new InvalidArgumentException('payload should be between 1-128 characters');
}
$this->payload = $payload;
return $this;
@ -236,7 +171,9 @@
public function setCurrency(string $currency): self
{
if(!Validate::length($currency, 3, 3))
{
throw new InvalidArgumentException('currency should be 3 characters');
}
$this->currency = $currency;
return $this;
@ -651,9 +588,7 @@
'payload' => $this->payload,
'provider_token' => $this->provider_token,
'currency' => $this->currency,
'prices' => array_map(function (LabeledPrice $price) {
return $price->toArray();
}, $this->prices),
'prices' => array_map(fn(LabeledPrice $labeled_price) => $labeled_price->toArray(), $this->prices),
'max_tip_amount' => $this->max_tip_amount,
'suggested_tip_amounts' => $this->suggested_tip_amounts,
'provider_data' => $this->provider_data,
@ -674,18 +609,17 @@
/**
* @inheritDoc
*/
public static function fromArray(array $data): ObjectTypeInterface
public static function fromArray(array $data): InputInvoiceMessageContent
{
$object = new self();
$object->type = InputMessageContentType::INVOICE;
$object->title = $data['title'] ?? null;
$object->description = $data['description'] ?? null;
$object->payload = $data['payload'] ?? null;
$object->provider_token = $data['provider_token'] ?? null;
$object->currency = $data['currency'] ?? null;
$object->prices = array_map(function (array $price) {
return LabeledPrice::fromArray($price);
}, $data['prices'] ?? []);
$object->prices = array_map(fn(array $prices) => LabeledPrice::fromArray($prices), $data['prices']);
$object->max_tip_amount = $data['max_tip_amount'] ?? null;
$object->suggested_tip_amounts = $data['suggested_tip_amounts'] ?? null;
$object->provider_data = $data['provider_data'] ?? null;

View file

@ -7,40 +7,18 @@
use InvalidArgumentException;
use TgBotLib\Classes\Validate;
use TgBotLib\Enums\Types\InputMessageContentType;
use TgBotLib\Interfaces\ObjectTypeInterface;
use TgBotLib\Objects\InputMessageContent;
class
InputLocationMessageContent implements ObjectTypeInterface
class InputLocationMessageContent extends InputMessageContent implements ObjectTypeInterface
{
/**
* @var float
*/
private $latitude;
/**
* @var float
*/
private $longitude;
/**
* @var float|null
*/
private $horizontal_accuracy;
/**
* @var int|null
*/
private $live_period;
/**
* @var int|null
*/
private $heading;
/**
* @var int|null
*/
private $proximity_alert_radius;
private float $latitude;
private float $longitude;
private ?float $horizontal_accuracy;
private ?int $live_period;
private ?int $heading;
private ?int $proximity_alert_radius;
/**
* Latitude of the location in degrees
@ -142,7 +120,9 @@
}
if($live_period < 60 || $live_period > 86400)
{
throw new InvalidArgumentException('live_period should be a value between 60-86400');
}
$this->live_period = $live_period;
return $this;
@ -176,7 +156,9 @@
}
if($heading < 1 || $heading > 360)
{
throw new InvalidArgumentException('heading should be a value between 1-360');
}
$this->heading = $heading;
return $this;
@ -209,16 +191,16 @@
}
if(!Validate::length($proximity_alert_radius, 1, 100000))
{
throw new InvalidArgumentException('proximity_alert_radius should be between 1-100000 characters');
}
$this->proximity_alert_radius = $proximity_alert_radius;
return $this;
}
/**
* Returns an array representation of the object
*
* @return array
* @inheritDoc
*/
public function toArray(): array
{
@ -233,15 +215,13 @@
}
/**
* Constructs object from an array representation
*
* @param array $data
* @return ObjectTypeInterface
* @inheritDoc
*/
public static function fromArray(array $data): ObjectTypeInterface
public static function fromArray(array $data): InputLocationMessageContent
{
$object = new self();
$object->type = InputMessageContentType::LOCATION;
$object->latitude = (float)$data['latitude'] ?? null;
$object->longitude = (float)$data['longitude'] ?? null;
$object->horizontal_accuracy = (float)$data['horizontal_accuracy'] ?? null;

View file

@ -7,30 +7,20 @@
use InvalidArgumentException;
use TgBotLib\Classes\Validate;
use TgBotLib\Enums\Types\InputMessageContentType;
use TgBotLib\Interfaces\ObjectTypeInterface;
use TgBotLib\Objects\InputMessageContent;
use TgBotLib\Objects\MessageEntity;
class InputTextMessageContent implements ObjectTypeInterface
class InputTextMessageContent extends InputMessageContent implements ObjectTypeInterface
{
/**
* @var string
*/
private $message_text;
/**
* @var string|null
*/
private $parse_mode;
private string $message_text;
private ?string $parse_mode;
/**
* @var MessageEntity[]|null
*/
private $entities;
/**
* @var bool
*/
private $disable_web_page_preview;
private ?array $entities;
private bool $disable_web_page_preview;
/**
* Text of the message to be sent, 1-4096 characters
@ -52,7 +42,9 @@
public function setMessageText(string $message_text): self
{
if(!Validate::length($message_text, 1, 4096))
{
throw new InvalidArgumentException('message_text should be between 1-4096 characters');
}
$this->message_text = $message_text;
return $this;
@ -85,7 +77,9 @@
}
if(!in_array(strtolower($parse_mode), ['markdown', 'html']))
{
throw new InvalidArgumentException('parse_mode should be either Markdown or HTML');
}
$this->parse_mode = strtolower($parse_mode);
return $this;
@ -119,7 +113,9 @@
foreach($entities as $entity)
{
if(!($entity instanceof MessageEntity))
{
throw new InvalidArgumentException('entities should be an array of MessageEntity objects');
}
}
$this->entities = $entities;
@ -172,7 +168,6 @@
}
return [
'message_text' => $this->message_text,
'parse_mode' => $this->parse_mode,
@ -182,15 +177,13 @@
}
/**
* Constructs object from an array representation
*
* @param array $data
* @return ObjectTypeInterface
* @inheritDoc
*/
public static function fromArray(array $data): ObjectTypeInterface
public static function fromArray(array $data): InputTextMessageContent
{
$object = new self();
$object->type = InputMessageContentType::TEXT;
$object->message_text = $data['message_text'] ?? null;
$object->parse_mode = $data['parse_mode'] ?? null;
$object->disable_web_page_preview = (bool)$data['disable_web_page_preview'] ?? false;

View file

@ -5,50 +5,20 @@
namespace TgBotLib\Objects\InputMessageContent;
use TgBotLib\Enums\Types\InputMessageContentType;
use TgBotLib\Interfaces\ObjectTypeInterface;
use TgBotLib\Objects\InputMessageContent;
class
InputVenueMessageContent implements ObjectTypeInterface
class InputVenueMessageContent extends InputMessageContent implements ObjectTypeInterface
{
/**
* @var float
*/
private $latitude;
/**
* @var float
*/
private $longitude;
/**
* @var string
*/
private $title;
/**
* @var string
*/
private $address;
/**
* @var string|null
*/
private $foursquare_id;
/**
* @var string|null
*/
private $foursquare_type;
/**
* @var string|null
*/
private $google_place_id;
/**
* @var string|null
*/
private $google_place_type;
private float $latitude;
private float $longitude;
private string $title;
private string $address;
private ?string $foursquare_id;
private ?string $foursquare_type;
private ?string $google_place_id;
private ?string $google_place_type;
/**
* Latitude of the venue in degrees
@ -238,9 +208,7 @@
}
/**
* Returns an array representation of the object
*
* @return array
* @inheritDoc
*/
public function toArray(): array
{
@ -257,15 +225,12 @@
}
/**
* Constructs object from an array representation
*
* @param array $data
* @return ObjectTypeInterface
* @inheritDoc
*/
public static function fromArray(array $data): ObjectTypeInterface
public static function fromArray(array $data): InputVenueMessageContent
{
$object = new self();
$object->type = InputMessageContentType::VENUE;
$object->latitude = (float)$data['latitude'] ?? null;
$object->longitude = (float)$data['longitude'] ?? null;
$object->title = $data['title'] ?? null;