|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
|
4
|
|
|
|
|
5
|
|
|
namespace unreal4u\TelegramAPI\Abstracts; |
|
6
|
|
|
|
|
7
|
|
|
use Psr\Log\LoggerInterface; |
|
8
|
|
|
use unreal4u\TelegramAPI\Exceptions\MissingMandatoryField; |
|
9
|
|
|
use unreal4u\TelegramAPI\Interfaces\TelegramMethodDefinitions; |
|
10
|
|
|
use unreal4u\TelegramAPI\InternalFunctionality\TelegramRawData; |
|
11
|
|
|
use unreal4u\TelegramAPI\Telegram\Types\Message; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Contains methods that all Telegram methods should implement |
|
15
|
|
|
*/ |
|
16
|
|
|
abstract class TelegramMethods implements TelegramMethodDefinitions |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* Most of the methods will return a Message object on success, so set that as the default. |
|
20
|
|
|
* |
|
21
|
|
|
* This function may however be overwritten if the method uses another object, there are many examples of this, so |
|
22
|
|
|
* just check out the rest of the code. A good place to start is GetUserProfilePhotos or LeaveChat |
|
23
|
|
|
* |
|
24
|
|
|
* @see unreal4u\TelegramAPI\Telegram\Methods\GetUserProfilePhotos |
|
25
|
|
|
* @see unreal4u\TelegramAPI\Telegram\Methods\LeaveChat |
|
26
|
|
|
* |
|
27
|
|
|
* @param TelegramRawData $data |
|
28
|
|
|
* @param LoggerInterface $logger |
|
29
|
|
|
* |
|
30
|
|
|
* @return TelegramTypes |
|
31
|
|
|
*/ |
|
32
|
8 |
|
public static function bindToObject(TelegramRawData $data, LoggerInterface $logger): TelegramTypes |
|
33
|
|
|
{ |
|
34
|
8 |
|
return new Message($data->getResult(), $logger); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Before making the actual request this method will be called |
|
39
|
|
|
* |
|
40
|
|
|
* It must be used to json_encode stuff, or do other changes in the internal class representation _before_ sending |
|
41
|
|
|
* it to the Telegram servers |
|
42
|
|
|
* |
|
43
|
|
|
* @return TelegramMethods |
|
44
|
|
|
*/ |
|
45
|
27 |
|
public function performSpecialConditions(): TelegramMethods |
|
46
|
|
|
{ |
|
47
|
27 |
|
if (!empty($this->reply_markup)) { |
|
48
|
1 |
|
$this->reply_markup = json_encode($this->reply_markup); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
27 |
|
return $this; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Exports the class to an array in order to send it to the Telegram servers without extra fields that we don't need |
|
56
|
|
|
* |
|
57
|
|
|
* @return array |
|
58
|
|
|
*/ |
|
59
|
33 |
|
final public function export(): array |
|
60
|
|
|
{ |
|
61
|
33 |
|
$finalArray = []; |
|
62
|
33 |
|
$mandatoryFields = $this->getMandatoryFields(); |
|
63
|
|
|
|
|
64
|
33 |
|
$cleanObject = new $this(); |
|
65
|
33 |
|
foreach ($cleanObject as $fieldId => $value) { |
|
66
|
29 |
|
if ($this->$fieldId === $cleanObject->$fieldId) { |
|
67
|
27 |
|
if (in_array($fieldId, $mandatoryFields)) { |
|
68
|
5 |
|
throw new MissingMandatoryField(sprintf( |
|
69
|
27 |
|
'The field "%s" is mandatory and empty, please correct', |
|
70
|
|
|
$fieldId |
|
71
|
|
|
)); |
|
72
|
|
|
} |
|
73
|
|
|
} else { |
|
74
|
26 |
|
$finalArray[$fieldId] = $this->$fieldId; |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
28 |
|
return $finalArray; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Will resolve the dependency of a mandatory inline_message_id OR a chat_id + message_id |
|
83
|
|
|
* |
|
84
|
|
|
* NOTE: This will use pass by reference instead of copy on write as the use-case for this functions allows this |
|
85
|
|
|
* |
|
86
|
|
|
* @param array $return |
|
87
|
|
|
* @return array |
|
88
|
|
|
*/ |
|
89
|
4 |
|
final protected function mandatoryUserOrInlineMessageId(array &$return): array |
|
90
|
|
|
{ |
|
91
|
4 |
|
if (empty($this->chat_id) && empty($this->message_id)) { |
|
|
|
|
|
|
92
|
3 |
|
$return[] = 'inline_message_id'; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
// On the other hand, chat_id and message_id are mandatory if inline_message_id is not filled in |
|
96
|
4 |
|
if (empty($this->inline_message_id)) { |
|
|
|
|
|
|
97
|
3 |
|
$return[] = 'chat_id'; |
|
98
|
3 |
|
$return[] = 'message_id'; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
4 |
|
return $return; |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: