Skip to content

Commit 2b8adde

Browse files
committed
Added all classes with make() syntax
0 parents  commit 2b8adde

File tree

11 files changed

+256
-0
lines changed

11 files changed

+256
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
vendor/
2+
composer.lock

composer.json

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"name": "php-telegram-bot/fluent-keyboard",
3+
"description": "Fluent Keyboard builder for ReplyKeyboardMarkup and InlineKeyboardMarkup.",
4+
"keywords": ["telegram", "bot", "api", "fluent", "keyboard", "builder"],
5+
"type": "library",
6+
"license": "MIT",
7+
"authors": [
8+
{
9+
"name": "Tii",
10+
"email": "mail@tii.one"
11+
}
12+
],
13+
"minimum-stability": "stable",
14+
"autoload": {
15+
"files": [
16+
"./src/helpers.php"
17+
],
18+
"psr-4": {
19+
"PhpTelegramBot\\FluentKeyboard\\": "./src"
20+
}
21+
},
22+
"require": {
23+
"php": "^7.3|^8.0",
24+
"ext-json": "*"
25+
}
26+
}

src/FluentEntity.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
namespace PhpTelegramBot\FluentKeyboard;
4+
5+
abstract class FluentEntity implements \JsonSerializable
6+
{
7+
/**
8+
* @var array $data
9+
*/
10+
protected $data = [];
11+
12+
protected $defaults = [];
13+
14+
public static function make()
15+
{
16+
return new static;
17+
}
18+
19+
public function __call($name, $arguments)
20+
{
21+
$key = snake_case($name);
22+
23+
$this->data[$key] = $arguments[0] ?? $this->getDefault($key);
24+
25+
return $this;
26+
}
27+
28+
public function jsonSerialize()
29+
{
30+
return $this->data;
31+
}
32+
33+
private function getDefault($key)
34+
{
35+
return $this->defaults[$key] ?? null;
36+
}
37+
38+
}

src/ForceReply.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace PhpTelegramBot\FluentKeyboard;
4+
5+
/**
6+
* @method self inputFieldPlaceholder(string $input_field_placeholder)
7+
* @method self selective(bool $selective = true)
8+
*/
9+
class ForceReply extends FluentEntity
10+
{
11+
12+
protected $data = [
13+
'force_reply' => true,
14+
];
15+
16+
protected $defaults = [
17+
'selective' => true,
18+
];
19+
20+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace PhpTelegramBot\FluentKeyboard\InlineKeyboard;
4+
5+
use PhpTelegramBot\FluentKeyboard\FluentEntity;
6+
7+
/**
8+
* @method self text(string $text)
9+
* @method self url(string $url)
10+
* @method self loginUrl(array $login_url)
11+
* @method self callbackData(string $callback_data)
12+
* @method self switchInlineQuery(string $switch_inline_query)
13+
* @method self switchInlineQueryCurrentChat(string $switch_inline_query_current_chat)
14+
* @method self callbackGame(string $callback_game)
15+
* @method self pay(bool $pay = true)
16+
*/
17+
class InlineKeyboardButton extends FluentEntity
18+
{
19+
20+
protected $defaults = [
21+
'pay' => true,
22+
];
23+
24+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace PhpTelegramBot\FluentKeyboard\InlineKeyboard;
4+
5+
use PhpTelegramBot\FluentKeyboard\FluentEntity;
6+
7+
class InlineKeyboardMarkup extends FluentEntity
8+
{
9+
10+
protected $data = [
11+
'inline_keyboard' => []
12+
];
13+
14+
/**
15+
* Adds a row to the keyboard
16+
* @param array $row
17+
* @return $this
18+
*/
19+
public function row(array $row = [])
20+
{
21+
$this->data['inline_keyboard'][] = $row;
22+
return $this;
23+
}
24+
25+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace PhpTelegramBot\FluentKeyboard\ReplyKeyboard;
4+
5+
use PhpTelegramBot\FluentKeyboard\FluentEntity;
6+
7+
/**
8+
* @method self text(string $text)
9+
* @method self requestContact(bool $request_contact)
10+
* @method self requestLocation(bool $request_location)
11+
* @method self requestPoll(KeyboardButtonPollType $request_poll)
12+
*/
13+
class KeyboardButton extends FluentEntity
14+
{
15+
16+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
namespace PhpTelegramBot\FluentKeyboard\ReplyKeyboard;
4+
5+
class KeyboardButtonPollType implements \JsonSerializable
6+
{
7+
8+
protected $data = [];
9+
10+
public function __construct(array $data = [])
11+
{
12+
$this->data = $data;
13+
}
14+
15+
public static function any()
16+
{
17+
return new static();
18+
}
19+
20+
public static function quiz()
21+
{
22+
return new static([
23+
'type' => 'quiz'
24+
]);
25+
}
26+
27+
public static function regular()
28+
{
29+
return new static([
30+
'type' => 'regular'
31+
]);
32+
}
33+
34+
public function jsonSerialize()
35+
{
36+
return $this->data;
37+
}
38+
39+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
namespace PhpTelegramBot\FluentKeyboard\ReplyKeyboard;
4+
5+
use PhpTelegramBot\FluentKeyboard\FluentEntity;
6+
7+
/**
8+
* @method self resizeKeyboard(bool $resize_keyboard = true)
9+
* @method self oneTimeKeyboard(bool $one_time_keyboard = true)
10+
* @method self inputFieldPlaceholder(string $input_field_placeholder)
11+
* @method self selective(bool $selective = true)
12+
*/
13+
class ReplyKeyboardMarkup extends FluentEntity
14+
{
15+
16+
protected $data = [
17+
'keyboard' => [],
18+
];
19+
20+
protected $defaults = [
21+
'resize_keyboard' => true,
22+
'one_time_keyboard' => true,
23+
'selective' => true,
24+
];
25+
26+
/**
27+
* Adds a row to the keyboard
28+
* @param array $row
29+
* @return $this
30+
*/
31+
public function row(array $row = [])
32+
{
33+
$this->data['keyboard'][] = $row;
34+
return $this;
35+
}
36+
37+
}

src/ReplyKeyboardRemove.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace PhpTelegramBot\FluentKeyboard;
4+
5+
/**
6+
* @method self selective(bool $value = true)
7+
*/
8+
class ReplyKeyboardRemove extends FluentEntity
9+
{
10+
11+
protected $data = [
12+
'remove_keyboard' => true,
13+
];
14+
15+
protected $defaults = [
16+
'selective' => true,
17+
];
18+
19+
}

0 commit comments

Comments
 (0)