Skip to content

Commit ed3fa91

Browse files
committed
Added stack() method for keyboards, streamlined tests
1 parent a490c37 commit ed3fa91

14 files changed

+409
-259
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ pass the variable to the `row()` method.
163163

164164
### ForceReply and ReplyKeyboardRemove
165165

166-
ForceReply and ReplyKeyboardRemove can be used the same way as a normal keyboard, but the do not receive any buttons:
166+
ForceReply and ReplyKeyboardRemove can be used the same way as a normal keyboard, but they do not receive any buttons:
167167

168168
```php
169169
$this->replyToUser('Thank you', [

src/InlineKeyboard/InlineKeyboardMarkup.php

Lines changed: 3 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,37 +3,11 @@
33
namespace PhpTelegramBot\FluentKeyboard\InlineKeyboard;
44

55
use PhpTelegramBot\FluentKeyboard\FluentEntity;
6+
use PhpTelegramBot\FluentKeyboard\KeyboardMarkup;
67

7-
class InlineKeyboardMarkup extends FluentEntity
8+
class InlineKeyboardMarkup extends KeyboardMarkup
89
{
910

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-
23-
return $this;
24-
}
25-
26-
public function button(InlineKeyboardButton $button)
27-
{
28-
$count = count($this->data['inline_keyboard']);
29-
if (count($this->data['inline_keyboard']) === 0) {
30-
$this->data['inline_keyboard'][] = [];
31-
$count++;
32-
}
33-
34-
$this->data['inline_keyboard'][$count - 1][] = $button;
35-
36-
return $this;
37-
}
11+
protected static $keyboardFieldName = 'inline_keyboard';
3812

3913
}

src/KeyboardMarkup.php

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
<?php
2+
3+
namespace PhpTelegramBot\FluentKeyboard;
4+
5+
abstract class KeyboardMarkup extends FluentEntity
6+
{
7+
8+
/**
9+
* Defines the field name that contains the rows and buttons.
10+
*
11+
* @var string
12+
*/
13+
protected static $keyboardFieldName = 'keyboard';
14+
15+
protected $currentRowIndex = 0;
16+
17+
public function __construct(array $data = [])
18+
{
19+
parent::__construct($data);
20+
21+
// Make sure we have one empty row from the beginning
22+
$this->data[static::$keyboardFieldName] = [[]];
23+
}
24+
25+
/**
26+
* Adds a new row to the keyboard.
27+
*
28+
* @param array $buttons
29+
* @return $this
30+
*/
31+
public function row(array $buttons = [])
32+
{
33+
$keyboard = &$this->data[static::$keyboardFieldName];
34+
35+
// Last row is not empty
36+
if (! empty($keyboard[$this->currentRowIndex])) {
37+
$keyboard[] = [];
38+
$this->currentRowIndex++;
39+
}
40+
41+
// argument is not empty
42+
if (! empty($buttons)) {
43+
$keyboard[$this->currentRowIndex] = $buttons;
44+
$this->currentRowIndex++;
45+
}
46+
47+
return $this;
48+
}
49+
50+
/**
51+
* Adds buttons one per row to the keyboard.
52+
*
53+
* @param array $buttons
54+
* @return $this
55+
*/
56+
public function stack(array $buttons)
57+
{
58+
// Every button gets its own row
59+
foreach ($buttons as $button) {
60+
$this->row([$button]);
61+
}
62+
63+
return $this;
64+
}
65+
66+
/**
67+
* Adds a button to the last row.
68+
*
69+
* @param \JsonSerializable $button
70+
* @return $this
71+
*/
72+
public function button(\JsonSerializable $button)
73+
{
74+
$keyboard = &$this->data[static::$keyboardFieldName];
75+
76+
$keyboard[$this->currentRowIndex][] = $button;
77+
78+
return $this;
79+
}
80+
81+
}

src/ReplyKeyboard/ReplyKeyboardMarkup.php

Lines changed: 3 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,49 +3,24 @@
33
namespace PhpTelegramBot\FluentKeyboard\ReplyKeyboard;
44

55
use PhpTelegramBot\FluentKeyboard\FluentEntity;
6+
use PhpTelegramBot\FluentKeyboard\KeyboardMarkup;
67

78
/**
89
* @method self resizeKeyboard(bool $resize_keyboard = true)
910
* @method self oneTimeKeyboard(bool $one_time_keyboard = true)
1011
* @method self inputFieldPlaceholder(string $input_field_placeholder)
1112
* @method self selective(bool $selective = true)
1213
*/
13-
class ReplyKeyboardMarkup extends FluentEntity
14+
class ReplyKeyboardMarkup extends KeyboardMarkup
1415
{
1516

16-
protected $data = [
17-
'keyboard' => [],
18-
];
17+
protected static $keyboardFieldName = 'keyboard';
1918

2019
protected $defaults = [
2120
'resize_keyboard' => true,
2221
'one_time_keyboard' => true,
2322
'selective' => true,
2423
];
2524

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-
35-
return $this;
36-
}
37-
38-
public function button(KeyboardButton $button)
39-
{
40-
$count = count($this->data['keyboard']);
41-
if ($count === 0) {
42-
$this->data['keyboard'][] = [];
43-
$count++;
44-
}
45-
46-
$this->data['keyboard'][$count - 1][] = $button;
47-
48-
return $this;
49-
}
5025

5126
}

tests/Pest.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@
2424
|
2525
*/
2626

27-
expect()->extend('toBeJsonSerializable', function () {
28-
return $this->jsonSerialize()->toBeArray();
27+
expect()->extend('toMatchEntity', function ($array) {
28+
return expect(json_encode($this->value))->json()->toMatchArray($array);
2929
});
3030

3131
/*
@@ -39,7 +39,7 @@
3939
|
4040
*/
4141

42-
function something()
43-
{
44-
// ..
45-
}
42+
//function something()
43+
//{
44+
// // ..
45+
//}

tests/Unit/ForceReplyTest.php

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,17 @@
44

55
it('creates valid JSON', function () {
66
$keyboard = ForceReply::make();
7-
$json = json_encode($keyboard);
87

9-
expect($json)->json()->toMatchArray([
8+
expect($keyboard)->toMatchEntity([
109
'force_reply' => true,
1110
]);
1211
});
1312

1413
it('can set known fields', function () {
1514
$keyboard = ForceReply::make()
1615
->selective();
17-
$json = json_encode($keyboard);
1816

19-
expect($json)->json()->toMatchArray([
17+
expect($keyboard)->toMatchEntity([
2018
'force_reply' => true,
2119
'selective' => true,
2220
]);
@@ -25,9 +23,8 @@
2523
it('can set unknown fields', function () {
2624
$keyboard = ForceReply::make()
2725
->unknownFields('unknown');
28-
$json = json_encode($keyboard);
2926

30-
expect($json)->json()->toMatchArray([
27+
expect($keyboard)->toMatchEntity([
3128
'force_reply' => true,
3229
'unknown_fields' => 'unknown'
3330
]);

tests/Unit/InlineKeyboardButtonTest.php

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,8 @@
44

55
it('creates valid JSON', function () {
66
$button = InlineKeyboardButton::make();
7-
$json = json_encode($button);
87

9-
expect($json)->json()->toMatchArray([
8+
expect($button)->toMatchEntity([
109

1110
]);
1211
});
@@ -21,9 +20,8 @@
2120
->switchInlineQueryCurrentChat('Switch Inline Query Current Chat')
2221
->callbackGame('Callback Game')
2322
->pay();
24-
$json = json_encode($button);
2523

26-
expect($json)->json()->toMatchArray([
24+
expect($button)->toMatchEntity([
2725
'text' => 'Text',
2826
'url' => 'http://example.com',
2927
'login_url' => [],
@@ -37,19 +35,17 @@
3735

3836
it('can set text via make', function () {
3937
$button = InlineKeyboardButton::make('Test');
40-
$json = json_encode($button);
4138

42-
expect($json)->json()->toMatchArray([
39+
expect($button)->toMatchEntity([
4340
'text' => 'Test',
4441
]);
4542
});
4643

4744
it('can set unknown fields', function () {
4845
$button = InlineKeyboardButton::make()
4946
->unknownField('unknown');
50-
$json = json_encode($button);
5147

52-
expect($json)->json()->toMatchArray([
48+
expect($button)->toMatchEntity([
5349
'unknown_field' => 'unknown',
5450
]);
5551
});

0 commit comments

Comments
 (0)