1 | <?php |
||
18 | abstract class TelegramMethods implements TelegramMethodDefinitions |
||
19 | { |
||
20 | /** |
||
21 | * @var TelegramResponse |
||
22 | */ |
||
23 | protected $response; |
||
24 | |||
25 | /** |
||
26 | * Most of the methods will return a Message object on success, so set that as the default. |
||
27 | * |
||
28 | * This function may however be overwritten if the method uses another object, there are many examples of this, so |
||
29 | * just check out the rest of the code. A good place to start is GetUserProfilePhotos or LeaveChat |
||
30 | * |
||
31 | * @see \unreal4u\TelegramAPI\Telegram\Methods\GetUserProfilePhotos |
||
32 | * @see \unreal4u\TelegramAPI\Telegram\Methods\LeaveChat |
||
33 | * |
||
34 | * @param TelegramResponse $data |
||
35 | * @param LoggerInterface $logger |
||
36 | * |
||
37 | * @return TelegramTypes |
||
38 | */ |
||
39 | 9 | public static function bindToObject(TelegramResponse $data, LoggerInterface $logger): TelegramTypes |
|
43 | |||
44 | /** |
||
45 | * Before making the actual request this method will be called |
||
46 | * |
||
47 | * It must be used to json_encode stuff, or do other changes in the internal class representation _before_ sending |
||
48 | * it to the Telegram servers |
||
49 | * |
||
50 | * @return TelegramMethods |
||
51 | */ |
||
52 | 31 | public function performSpecialConditions(): TelegramMethods |
|
60 | |||
61 | /** |
||
62 | * Exports the class to an array in order to send it to the Telegram servers without extra fields that we don't need |
||
63 | * |
||
64 | * @return array |
||
65 | * @throws MissingMandatoryField |
||
66 | */ |
||
67 | 37 | final public function export(): array |
|
68 | { |
||
69 | 37 | $finalArray = []; |
|
70 | 37 | $mandatoryFields = $this->getMandatoryFields(); |
|
71 | |||
72 | 37 | $cleanObject = new $this(); |
|
73 | 37 | foreach ($cleanObject as $fieldId => $value) { |
|
74 | 37 | if ($this->$fieldId === $cleanObject->$fieldId) { |
|
75 | 37 | if (in_array($fieldId, $mandatoryFields, true)) { |
|
76 | 6 | throw new MissingMandatoryField(sprintf( |
|
77 | 37 | 'The field "%s" is mandatory and empty, please correct', |
|
78 | $fieldId |
||
79 | )); |
||
80 | } |
||
81 | } else { |
||
82 | 33 | $finalArray[$fieldId] = $this->$fieldId; |
|
83 | } |
||
84 | } |
||
85 | |||
86 | 31 | return $finalArray; |
|
87 | } |
||
88 | |||
89 | /** |
||
90 | * Will resolve the dependency of a mandatory inline_message_id OR a chat_id + message_id |
||
91 | * |
||
92 | * NOTE: This will use pass by reference instead of copy on write as the use-case for this functions allows this |
||
93 | * |
||
94 | * @param array $return |
||
95 | * @return array |
||
96 | */ |
||
97 | 4 | final protected function mandatoryUserOrInlineMessageId(array &$return): array |
|
111 | |||
112 | /** |
||
113 | * ReplyMarkup fields require a bit of work before sending them |
||
114 | * |
||
115 | * This happens because reply markup are a type thus they don't have an export mechanism to do the job |
||
116 | * |
||
117 | * @param TelegramTypes $replyMarkup |
||
118 | * @return TelegramTypes |
||
119 | */ |
||
120 | 1 | final private function formatReplyMarkup(TelegramTypes $replyMarkup): TelegramTypes |
|
121 | { |
||
122 | 1 | if ($replyMarkup instanceof Markup) { |
|
123 | $replyMarkup->inline_keyboard = $this->getArrayFromKeyboard($replyMarkup->inline_keyboard); |
||
124 | } elseif ($replyMarkup instanceof ReplyKeyboardMarkup) { |
||
125 | 1 | $replyMarkup->keyboard = $this->getArrayFromKeyboard($replyMarkup->keyboard); |
|
126 | } |
||
127 | |||
128 | 1 | return $replyMarkup; |
|
129 | } |
||
130 | |||
131 | 1 | final private function getArrayFromKeyboard(array $keyboardArray): array |
|
153 | |||
154 | /** |
||
155 | * Does the definitive export of those fields in a reply markup item that are filled in |
||
156 | * |
||
157 | * @param TelegramTypes $markupItem |
||
158 | * @return array |
||
159 | */ |
||
160 | final private function exportReplyMarkupItem(TelegramTypes $markupItem): array |
||
172 | } |
||
173 |
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: