Skip to content

Commit

Permalink
Common functionality for the "user_id + message_id or inline_message_…
Browse files Browse the repository at this point in the history
…id" dilemma
  • Loading branch information
unreal4u committed Jan 4, 2017
1 parent a709641 commit 9bca72e
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 13 deletions.
23 changes: 23 additions & 0 deletions src/Abstracts/TelegramMethods.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,27 @@ final public function export(): array

return $finalArray;
}

/**
* Will resolve the dependency of a mandatory inline_message_id OR a chat_id + message_id
*
* NOTE: This will use pass by reference instead of copy on write as the use-case for this functions allows this
*
* @param array $return
* @return array
*/
final protected function mandatoryUserOrInlineMessageId(array &$return): array
{
if (empty($this->chat_id) && empty($this->message_id)) {
$return[] = 'inline_message_id';
}

// On the other hand, chat_id and message_id are mandatory if inline_message_id is not filled in
if (empty($this->inline_message_id)) {
$return[] = 'chat_id';
$return[] = 'message_id';
}

return $return;
}
}
3 changes: 2 additions & 1 deletion src/Telegram/Methods/EditMessageCaption.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ class EditMessageCaption extends TelegramMethods

public function getMandatoryFields(): array
{
return [];
$returnValue = [];
return $this->mandatoryUserOrInlineMessageId($returnValue);
}
}
3 changes: 2 additions & 1 deletion src/Telegram/Methods/EditMessageReplyMarkup.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ class EditMessageReplyMarkup extends TelegramMethods

public function getMandatoryFields(): array
{
return [];
$returnValue = [];
return $this->mandatoryUserOrInlineMessageId($returnValue);
}
}
12 changes: 1 addition & 11 deletions src/Telegram/Methods/EditMessageText.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,17 +64,7 @@ class EditMessageText extends TelegramMethods
public function getMandatoryFields(): array
{
$returnValue[] = 'text';
// Inline_message_id is mandatory if no chat_id and message_id are filled in
if (empty($this->chat_id) && empty($this->message_id)) {
$returnValue[] = 'inline_message_id';
}

// On the other hand, chat_id and message_id are mandatory if inline_message_id is not filled in
if (empty($this->inline_message_id)) {
$returnValue[] = 'chat_id';
$returnValue[] = 'message_id';
}

$this->mandatoryUserOrInlineMessageId($returnValue);
return $returnValue;
}
}

0 comments on commit 9bca72e

Please sign in to comment.