Completed
Push — master ( 7c9f66...4f81cd )
by Irfaq
02:33
created

Message::isType()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

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