mirror of
https://github.com/BushlanovDev/max-bot-api-client-php.git
synced 2026-08-17 08:52:41 +00:00
fix #2 Error: Cannot instantiate abstract class AbstractAttachment после отправки номера телефона
This commit is contained in:
+4
-5
@@ -1,5 +1,4 @@
|
||||
/.gitattributes export-ignore
|
||||
/.gitignore export-ignore
|
||||
/.github export-ignore
|
||||
/phpunit.xml export-ignore
|
||||
/tests export-ignore
|
||||
/.* export-ignore
|
||||
/phpunit.xml export-ignore
|
||||
/phpstan.neon export-ignore
|
||||
/tests export-ignore
|
||||
|
||||
@@ -4,7 +4,7 @@ declare(strict_types=1);
|
||||
|
||||
namespace BushlanovDev\MaxMessengerBot\Enums;
|
||||
|
||||
enum AttachmentType: string
|
||||
enum AttachmentType: string
|
||||
{
|
||||
case Image = 'image';
|
||||
case Video = 'video';
|
||||
|
||||
+44
-26
@@ -47,6 +47,7 @@ use BushlanovDev\MaxMessengerBot\Models\Markup\StrongMarkup;
|
||||
use BushlanovDev\MaxMessengerBot\Models\Markup\UnderlineMarkup;
|
||||
use BushlanovDev\MaxMessengerBot\Models\Markup\UserMentionMarkup;
|
||||
use BushlanovDev\MaxMessengerBot\Models\Message;
|
||||
use BushlanovDev\MaxMessengerBot\Models\MessageBody;
|
||||
use BushlanovDev\MaxMessengerBot\Models\Result;
|
||||
use BushlanovDev\MaxMessengerBot\Models\Subscription;
|
||||
use BushlanovDev\MaxMessengerBot\Models\UpdateList;
|
||||
@@ -136,23 +137,54 @@ class ModelFactory
|
||||
*/
|
||||
public function createMessage(array $data): Message
|
||||
{
|
||||
if (isset($data['body']['attachments']) && is_array($data['body']['attachments'])) {
|
||||
$data['body']['attachments'] = array_map(
|
||||
[$this, 'createAttachment'],
|
||||
$data['body']['attachments'],
|
||||
);
|
||||
}
|
||||
|
||||
if (isset($data['body']['markup']) && is_array($data['body']['markup'])) {
|
||||
$data['body']['markup'] = array_map(
|
||||
[$this, 'createMarkupElement'],
|
||||
$data['body']['markup'],
|
||||
);
|
||||
if (isset($data['body']) && is_array($data['body'])) {
|
||||
$data['body'] = $this->createMessageBody($data['body']);
|
||||
}
|
||||
|
||||
return Message::fromArray($data);
|
||||
}
|
||||
|
||||
/**
|
||||
* List of messages.
|
||||
*
|
||||
* @param array<string, mixed> $data
|
||||
*
|
||||
* @return Message[]
|
||||
*/
|
||||
public function createMessages(array $data): array
|
||||
{
|
||||
return isset($data['messages']) && is_array($data['messages'])
|
||||
? array_map([$this, 'createMessage'], $data['messages'])
|
||||
: [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a MessageBody object from raw API data, handling polymorphic attachments and markup.
|
||||
*
|
||||
* @param array<string, mixed> $data
|
||||
*
|
||||
* @return MessageBody
|
||||
* @throws ReflectionException
|
||||
*/
|
||||
private function createMessageBody(array $data): MessageBody
|
||||
{
|
||||
if (isset($data['attachments']) && is_array($data['attachments'])) {
|
||||
$data['attachments'] = array_map(
|
||||
[$this, 'createAttachment'],
|
||||
$data['attachments'],
|
||||
);
|
||||
}
|
||||
|
||||
if (isset($data['markup']) && is_array($data['markup'])) {
|
||||
$data['markup'] = array_map(
|
||||
[$this, 'createMarkupElement'],
|
||||
$data['markup'],
|
||||
);
|
||||
}
|
||||
|
||||
return MessageBody::fromArray($data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a specific Attachment model based on the 'type' field.
|
||||
*
|
||||
@@ -237,20 +269,6 @@ class ModelFactory
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* List of messages.
|
||||
*
|
||||
* @param array<string, mixed> $data
|
||||
*
|
||||
* @return Message[]
|
||||
*/
|
||||
public function createMessages(array $data): array
|
||||
{
|
||||
return isset($data['messages']) && is_array($data['messages'])
|
||||
? array_map([$this, 'createMessage'], $data['messages'])
|
||||
: [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Endpoint you should upload to your binaries.
|
||||
*
|
||||
|
||||
@@ -12,7 +12,8 @@ final readonly class InlineKeyboardAttachment extends AbstractAttachment
|
||||
/**
|
||||
* @param KeyboardPayload $payload Keyboard payload.
|
||||
*/
|
||||
public function __construct(public KeyboardPayload $payload) {
|
||||
public function __construct(public KeyboardPayload $payload)
|
||||
{
|
||||
parent::__construct(AttachmentType::InlineKeyboard);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,9 +14,7 @@ final readonly class PhotoToken extends AbstractModel
|
||||
/**
|
||||
* @param string $token Encoded information of uploaded image.
|
||||
*/
|
||||
public function __construct(
|
||||
public string $token,
|
||||
) {
|
||||
public function __construct(public string $token)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -4,7 +4,6 @@ declare(strict_types=1);
|
||||
|
||||
namespace BushlanovDev\MaxMessengerBot\Models\Attachments\Payloads;
|
||||
|
||||
use BushlanovDev\MaxMessengerBot\Attributes\ArrayOf;
|
||||
use BushlanovDev\MaxMessengerBot\Models\Attachments\Buttons\Reply\AbstractReplyButton;
|
||||
|
||||
final readonly class ReplyKeyboardAttachmentRequestPayload extends AbstractAttachmentRequestPayload
|
||||
@@ -15,7 +14,6 @@ final readonly class ReplyKeyboardAttachmentRequestPayload extends AbstractAttac
|
||||
* @param int|null $directUserId If set, reply keyboard will only be shown to this participant.
|
||||
*/
|
||||
public function __construct(
|
||||
#[ArrayOf(AbstractReplyButton::class)]
|
||||
public array $buttons,
|
||||
public bool $direct = false,
|
||||
public ?int $directUserId = null,
|
||||
|
||||
@@ -12,8 +12,7 @@ final readonly class UploadedInfoAttachmentRequestPayload extends AbstractAttach
|
||||
/**
|
||||
* @param string $token The unique token received after a successful file upload.
|
||||
*/
|
||||
public function __construct(
|
||||
public string $token,
|
||||
) {
|
||||
public function __construct(public string $token)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,7 +12,8 @@ final readonly class ReplyKeyboardAttachment extends AbstractAttachment
|
||||
/**
|
||||
* @param AbstractReplyButton[][] $buttons
|
||||
*/
|
||||
public function __construct(public array $buttons) {
|
||||
public function __construct(public array $buttons)
|
||||
{
|
||||
parent::__construct(AttachmentType::ReplyKeyboard);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,7 +4,6 @@ declare(strict_types=1);
|
||||
|
||||
namespace BushlanovDev\MaxMessengerBot\Models;
|
||||
|
||||
use BushlanovDev\MaxMessengerBot\Attributes\ArrayOf;
|
||||
use BushlanovDev\MaxMessengerBot\Models\Attachments\AbstractAttachment;
|
||||
use BushlanovDev\MaxMessengerBot\Models\Markup\AbstractMarkup;
|
||||
|
||||
@@ -24,9 +23,7 @@ final readonly class MessageBody extends AbstractModel
|
||||
public string $mid,
|
||||
public int $seq,
|
||||
public ?string $text,
|
||||
#[ArrayOf(AbstractAttachment::class)]
|
||||
public ?array $attachments,
|
||||
#[ArrayOf(AbstractMarkup::class)]
|
||||
public ?array $markup,
|
||||
) {
|
||||
}
|
||||
|
||||
@@ -4,12 +4,35 @@ declare(strict_types=1);
|
||||
|
||||
namespace BushlanovDev\MaxMessengerBot\Tests\Models;
|
||||
|
||||
use BushlanovDev\MaxMessengerBot\ModelFactory;
|
||||
use BushlanovDev\MaxMessengerBot\Models\Attachments\AbstractAttachment;
|
||||
use BushlanovDev\MaxMessengerBot\Models\Attachments\ContactAttachment;
|
||||
use BushlanovDev\MaxMessengerBot\Models\Attachments\Payloads\ContactAttachmentPayload;
|
||||
use BushlanovDev\MaxMessengerBot\Models\Attachments\Payloads\PhotoAttachmentPayload;
|
||||
use BushlanovDev\MaxMessengerBot\Models\Attachments\PhotoAttachment;
|
||||
use BushlanovDev\MaxMessengerBot\Models\Markup\AbstractMarkup;
|
||||
use BushlanovDev\MaxMessengerBot\Models\Markup\StrongMarkup;
|
||||
use BushlanovDev\MaxMessengerBot\Models\Message;
|
||||
use BushlanovDev\MaxMessengerBot\Models\MessageBody;
|
||||
use BushlanovDev\MaxMessengerBot\Models\Recipient;
|
||||
use BushlanovDev\MaxMessengerBot\Models\User;
|
||||
use PHPUnit\Framework\Attributes\CoversClass;
|
||||
use PHPUnit\Framework\Attributes\Test;
|
||||
use PHPUnit\Framework\Attributes\UsesClass;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
#[CoversClass(MessageBody::class)]
|
||||
#[UsesClass(AbstractAttachment::class)]
|
||||
#[UsesClass(ContactAttachment::class)]
|
||||
#[UsesClass(ContactAttachmentPayload::class)]
|
||||
#[UsesClass(PhotoAttachmentPayload::class)]
|
||||
#[UsesClass(PhotoAttachment::class)]
|
||||
#[UsesClass(ModelFactory::class)]
|
||||
#[UsesClass(AbstractMarkup::class)]
|
||||
#[UsesClass(StrongMarkup::class)]
|
||||
#[UsesClass(Message::class)]
|
||||
#[UsesClass(Recipient::class)]
|
||||
#[UsesClass(User::class)]
|
||||
final class MessageBodyTest extends TestCase
|
||||
{
|
||||
#[Test]
|
||||
@@ -59,4 +82,57 @@ final class MessageBodyTest extends TestCase
|
||||
$this->assertIsArray($array);
|
||||
$this->assertSame($data, $array);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function createMessageCorrectlyHydratesComplexMessageBody(): void
|
||||
{
|
||||
$messageData = [
|
||||
'timestamp' => time(),
|
||||
'body' => [
|
||||
'mid' => 'mid.poly.test',
|
||||
'seq' => 200,
|
||||
'text' => 'Message with mixed content',
|
||||
'attachments' => [
|
||||
[
|
||||
'type' => 'contact',
|
||||
'payload' => [
|
||||
'vcf_info' => 'vcf...',
|
||||
'max_info' => [
|
||||
'user_id' => 1111,
|
||||
'first_name' => 'aaaaa',
|
||||
'is_bot' => false,
|
||||
'last_activity_time' => 1754385571000,
|
||||
],
|
||||
],
|
||||
],
|
||||
[
|
||||
'type' => 'image',
|
||||
'payload' => ['photo_id' => 1, 'token' => 't', 'url' => 'u'],
|
||||
]
|
||||
],
|
||||
'markup' => [
|
||||
['type' => 'strong', 'from' => 0, 'length' => 7],
|
||||
]
|
||||
],
|
||||
'recipient' => ['chat_type' => 'dialog', 'user_id' => 123],
|
||||
];
|
||||
|
||||
$factory = new ModelFactory();
|
||||
$message = $factory->createMessage($messageData);
|
||||
|
||||
$this->assertInstanceOf(Message::class, $message);
|
||||
$this->assertInstanceOf(MessageBody::class, $message->body);
|
||||
|
||||
$attachments = $message->body->attachments;
|
||||
$this->assertIsArray($attachments);
|
||||
$this->assertCount(2, $attachments);
|
||||
$this->assertInstanceOf(ContactAttachment::class, $attachments[0]);
|
||||
$this->assertInstanceOf(PhotoAttachment::class, $attachments[1]);
|
||||
|
||||
$markup = $message->body->markup;
|
||||
$this->assertIsArray($markup);
|
||||
$this->assertCount(1, $markup);
|
||||
$this->assertInstanceOf(StrongMarkup::class, $markup[0]);
|
||||
$this->assertSame(0, $markup[0]->from);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user