Passed
Push — master ( f853f9...6a6913 )
by Shahrad
02:10
created

Update::isOk()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
4
namespace TelegramBot\Entities;
5
6
use TelegramBot\Entities\Payments\PreCheckoutQuery;
7
use TelegramBot\Entities\Payments\ShippingQuery;
8
use TelegramBot\Entity;
9
10
/**
11
 * Class Update
12
 *
13
 * @link https://core.telegram.org/bots/api#update
14
 *
15
 * @method int                getUpdateId()             The update's unique identifier. Update identifiers start from a certain positive number and increase sequentially. This ID becomes especially handy if you’re using Webhooks, since it allows you to ignore repeated updates or to restore the correct update sequence, should they get out of order.
16
 * @method Message            getMessage()              Optional. New incoming message of any kind — text, photo, sticker, etc.
17
 * @method Message            getEditedMessage()        Optional. New version of a message that is known to the bot and was edited
18
 * @method Message            getChannelPost()          Optional. New post in the channel, can be any kind — text, photo, sticker, etc.
19
 * @method Message            getEditedChannelPost()    Optional. New version of a post in the channel that is known to the bot and was edited
20
 * @method InlineQuery        getInlineQuery()          Optional. New incoming inline query
21
 * @method ChosenInlineResult getChosenInlineResult()   Optional. The result of an inline query that was chosen by a user and sent to their chat partner.
22
 * @method CallbackQuery      getCallbackQuery()        Optional. New incoming callback query
23
 * @method ShippingQuery      getShippingQuery()        Optional. New incoming shipping query. Only for invoices with flexible price
24
 * @method PreCheckoutQuery   getPreCheckoutQuery()     Optional. New incoming pre-checkout query. Contains full information about checkout
25
 * @method Poll               getPoll()                 Optional. New poll state. Bots receive only updates about polls, which are sent or stopped by the bot
26
 * @method PollAnswer         getPollAnswer()           Optional. A user changed their answer in a non-anonymous poll. Bots receive new votes only in polls that were sent by the bot itself.
27
 * @method ChatMemberUpdated  getMyChatMember()         Optional. The bot's chat member status was updated in a chat. For private chats, this update is received only when the bot is blocked or unblocked by the user.
28
 * @method ChatMemberUpdated  getChatMember()           Optional. A chat member's status was updated in a chat. The bot must be an administrator in the chat and must explicitly specify “chat_member” in the list of allowed_updates to receive these updates.
29
 * @method ChatJoinRequest    getChatJoinRequest()      Optional. A request to join the chat has been sent. The bot must have the can_invite_users administrator right in the chat to receive these updates.
30
 * @method WebAppData         getWebData()              Optional. The data from WebApp
31
 */
32
class Update extends Entity
33
{
34
35
    public const TYPE_MESSAGE = 'message';
36
    public const TYPE_EDITED_MESSAGE = 'edited_message';
37
    public const TYPE_CHANNEL_POST = 'channel_post';
38
    public const TYPE_EDITED_CHANNEL_POST = 'edited_channel_post';
39
    public const TYPE_INLINE_QUERY = 'inline_query';
40
    public const TYPE_CHOSEN_INLINE_RESULT = 'chosen_inline_result';
41
    public const TYPE_CALLBACK_QUERY = 'callback_query';
42
    public const TYPE_SHIPPING_QUERY = 'shipping_query';
43
    public const TYPE_PRE_CHECKOUT_QUERY = 'pre_checkout_query';
44
    public const TYPE_POLL = 'poll';
45
    public const TYPE_POLL_ANSWER = 'poll_answer';
46
    public const TYPE_MY_CHAT_MEMBER = 'my_chat_member';
47
    public const TYPE_CHAT_MEMBER = 'chat_member';
48
    public const TYPE_CHAT_JOIN_REQUEST = 'chat_join_request';
49
    public const TYPE_WEB_DATA = 'web_data';
50
51
    /**
52
     * {@inheritdoc}
53
     */
54
    protected function subEntities(): array
55
    {
56
        return [
57
            self::TYPE_MESSAGE => Message::class,
58
            self::TYPE_EDITED_MESSAGE => EditedMessage::class,
59
            self::TYPE_CHANNEL_POST => ChannelPost::class,
60
            self::TYPE_EDITED_CHANNEL_POST => EditedChannelPost::class,
61
            self::TYPE_INLINE_QUERY => InlineQuery::class,
62
            self::TYPE_CHOSEN_INLINE_RESULT => ChosenInlineResult::class,
63
            self::TYPE_CALLBACK_QUERY => CallbackQuery::class,
64
            self::TYPE_SHIPPING_QUERY => ShippingQuery::class,
65
            self::TYPE_PRE_CHECKOUT_QUERY => PreCheckoutQuery::class,
66
            self::TYPE_POLL => Poll::class,
67
            self::TYPE_POLL_ANSWER => PollAnswer::class,
68
            self::TYPE_MY_CHAT_MEMBER => ChatMemberUpdated::class,
69
            self::TYPE_CHAT_MEMBER => ChatMemberUpdated::class,
70
            self::TYPE_CHAT_JOIN_REQUEST => ChatJoinRequest::class,
71
            self::TYPE_WEB_DATA => WebAppData::class,
72
        ];
73
    }
74
75
    /**
76
     * Get the list of all available update types
77
     *
78
     * @return string[]
79
     */
80
    public static function getUpdateTypes(): array
81
    {
82
        return array_keys((new self([]))->subEntities());
83
    }
84
85
    /**
86
     * Get the update type based on the set properties
87
     *
88
     * @return string|null
89
     */
90
    public function getUpdateType(): ?string
91
    {
92
        foreach (self::getUpdateTypes() as $type) {
93
            if ($this->getProperty($type)) {
94
                return $type;
95
            }
96
        }
97
98
        return null;
99
    }
100
101
}
102