Fork me on Github

Class Finite\StateMachine\ ListenableStateMachine

An Event-aware State machine.

Uses the Symfony EventDispatcher Component


Inherits from: StateMachine

StatefulInterface $object The stateful object
array $states The available states
array $transitions The available transitions
StateInterface $currentState The current state

Methods

__construct( StatefulInterface $object = null)

from StateMachine
initialize()

Initialize the State Machine current state

mixed apply( string $transitionName)

Apply a transition

bool can( string| TransitionInterface $transition)

Returns if the transition is applicable

from StateMachine
addState( string| StateInterface $state)

from StateMachine
addTransition( string| TransitionInterface $transition, string| null $initialState = null, string| null $finalState = null)

from StateMachine
TransitionInterface getTransition( string $name)

Returns a transition by its name

from StateMachine
StateInterface getState( string $name)

from StateMachine
array<string> getTransitions()

Returns an array containing all the transitions names

from StateMachine
array<string> getStates()

Returns an array containing all the states names

from StateMachine
setObject( StatefulInterface $object)

from StateMachine
StatefulInterface getObject()

from StateMachine
StateInterface getCurrentState()

from StateMachine
setEventDispatcher( EventDispatcherInterface $dispatcher)

Details

public __construct( StatefulInterface $object = null)

Parameters
StatefulInterface $object

public initialize()

Initialize the State Machine current state

public mixed apply( string $transitionName)

Apply a transition

Parameters
string $transitionName
Return Value
mixed

public bool can( string| TransitionInterface $transition)

Returns if the transition is applicable

Parameters
string| TransitionInterface $transition
Return Value
bool

public addState( string| StateInterface $state)

Parameters
string| StateInterface $state

public addTransition( string| TransitionInterface $transition, string| null $initialState = null, string| null $finalState = null)

Parameters
string| TransitionInterface $transition
string| null $initialState
string| null $finalState
Exceptions
InvalidArgumentException

public TransitionInterface getTransition( string $name)

Returns a transition by its name

Parameters
string $name
Return Value
TransitionInterface
Exceptions
TransitionException

public StateInterface getState( string $name)

Parameters
string $name
Return Value
StateInterface
Exceptions
TransitionException

public array<string> getTransitions()

Returns an array containing all the transitions names

Return Value
array<string>

public array<string> getStates()

Returns an array containing all the states names

Return Value
array<string>

public setObject( StatefulInterface $object)

Parameters
StatefulInterface $object

public StatefulInterface getObject()

Return Value
StatefulInterface

public StateInterface getCurrentState()

Return Value
StateInterface

public setEventDispatcher( EventDispatcherInterface $dispatcher)

Parameters
EventDispatcherInterface $dispatcher