@@ -20,6 +20,8 @@ class Update
2020{
2121 private int $ method ;
2222 private ?int $ lastUpdateID ;
23+ private object $ data ;
24+ private array $ customs = [ ];
2325 public bool $ ok ;
2426 public object |array $ result ;
2527
@@ -31,23 +33,17 @@ class Update
3133 *
3234 * @param object|null $data The data object containing Telegram updates.
3335 * @param int $updatesMethod The updates method to use.
34- * @param bool $enableDefaultUpdates Whether the default updates should be enabled or not.
3536 */
36- public function __construct (?object $ data , int $ updatesMethod, bool $ enableDefaultUpdates = false )
37+ public function __construct (?object $ data , int $ updatesMethod )
3738 {
3839 self ::validateUpdateMethod ($ updatesMethod );
3940 $ this ->method = $ updatesMethod ;
4041
41- if ( $ data !== null ) {
42- $ this ->lastUpdateID = isset ($ data ->result [0 ]) ? $ data ->result [array_key_last ($ data ->result )]->update_id ?? null : null ;
42+ $ this -> data = $ data ;
43+ $ this ->lastUpdateID = isset ($ data ->result [0 ]) ? $ data ->result [array_key_last ($ data ->result )]->update_id ?? null : null ;
4344
44- if ($ enableDefaultUpdates ) {
45- $ data = $ this ->getDefaultUpdates ($ data );
46- }
47-
48- foreach ($ data as $ key => $ value ) {
49- $ this ->$ key = $ value ;
50- }
45+ foreach ($ data as $ key => $ value ) {
46+ $ this ->$ key = $ value ;
5147 }
5248 }
5349
@@ -65,39 +61,6 @@ public static function validateUpdateMethod(int $method): void
6561 }
6662 }
6763
68- private function getDefaultUpdates (object $ data ): object
69- {
70- foreach ($ data ->result ?? [$ data ] as &$ upd ) {
71- $ upd ->msg = $ upd ->message ??
72- $ upd ->edited_message ??
73- $ upd ->channel_post ??
74- $ upd ->edited_channel_post ??
75- $ upd ->callback_query ->message ??
76- null ;
77-
78- $ upd ->user = $ upd ->callback_query ->from ??
79- $ upd ->msg ->from ??
80- $ upd ->msg ->sender_chat ??
81- $ upd ->inline_query ->from ??
82- $ upd ->chosen_inline_result ->from ??
83- $ upd ->callback_query ->from ??
84- $ upd ->shipping_query ->from ??
85- $ upd ->poll_answer ->user ??
86- $ upd ->chat_member ->from ??
87- $ upd ->chat_join_request ->from ??
88- null ;
89-
90- $ upd ->chat = $ upd ->msg ->chat ??
91- $ upd ->callback_query ->message ->chat ??
92- $ upd ->my_chat_member ->chat ??
93- $ upd ->chat_member ->chat ??
94- $ upd ->chat_join_request ->chat ??
95- null ;
96- }
97-
98- return $ data ;
99- }
100-
10164 /**
10265 * Checks if the update is from Telegram.
10366 *
@@ -131,4 +94,65 @@ public function getLastUpdateId(): ?int
13194 {
13295 return $ this ->lastUpdateID ;
13396 }
97+
98+ /**
99+ * Get the message from the update.
100+ *
101+ * @return object|null The message object if present; otherwise, null.
102+ */
103+ public function getMessage (): ?object
104+ {
105+ if (!isset ($ customs ['message ' ])) {
106+ $ this ->customs ['message ' ] = $ this ->data ->message ??
107+ $ this ->data ->edited_message ??
108+ $ this ->data ->channel_post ??
109+ $ this ->data ->edited_channel_post ??
110+ $ this ->data ->callback_query ->message ??
111+ null ;
112+ }
113+
114+ return $ this ->customs ['message ' ];
115+ }
116+
117+ /**
118+ * Get the chat from the update.
119+ *
120+ * @return object|null The chat object if present; otherwise, null.
121+ */
122+ public function getChat (): ?object {
123+ if (!isset ($ customs ['chat ' ])) {
124+ $ this ->customs ['message ' ] = $ this ->customs ['message ' ]->chat ??
125+ $ this ->data ->callback_query ->message ->chat ??
126+ $ this ->data ->my_chat_member ->chat ??
127+ $ this ->data ->chat_member ->chat ??
128+ $ this ->data ->chat_join_request ->chat ??
129+ null ;
130+ }
131+
132+ return $ this ->customs ['message ' ];
133+ }
134+
135+ /**
136+ * Get the user from the update.
137+ *
138+ * @return object|null The user object if present; otherwise, null.
139+ */
140+ public function getUser (): ?object
141+ {
142+ if (!isset ($ customs ['user ' ])) {
143+ $ this ->customs ['message ' ] = $ this ->data ->callback_query ->from ??
144+ $ this ->customs ['message ' ]->from ??
145+ $ this ->customs ['message ' ]->sender_chat ??
146+ $ this ->data ->inline_query ->from ??
147+ $ this ->data ->chosen_inline_result ->from ??
148+ $ this ->data ->callback_query ->from ??
149+ $ this ->data ->shipping_query ->from ??
150+ $ this ->data ->poll_answer ->user ??
151+ $ this ->data ->chat_member ->from ??
152+ $ this ->data ->chat_join_request ->from ??
153+ null ;
154+ }
155+
156+ return $ this ->customs ['user ' ];
157+ }
134158}
0 commit comments