Added PhotoAttachment

This commit is contained in:
Alex
2025-07-19 15:40:42 +03:00
parent b9c04bed33
commit 4661edffdf
11 changed files with 420 additions and 14 deletions
@@ -0,0 +1,32 @@
<?php
declare(strict_types=1);
namespace BushlanovDev\MaxMessengerBot\Models\Attachments\Payloads;
use BushlanovDev\MaxMessengerBot\Attributes\ArrayOf;
use InvalidArgumentException;
/**
* Request to attach image. All fields are mutually exclusive.
*/
final readonly class PhotoAttachmentPayload extends AbstractAttachmentPayload
{
/**
* @param string|null $url Any external image URL you want to attach.
* @param string|null $token Token of any existing attachment.
* @param PhotoToken[]|null $photos Tokens were obtained after uploading images.
*/
public function __construct(
public ?string $url = null,
public ?string $token = null,
#[ArrayOf(PhotoToken::class)]
public ?array $photos = null,
) {
if (count(array_filter([$this->url, $this->token, $this->photos])) !== 1) {
throw new InvalidArgumentException(
'Provide exactly one of "url", "token", or "photos" for PhotoAttachmentPayload.'
);
}
}
}
@@ -0,0 +1,22 @@
<?php
declare(strict_types=1);
namespace BushlanovDev\MaxMessengerBot\Models\Attachments\Payloads;
use BushlanovDev\MaxMessengerBot\Models\AbstractModel;
/**
* Encoded information of uploaded image
*/
final readonly class PhotoToken extends AbstractModel
{
/**
* @param string $token Encoded information of uploaded image.
*/
public function __construct(
public string $token,
) {
}
}
@@ -0,0 +1,59 @@
<?php
declare(strict_types=1);
namespace BushlanovDev\MaxMessengerBot\Models\Attachments\Requests;
use BushlanovDev\MaxMessengerBot\Enums\AttachmentType;
use BushlanovDev\MaxMessengerBot\Models\Attachments\Payloads\PhotoAttachmentPayload;
use BushlanovDev\MaxMessengerBot\Models\Attachments\Payloads\PhotoToken;
/**
* Request to attach some data to message.
*/
final readonly class PhotoAttachmentRequest extends AbstractAttachmentRequest
{
/**
* Creates a request to attach an image by URL.
*
* @param string $url
*
* @return PhotoAttachmentRequest
*/
public static function fromUrl(string $url): self
{
return new self(new PhotoAttachmentPayload(url: $url));
}
/**
* Creates a request to attach an image using the token received after uploading.
*
* @param string $token
*
* @return PhotoAttachmentRequest
*/
public static function fromToken(string $token): self
{
return new self(new PhotoAttachmentPayload(token: $token));
}
/**
* Creates a request to attach an image using the tokens received after uploading.
*
* @param PhotoToken[] $photos
*
* @return PhotoAttachmentRequest
*/
public static function fromPhotos(array $photos): self
{
return new self(new PhotoAttachmentPayload(photos: $photos));
}
/**
* @param PhotoAttachmentPayload $payload Request to attach image.
*/
private function __construct(PhotoAttachmentPayload $payload)
{
parent::__construct(AttachmentType::Image, $payload);
}
}