Skip to content

Commit 89a00f1

Browse files
committed
Return an empty array for Entity properties with no items, instead of null
1 parent 89af4d4 commit 89a00f1

File tree

8 files changed

+32
-54
lines changed

8 files changed

+32
-54
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ Exclamation symbols (:exclamation:) note something of importance e.g. breaking c
77
### Added
88
### Changed
99
- Use PSR-12 for code style.
10+
- [:exclamation:][unreleased-bc] Return an empty array for Entity properties with no items, instead of `null`.
1011
### Deprecated
1112
### Removed
1213
### Fixed
@@ -264,6 +265,7 @@ Exclamation symbols (:exclamation:) note something of importance e.g. breaking c
264265
- Move `hideKeyboard` to `removeKeyboard`.
265266

266267
[unreleased-sql-migration]: https://github.com/php-telegram-bot/core/tree/develop/utils/db-schema-update/unreleased.sql
268+
[unreleased-bc]: https://github.com/php-telegram-bot/core/wiki/Breaking-backwards-compatibility#unreleased
267269
[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
268270
[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
269271
[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/Games/Game.php

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,11 @@ protected function subEntities()
4747
* This method overrides the default getPhoto method
4848
* and returns a nice array of PhotoSize objects.
4949
*
50-
* @return null|\Longman\TelegramBot\Entities\PhotoSize[]
50+
* @return PhotoSize[]
5151
*/
5252
public function getPhoto()
5353
{
54-
$pretty_array = $this->makePrettyObjectArray(PhotoSize::class, 'photo');
55-
56-
return empty($pretty_array) ? null : $pretty_array;
54+
return $this->makePrettyObjectArray(PhotoSize::class, 'photo');
5755
}
5856

5957
/**
@@ -62,12 +60,10 @@ public function getPhoto()
6260
* This method overrides the default getTextEntities method
6361
* and returns a nice array of MessageEntity objects.
6462
*
65-
* @return null|\Longman\TelegramBot\Entities\MessageEntity[]
63+
* @return MessageEntity[]
6664
*/
6765
public function getTextEntities()
6866
{
69-
$pretty_array = $this->makePrettyObjectArray(MessageEntity::class, 'text_entities');
70-
71-
return empty($pretty_array) ? null : $pretty_array;
67+
return $this->makePrettyObjectArray(MessageEntity::class, 'text_entities');
7268
}
7369
}

src/Entities/Message.php

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -118,13 +118,11 @@ public function __construct(array $data, $bot_username = '')
118118
* This method overrides the default getPhoto method
119119
* and returns a nice array of PhotoSize objects.
120120
*
121-
* @return null|PhotoSize[]
121+
* @return PhotoSize[]
122122
*/
123123
public function getPhoto()
124124
{
125-
$pretty_array = $this->makePrettyObjectArray(PhotoSize::class, 'photo');
126-
127-
return empty($pretty_array) ? null : $pretty_array;
125+
return $this->makePrettyObjectArray(PhotoSize::class, 'photo');
128126
}
129127

130128
/**
@@ -133,13 +131,11 @@ public function getPhoto()
133131
* This method overrides the default getNewChatPhoto method
134132
* and returns a nice array of PhotoSize objects.
135133
*
136-
* @return null|PhotoSize[]
134+
* @return PhotoSize[]
137135
*/
138136
public function getNewChatPhoto()
139137
{
140-
$pretty_array = $this->makePrettyObjectArray(PhotoSize::class, 'new_chat_photo');
141-
142-
return empty($pretty_array) ? null : $pretty_array;
138+
return $this->makePrettyObjectArray(PhotoSize::class, 'new_chat_photo');
143139
}
144140

145141
/**
@@ -148,13 +144,11 @@ public function getNewChatPhoto()
148144
* This method overrides the default getNewChatMembers method
149145
* and returns a nice array of User objects.
150146
*
151-
* @return null|User[]
147+
* @return User[]
152148
*/
153149
public function getNewChatMembers()
154150
{
155-
$pretty_array = $this->makePrettyObjectArray(User::class, 'new_chat_members');
156-
157-
return empty($pretty_array) ? null : $pretty_array;
151+
return $this->makePrettyObjectArray(User::class, 'new_chat_members');
158152
}
159153

160154
/**
@@ -163,13 +157,11 @@ public function getNewChatMembers()
163157
* This method overrides the default getEntities method
164158
* and returns a nice array of MessageEntity objects.
165159
*
166-
* @return null|MessageEntity[]
160+
* @return MessageEntity[]
167161
*/
168162
public function getEntities()
169163
{
170-
$pretty_array = $this->makePrettyObjectArray(MessageEntity::class, 'entities');
171-
172-
return empty($pretty_array) ? null : $pretty_array;
164+
return $this->makePrettyObjectArray(MessageEntity::class, 'entities');
173165
}
174166

175167
/**
@@ -178,13 +170,11 @@ public function getEntities()
178170
* This method overrides the default getCaptionEntities method
179171
* and returns a nice array of MessageEntity objects.
180172
*
181-
* @return null|MessageEntity[]
173+
* @return MessageEntity[]
182174
*/
183175
public function getCaptionEntities()
184176
{
185-
$pretty_array = $this->makePrettyObjectArray(MessageEntity::class, 'caption_entities');
186-
187-
return empty($pretty_array) ? null : $pretty_array;
177+
return $this->makePrettyObjectArray(MessageEntity::class, 'caption_entities');
188178
}
189179

190180
/**

src/Entities/Poll.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,10 @@ protected function subEntities()
3939
* This method overrides the default getOptions method
4040
* and returns a nice array of PollOption objects.
4141
*
42-
* @return null|PollOption[]
42+
* @return PollOption[]
4343
*/
4444
public function getOptions()
4545
{
46-
$pretty_array = $this->makePrettyObjectArray(PollOption::class, 'options');
47-
48-
return empty($pretty_array) ? null : $pretty_array;
46+
return $this->makePrettyObjectArray(PollOption::class, 'options');
4947
}
5048
}

src/Entities/StickerSet.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,10 @@ class StickerSet extends Entity
2727
* This method overrides the default getStickers method
2828
* and returns a nice array of Sticker objects.
2929
*
30-
* @return null|Sticker[]
30+
* @return Sticker[]
3131
*/
3232
public function getStickers()
3333
{
34-
$pretty_array = $this->makePrettyObjectArray(Sticker::class, 'stickers');
35-
36-
return empty($pretty_array) ? null : $pretty_array;
34+
return $this->makePrettyObjectArray(Sticker::class, 'stickers');
3735
}
3836
}

src/Entities/TelegramPassport/EncryptedPassportElement.php

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,11 @@ protected function subEntities()
5050
* This method overrides the default getFiles method
5151
* and returns a nice array of PassportFile objects.
5252
*
53-
* @return null|PassportFile[]
53+
* @return PassportFile[]
5454
*/
5555
public function getFiles()
5656
{
57-
$pretty_array = $this->makePrettyObjectArray(PassportFile::class, 'files');
58-
59-
return empty($pretty_array) ? null : $pretty_array;
57+
return $this->makePrettyObjectArray(PassportFile::class, 'files');
6058
}
6159

6260
/**
@@ -65,12 +63,10 @@ public function getFiles()
6563
* This method overrides the default getTranslation method
6664
* and returns a nice array of PassportFile objects.
6765
*
68-
* @return null|PassportFile[]
66+
* @return PassportFile[]
6967
*/
7068
public function getTranslation()
7169
{
72-
$pretty_array = $this->makePrettyObjectArray(PassportFile::class, 'translation');
73-
74-
return empty($pretty_array) ? null : $pretty_array;
70+
return $this->makePrettyObjectArray(PassportFile::class, 'translation');
7571
}
7672
}

src/Entities/TelegramPassport/PassportData.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,10 @@ protected function subEntities()
4040
* This method overrides the default getData method
4141
* and returns a nice array of EncryptedPassportElement objects.
4242
*
43-
* @return null|EncryptedPassportElement[]
43+
* @return EncryptedPassportElement[]
4444
*/
4545
public function getData()
4646
{
47-
$pretty_array = $this->makePrettyObjectArray(EncryptedPassportElement::class, 'data');
48-
49-
return empty($pretty_array) ? null : $pretty_array;
47+
return $this->makePrettyObjectArray(EncryptedPassportElement::class, 'data');
5048
}
5149
}

0 commit comments

Comments
 (0)