Skip to content

Commit c5d55a5

Browse files
committed
Added DataAttachment & LinkedMessage & Markups
1 parent 51b105b commit c5d55a5

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+990
-164
lines changed

src/Enums/AttachmentType.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,5 @@ enum AttachmentType: string
1616
case ReplyKeyboard = 'reply_keyboard';
1717
case Location = 'location';
1818
case Share = 'share';
19+
case Data = 'data';
1920
}

src/Enums/MarkupType.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace BushlanovDev\MaxMessengerBot\Enums;
6+
7+
enum MarkupType: string
8+
{
9+
case Strong = 'strong';
10+
case Emphasized = 'emphasized';
11+
case Monospaced = 'monospaced';
12+
case Link = 'link';
13+
case Strikethrough = 'strikethrough';
14+
case Underline = 'underline';
15+
case UserMention = 'user_mention';
16+
case Heading = 'heading';
17+
case Highlighted = 'highlighted';
18+
}

src/ModelFactory.php

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,27 @@
44

55
namespace BushlanovDev\MaxMessengerBot;
66

7+
use BushlanovDev\MaxMessengerBot\Enums\AttachmentType;
8+
use BushlanovDev\MaxMessengerBot\Enums\MarkupType;
79
use BushlanovDev\MaxMessengerBot\Enums\UpdateType;
10+
use BushlanovDev\MaxMessengerBot\Models\Attachments\AbstractAttachment;
11+
use BushlanovDev\MaxMessengerBot\Models\Attachments\DataAttachment;
12+
use BushlanovDev\MaxMessengerBot\Models\Attachments\ShareAttachment;
813
use BushlanovDev\MaxMessengerBot\Models\BotInfo;
914
use BushlanovDev\MaxMessengerBot\Models\Chat;
1015
use BushlanovDev\MaxMessengerBot\Models\ChatList;
1116
use BushlanovDev\MaxMessengerBot\Models\ChatMember;
1217
use BushlanovDev\MaxMessengerBot\Models\ChatMembersList;
18+
use BushlanovDev\MaxMessengerBot\Models\Markup\AbstractMarkup;
19+
use BushlanovDev\MaxMessengerBot\Models\Markup\EmphasizedMarkup;
20+
use BushlanovDev\MaxMessengerBot\Models\Markup\HeadingMarkup;
21+
use BushlanovDev\MaxMessengerBot\Models\Markup\HighlightedMarkup;
22+
use BushlanovDev\MaxMessengerBot\Models\Markup\LinkMarkup;
23+
use BushlanovDev\MaxMessengerBot\Models\Markup\MonospacedMarkup;
24+
use BushlanovDev\MaxMessengerBot\Models\Markup\StrikethroughMarkup;
25+
use BushlanovDev\MaxMessengerBot\Models\Markup\StrongMarkup;
26+
use BushlanovDev\MaxMessengerBot\Models\Markup\UnderlineMarkup;
27+
use BushlanovDev\MaxMessengerBot\Models\Markup\UserMentionMarkup;
1328
use BushlanovDev\MaxMessengerBot\Models\Message;
1429
use BushlanovDev\MaxMessengerBot\Models\Result;
1530
use BushlanovDev\MaxMessengerBot\Models\Subscription;
@@ -100,9 +115,42 @@ public function createSubscriptions(array $data): array
100115
*/
101116
public function createMessage(array $data): Message
102117
{
118+
if (isset($data['body']['attachments']) && is_array($data['body']['attachments'])) {
119+
$data['body']['attachments'] = array_map(
120+
[$this, 'createAttachment'],
121+
$data['body']['attachments'],
122+
);
123+
}
124+
125+
if (isset($data['body']['markup']) && is_array($data['body']['markup'])) {
126+
$data['body']['markup'] = array_map(
127+
[$this, 'createMarkupElement'],
128+
$data['body']['markup'],
129+
);
130+
}
131+
103132
return Message::fromArray($data);
104133
}
105134

135+
/**
136+
* Creates a specific Attachment model based on the 'type' field.
137+
*
138+
* @param array<string, mixed> $data
139+
*
140+
* @return AbstractAttachment
141+
* @throws ReflectionException
142+
*/
143+
public function createAttachment(array $data): AbstractAttachment
144+
{
145+
return match (AttachmentType::tryFrom($data['type'] ?? '')) {
146+
AttachmentType::Data => DataAttachment::fromArray($data),
147+
AttachmentType::Share => ShareAttachment::fromArray($data),
148+
default => throw new LogicException(
149+
'Unknown or unsupported Attachment type: ' . ($data['type'] ?? 'none')
150+
),
151+
};
152+
}
153+
106154
/**
107155
* List of messages.
108156
*
@@ -248,4 +296,30 @@ public function createVideoAttachmentDetails(array $data): VideoAttachmentDetail
248296
{
249297
return VideoAttachmentDetails::fromArray($data);
250298
}
299+
300+
/**
301+
* Creates a specific Markup model based on the 'type' field.
302+
*
303+
* @param array<string, mixed> $data
304+
*
305+
* @return AbstractMarkup
306+
* @throws ReflectionException
307+
*/
308+
public function createMarkupElement(array $data): AbstractMarkup
309+
{
310+
return match (MarkupType::tryFrom($data['type'] ?? '')) {
311+
MarkupType::Strong => StrongMarkup::fromArray($data),
312+
MarkupType::Emphasized => EmphasizedMarkup::fromArray($data),
313+
MarkupType::Monospaced => MonospacedMarkup::fromArray($data),
314+
MarkupType::Strikethrough => StrikethroughMarkup::fromArray($data),
315+
MarkupType::Underline => UnderlineMarkup::fromArray($data),
316+
MarkupType::Heading => HeadingMarkup::fromArray($data),
317+
MarkupType::Highlighted => HighlightedMarkup::fromArray($data),
318+
MarkupType::Link => LinkMarkup::fromArray($data),
319+
MarkupType::UserMention => UserMentionMarkup::fromArray($data),
320+
default => throw new LogicException(
321+
'Unknown or unsupported markup type: ' . ($data['type'] ?? 'none')
322+
),
323+
};
324+
}
251325
}

src/Models/AbstractModel.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,10 @@ private static function castValue(mixed $value, ReflectionProperty $property): m
118118

119119
$typeName = $type->getName();
120120

121+
if (is_object($value) && is_a($value, $typeName)) {
122+
return $value;
123+
}
124+
121125
if ($type->isBuiltin()) {
122126
return match ($typeName) {
123127
'int' => (int)$value,
@@ -168,7 +172,10 @@ private static function castArray(mixed $value, ReflectionProperty $property): a
168172
}
169173

170174
if (is_subclass_of($itemClassName, self::class)) {
171-
return array_map(fn($item) => $itemClassName::fromArray($item), $value);
175+
return array_map(
176+
fn($item) => is_a($item, $itemClassName) ? $item : $itemClassName::fromArray($item),
177+
$value,
178+
);
172179
}
173180

174181
return $value;
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace BushlanovDev\MaxMessengerBot\Models\Attachments;
6+
7+
use BushlanovDev\MaxMessengerBot\Enums\AttachmentType;
8+
use BushlanovDev\MaxMessengerBot\Models\AbstractModel;
9+
10+
/**
11+
* Represents a generic attachment received from the API.
12+
*/
13+
abstract readonly class AbstractAttachment extends AbstractModel
14+
{
15+
public function __construct(public AttachmentType $type)
16+
{
17+
}
18+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace BushlanovDev\MaxMessengerBot\Models\Attachments;
6+
7+
use BushlanovDev\MaxMessengerBot\Enums\AttachmentType;
8+
9+
/**
10+
* Represents an attachment containing a payload from a SendMessageButton.
11+
*/
12+
final readonly class DataAttachment extends AbstractAttachment
13+
{
14+
/**
15+
* @param string $data The payload from the button.
16+
*/
17+
public function __construct(public string $data)
18+
{
19+
parent::__construct(AttachmentType::Data);
20+
}
21+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace BushlanovDev\MaxMessengerBot\Models\Attachments;
6+
7+
use BushlanovDev\MaxMessengerBot\Enums\AttachmentType;
8+
use BushlanovDev\MaxMessengerBot\Models\Attachments\Payloads\ShareAttachmentRequestPayload;
9+
10+
/**
11+
* Represents a share (URL preview) attachment.
12+
*/
13+
final readonly class ShareAttachment extends AbstractAttachment
14+
{
15+
public function __construct(
16+
public ShareAttachmentRequestPayload $payload,
17+
public ?string $title,
18+
public ?string $description,
19+
public ?string $imageUrl,
20+
) {
21+
parent::__construct(AttachmentType::Share);
22+
}
23+
}

src/Models/Chat.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
* @param int|null $ownerId Identifier of chat owner. Visible only for chat admins
2525
* @param string|null $link Link on chat.
2626
* @param string|null $description Chat description.
27-
* @param User|null $dialogWithUser Another user in conversation. For `dialog` type chats only.
27+
* @param UserWithPhoto|null $dialogWithUser Another user in conversation. For `dialog` type chats only.
2828
* @param int|null $messagesCount Messages count in chat. Only for group chats and channels. Not available for dialogs.
2929
* @param string|null $chatMessageId Identifier of message that contains `chat` button initialized chat.
3030
* @param Message|null $pinnedMessage Pinned message in chat or channel. Returned only when single chat is requested.
@@ -41,7 +41,7 @@ public function __construct(
4141
public ?int $ownerId,
4242
public ?string $link,
4343
public ?string $description,
44-
public ?User $dialogWithUser,
44+
public ?UserWithPhoto $dialogWithUser,
4545
public ?int $messagesCount,
4646
public ?string $chatMessageId,
4747
public ?Message $pinnedMessage,

src/Models/LinkedMessage.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace BushlanovDev\MaxMessengerBot\Models;
6+
7+
use BushlanovDev\MaxMessengerBot\Enums\MessageLinkType;
8+
9+
/**
10+
* Represents a forwarded or replied message linked to the main message.
11+
*/
12+
final readonly class LinkedMessage extends AbstractModel
13+
{
14+
/**
15+
* @param MessageLinkType $type Type of linked message (forward or reply).
16+
* @param MessageBody $message The body of the original message.
17+
* @param User|null $sender The sender of the original message. Can be null if posted on behalf of a channel.
18+
* @param int|null $chatId The chat where the message was originally posted (for forwarded messages).
19+
*/
20+
public function __construct(
21+
public MessageLinkType $type,
22+
public MessageBody $message,
23+
public ?User $sender,
24+
public ?int $chatId,
25+
) {
26+
}
27+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace BushlanovDev\MaxMessengerBot\Models\Markup;
6+
7+
use BushlanovDev\MaxMessengerBot\Enums\MarkupType;
8+
use BushlanovDev\MaxMessengerBot\Models\AbstractModel;
9+
10+
/**
11+
* Base class for a text markup element.
12+
*/
13+
abstract readonly class AbstractMarkup extends AbstractModel
14+
{
15+
/**
16+
* @param MarkupType $type The type of the markup element.
17+
* @param int $from Element start index (zero-based) in text.
18+
* @param int $length Length of the markup element.
19+
*/
20+
public function __construct(
21+
public MarkupType $type,
22+
public int $from,
23+
public int $length,
24+
) {
25+
}
26+
}

0 commit comments

Comments
 (0)