Skip to content

Commit

Permalink
Merge pull request #364 from sadegh19b/feat/rtl-keyboards
Browse files Browse the repository at this point in the history
Feat: Add `rightToLeft` to `Keyboard` & `ReplyKeyboard`
  • Loading branch information
fabio-ivona authored Apr 2, 2023
2 parents f83baf1 + f744dcf commit 6650c61
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 2 deletions.
19 changes: 19 additions & 0 deletions docs/content/en/features/keyboards.md
Original file line number Diff line number Diff line change
Expand Up @@ -199,3 +199,22 @@ Keyboard::make()
->button('Dismiss')->action('dismiss')->param('id', '42')->width(0.5)
->when($userCanDelete, fn(Keyboard $keyboard) => $keyboard->button('Delete')->action('delete')->param('id', '42')->width(0.5))
```

## Right to left layout

A `rightToLeft` method allows to change buttons layout from left-to-right to right-to-left (RTL).

```php
use DefStudio\Telegraph\Keyboard\Button;
use DefStudio\Telegraph\Keyboard\Keyboard;

$keyboard = Keyboard::make()
->row([
Button::make('Delete')->action('delete')->param('id', '42'),
Button::make('Dismiss')->action('dismiss')->param('id', '42'),
])
->row([
Button::make('open')->url('https://test.it'),
])
->rightToLeft();
```
11 changes: 10 additions & 1 deletion src/Keyboard/Keyboard.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ class Keyboard implements Arrayable
/** @var Collection<array-key, Button> */
protected Collection $buttons;

protected bool $rtl = false;

public function __construct()
{
/* @phpstan-ignore-next-line */
Expand All @@ -35,6 +37,13 @@ public function when(bool $condition, callable $callback): Keyboard
return $this;
}

public function rightToLeft(bool $condition = true): Keyboard
{
$this->rtl = $condition;

return $this;
}

protected function clone(): Keyboard
{
$clone = Keyboard::make();
Expand Down Expand Up @@ -222,6 +231,6 @@ public function toArray(): array

$keyboard[] = $row;

return $keyboard;
return $this->rtl ? array_map('array_reverse', $keyboard) : $keyboard;
}
}
10 changes: 9 additions & 1 deletion src/Keyboard/ReplyKeyboard.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class ReplyKeyboard implements Arrayable
/** @var Collection<array-key, ReplyButton> */
protected Collection $buttons;

protected bool $rtl = false;
protected bool $resize = false;
protected bool $oneTime = false;
protected bool $selective = false;
Expand Down Expand Up @@ -41,6 +42,13 @@ public function when(bool $condition, callable $callback): self
return $this;
}

public function rightToLeft(bool $condition = true): self
{
$this->rtl = $condition;

return $this;
}

protected function clone(): self
{
$clone = self::make();
Expand Down Expand Up @@ -265,7 +273,7 @@ public function toArray(): array

$keyboard[] = $row;

return $keyboard;
return $this->rtl ? array_map('array_reverse', $keyboard) : $keyboard;
}

/**
Expand Down
25 changes: 25 additions & 0 deletions tests/Unit/Keyboards/KeyboardTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -186,3 +186,28 @@
],
]);
});

it('can right to left layout for buttons', function () {
$keyboard = Keyboard::make()
->row([
Button::make('foo')->url('bar'),
Button::make('baz')->action('quuz'),
])
->row([
Button::make('baz')->action('quuz'),
Button::make('foo')->url('bar'),
]);

$keyboard->rightToLeft();

expect($keyboard->toArray())->toMatchArray([
[
['text' => 'baz', 'callback_data' => 'action:quuz'],
['text' => 'foo', 'url' => 'bar'],
],
[
['text' => 'foo', 'url' => 'bar'],
['text' => 'baz', 'callback_data' => 'action:quuz'],
],
]);
});
27 changes: 27 additions & 0 deletions tests/Unit/Keyboards/ReplyKeyboardTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -167,3 +167,30 @@
],
]);
});

it('can right to left layout for buttons', function () {
$keyboard = ReplyKeyboard::make()
->row([
ReplyButton::make('quzz')->requestLocation(),
ReplyButton::make('baz')->requestPoll(),
ReplyButton::make('foo'),
])
->row([
ReplyButton::make('foo'),
ReplyButton::make('baz')->requestLocation(),
]);

$keyboard->rightToLeft();

expect($keyboard->toArray())->toMatchArray([
[
['text' => 'foo'],
['text' => 'baz', 'request_poll' => ['type' => 'regular']],
['text' => 'quzz', 'request_location' => true],
],
[
['text' => 'baz', 'request_location' => true],
['text' => 'foo'],
],
]);
});

0 comments on commit 6650c61

Please sign in to comment.