Skip to content

Commit 9f13a6b

Browse files
committed
Added Factory class and resolve method
1 parent e71fbcd commit 9f13a6b

File tree

5 files changed

+49
-9
lines changed

5 files changed

+49
-9
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
4+
namespace Longman\TelegramBot\Entities\ChatMember;
5+
6+
7+
use Longman\TelegramBot\Entities\Entity;
8+
9+
class Factory extends \Longman\TelegramBot\Entities\Factory
10+
{
11+
public function make(array $data, string $bot_username): Entity
12+
{
13+
var_dump($data);
14+
}
15+
}

src/Entities/ChatMemberUpdated.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111

1212
namespace Longman\TelegramBot\Entities;
1313

14+
use Longman\TelegramBot\Entities\ChatMember\ChatMember;
15+
use Longman\TelegramBot\Entities\ChatMember\Factory as ChatMemberFactory;
16+
1417
/**
1518
* Class ChatMemberUpdated
1619
*
@@ -35,8 +38,8 @@ protected function subEntities(): array
3538
return [
3639
'chat' => Chat::class,
3740
'from' => User::class,
38-
'old_chat_member' => ChatMember::class,
39-
'new_chat_member' => ChatMember::class,
41+
'old_chat_member' => ChatMemberFactory::class,
42+
'new_chat_member' => ChatMemberFactory::class,
4043
'invite_link' => ChatInviteLink::class,
4144
];
4245
}

src/Entities/Entity.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ public function __call($method, $args)
141141
return $this->makePrettyObjectArray(reset($class), $property_name);
142142
}
143143

144-
return new $class($property, $this->getProperty('bot_username'));
144+
return Factory::resolveEntityClass($class, $property, $this->getProperty('bot_username'));
145145
}
146146

147147
return $property;
@@ -173,11 +173,12 @@ protected function makePrettyObjectArray(string $class, string $property): array
173173
{
174174
$new_objects = [];
175175

176+
$bot_username = $this->getProperty('bot_username');
176177
try {
177178
if ($objects = $this->getProperty($property)) {
178179
foreach ($objects as $object) {
179180
if (!empty($object)) {
180-
$new_objects[] = new $class($object);
181+
$new_objects[] = Factory::resolveEntityClass($class, $object, $bot_username);
181182
}
182183
}
183184
}

src/Entities/Factory.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
4+
namespace Longman\TelegramBot\Entities;
5+
6+
7+
abstract class Factory
8+
{
9+
abstract public function make(array $data, string $bot_username): Entity;
10+
11+
public static function resolveEntityClass(string $class, array $property, string $bot_username = ''): Entity
12+
{
13+
if (is_subclass_of($class, Factory::class)) {
14+
return $class->make($property, $bot_username);
15+
}
16+
17+
return new $class($property, $bot_username);
18+
}
19+
}

src/Entities/ServerResponse.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99

1010
namespace Longman\TelegramBot\Entities;
1111

12+
use Longman\TelegramBot\Entities\ChatMember\ChatMember;
13+
use Longman\TelegramBot\Entities\ChatMember\Factory as ChatMemberFactory;
1214
use Longman\TelegramBot\Entities\Games\GameHighScore;
1315
use Longman\TelegramBot\Request;
1416

@@ -104,7 +106,7 @@ public function printError($return = false)
104106
* @param array $result
105107
* @param string $bot_username
106108
*
107-
* @return Chat|ChatMember|File|Message|User|UserProfilePhotos|WebhookInfo
109+
* @return Chat|ChatMember|File|Message|User|UserProfilePhotos|WebhookInfo|Entity
108110
*/
109111
private function createResultObject(array $result, string $bot_username)
110112
{
@@ -115,7 +117,7 @@ private function createResultObject(array $result, string $bot_username)
115117

116118
$result_object_types = [
117119
'getChat' => Chat::class,
118-
'getChatMember' => ChatMember::class,
120+
'getChatMember' => ChatMemberFactory::class,
119121
'getFile' => File::class,
120122
'getMe' => User::class,
121123
'getStickerSet' => StickerSet::class,
@@ -125,7 +127,7 @@ private function createResultObject(array $result, string $bot_username)
125127

126128
$object_class = array_key_exists($action, $result_object_types) ? $result_object_types[$action] : Message::class;
127129

128-
return new $object_class($result, $bot_username);
130+
return Factory::resolveEntityClass($object_class, $result, $bot_username);
129131
}
130132

131133
/**
@@ -143,7 +145,7 @@ private function createResultObjects(array $result, string $bot_username): array
143145

144146
$result_object_types = [
145147
'getMyCommands' => BotCommand::class,
146-
'getChatAdministrators' => ChatMember::class,
148+
'getChatAdministrators' => ChatMemberFactory::class,
147149
'getGameHighScores' => GameHighScore::class,
148150
'sendMediaGroup' => Message::class,
149151
];
@@ -154,7 +156,7 @@ private function createResultObjects(array $result, string $bot_username): array
154156
// We don't need to save the raw_data of the response object!
155157
$data['raw_data'] = null;
156158

157-
$results[] = new $object_class($data, $bot_username);
159+
$results[] = Factory::resolveEntityClass($object_class, $data, $bot_username);
158160
}
159161

160162
return $results;

0 commit comments

Comments
 (0)