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\Method; |
12
|
|
|
|
13
|
|
|
use Teebot\Entity\AbstractEntity; |
14
|
|
|
use Teebot\Exception\Critical; |
15
|
|
|
use Teebot\Command\Executor; |
16
|
|
|
use Teebot\Exception\Output; |
17
|
|
|
use Teebot\Traits\Property; |
18
|
|
|
use Teebot\Entity\ReplyKeyboardMarkup; |
19
|
|
|
use Teebot\Entity\ReplyKeyboardHide; |
20
|
|
|
use Teebot\Entity\ForceReply; |
21
|
|
|
|
22
|
|
|
abstract class AbstractMethod { |
23
|
|
|
|
24
|
|
|
use Property; |
25
|
|
|
|
26
|
|
|
const NAME = null; |
27
|
|
|
|
28
|
|
|
const RETURN_ENTITY = null; |
29
|
|
|
|
30
|
|
|
protected $parent = null; |
31
|
|
|
|
32
|
|
|
protected $hasAttachedData = false; |
33
|
|
|
|
34
|
|
|
protected $reply_markup; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* List of properties supported by method in format: property name => required or not |
38
|
|
|
* |
39
|
|
|
* @var array |
40
|
|
|
*/ |
41
|
|
|
protected $supportedProperties = []; |
42
|
|
|
|
43
|
|
|
protected $supportedMarkups = [ |
44
|
|
|
ReplyKeyboardMarkup::class, |
45
|
|
|
ReplyKeyboardHide::class, |
46
|
|
|
ForceReply::class |
47
|
|
|
]; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Constructs extended method's class and sets properties from array if passed. |
51
|
|
|
* |
52
|
|
|
* @param array $args |
53
|
|
|
*/ |
54
|
|
|
public function __construct($args = []) |
55
|
|
|
{ |
56
|
|
|
if (empty($args)) { |
57
|
|
|
return; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
$this->setProperties($args); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Returns method's name. |
65
|
|
|
* |
66
|
|
|
* @return string |
67
|
|
|
*/ |
68
|
|
|
public function getName() |
69
|
|
|
{ |
70
|
|
|
return static::NAME; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Returns entity's name that should be return in method execution result. |
75
|
|
|
* |
76
|
|
|
* @return string |
77
|
|
|
*/ |
78
|
|
|
public function getReturnEntity() |
79
|
|
|
{ |
80
|
|
|
return static::RETURN_ENTITY; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Triggers execution of the method |
85
|
|
|
* |
86
|
|
|
* @param bool $silentMode Execute method silently without processing the result |
87
|
|
|
* |
88
|
|
|
* @return \Teebot\Response |
89
|
|
|
*/ |
90
|
|
|
public function trigger($silentMode = true) |
91
|
|
|
{ |
92
|
|
|
$executor = Executor::getInstance(); |
93
|
|
|
|
94
|
|
|
return $executor->callRemoteMethod($this, $silentMode, $this->parent); |
|
|
|
|
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Returns flag which idicates that method has attached data (audio, voice, video, photo etc.) |
99
|
|
|
* |
100
|
|
|
* @return bool |
101
|
|
|
*/ |
102
|
|
|
public function hasAttachedData() |
103
|
|
|
{ |
104
|
|
|
return $this->hasAttachedData; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Checks that passed markup is currently supported |
109
|
|
|
* |
110
|
|
|
* @param AbstractEntity $markup Markup class instance |
111
|
|
|
* |
112
|
|
|
* @return bool |
113
|
|
|
*/ |
114
|
|
|
protected function isValidMarkup(AbstractEntity $markup) |
115
|
|
|
{ |
116
|
|
|
foreach ($this->supportedMarkups as $className) { |
117
|
|
|
if ($markup instanceof $className) { |
118
|
|
|
return true; |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
return false; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Sets reply markup class |
127
|
|
|
* |
128
|
|
|
* @param AbstractEntity $markup Markup class instance |
129
|
|
|
* |
130
|
|
|
* @return $this |
131
|
|
|
*/ |
132
|
|
|
public function setReplyMarkup(AbstractEntity $markup) |
133
|
|
|
{ |
134
|
|
|
try { |
135
|
|
|
$isValidMarkup = $this->isValidMarkup($markup); |
136
|
|
|
|
137
|
|
|
if (!$isValidMarkup) { |
138
|
|
|
throw new Critical("Markup is not supported!"); |
139
|
|
|
} |
140
|
|
|
} catch (Critical $e) { |
141
|
|
|
Output::log($e); |
142
|
|
|
|
143
|
|
|
$markup = null; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
$this->reply_markup = $markup; |
147
|
|
|
|
148
|
|
|
return $this; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Returns parent entity which triggered method's execution. |
153
|
|
|
* |
154
|
|
|
* @return AbstractEntity |
155
|
|
|
*/ |
156
|
|
|
public function getParent() |
157
|
|
|
{ |
158
|
|
|
return $this->parent; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Sets parent entity which triggered method's execution. |
163
|
|
|
* |
164
|
|
|
* @param AbstractEntity $parent |
165
|
|
|
* |
166
|
|
|
* @return $this |
167
|
|
|
*/ |
168
|
|
|
public function setParent(AbstractEntity $parent) |
169
|
|
|
{ |
170
|
|
|
$this->parent = $parent; |
171
|
|
|
|
172
|
|
|
return $this; |
173
|
|
|
} |
174
|
|
|
} |
175
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: