Skip to content

Commit

Permalink
Use Laravel Collection as memory
Browse files Browse the repository at this point in the history
  • Loading branch information
alies-dev committed May 14, 2024
1 parent 7bd91ce commit aede9f1
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 15 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,7 @@ final class TelegramWebhookController
- `isEnd()` - Check the end of the dialog
- 🔐 `end()` - End dialog
- 🔐 `jump(string $stepName)` - Jump to the particular step, where `$step` is the `public` method name
- 🔐 `remember(string $key, mixed $value)` - Add a new key-value to `Dialog::$memory` array to make this data available on next steps
- 🔐 `forget(string $key)` - Remove a value from `Dialog::$memory`by key.
- 🔐 `memory` - Laravel Collection to store intermediate data between steps


### `DialogManager` class API
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"homepage": "https://github.com/koot-labs/telegram-bot-dialogs",
"require": {
"php": ">=8.0",
"illuminate/collections": "^8.0 || ^9.0 || ^10.0 || ^11.0",
"illuminate/support": "^8.0 || ^9.0 || ^10.0 || ^11.0",
"irazasyed/telegram-bot-sdk": "^3.1",
"predis/predis": "^1.0 || ^2.0",
Expand Down
58 changes: 48 additions & 10 deletions src/Dialog.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,42 @@

namespace KootLabs\TelegramBotDialogs;

use Illuminate\Support\Collection;
use KootLabs\TelegramBotDialogs\Exceptions\InvalidDialogStep;
use KootLabs\TelegramBotDialogs\Exceptions\UnexpectedUpdateType;
use Telegram\Bot\Api;
use Telegram\Bot\Objects\Update;

abstract class Dialog
{
/** @var int id of the Chat the Dialog is bounded to */
/**
* @readonly
* @var int id of the Chat the Dialog is bounded to
*/
protected int $chat_id;

/** @var int|null id of the User the Dialog is bounded to. Makes sense for multiuser chats only. */
/**
* @readonly
* @var int|null id of the User the Dialog is bounded to. Makes sense for multiuser chats only.
*/
protected ?int $user_id = null;

/** @var array<string, mixed> Key-value storage to store data between steps. */
protected array $memory = [];
/** @var \Illuminate\Support\Collection<array-key, mixed> Key-value storage to store data between steps. */
protected Collection $memory;

/** @var int<-1, max> Seconds to store state of the Dialog after latest activity on it. */
/**
* @readonly
* @var int<-1, max> Seconds to store state of the Dialog after latest activity on it.
*/
protected int $ttl = 300;

/** @var \Telegram\Bot\Api Associated Bot instance that will perform API calls. */
protected Api $bot;

/** @var list<string|array{name: string, response: string, options:array}> List of method to execute. The order defines the sequence */
/**
* @readonly
* @var list<string|array{name: string, response: string, options:array}> List of method to execute. The order defines the sequence
*/
protected array $steps = [];

/** @var int Index of the next step. */
Expand All @@ -45,6 +58,8 @@ public function __construct(int $chatId, Api $bot = null, int $userId = null)
$this->chat_id = $chatId;
$this->user_id = $userId;

$this->memory = new Collection();

if ($bot instanceof Api) {
$this->bot = $bot;
}
Expand Down Expand Up @@ -177,16 +192,22 @@ final public function end(): void
$this->next = count($this->steps);
}

/** Remember information for next steps. */
/**
* @api Remember information for next steps.
* @deprecated Will be removed in v1.0. $this->memory is a Collection, please use it directly.
*/
final protected function remember(string $key, mixed $value): void
{
$this->memory[$key] = $value;
$this->memory->put($key, $value);
}

/** @api Forget information from next steps. */
/**
* @api Forget information from next steps.
* @deprecated Will be removed in v1.0. $this->memory is a Collection, please use it directly.
*/
final protected function forget(string $key): void
{
unset($this->memory[$key]);
$this->memory->forget($key);
}

/** Check if Dialog started */
Expand Down Expand Up @@ -258,11 +279,28 @@ private function proceedConfiguredStep(array $stepConfig, Update $update, int $c
/** @return array<string, mixed> */
public function __serialize(): array
{
// serialize non-readonly properties only
return [
'chat_id' => $this->chat_id,
'user_id' => $this->user_id,
'next' => $this->next,
'memory' => $this->memory,
'afterProceedJumpToIndex' => $this->afterProceedJumpToIndex,
];
}

/** @todo remove it in v1.0 */
public function __unserialize(array $data): void
{
// unserialize non-readonly properties only
$this->chat_id = $data['chat_id'];
$this->user_id = $data['user_id'];
$this->next = $data['next'];
$this->afterProceedJumpToIndex = $data['afterProceedJumpToIndex'];

// enforce migration from array to Collection, @todo remove it in v1.0
$this->memory = is_array($data['memory'])
? collect($data['memory'])
: $data['memory'];
}
}
4 changes: 1 addition & 3 deletions src/Exceptions/ControlFlow/DialogControlFlowException.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,4 @@
use KootLabs\TelegramBotDialogs\Exceptions\DialogException;

/** @internal */
interface DialogControlFlowException extends DialogException
{
}
interface DialogControlFlowException extends DialogException {}

0 comments on commit aede9f1

Please sign in to comment.