mirror of
https://github.com/BushlanovDev/max-bot-api-client-php.git
synced 2026-11-26 14:22:18 +00:00
Added editBotInfo & editChat & getVideoAttachmentDetails
This commit is contained in:
@@ -42,6 +42,65 @@ abstract readonly class AbstractModel
|
||||
return new static(...$constructorArgs); // @phpstan-ignore-line
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, mixed>
|
||||
* @throws ReflectionException
|
||||
*/
|
||||
public function toArray(): array
|
||||
{
|
||||
$reflectionClass = new ReflectionClass($this);
|
||||
$properties = $reflectionClass->getProperties(ReflectionProperty::IS_PUBLIC);
|
||||
$result = [];
|
||||
|
||||
foreach ($properties as $property) {
|
||||
if (!$property->isInitialized($this)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$phpPropertyName = $property->getName();
|
||||
$value = $property->getValue($this);
|
||||
|
||||
$jsonKey = self::toSnakeCase($phpPropertyName);
|
||||
|
||||
$result[$jsonKey] = $this->convertValue($value);
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $input
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected static function toSnakeCase(string $input): string
|
||||
{
|
||||
return strtolower((string)preg_replace('/(?<!^)[A-Z]/', '_$0', $input));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $value
|
||||
*
|
||||
* @return mixed
|
||||
* @throws ReflectionException
|
||||
*/
|
||||
protected function convertValue(mixed $value): mixed
|
||||
{
|
||||
if ($value instanceof AbstractModel) {
|
||||
return $value->toArray();
|
||||
}
|
||||
|
||||
if (is_array($value)) {
|
||||
return array_map([$this, 'convertValue'], $value);
|
||||
}
|
||||
|
||||
if ($value instanceof BackedEnum) {
|
||||
return $value->value;
|
||||
}
|
||||
|
||||
return $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $value
|
||||
* @param ReflectionProperty $property
|
||||
@@ -114,63 +173,4 @@ abstract readonly class AbstractModel
|
||||
|
||||
return $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $input
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
private static function toSnakeCase(string $input): string
|
||||
{
|
||||
return strtolower((string)preg_replace('/(?<!^)[A-Z]/', '_$0', $input));
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, mixed>
|
||||
* @throws ReflectionException
|
||||
*/
|
||||
public function toArray(): array
|
||||
{
|
||||
$reflectionClass = new ReflectionClass($this);
|
||||
$properties = $reflectionClass->getProperties(ReflectionProperty::IS_PUBLIC);
|
||||
$result = [];
|
||||
|
||||
foreach ($properties as $property) {
|
||||
if (!$property->isInitialized($this)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$phpPropertyName = $property->getName();
|
||||
$value = $property->getValue($this);
|
||||
|
||||
$jsonKey = self::toSnakeCase($phpPropertyName);
|
||||
|
||||
$result[$jsonKey] = $this->convertValue($value);
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $value
|
||||
*
|
||||
* @return mixed
|
||||
* @throws ReflectionException
|
||||
*/
|
||||
private function convertValue(mixed $value): mixed
|
||||
{
|
||||
if ($value instanceof AbstractModel) {
|
||||
return $value->toArray();
|
||||
}
|
||||
|
||||
if (is_array($value)) {
|
||||
return array_map([$this, 'convertValue'], $value);
|
||||
}
|
||||
|
||||
if ($value instanceof BackedEnum) {
|
||||
return $value->value;
|
||||
}
|
||||
|
||||
return $value;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,42 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace BushlanovDev\MaxMessengerBot\Models;
|
||||
|
||||
/**
|
||||
* An abstract base for PATCH request models. It uses a variadic constructor
|
||||
* to capture named arguments, tracking which fields were explicitly set.
|
||||
*/
|
||||
abstract readonly class AbstractPatchModel extends AbstractModel
|
||||
{
|
||||
/**
|
||||
* @var array<int|string, mixed>
|
||||
*/
|
||||
protected array $patchData;
|
||||
|
||||
/**
|
||||
* Captures all passed named arguments into an associative array.
|
||||
*
|
||||
* @param mixed ...$params
|
||||
*/
|
||||
public function __construct(...$params)
|
||||
{
|
||||
$this->patchData = $params;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, mixed>
|
||||
* @throws \ReflectionException
|
||||
*/
|
||||
final public function toArray(): array
|
||||
{
|
||||
$result = [];
|
||||
foreach ($this->patchData as $key => $value) {
|
||||
$jsonKey = $this->toSnakeCase((string)$key);
|
||||
$result[$jsonKey] = $this->convertValue($value);
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace BushlanovDev\MaxMessengerBot\Models;
|
||||
|
||||
use BushlanovDev\MaxMessengerBot\Models\Attachments\Payloads\PhotoAttachmentRequestPayload;
|
||||
|
||||
/**
|
||||
* Represents the data to patch for a bot. Instantiate with named arguments.
|
||||
*
|
||||
* Example: new BotPatch(name: 'New Bot Name', description: null);
|
||||
*
|
||||
* @property-read string|null $name
|
||||
* @property-read string|null $description
|
||||
* @property-read BotCommand[]|null $commands
|
||||
* @property-read PhotoAttachmentRequestPayload|null $photo
|
||||
*/
|
||||
final readonly class BotPatch extends AbstractPatchModel
|
||||
{
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace BushlanovDev\MaxMessengerBot\Models;
|
||||
|
||||
use BushlanovDev\MaxMessengerBot\Models\Attachments\Payloads\PhotoAttachmentRequestPayload;
|
||||
|
||||
/**
|
||||
* Represents the data to patch for a chat. Instantiate with named arguments.
|
||||
*
|
||||
* Example: new ChatPatch(title: 'New Chat Title');
|
||||
*
|
||||
* @property-read PhotoAttachmentRequestPayload|null $icon
|
||||
* @property-read string|null $title
|
||||
* @property-read string|null $pin Message ID to be pinned.
|
||||
* @property-read bool|null $notify
|
||||
*/
|
||||
final readonly class ChatPatch extends AbstractPatchModel
|
||||
{
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace BushlanovDev\MaxMessengerBot\Models;
|
||||
|
||||
use BushlanovDev\MaxMessengerBot\Models\Attachments\Payloads\PhotoAttachmentRequestPayload;
|
||||
|
||||
/**
|
||||
* Contains detailed information about a video attachment.
|
||||
*/
|
||||
final readonly class VideoAttachmentDetails extends AbstractModel
|
||||
{
|
||||
/**
|
||||
* @param string $token The video attachment token.
|
||||
* @param int $width The width of the video in pixels.
|
||||
* @param int $height The height of the video in pixels.
|
||||
* @param int $duration The duration of the video in seconds.
|
||||
* @param VideoUrls|null $urls URLs to download or play the video. Can be null if the video is unavailable.
|
||||
* @param PhotoAttachmentRequestPayload|null $thumbnail The video's thumbnail image information.
|
||||
*/
|
||||
public function __construct(
|
||||
public string $token,
|
||||
public int $width,
|
||||
public int $height,
|
||||
public int $duration,
|
||||
public ?VideoUrls $urls = null,
|
||||
public ?PhotoAttachmentRequestPayload $thumbnail = null,
|
||||
) {
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace BushlanovDev\MaxMessengerBot\Models;
|
||||
|
||||
/**
|
||||
* Contains URLs for a video attachment in various resolutions.
|
||||
*/
|
||||
final readonly class VideoUrls extends AbstractModel
|
||||
{
|
||||
/**
|
||||
* @param string|null $mp4_1080 Video URL in 1080p resolution, if available.
|
||||
* @param string|null $mp4_720 Video URL in 720p resolution, if available.
|
||||
* @param string|null $mp4_480 Video URL in 480p resolution, if available.
|
||||
* @param string|null $mp4_360 Video URL in 360p resolution, if available.
|
||||
* @param string|null $mp4_240 Video URL in 240p resolution, if available.
|
||||
* @param string|null $mp4_144 Video URL in 144p resolution, if available.
|
||||
* @param string|null $hls Live streaming URL (HLS), if available.
|
||||
*/
|
||||
public function __construct(
|
||||
public ?string $mp4_1080 = null,
|
||||
public ?string $mp4_720 = null,
|
||||
public ?string $mp4_480 = null,
|
||||
public ?string $mp4_360 = null,
|
||||
public ?string $mp4_240 = null,
|
||||
public ?string $mp4_144 = null,
|
||||
public ?string $hls = null,
|
||||
) {
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user