Skip to content

Commit c8329d6

Browse files
authored
Merge pull request #969 from noplanman/return_empty_array_for_items
Smarten up subEntities and return empty array for Entity items
2 parents 79747ac + 8cbdfef commit c8329d6

17 files changed

+74
-245
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,13 @@ Exclamation symbols (:exclamation:) note something of importance e.g. breaking c
1010
### Changed
1111
- Use PSR-12 for code style.
1212
- Some general housekeeping. (#972)
13+
- [:exclamation:][unreleased-bc] Return an empty array for Entity properties with no items, instead of `null`. (#969)
1314
### Deprecated
1415
### Removed
1516
- Botan.io integration completely removed.
1617
### Fixed
1718
- `forward_date` is now correctly saved to the DB.
19+
- Broken `StickerSet::getStickers()` method.
1820
### Security
1921
- Security disclosure managed by Tidelift.
2022

@@ -269,6 +271,7 @@ Exclamation symbols (:exclamation:) note something of importance e.g. breaking c
269271
- Move `hideKeyboard` to `removeKeyboard`.
270272

271273
[unreleased-sql-migration]: https://github.com/php-telegram-bot/core/tree/develop/utils/db-schema-update/unreleased.sql
274+
[unreleased-bc]: https://github.com/php-telegram-bot/core/wiki/Breaking-backwards-compatibility#unreleased
272275
[0.57.0-sql-migration]: https://github.com/php-telegram-bot/core/tree/master/utils/db-schema-update/0.56.0-0.57.0.sql
273276
[0.55.0-sql-migration]: https://github.com/php-telegram-bot/core/tree/master/utils/db-schema-update/0.54.1-0.55.0.sql
274277
[0.55.0-bc-move-animation-out-of-games-namespace]: https://github.com/php-telegram-bot/core/wiki/Breaking-backwards-compatibility#move-animation-out-of-games-namespace

src/DB.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -844,7 +844,7 @@ public static function insertPollRequest(Poll $poll)
844844

845845
$sth->bindValue(':id', $poll->getId());
846846
$sth->bindValue(':question', $poll->getQuestion());
847-
$sth->bindValue(':options', self::entitiesArrayToJson($poll->getOptions()));
847+
$sth->bindValue(':options', self::entitiesArrayToJson($poll->getOptions() ?: null));
848848
$sth->bindValue(':is_closed', $poll->getIsClosed());
849849
$sth->bindValue(':created_at', self::getTimestamp());
850850

@@ -976,13 +976,13 @@ public static function insertMessageRequest(Message $message)
976976
$sth->bindValue(':media_group_id', $message->getMediaGroupId());
977977
$sth->bindValue(':author_signature', $message->getAuthorSignature());
978978
$sth->bindValue(':text', $message->getText());
979-
$sth->bindValue(':entities', self::entitiesArrayToJson($message->getEntities()));
980-
$sth->bindValue(':caption_entities', self::entitiesArrayToJson($message->getCaptionEntities()));
979+
$sth->bindValue(':entities', self::entitiesArrayToJson($message->getEntities() ?: null));
980+
$sth->bindValue(':caption_entities', self::entitiesArrayToJson($message->getCaptionEntities() ?: null));
981981
$sth->bindValue(':audio', $message->getAudio());
982982
$sth->bindValue(':document', $message->getDocument());
983983
$sth->bindValue(':animation', $message->getAnimation());
984984
$sth->bindValue(':game', $message->getGame());
985-
$sth->bindValue(':photo', self::entitiesArrayToJson($message->getPhoto()));
985+
$sth->bindValue(':photo', self::entitiesArrayToJson($message->getPhoto() ?: null));
986986
$sth->bindValue(':sticker', $message->getSticker());
987987
$sth->bindValue(':video', $message->getVideo());
988988
$sth->bindValue(':voice', $message->getVoice());
@@ -995,7 +995,7 @@ public static function insertMessageRequest(Message $message)
995995
$sth->bindValue(':new_chat_members', $new_chat_members_ids);
996996
$sth->bindValue(':left_chat_member', $left_chat_member_id);
997997
$sth->bindValue(':new_chat_title', $message->getNewChatTitle());
998-
$sth->bindValue(':new_chat_photo', self::entitiesArrayToJson($message->getNewChatPhoto()));
998+
$sth->bindValue(':new_chat_photo', self::entitiesArrayToJson($message->getNewChatPhoto() ?: null));
999999
$sth->bindValue(':delete_chat_photo', $message->getDeleteChatPhoto());
10001000
$sth->bindValue(':group_chat_created', $message->getGroupChatCreated());
10011001
$sth->bindValue(':supergroup_chat_created', $message->getSupergroupChatCreated());
@@ -1058,7 +1058,7 @@ public static function insertEditedMessageRequest(Message $edited_message)
10581058
$sth->bindValue(':user_id', $user_id);
10591059
$sth->bindValue(':edit_date', $edit_date);
10601060
$sth->bindValue(':text', $edited_message->getText());
1061-
$sth->bindValue(':entities', self::entitiesArrayToJson($edited_message->getEntities()));
1061+
$sth->bindValue(':entities', self::entitiesArrayToJson($edited_message->getEntities() ?: null));
10621062
$sth->bindValue(':caption', $edited_message->getCaption());
10631063

10641064
return $sth->execute();

src/Entities/CallbackQuery.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class CallbackQuery extends Entity
3030
/**
3131
* {@inheritdoc}
3232
*/
33-
public function subEntities()
33+
protected function subEntities()
3434
{
3535
return [
3636
'from' => User::class,

src/Entities/Chat.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ class Chat extends Entity
4646
/**
4747
* {@inheritdoc}
4848
*/
49-
public function subEntities()
49+
protected function subEntities()
5050
{
5151
return [
5252
'photo' => ChatPhoto::class,

src/Entities/ChatMember.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class ChatMember extends Entity
3838
/**
3939
* {@inheritdoc}
4040
*/
41-
public function subEntities()
41+
protected function subEntities()
4242
{
4343
return [
4444
'user' => User::class,

src/Entities/Entity.php

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

140140
if (isset($sub_entities[$property_name])) {
141-
return new $sub_entities[$property_name]($property, $this->getProperty('bot_username'));
141+
$class = $sub_entities[$property_name];
142+
143+
if (is_array($class)) {
144+
return $this->makePrettyObjectArray(reset($class), $property_name);
145+
}
146+
147+
return new $class($property, $this->getProperty('bot_username'));
142148
}
143149

144150
return $property;

src/Entities/Games/Game.php

Lines changed: 8 additions & 36 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,39 +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 null|PhotoSize[]
51-
*/
52-
public function getPhoto()
53-
{
54-
$pretty_array = $this->makePrettyObjectArray(PhotoSize::class, 'photo');
55-
56-
return empty($pretty_array) ? null : $pretty_array;
57-
}
58-
59-
/**
60-
* Optional. Special entities that appear in text, such as usernames, URLs, bot commands, etc.
61-
*
62-
* This method overrides the default getTextEntities method
63-
* and returns a nice array of MessageEntity objects.
64-
*
65-
* @return null|MessageEntity[]
66-
*/
67-
public function getTextEntities()
68-
{
69-
$pretty_array = $this->makePrettyObjectArray(MessageEntity::class, 'text_entities');
70-
71-
return empty($pretty_array) ? null : $pretty_array;
72-
}
7345
}

src/Entities/Message.php

Lines changed: 10 additions & 91 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,102 +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-
public function __construct(array $data, $bot_username = '')
109-
{
110-
parent::__construct($data, $bot_username);
111-
}
112-
113-
/**
114-
* Optional. Message is a photo, available sizes of the photo
115-
*
116-
* This method overrides the default getPhoto method
117-
* and returns a nice array of PhotoSize objects.
118-
*
119-
* @return null|PhotoSize[]
120-
*/
121-
public function getPhoto()
122-
{
123-
$pretty_array = $this->makePrettyObjectArray(PhotoSize::class, 'photo');
124-
125-
return empty($pretty_array) ? null : $pretty_array;
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 null|PhotoSize[]
135-
*/
136-
public function getNewChatPhoto()
137-
{
138-
$pretty_array = $this->makePrettyObjectArray(PhotoSize::class, 'new_chat_photo');
139-
140-
return empty($pretty_array) ? null : $pretty_array;
141-
}
142-
143-
/**
144-
* Optional. A new member(s) was added to the group, information about them (one of this members may be the bot itself)
145-
*
146-
* This method overrides the default getNewChatMembers method
147-
* and returns a nice array of User objects.
148-
*
149-
* @return null|User[]
150-
*/
151-
public function getNewChatMembers()
152-
{
153-
$pretty_array = $this->makePrettyObjectArray(User::class, 'new_chat_members');
154-
155-
return empty($pretty_array) ? null : $pretty_array;
156-
}
157-
158-
/**
159-
* Optional. For text messages, special entities like usernames, URLs, bot commands, etc. that appear in the text
160-
*
161-
* This method overrides the default getEntities method
162-
* and returns a nice array of MessageEntity objects.
163-
*
164-
* @return null|MessageEntity[]
165-
*/
166-
public function getEntities()
167-
{
168-
$pretty_array = $this->makePrettyObjectArray(MessageEntity::class, 'entities');
169-
170-
return empty($pretty_array) ? null : $pretty_array;
171-
}
172-
173-
/**
174-
* Optional. For messages with a caption, special entities like usernames, URLs, bot commands, etc. that appear in the caption
175-
*
176-
* This method overrides the default getCaptionEntities method
177-
* and returns a nice array of MessageEntity objects.
178-
*
179-
* @return null|MessageEntity[]
180-
*/
181-
public function getCaptionEntities()
182-
{
183-
$pretty_array = $this->makePrettyObjectArray(MessageEntity::class, 'caption_entities');
184-
185-
return empty($pretty_array) ? null : $pretty_array;
186-
}
187-
188107
/**
189108
* return the entire command like /echo or /echo@bot1 if specified
190109
*

src/Entities/Payments/OrderInfo.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class OrderInfo extends Entity
2929
/**
3030
* {@inheritdoc}
3131
*/
32-
public function subEntities()
32+
protected function subEntities()
3333
{
3434
return [
3535
'shipping_address' => ShippingAddress::class,

src/Entities/Payments/PreCheckoutQuery.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class PreCheckoutQuery extends Entity
3535
/**
3636
* {@inheritdoc}
3737
*/
38-
public function subEntities()
38+
protected function subEntities()
3939
{
4040
return [
4141
'from' => User::class,

0 commit comments

Comments
 (0)