Message::detectType()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 30

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 30
ccs 0
cts 26
cp 0
rs 9.44
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
1
<?php
2
3
namespace Telegram\Bot\Objects;
4
5
/**
6
 * Class Message.
7
 *
8
 *
9
 * @method int              getMessageId()              Unique message identifier.
10
 * @method User             getFrom()                   (Optional). Sender, can be empty for messages sent to channels.
11
 * @method int              getDate()                   Date the message was sent in Unix time.
12
 * @method Chat             getChat()                   Conversation the message belongs to.
13
 * @method User             getForwardFrom()            (Optional). For forwarded messages, sender of the original message.
14
 * @method int              getForwardDate()            (Optional). For forwarded messages, date the original message was sent in Unix time.
15
 * @method Message          getReplyToMessage()         (Optional). For replies, the original message. Note that the Message object in this field will not contain further reply_to_message fields even if it itself is a reply.
16
 * @method int              getEditDate()               (Optional). Date the message was last edited in Unix time.
17
 * @method MessageEntity[]  getEntities()               (Optional). For text messages, special entities like usernames, URLs, bot commands, etc. that appear in the text.
18
 * @method Audio            getAudio()                  (Optional). Message is an audio file, information about the file.
19
 * @method Document         getDocument()               (Optional). Message is a general file, information about the file.
20
 * @method PhotoSize[]      getPhoto()                  (Optional). Message is a photo, available sizes of the photo.
21
 * @method Sticker          getSticker()                (Optional). Message is a sticker, information about the sticker.
22
 * @method Video            getVideo()                  (Optional). Message is a video, information about the video.
23
 * @method Voice            getVoice()                  (Optional). Message is a voice message, information about the file.
24
 * @method Contact          getContact()                (Optional). Message is a shared contact, information about the contact.
25
 * @method Location         getLocation()               (Optional). Message is a shared location, information about the location.
26
 * @method Venue            getVenue()                  (Optional). Message is a venue, information about the venue.
27
 * @method User             getNewChatMember()          (Optional). A new member was added to the group, information about them (this member may be the bot itself).
28
 * @method User             getLeftChatMember()         (Optional). A member was removed from the group, information about them (this member may be the bot itself).
29
 * @method string           getNewChatTitle()           (Optional). A chat title was changed to this value.
30
 * @method PhotoSize[]      getNewChatPhoto()           (Optional). A chat photo was change to this value.
31
 * @method bool             getDeleteChatPhoto()        (Optional). Service message: the chat photo was deleted.
32
 * @method bool             getGroupChatCreated()       (Optional). Service message: the group has been created.
33
 * @method bool             getSupergroupChatCreated()  (Optional). Service message: the super group has been created.
34
 * @method bool             getChannelChatCreated()     (Optional). Service message: the channel has been created.
35
 * @method int              getMigrateToChatId()        (Optional). The group has been migrated to a supergroup with the specified identifier, not exceeding 1e13 by absolute value.
36
 * @method int              getMigrateFromChatId()      (Optional). The supergroup has been migrated from a group with the specified identifier, not exceeding 1e13 by absolute value.
37
 * @method Message          getPinnedMessage()          (Optional). Specified message was pinned. Note that the Message object in this field will not contain further reply_to_message fields even if it is itself a reply.
38
 */
39
class Message extends BaseObject
40
{
41
    /**
42
     * {@inheritdoc}
43
     */
44 28
    public function relations()
45
    {
46
        return [
47 28
            'from'             => User::class,
48 28
            'chat'             => Chat::class,
49 28
            'forward_from'     => User::class,
50 28
            'forward_from_chat'=> User::class,
51 28
            'reply_to_message' => self::class,
52 28
            'entities'         => MessageEntity::class,
53 28
            'audio'            => Audio::class,
54 28
            'document'         => Document::class,
55 28
            'photo'            => PhotoSize::class,
56 28
            'sticker'          => Sticker::class,
57 28
            'video'            => Video::class,
58 28
            'voice'            => Voice::class,
59 28
            'contact'          => Contact::class,
60 28
            'location'         => Location::class,
61 28
            'venue'            => Venue::class,
62 28
            'new_chat_member'  => User::class,
63 28
            'left_chat_member' => User::class,
64 28
            'new_chat_photo'   => PhotoSize::class,
65 28
            'pinned_message'   => Message::class,
66 28
        ];
67
    }
68
69
    /**
70
     * (Optional). For text messages, the actual UTF-8 text of the message.
71
     *
72
     * @return string
73
     */
74 8
    public function getText()
75
    {
76 8
        return $this->get('text');
77
    }
78
79
    /**
80
     * (Optional). Caption for the document, photo or video contact.
81
     *
82
     * @return string
83
     */
84
    public function getCaption()
85
    {
86
        return $this->get('caption');
87
    }
88
    
89
    /**
90
     * Determine if the message is of given type
91
     *
92
     * @param string         $type
93
     *
94
     * @return bool
95
     */
96
    public function isType($type)
97
    {
98
        if ($this->has(strtolower($type))) {
99
            return true;
100
        }
101
102
        return $this->detectType() === $type;
103
    }
104
    
105
    
106
    /**
107
     * Detect type based on properties.
108
     *
109
     * @return string|null
110
     */
111
    public function detectType()
112
    {
113
        $types = [
114
            'text',
115
            'audio',
116
            'document',
117
            'photo',
118
            'sticker',
119
            'video',
120
            'voice',
121
            'contact',
122
            'location',
123
            'venue',
124
            'new_chat_member',
125
            'left_chat_member',
126
            'new_chat_title',
127
            'new_chat_photo',
128
            'delete_chat_photo',
129
            'group_chat_created',
130
            'supergroup_chat_created',
131
            'channel_chat_created',
132
            'migrate_to_chat_id',
133
            'migrate_from_chat_id',
134
            'pinned_message',
135
        ];
136
137
        return $this->keys()
138
            ->intersect($types)
139
            ->pop();
140
    }
141
}
142