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 | public static function bindToObject(TelegramRawData $data, LoggerInterface $logger): TelegramTypes |
||
35 | { |
||
36 | return new Message($data->getResult(), $logger); |
||
37 | } |
||
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 | 7 | public function performSpecialConditions(): TelegramMethods |
|
48 | { |
||
49 | 7 | if (!empty($this->reply_markup)) { |
|
50 | $this->reply_markup = json_encode($this->formatReplyMarkup($this->reply_markup)); |
||
|
|||
51 | } |
||
52 | |||
53 | // Several classes may send a parse mode, so check before sending |
||
54 | // TODO Do I want to validate data in here? Should I? |
||
55 | /* |
||
56 | * if (!empty($this->parse_mode)) { |
||
57 | if (strtoupper($this->parse_mode) !== 'HTML' || strtoupper($this->parse_mode) !== 'MARKDOWN') { |
||
58 | throw new InvalidParseMode(sprintf( |
||
59 | 'An invalid value for parse_mode has been given. Please use HTML or Markdown. Provided: "%s"', |
||
60 | $this->parse_mode |
||
61 | )); |
||
62 | } |
||
63 | } |
||
64 | */ |
||
65 | |||
66 | 7 | return $this; |
|
67 | } |
||
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 | 13 | final public function export(): array |
|
76 | { |
||
77 | 13 | $finalArray = []; |
|
78 | 13 | $mandatoryFields = $this->getMandatoryFields(); |
|
79 | |||
80 | 13 | $cleanObject = new $this(); |
|
81 | 13 | foreach ($cleanObject as $fieldId => $value) { |
|
82 | 12 | if ($this->$fieldId === $cleanObject->$fieldId) { |
|
83 | 11 | if (in_array($fieldId, $mandatoryFields, true)) { |
|
84 | 6 | throw new MissingMandatoryField(sprintf( |
|
85 | 6 | 'The field "%s" is mandatory and empty, please correct', |
|
86 | 11 | $fieldId |
|
87 | )); |
||
88 | } |
||
89 | } else { |
||
90 | 8 | $finalArray[$fieldId] = $this->$fieldId; |
|
91 | } |
||
92 | } |
||
93 | |||
94 | 7 | 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 | final private function formatReplyMarkup(TelegramTypes $replyMarkup): TelegramTypes |
||
129 | { |
||
130 | if ($replyMarkup instanceof Markup) { |
||
131 | $replyMarkup->inline_keyboard = $this->getArrayFromKeyboard($replyMarkup->inline_keyboard); |
||
132 | } elseif ($replyMarkup instanceof ReplyKeyboardMarkup) { |
||
133 | $replyMarkup->keyboard = $this->getArrayFromKeyboard($replyMarkup->keyboard); |
||
134 | } |
||
135 | |||
136 | return $replyMarkup; |
||
137 | } |
||
138 | |||
139 | final private function getArrayFromKeyboard(array $keyboardArray): array |
||
140 | { |
||
141 | $finalCleanArray = []; |
||
142 | |||
143 | // A keyboard is an array of an array of objects or strings |
||
144 | foreach ($keyboardArray as $rowItems) { |
||
145 | $elements = []; |
||
146 | foreach ($rowItems as $rowItem) { |
||
147 | if (is_object($rowItem)) { |
||
148 | // Button is effectively an object |
||
149 | $elements[] = $this->exportReplyMarkupItem($rowItem); |
||
150 | } else { |
||
151 | // Add support for old style simple text buttons |
||
152 | $elements[] = $rowItem; |
||
153 | } |
||
154 | } |
||
155 | |||
156 | $finalCleanArray[] = $elements; |
||
157 | } |
||
158 | |||
159 | return $finalCleanArray; |
||
160 | } |
||
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: