I'm trying to create a bot for Telegram messenger while trying to learn OOP. I'm really lost on how to approach the problem. I have a Message entity, with all the getters and setters, this I think it's pretty straigthforward. My problem is that I want to create two (or more) types of factories
1) a simple message where you just feed the factory with the chat_id you want to send the message and the text, that could be something like this:
<?php
namespace Telegram\Domain\Factory;
use Telegram\Domain\Entity\Message;
class MessageRaw extends MessageAbstract {
public function createMessage($chat_id, $text) {
$message = new Message();
$message->setChatId($chat_id);
$message->setText($text);
return $message;
}
}
where MessageAbstract is
<?php
namespace Telegram\Domain\Factory;
abstract class MessageAbstract {
abstract public function createMessage($chat_id, $text);
}
2) A message with a keyboard (Telegram gives you the possibility to include a custom keyboard when you send a message). I have the problem here, the keyboard is given as an array, so this would be one more argument to a createMessage.
So my problem is, should I always give the $keyboard argument whether it is a simple message or a message with keyboard? Or are these two types of messages different enough so that they should be created from different classes (I think not)? Or maybe I shouldn't do it in a factory, but with setters and getters?
TLDR: How to create an object with different number of arguments in a fancy way, something like this
$MessageRaw = new MessageRaw($chat_id, $text);
$MessageNumericKeyboard = new MessageNumericKeyboard($chat_id, $text); //numeric keyboard is standard so can be set in the createMessage Function
$MessageCustomKeyboard = new MessageCustomKeyboard($chat_id, $text, ['A', 'B']); //should it be done like this?
MessageRaw
here, why don't you useMessage
directly? Anyway if you insist on using this, then try to always send the entire object which means you sendnew Message()
instead of $chat_id, $text (first it would allow you to typehint the variable; and 2nd it would give you access to all available methods on that object). – Latimer