1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Base abstract class for supported by Telegram Method classes. |
5
|
|
|
* |
6
|
|
|
* @package Teebot (Telegram bot framework) |
7
|
|
|
* |
8
|
|
|
* @author Stanislav Drozdov <[email protected]> |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Teebot\Api\Method; |
12
|
|
|
|
13
|
|
|
use Teebot\Api\Traits\Property; |
14
|
|
|
use Teebot\Api\Entity\{ |
15
|
|
|
EntityInterface, Inline\InlineKeyboardMarkup, ReplyKeyboardMarkup, ReplyKeyboardHide, ForceReply |
16
|
|
|
}; |
17
|
|
|
|
18
|
|
|
abstract class AbstractMethod implements MethodInterface { |
19
|
|
|
|
20
|
|
|
use Property; |
21
|
|
|
|
22
|
|
|
protected const NAME = null; |
23
|
|
|
|
24
|
|
|
protected const RETURN_ENTITY = null; |
25
|
|
|
|
26
|
|
|
protected $parent = null; |
27
|
|
|
|
28
|
|
|
protected $hasAttachedData = false; |
29
|
|
|
|
30
|
|
|
protected $reply_markup; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var array |
34
|
|
|
*/ |
35
|
|
|
protected $supportedMarkups = [ |
36
|
|
|
InlineKeyboardMarkup::class, |
37
|
|
|
ReplyKeyboardMarkup::class, |
38
|
|
|
ReplyKeyboardHide::class, |
39
|
|
|
ForceReply::class |
40
|
|
|
]; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Constructs extended method's class and sets properties from array if passed. |
44
|
|
|
* |
45
|
|
|
* @param array $args |
46
|
|
|
*/ |
47
|
|
|
public function __construct(array $args = []) |
48
|
|
|
{ |
49
|
|
|
if (empty($args)) { |
50
|
|
|
return; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
$this->setProperties($args); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Returns method's name. |
58
|
|
|
* |
59
|
|
|
* @return string |
60
|
|
|
*/ |
61
|
|
|
public function getName(): string |
62
|
|
|
{ |
63
|
|
|
return static::NAME; |
|
|
|
|
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Returns entity's name that should be return in method execution result. |
68
|
|
|
* |
69
|
|
|
* @return null|string |
70
|
|
|
*/ |
71
|
|
|
public function getReturnEntity(): ?string |
72
|
|
|
{ |
73
|
|
|
return static::RETURN_ENTITY; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Returns flag which indicates that method has attached data (audio, voice, video, photo etc.) |
78
|
|
|
* |
79
|
|
|
* @return bool |
80
|
|
|
*/ |
81
|
|
|
public function hasAttachedData(): bool |
82
|
|
|
{ |
83
|
|
|
return $this->hasAttachedData; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Checks that passed markup is currently supported |
88
|
|
|
* |
89
|
|
|
* @param EntityInterface $markup Markup class instance |
90
|
|
|
* |
91
|
|
|
* @return bool |
92
|
|
|
*/ |
93
|
|
|
protected function isValidMarkup(EntityInterface $markup): bool |
94
|
|
|
{ |
95
|
|
|
foreach ($this->supportedMarkups as $className) { |
96
|
|
|
if ($markup instanceof $className) { |
97
|
|
|
return true; |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
return false; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Sets reply markup class |
106
|
|
|
* |
107
|
|
|
* @param EntityInterface $markup Markup class instance |
108
|
|
|
* |
109
|
|
|
* @return MethodInterface |
110
|
|
|
*/ |
111
|
|
|
public function setReplyMarkup(EntityInterface $markup): MethodInterface |
112
|
|
|
{ |
113
|
|
|
$this->reply_markup = !$this->isValidMarkup($markup) ? null : $markup; |
114
|
|
|
|
115
|
|
|
return $this; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Returns reply markup as JSON encoded string if reply_markup is an instance of EntityInterface |
120
|
|
|
* |
121
|
|
|
* @return string|mixed |
122
|
|
|
*/ |
123
|
|
|
public function getReplyMarkup() |
124
|
|
|
{ |
125
|
|
|
if ($this->reply_markup instanceof EntityInterface) { |
126
|
|
|
|
127
|
|
|
return str_replace('\\\\', '\\', $this->reply_markup->asJson()); |
|
|
|
|
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
return $this->reply_markup; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Returns parent entity which triggered method's execution. |
135
|
|
|
* |
136
|
|
|
* @return MethodInterface |
137
|
|
|
*/ |
138
|
|
|
public function getParent(): MethodInterface |
139
|
|
|
{ |
140
|
|
|
return $this->parent; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Sets parent entity which triggered method's execution. |
145
|
|
|
* |
146
|
|
|
* @param MethodInterface $parent |
147
|
|
|
* |
148
|
|
|
* @return MethodInterface |
149
|
|
|
*/ |
150
|
|
|
public function setParent(MethodInterface $parent): MethodInterface |
151
|
|
|
{ |
152
|
|
|
$this->parent = $parent; |
153
|
|
|
|
154
|
|
|
return $this; |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
|