1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
4
|
|
|
|
5
|
|
|
namespace unreal4u\TelegramAPI\Telegram\Methods; |
6
|
|
|
|
7
|
|
|
use Psr\Log\LoggerInterface; |
8
|
|
|
use unreal4u\TelegramAPI\Abstracts\TelegramMethods; |
9
|
|
|
use unreal4u\TelegramAPI\Abstracts\TelegramTypes; |
10
|
|
|
use unreal4u\TelegramAPI\Exceptions\InvalidResultType; |
11
|
|
|
use unreal4u\TelegramAPI\InternalFunctionality\TelegramResponse; |
12
|
|
|
use unreal4u\TelegramAPI\Telegram\Types\Custom\ResultBoolean; |
13
|
|
|
use unreal4u\TelegramAPI\Telegram\Types\Inline\Keyboard\Markup; |
14
|
|
|
use unreal4u\TelegramAPI\Telegram\Types\InputMedia; |
15
|
|
|
use unreal4u\TelegramAPI\Telegram\Types\Message; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Use this method to edit audio, document, photo, or video messages. If a message is a part of a message album, then it |
19
|
|
|
* can be edited only to a photo or a video. Otherwise, message type can be changed arbitrarily. When inline message is |
20
|
|
|
* edited, new file can't be uploaded. Use previously uploaded file via its file_id or specify a URL. On success, if the |
21
|
|
|
* edited message was sent by the bot, the edited Message is returned, otherwise True is returned. |
22
|
|
|
* |
23
|
|
|
* Objects defined as-is july 2018 |
24
|
|
|
* |
25
|
|
|
* @see https://core.telegram.org/bots/api#editmessagemedia |
26
|
|
|
*/ |
27
|
|
|
class EditMessageMedia extends TelegramMethods |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* Required if inline_message_id is not specified. Unique identifier for the target chat or username of the target |
31
|
|
|
* channel (in the format @channelusername) |
32
|
|
|
* @var string |
33
|
|
|
*/ |
34
|
|
|
public $chat_id = ''; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Required if inline_message_id is not specified. Unique identifier of the sent message |
38
|
|
|
* @var int |
39
|
|
|
*/ |
40
|
|
|
public $message_id = 0; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Required if chat_id and message_id are not specified. Identifier of the inline message |
44
|
|
|
* @var string |
45
|
|
|
*/ |
46
|
|
|
public $inline_message_id = ''; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* A JSON-serialized object for a new media content of the message |
50
|
|
|
* @var InputMedia |
51
|
|
|
*/ |
52
|
|
|
public $media; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Optional. A JSON-serialized object for an inline keyboard. |
56
|
|
|
* @var Markup |
57
|
|
|
*/ |
58
|
|
|
public $reply_markup; |
59
|
|
|
|
60
|
|
|
public function getMandatoryFields(): array |
61
|
|
|
{ |
62
|
|
|
$returnValue[] = 'media'; |
|
|
|
|
63
|
|
|
$this->mandatoryUserOrInlineMessageId($returnValue); |
64
|
|
|
return $returnValue; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param TelegramRawData $data |
69
|
|
|
* @param LoggerInterface $logger |
70
|
|
|
* @return TelegramTypes |
71
|
|
|
* @throws InvalidResultType |
72
|
|
|
*/ |
73
|
|
|
public static function bindToObject(TelegramResponse $data, LoggerInterface $logger): TelegramTypes |
74
|
|
|
{ |
75
|
|
|
$typeOfResult = $data->getTypeOfResult(); |
76
|
|
|
switch ($typeOfResult) { |
77
|
|
|
case 'array': |
78
|
|
|
return new Message($data->getResult(), $logger); |
79
|
|
|
case 'boolean': |
80
|
|
|
return new ResultBoolean($data->getResultBoolean(), $logger); |
81
|
|
|
default: |
82
|
|
|
throw new InvalidResultType(sprintf( |
83
|
|
|
'Result is of type: %s. Expecting one of array or boolean', |
84
|
|
|
$typeOfResult |
85
|
|
|
)); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArray
is initialized the first time when the foreach loop is entered. You can also see that the value of thebar
key is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.