Skip to content

Commit c77ccd6

Browse files
committed
Simplify the process of getting arrays of items from Entities.
1 parent 89a00f1 commit c77ccd6

File tree

9 files changed

+59
-211
lines changed

9 files changed

+59
-211
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ Exclamation symbols (:exclamation:) note something of importance e.g. breaking c
1212
### Removed
1313
### Fixed
1414
- `forward_date` is now correctly saved to the DB.
15+
- Broken `StickerSet::getStickers()` method.
1516
### Security
1617

1718
## [0.57.0] - 2019-06-01

src/Entities/Entity.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,13 @@ public function __call($method, $args)
142142
$sub_entities = $this->subEntities();
143143

144144
if (isset($sub_entities[$property_name])) {
145-
return new $sub_entities[$property_name]($property, $this->getProperty('bot_username'));
145+
$class = $sub_entities[$property_name];
146+
147+
if (is_array($class)) {
148+
return $this->makePrettyObjectArray(reset($class), $property_name);
149+
}
150+
151+
return new $class($property, $this->getProperty('bot_username'));
146152
}
147153

148154
return $property;

src/Entities/Games/Game.php

Lines changed: 8 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,12 @@
2222
*
2323
* @link https://core.telegram.org/bots/api#game
2424
*
25-
* @method string getTitle() Title of the game
26-
* @method string getDescription() Description of the game
27-
* @method string getText() Optional. Brief description of the game or high scores included in the game message. Can be automatically edited to include current high scores for the game when the bot calls setGameScore, or manually edited using editMessageText. 0-4096 characters.
28-
* @method Animation getAnimation() Optional. Animation that will be displayed in the game message in chats. Upload via BotFather
25+
* @method string getTitle() Title of the game
26+
* @method string getDescription() Description of the game
27+
* @method PhotoSize[] getPhoto() Photo that will be displayed in the game message in chats.
28+
* @method string getText() Optional. Brief description of the game or high scores included in the game message. Can be automatically edited to include current high scores for the game when the bot calls setGameScore, or manually edited using editMessageText. 0-4096 characters.
29+
* @method MessageEntity[] getTextEntities() Optional. Special entities that appear in text, such as usernames, URLs, bot commands, etc.
30+
* @method Animation getAnimation() Optional. Animation that will be displayed in the game message in chats. Upload via BotFather
2931
**/
3032
class Game extends Entity
3133
{
@@ -35,35 +37,9 @@ class Game extends Entity
3537
protected function subEntities()
3638
{
3739
return [
38-
'photo' => PhotoSize::class,
39-
'text_entities' => MessageEntity::class,
40+
'photo' => [PhotoSize::class],
41+
'text_entities' => [MessageEntity::class],
4042
'animation' => Animation::class,
4143
];
4244
}
43-
44-
/**
45-
* Photo that will be displayed in the game message in chats.
46-
*
47-
* This method overrides the default getPhoto method
48-
* and returns a nice array of PhotoSize objects.
49-
*
50-
* @return PhotoSize[]
51-
*/
52-
public function getPhoto()
53-
{
54-
return $this->makePrettyObjectArray(PhotoSize::class, 'photo');
55-
}
56-
57-
/**
58-
* Optional. Special entities that appear in text, such as usernames, URLs, bot commands, etc.
59-
*
60-
* This method overrides the default getTextEntities method
61-
* and returns a nice array of MessageEntity objects.
62-
*
63-
* @return MessageEntity[]
64-
*/
65-
public function getTextEntities()
66-
{
67-
return $this->makePrettyObjectArray(MessageEntity::class, 'text_entities');
68-
}
6945
}

src/Entities/Message.php

Lines changed: 10 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,13 @@
3434
* @method int getEditDate() Optional. Date the message was last edited in Unix time
3535
* @method string getMediaGroupId() Optional. The unique identifier of a media message group this message belongs to
3636
* @method string getAuthorSignature() Optional. Signature of the post author for messages in channels
37+
* @method MessageEntity[] getEntities() Optional. For text messages, special entities like usernames, URLs, bot commands, etc. that appear in the text
38+
* @method MessageEntity[] getCaptionEntities() Optional. For messages with a caption, special entities like usernames, URLs, bot commands, etc. that appear in the caption
3739
* @method Audio getAudio() Optional. Message is an audio file, information about the file
3840
* @method Document getDocument() Optional. Message is a general file, information about the file
3941
* @method Animation getAnimation() Optional. Message is an animation, information about the animation. For backward compatibility, when this field is set, the document field will also be set
4042
* @method Game getGame() Optional. Message is a game, information about the game.
43+
* @method PhotoSize[] getPhoto() Optional. Message is a photo, available sizes of the photo
4144
* @method Sticker getSticker() Optional. Message is a sticker, information about the sticker
4245
* @method Video getVideo() Optional. Message is a video, information about the video
4346
* @method Voice getVoice() Optional. Message is a voice message, information about the file
@@ -47,8 +50,10 @@
4750
* @method Location getLocation() Optional. Message is a shared location, information about the location
4851
* @method Venue getVenue() Optional. Message is a venue, information about the venue
4952
* @method Poll getPoll() Optional. Message is a native poll, information about the poll
53+
* @method User[] getNewChatMembers() Optional. A new member(s) was added to the group, information about them (one of this members may be the bot itself)
5054
* @method User getLeftChatMember() Optional. A member was removed from the group, information about them (this member may be the bot itself)
5155
* @method string getNewChatTitle() Optional. A chat title was changed to this value
56+
* @method PhotoSize[] getNewChatPhoto() Optional. A chat photo was changed to this value
5257
* @method bool getDeleteChatPhoto() Optional. Service message: the chat photo was deleted
5358
* @method bool getGroupChatCreated() Optional. Service message: the group has been created
5459
* @method bool getSupergroupChatCreated() Optional. Service message: the supergroup has been created. This field can't be received in a message coming through updates, because bot can’t be a member of a supergroup when it is created. It can only be found in reply_to_message if someone replies to a very first message in a directly created supergroup.
@@ -74,13 +79,13 @@ protected function subEntities()
7479
'forward_from' => User::class,
7580
'forward_from_chat' => Chat::class,
7681
'reply_to_message' => ReplyToMessage::class,
77-
'entities' => MessageEntity::class,
78-
'caption_entities' => MessageEntity::class,
82+
'entities' => [MessageEntity::class],
83+
'caption_entities' => [MessageEntity::class],
7984
'audio' => Audio::class,
8085
'document' => Document::class,
8186
'animation' => Animation::class,
8287
'game' => Game::class,
83-
'photo' => PhotoSize::class,
88+
'photo' => [PhotoSize::class],
8489
'sticker' => Sticker::class,
8590
'video' => Video::class,
8691
'voice' => Voice::class,
@@ -89,94 +94,16 @@ protected function subEntities()
8994
'location' => Location::class,
9095
'venue' => Venue::class,
9196
'poll' => Poll::class,
92-
'new_chat_members' => User::class,
97+
'new_chat_members' => [User::class],
9398
'left_chat_member' => User::class,
94-
'new_chat_photo' => PhotoSize::class,
99+
'new_chat_photo' => [PhotoSize::class],
95100
'pinned_message' => Message::class,
96101
'invoice' => Invoice::class,
97102
'successful_payment' => SuccessfulPayment::class,
98103
'passport_data' => PassportData::class,
99104
];
100105
}
101106

102-
/**
103-
* Message constructor
104-
*
105-
* @param array $data
106-
* @param string $bot_username
107-
*
108-
* @throws \Longman\TelegramBot\Exception\TelegramException
109-
*/
110-
public function __construct(array $data, $bot_username = '')
111-
{
112-
parent::__construct($data, $bot_username);
113-
}
114-
115-
/**
116-
* Optional. Message is a photo, available sizes of the photo
117-
*
118-
* This method overrides the default getPhoto method
119-
* and returns a nice array of PhotoSize objects.
120-
*
121-
* @return PhotoSize[]
122-
*/
123-
public function getPhoto()
124-
{
125-
return $this->makePrettyObjectArray(PhotoSize::class, 'photo');
126-
}
127-
128-
/**
129-
* Optional. A chat photo was changed to this value
130-
*
131-
* This method overrides the default getNewChatPhoto method
132-
* and returns a nice array of PhotoSize objects.
133-
*
134-
* @return PhotoSize[]
135-
*/
136-
public function getNewChatPhoto()
137-
{
138-
return $this->makePrettyObjectArray(PhotoSize::class, 'new_chat_photo');
139-
}
140-
141-
/**
142-
* Optional. A new member(s) was added to the group, information about them (one of this members may be the bot itself)
143-
*
144-
* This method overrides the default getNewChatMembers method
145-
* and returns a nice array of User objects.
146-
*
147-
* @return User[]
148-
*/
149-
public function getNewChatMembers()
150-
{
151-
return $this->makePrettyObjectArray(User::class, 'new_chat_members');
152-
}
153-
154-
/**
155-
* Optional. For text messages, special entities like usernames, URLs, bot commands, etc. that appear in the text
156-
*
157-
* This method overrides the default getEntities method
158-
* and returns a nice array of MessageEntity objects.
159-
*
160-
* @return MessageEntity[]
161-
*/
162-
public function getEntities()
163-
{
164-
return $this->makePrettyObjectArray(MessageEntity::class, 'entities');
165-
}
166-
167-
/**
168-
* Optional. For messages with a caption, special entities like usernames, URLs, bot commands, etc. that appear in the caption
169-
*
170-
* This method overrides the default getCaptionEntities method
171-
* and returns a nice array of MessageEntity objects.
172-
*
173-
* @return MessageEntity[]
174-
*/
175-
public function getCaptionEntities()
176-
{
177-
return $this->makePrettyObjectArray(MessageEntity::class, 'caption_entities');
178-
}
179-
180107
/**
181108
* return the entire command like /echo or /echo@bot1 if specified
182109
*
@@ -254,7 +181,6 @@ public function getText($without_cmd = false)
254181
* Bot added in chat
255182
*
256183
* @return bool
257-
* @throws \Longman\TelegramBot\Exception\TelegramException
258184
*/
259185
public function botAddedInChat()
260186
{

src/Entities/Payments/ShippingOption.php

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@
1919
*
2020
* @link https://core.telegram.org/bots/api#shippingoption
2121
*
22-
* @method string getId() Shipping option identifier
23-
* @method string getTitle() Option title
22+
* @method string getId() Shipping option identifier
23+
* @method string getTitle() Option title
24+
* @method LabeledPrice[] getPrices() List of price portions
2425
**/
2526
class ShippingOption extends Entity
2627
{
@@ -30,19 +31,7 @@ class ShippingOption extends Entity
3031
protected function subEntities()
3132
{
3233
return [
33-
'prices' => LabeledPrice::class,
34+
'prices' => [LabeledPrice::class],
3435
];
3536
}
36-
37-
/**
38-
* List of price portions
39-
*
40-
* This method overrides the default getPrices method and returns a nice array
41-
*
42-
* @return LabeledPrice[]
43-
*/
44-
public function getPrices()
45-
{
46-
return $this->makePrettyObjectArray(LabeledPrice::class, 'prices');
47-
}
4837
}

src/Entities/Poll.php

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,10 @@
1717
*
1818
* @link https://core.telegram.org/bots/api#poll
1919
*
20-
* @method string getId() Unique poll identifier
21-
* @method string getQuestion() Poll question, 1-255 characters
22-
* @method bool getIsClosed() True, if the poll is closed
20+
* @method string getId() Unique poll identifier
21+
* @method string getQuestion() Poll question, 1-255 characters
22+
* @method PollOption[] getOptions() List of poll options
23+
* @method bool getIsClosed() True, if the poll is closed
2324
*/
2425
class Poll extends Entity
2526
{
@@ -29,20 +30,7 @@ class Poll extends Entity
2930
protected function subEntities()
3031
{
3132
return [
32-
'options' => PollOption::class,
33+
'options' => [PollOption::class],
3334
];
3435
}
35-
36-
/**
37-
* List of poll options
38-
*
39-
* This method overrides the default getOptions method
40-
* and returns a nice array of PollOption objects.
41-
*
42-
* @return PollOption[]
43-
*/
44-
public function getOptions()
45-
{
46-
return $this->makePrettyObjectArray(PollOption::class, 'options');
47-
}
4836
}

src/Entities/StickerSet.php

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,20 @@
1515
*
1616
* @link https://core.telegram.org/bots/api#stickerset
1717
*
18-
* @method string getName() Sticker set name
19-
* @method string getTitle() Sticker set title
20-
* @method bool getContainsMasks() True, if the sticker set contains masks
18+
* @method string getName() Sticker set name
19+
* @method string getTitle() Sticker set title
20+
* @method bool getContainsMasks() True, if the sticker set contains masks
21+
* @method Sticker[] getStickers() List of all set stickers
2122
*/
2223
class StickerSet extends Entity
2324
{
2425
/**
25-
* List of all set stickers
26-
*
27-
* This method overrides the default getStickers method
28-
* and returns a nice array of Sticker objects.
29-
*
30-
* @return Sticker[]
26+
* {@inheritdoc}
3127
*/
32-
public function getStickers()
28+
protected function subEntities()
3329
{
34-
return $this->makePrettyObjectArray(Sticker::class, 'stickers');
30+
return [
31+
'stickers' => [Sticker::class],
32+
];
3533
}
3634
}

0 commit comments

Comments
 (0)