1 | <?php |
||
23 | abstract class TelegramMethods implements TelegramMethodDefinitions |
||
24 | { |
||
25 | /** |
||
26 | * @var TelegramResponse |
||
27 | */ |
||
28 | protected $response; |
||
29 | |||
30 | /** |
||
31 | * Most of the methods will return a Message object on success, so set that as the default. |
||
32 | * |
||
33 | * This function may however be overwritten if the method uses another object, there are many examples of this, so |
||
34 | * just check out the rest of the code. A good place to start is GetUserProfilePhotos or LeaveChat |
||
35 | * |
||
36 | * @see \unreal4u\TelegramAPI\Telegram\Methods\GetUserProfilePhotos |
||
37 | * @see \unreal4u\TelegramAPI\Telegram\Methods\LeaveChat |
||
38 | * |
||
39 | * @param TelegramResponse $data |
||
40 | * @param LoggerInterface $logger |
||
41 | * |
||
42 | * @return TelegramTypes |
||
43 | */ |
||
44 | 9 | public static function bindToObject(TelegramResponse $data, LoggerInterface $logger): TelegramTypes |
|
48 | |||
49 | /** |
||
50 | * Before making the actual request this method will be called |
||
51 | * |
||
52 | * It must be used to json_encode stuff, or do other changes in the internal class representation _before_ sending |
||
53 | * it to the Telegram servers |
||
54 | * |
||
55 | * @return TelegramMethods |
||
56 | */ |
||
57 | 32 | public function performSpecialConditions(): TelegramMethods |
|
65 | |||
66 | /** |
||
67 | * Ensure we have a method we can always call in order to check if we have any local files |
||
68 | * |
||
69 | * @see \unreal4u\TelegramAPI\InternalFunctionality\PostOptionsConstructor::checkIsMultipart |
||
70 | * |
||
71 | * @return bool |
||
72 | */ |
||
73 | 20 | public function hasLocalFiles(): bool |
|
77 | |||
78 | /** |
||
79 | * Yields all local files (if present) |
||
80 | * |
||
81 | * @return Generator|InputFile[] |
||
82 | */ |
||
83 | public function getLocalFiles(): Generator |
||
87 | |||
88 | /** |
||
89 | * Exports the class to an array in order to send it to the Telegram servers without extra fields that we don't need |
||
90 | * |
||
91 | * @return array |
||
92 | * @throws MissingMandatoryField |
||
93 | */ |
||
94 | 38 | final public function export(): array |
|
95 | { |
||
96 | 38 | $finalArray = []; |
|
97 | 38 | $mandatoryFields = $this->getMandatoryFields(); |
|
98 | |||
99 | 38 | $cleanObject = new $this(); |
|
100 | 38 | foreach ($cleanObject as $fieldId => $value) { |
|
101 | 38 | if ($this->$fieldId === $cleanObject->$fieldId) { |
|
102 | 38 | if (\in_array($fieldId, $mandatoryFields, true)) { |
|
103 | 6 | $missingMandatoryField = new MissingMandatoryField(sprintf( |
|
104 | 6 | 'The field "%s" for class "%s" is mandatory and empty, please correct', |
|
105 | $fieldId, |
||
106 | 6 | get_class($cleanObject) |
|
107 | )); |
||
108 | 6 | $missingMandatoryField->method = get_class($cleanObject); |
|
109 | 6 | $missingMandatoryField->methodInstance = $this; |
|
110 | 38 | throw $missingMandatoryField; |
|
111 | } |
||
112 | } else { |
||
113 | 25 | $finalArray[$fieldId] = $this->$fieldId; |
|
114 | } |
||
115 | } |
||
116 | |||
117 | 32 | return $finalArray; |
|
118 | } |
||
119 | |||
120 | /** |
||
121 | * Will resolve the dependency of a mandatory inline_message_id OR a chat_id + message_id |
||
122 | * |
||
123 | * NOTE: This will use pass by reference instead of copy on write as the use-case for this functions allows this |
||
124 | * |
||
125 | * @param array $return |
||
126 | * @return array |
||
127 | */ |
||
128 | 4 | final protected function mandatoryUserOrInlineMessageId(array &$return): array |
|
142 | |||
143 | /** |
||
144 | * ReplyMarkup fields require a bit of work before sending them |
||
145 | * |
||
146 | * This happens because reply markup are a type thus they don't have an export mechanism to do the job |
||
147 | * |
||
148 | * @param TelegramTypes $replyMarkup |
||
149 | * @return TelegramTypes |
||
150 | */ |
||
151 | 1 | private function formatReplyMarkup(TelegramTypes $replyMarkup): TelegramTypes |
|
161 | |||
162 | 1 | private function getArrayFromKeyboard(array $keyboardArray): array |
|
184 | |||
185 | /** |
||
186 | * Does the definitive export of those fields in a reply markup item that are filled in |
||
187 | * |
||
188 | * @param TelegramTypes $markupItem |
||
189 | * @return array |
||
190 | */ |
||
191 | private function exportReplyMarkupItem(TelegramTypes $markupItem): array |
||
203 | } |
||
204 |
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: