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\InternalFunctionality\TelegramRawData; |
11
|
|
|
use unreal4u\TelegramAPI\Telegram\Types\Custom\GameHighScoreArray; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Use this method to get data for high score tables. Will return the score of the specified user and several of his |
15
|
|
|
* neighbors in a game. On success, returns an Array of GameHighScore objects. |
16
|
|
|
* |
17
|
|
|
* This method will currently return scores for the target user, plus two of his closest neighbors on each side. Will |
18
|
|
|
* also return the top three users if the user and his neighbors are not among them. Please note that this behavior is |
19
|
|
|
* subject to change. |
20
|
|
|
* |
21
|
|
|
* Objects defined as-is January 2017 |
22
|
|
|
* |
23
|
|
|
* @see https://core.telegram.org/bots/api#getgamescore |
24
|
|
|
*/ |
25
|
|
|
class GetGameScore extends TelegramMethods |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* Target user id |
29
|
|
|
* @var int |
30
|
|
|
*/ |
31
|
|
|
public $user_id = 0; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Required if inline_message_id is not specified. Unique identifier for the target chat |
35
|
|
|
* @var int |
36
|
|
|
*/ |
37
|
|
|
public $chat_id; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Required if inline_message_id is not specified. Identifier of the sent message |
41
|
|
|
* @var int |
42
|
|
|
*/ |
43
|
|
|
public $message_id; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Required if chat_id and message_id are not specified. Identifier of the inline message |
47
|
|
|
* @var string |
48
|
|
|
*/ |
49
|
|
|
public $inline_message_id; |
50
|
|
|
|
51
|
|
|
public static function bindToObject(TelegramRawData $data, LoggerInterface $logger): TelegramTypes |
52
|
|
|
{ |
53
|
|
|
return new GameHighScoreArray($data->getResult(), $logger); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function getMandatoryFields(): array |
57
|
|
|
{ |
58
|
|
|
// user_id and score are always mandatory |
59
|
|
|
$returnValue[] = 'user_id'; |
|
|
|
|
60
|
|
|
return $this->mandatoryUserOrInlineMessageId($returnValue); |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
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.