Skip to content

Refactor entities #291

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 36 commits into from
Nov 24, 2016
Merged

Conversation

noplanman
Copy link
Member

@noplanman noplanman commented Sep 2, 2016

  • Complete rewrite of the Entities, making them more lightweight and almost code-free.
  • Methods are now defined by DocBlock comments instead of actual code.
  • Magic getters and setters are used for all Entities, including InlineQueryResult objects.
  • Updated tests and example code.
  • New namespaces for Keyboards and InlineQuery entities.
  • New Keyboard and KeyboardButton creation, making it easier to create keyboards.

* origin/master:
  Ask if a self-signed certificate is being used. [skip ci]
  Add issue and pull request templates.
  Update inline comments for hook.php and getUpdatesCLI.php Make sure that logging is set directly after Telegram object initialisation. (thanks @jacklul for pointing this out) Proofread and update utils.md file.
  Update readme and examples for enableAdmin(s) and add an extra test for string numbers.
* upstream/develop: (37 commits)
  Fix constraint error reported by @jacklul Close  php-telegram-bot#289
  Small typos and comment corrections.
  Simplify isAdmin method.
  Minor fixes.
  Various little code and comment updates.
  Refactor JSON entities array string generation. Add new helper method entitiesArrayToJson.
  Tidy up selectChats method.
  Tidy up insertEditedMessageRequest method.
  Tidy up insertMessageRequest method.
  Tidy up insertCallbackQueryRequest method.
  Tidy up insertChosenInlineResultRequest method.
  Tidy up insertInlineQueryRequest method.
  Tidy up insertChat method.
  Tidy up insertUser method.
  Tidy up insertRequest method.
  Tidy up insertTelegramUpdate method.
  DRY up getTimestamp method.
  Tidy up selectMessages method.
  Tidy up selectTelegramUpdate method.
  Simplify defineTables method.
  ...
Update all keyboard commands to follow new structure.
@noplanman
Copy link
Member Author

I've just finished an initial version of the keyboard rewrites. Apart from supporting the keyboard object creation as it was till now, I've implemented a faster way of creating standard keyboards:

/**
 * Gives 2 rows, each with 1 button.
 *
 * [           Button 1           ]
 * [           Button 2           ]
 */
$keyboard = new Keyboard('Button 1', 'Button 2');

/**
 * Gives 1 row, with 2 buttons.
 *
 * [   Button 1   ][   Button 2   ]
 */
$keyboard = new Keyboard(['Button 1', 'Button 2']);

/**
 * Gives 2 rows, with 1 and 2 buttons.
 *
 * [           Button 1           ]
 * [   Button 2   ][   Button 3   ]
 */
$keyboard = new Keyboard('Button 1', ['Button 2', 'Button 3']);

// or...
$keyboard = (new Keyboard('Button 1'))
    ->addRow('Button 2', 'Button 3');

// or with an empty keyboard initialisation...
$keyboard = (new Keyboard([]))
    ->addRow('Button 1')
    ->addRow('Button 2', 'Button 3');

Here a few other ways to do the same thing with added properties:

$keyboard = new Keyboard(
    ['text' => 'Send Contact', 'request_contact' => true],
    ['text' => 'Send Location', 'request_location' => true]
);
$keyboard = new Keyboard(
    (new KeyboardButton('Send Contact'))->setRequestContact(true),
    (new KeyboardButton('Send Location'))->setRequestLocation(true)
);

Changing keyboard properties is easy:

$keyboard
    ->setResizeKeyboard(true)
    ->setOneTimeKeyboard(true)
    ->setSelective(false);

ForceReply and Hide keyboards are easier now too:

$keyboard = Keyboard::forceReply();
$keyboard = Keyboard::hide();

Note: These also return a Keyboard object, that can be manipulated the same way as any other keyboard.

Same system applies for InlineKeyboard.

This isn't 100% done yet and I need to refactor a bit and write the tests, but what do you think of this idea?

@akalongman @MBoretto @jacklul
Please give me your feedback 👍

@MBoretto
Copy link
Collaborator

Hello I would like to address this point:
all condition like:
$variable !== null

Need to be changed to:

isset($variable)

see for example User.php entity
then we also need to check carefully if all the updates are stored correctly in the database
You renamed the Unit dir in unit!
Amazing work!

@noplanman
Copy link
Member Author

noplanman commented Sep 28, 2016

Conditions like $variable !== null make sense if we're checking for null only.
For other cases isset($variable) is ok, but should be used with caution.

Wherever I check for null it normally has it's reason, but I will double check!

* upstream/develop:
  implement getWebhookInfo method
  Fix duplicate test case
  Add test for unit testing class Entitiel/User
  Move AudioTest.php from tests/Unit* to tests/unit*
  Fix error importing class TestHelpers
  Correcting grammatical errors
  Add unit test for Audio class
  Add helper method generate fake audio recorded data
  Add dependency for ext-mbstring, as suggested by @IronLady in php-telegram-bot#92 [skip ci]
@MBoretto MBoretto merged commit 38a8658 into php-telegram-bot:develop Nov 24, 2016
@noplanman noplanman deleted the refactor_entities branch December 1, 2016 18:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants