dbannik / finite-state-machine
dev-main
2024-12-25 15:22 UTC
Requires
- php: ^8.2
Requires (Dev)
- ext-xdebug: *
- friendsofphp/php-cs-fixer: ^3.65
- phpstan/phpstan: ^2.0.4
- phpstan/phpstan-phpunit: ^2.0.3
- phpunit/phpunit: ^11.5.2
- rregeer/phpunit-coverage-check: ^0.3.1
This package is auto-updated.
Last update: 2025-03-25 14:58:52 UTC
README
install
composer require dbannik/finite-state-machine
States
use Dbannik\FiniteStateMachine\StateInterface;
use Dbannik\FiniteStateMachine\Attribute\AsTransitionTo;
use Dbannik\FiniteStateMachine\Attribute\AsEntityState;
#[AsEntityState(value: 'played')]
final readonly class PlayedState implements StateInterface {
#[AsTransitionTo(nextState: PausedState::class, input: PauseAction::class)]
public function pause(AudioPlayer $player, PauseAction $action) {
var_dump('Played -> Pause');
}
#[AsTransitionTo(nextState: StoppedState::class, input: StopAction::class)]
public function stop(AudioPlayer $player, StopAction $action) {
var_dump('Played -> Stop');
}
}
#[AsEntityState(value: 'paused')]
final readonly class PausedState implements StateInterface {
#[AsTransitionTo(nextState: PlayedState::class, input: PlayAction::class)]
public function play(AudioPlayer $player, PlayAction $action) {
var_dump('Paused -> Play');
}
#[AsTransitionTo(nextState: StoppedState::class, input: StopAction::class)]
public function stop(AudioPlayer $player, StopAction $action) {
var_dump('Paused -> Stop');
}
}
#[AsEntityState(value: 'stopped')]
final readonly class StoppedState implements StateInterface {
#[AsTransitionTo(nextState: PlayedState::class, input: PlayAction::class)]
public function play(AudioPlayer $player, PlayAction $action) {
var_dump('Stopped -> Play');
}
}
Inputs parameters
class PlayAction {}
class PauseAction {}
class StopAction {}
Generate graphics
$graphGenerator = new GraphGenerator(new StatesMetadata([
new PlayedState(),
new PausedState(),
new StoppedState(),
]));
$graphGenerator->generate(__DIR__ . '/player.md');
File palyer.md
stateDiagram-v2
classDef end fill:white,color:black,font-weight:bold,stroke-width:2px,stroke:blue
classDef start fill:white,color:black,font-weight:bold,stroke-width:2px,stroke:green
PlayedState-->PausedState: PauseAction
PlayedState-->StoppedState: StopAction
PausedState-->PlayedState: PlayAction
PausedState-->StoppedState: StopAction
StoppedState-->PlayedState: PlayAction
Entity
final class AudioPlayer {
public string $state = 'stopped';
}
Transition
use Dbannik\FiniteStateMachine\TransitionState;
$statesMetadata = new StatesMetadata([
new PlayedState(),
new PausedState(),
new StoppedState(),
]);
$transition = new TransitionState(
getState: fn (AudioPlayer $player) => $player->state,
setState: fn (AudioPlayer $player, string $state) => $player->state = $state,
statesMetadata: $statesMetadata,
entity: new AudioPlayer(),
);
$transition->transition(new PlayAction()); # Stopped -> Play
$transition->transition(new PauseAction()); # Played -> Pause
$transition->transition(new StopAction()); # Paused -> Stop