|
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
|
24 |
|
public function performSpecialConditions(): TelegramMethods |
|
46
|
|
|
{ |
|
47
|
24 |
|
if (!empty($this->reply_markup)) { |
|
48
|
1 |
|
$this->reply_markup = json_encode($this->reply_markup); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
24 |
|
return $this; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
24 |
|
public function export(): array |
|
55
|
|
|
{ |
|
56
|
24 |
|
$finalArray = []; |
|
57
|
24 |
|
$objectProspect = get_object_vars($this); |
|
58
|
24 |
|
$cleanObject = new $this(); |
|
59
|
24 |
|
foreach ($objectProspect as $fieldId => $value) { |
|
60
|
|
|
// Strict comparison, type checking! |
|
61
|
20 |
|
if ($objectProspect[$fieldId] === $cleanObject->$fieldId) { |
|
62
|
18 |
|
if (in_array($fieldId, $this->getMandatoryFields())) { |
|
63
|
|
|
throw new MissingMandatoryField(sprintf( |
|
64
|
18 |
|
'The field "%s" is mandatory and empty, please correct', |
|
65
|
|
|
$fieldId |
|
66
|
|
|
)); |
|
67
|
|
|
} |
|
68
|
|
|
} else { |
|
69
|
20 |
|
$finalArray[$fieldId] = $value; |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
24 |
|
return $finalArray; |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|