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

Update::detectType()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 13
ccs 0
cts 9
cp 0
rs 9.4285
cc 1
eloc 9
nc 1
nop 0
crap 2
1
<?php
2
3
namespace Telegram\Bot\Objects;
4
5
/**
6
 * Class Update.
7
 *
8
 *
9
 * @method int                  getUpdateId()               The update's unique identifier. Update identifiers start from a certain positive number and increase sequentially.
10
 * @method Message              getMessage()                (Optional). New incoming message of any kind - text, photo, sticker, etc.
11
 * @method InlineQuery          getInlineQuery()            (Optional). New incoming inline query.
12
 * @method ChosenInlineResult   getChosenInlineResult()     (Optional). A result of an inline query that was chosen by the user and sent to their chat partner.
13
 * @method CallbackQuery        getCallbackQuery()          (Optional). Incoming callback query.
14
 *
15
 * @link https://core.telegram.org/bots/api#update
16
 */
17
class Update extends BaseObject
18
{
19
    /**
20
     * {@inheritdoc}
21
     */
22 6
    public function relations()
23
    {
24
        return [
25 6
            'message'              => Message::class,
26 6
            'inline_query'         => InlineQuery::class,
27 6
            'chosen_inline_result' => ChosenInlineResult::class,
28 6
            'callback_query'       => CallbackQuery::class,
29 6
        ];
30
    }
31
32
    /**
33
     * Get recent message.
34
     *
35
     * @return Update
36
     */
37
    public function recentMessage()
38
    {
39
        return new static($this->last());
40
    }
41
    
42
    /**
43
     * Determine if the update is of given type
44
     *
45
     * @param string         $type
46
     *
47
     * @return bool
48
     */
49
    public function isType($type)
50
    {
51
        if ($this->has(strtolower($type))) {
52
            return true;
53
        }
54
    
55
        return $this->detectType() === $type;
56
    }
57
    
58
    /**
59
     * Detect type based on properties.
60
     *
61
     * @return string|null
62
     */
63
    public function detectType()
64
    {
65
        $types = [
66
            'message',
67
            'inline_query',
68
            'chosen_inline_result',
69
            'callback_query',
70
        ];
71
72
        return $this->keys()
73
            ->intersect($types)
74
            ->pop();
75
    }
76
}
77