Completed
Pull Request — develop (#291)
by Armando
15:42
created

Chat::isSuperGroup()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
ccs 0
cts 0
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php
2
/**
3
 * This file is part of the TelegramBot package.
4
 *
5
 * (c) Avtandil Kikabidze aka LONGMAN <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Longman\TelegramBot\Entities;
12
13
/**
14
 * Class Chat
15
 *
16
 * @link https://core.telegram.org/bots/api#chat
17
 *
18
 * @property int    $id            Unique identifier for this chat. This number may be greater than 32 bits and some programming languages may have difficulty/silent defects in interpreting it. But it smaller than 52 bits, so a signed 64 bit integer or double-precision float type are safe for storing this identifier.
19
 * @property string $type          Type of chat, can be either "private", "group", "supergroup" or "channel"
20
 * @property string $title         Optional. Title, for channels and group chats
21
 * @property string $username      Optional. Username, for private chats, supergroups and channels if available
22
 * @property string $first_name    Optional. First name of the other party in a private chat
23
 * @property string $last_name     Optional. Last name of the other party in a private chat
24
 * @method   int    getId()        Unique identifier for this chat. This number may be greater than 32 bits and some programming languages may have difficulty/silent defects in interpreting it. But it smaller than 52 bits, so a signed 64 bit integer or double-precision float type are safe for storing this identifier.
25
 * @method   string getType()      Type of chat, can be either "private ", "group", "supergroup" or "channel"
26
 * @method   string getTitle()     Optional. Title, for channels and group chats
27
 * @method   string getUsername()  Optional. Username, for private chats, supergroups and channels if available
28
 * @method   string getFirstName() Optional. First name of the other party in a private chat
29
 * @method   string getLastName()  Optional. Last name of the other party in a private chat
30
 */
31
class Chat extends Entity
32
{
33
    public function __construct($data)
34
    {
35
        parent::__construct($data);
36
37
        if (!$this->getType()) {
38
            if ($this->getId() > 0) {
39
                $this->type = 'private';
40
            } elseif ($this->getId() < 0) {
41
                $this->type = 'group';
42
            }
43
        }
44
    }
45
46
    /**
47
     * Try mention
48
     *
49
     * @return string|null
50
     */
51
    public function tryMention()
52 19
    {
53
        if ($this->isPrivateChat()) {
54 19
            if ($this->username === null) {
55 19
                if ($this->last_name !== null) {
56
                    return $this->first_name . ' ' . $this->last_name;
57
                }
58
59 19
                return $this->first_name;
60 15
            }
61
62 10
            return '@' . $this->username;
63 9
        }
64 1
65 1
        return $this->getTitle();
66
    }
67
68
    /**
69
     * Check if this is a group chat
70
     *
71 19
     * @return bool
72 19
     */
73 19
    public function isGroupChat()
74 19
    {
75 19
        return $this->type === 'group' || $this->id < 0;
76
    }
77
78
    /**
79
     * Check if this is a private chat
80
     *
81
     * @return bool
82
     */
83
    public function isPrivateChat()
84
    {
85
        return $this->type === 'private';
86
    }
87
88
    /**
89
     * Check if this is a super group
90
     *
91
     * @return bool
92
     */
93
    public function isSuperGroup()
94
    {
95
        return $this->type === 'supergroup';
96
    }
97
98
    /**
99
     * Check if this is a channel
100
     *
101
     * @return bool
102
     */
103
    public function isChannel()
104
    {
105
        return $this->type === 'channel';
106
    }
107
}
108