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