PHP Class izzum\statemachine\Transition

It has functionality to accept a Rule (guard logic) and a Command (transition logic) as well as callables for the guard logic and transition logic . callables are: closures, anonymous functions, user defined functions, instance methods, static methods etc. see the php manual. The guards are used to check whether a transition can take place (Rule and callable) The logic parts are used to execute the transition logic (Command and callable) Rules and commands should be able to be found/autoloaded by the application If transitions share the same states (both to and from) then they should point to the same object reference (same states should share the exact same state configuration).
Exibir arquivo Open project: rolfvreijdenberger/izzum-statemachine Class Usage Examples

Protected Properties

Property Type Description
$callable_guard callable the callable to call as part of the transition guard (should return a boolean)
$callable_transition callable the callable to call as part of the transition logic
$command string This can actually be a ',' seperated string of multiple commands.
$description string a description for the state
$event string an event code that can trigger this transitions
$rule string This can actually be a ',' seperated string of multiple rules.
$state_from State the state this transition starts from
$state_to State the state this transition points to

Public Methods

Method Description
__construct ( State $state_from, State $state_to, string $event = null, string $rule = self::RULE_EMPTY, string $command = self::COMMAND_EMPTY, callable $callable_guard = self::CALLABLE_NULL, callable $callable_transition = self::CALLABLE_NULL )
__toString ( ) : string
can ( Context $context ) : boolean is a transition possible? Check the guard Rule with the domain object injected.
getCommand ( Context $context ) : izzum\command\ICommand returns the associated Command for this Transition.
getCommandName ( ) : string return the command name(s).
getCopy ( State $from, State $to ) : Transition for transitions that contain regex states, we need to be able to copy an existing (subclass of this) transition with all it's fields.
getDescription ( ) : string get the description for this transition (if any)
getEvent ( ) : string get the event name by which this transition can be triggered
getGuardCallable ( ) : callable returns the callable for the guard logic.
getName ( ) : string get the transition name.
getRule ( Context $context ) : izzum\rules\IRule returns the associated Rule for this Transition, configured with a 'reference' (stateful) object
getRuleName ( )
getStateFrom ( ) : State get the state this transition points from
getStateTo ( ) : State get the state this transition points to
getTransitionCallable ( ) : callable returns the callable for the transition logic.
isTriggeredBy ( string $event ) : boolean Can this transition be triggered by a certain event? This also matches on the transition name.
process ( Context $context ) : void Process the transition for the statemachine and execute the associated Command with the domain object injected.
setCommandName ( $command )
setDescription ( string $description ) set the description of the transition (for uml generation for example)
setEvent ( string $event ) set the event name by which this transition can be triggered.
setGuardCallable ( callable $callable ) the callable to call as part of the transition guard
setRuleName ( $rule )
setTransitionCallable ( callable $callable ) the callable to call as part of the transition logic
toString ( ) : string

Protected Methods

Method Description
callCallable ( callable $callable, Context $context, $type = 'n/a' ) calls the $callable as part of the transition

Method Details

__construct() public method

public __construct ( State $state_from, State $state_to, string $event = null, string $rule = self::RULE_EMPTY, string $command = self::COMMAND_EMPTY, callable $callable_guard = self::CALLABLE_NULL, callable $callable_transition = self::CALLABLE_NULL )
$state_from State
$state_to State
$event string optional: an event name by which this transition can be triggered
$rule string optional: one or more fully qualified Rule (sub)class name(s) to check to see if we are allowed to transition. This can actually be a ',' seperated string of multiple rules that will be applied as a chained 'and' rule.
$command string optional: one or more fully qualified Command (sub)class name(s) to execute for a transition. This can actually be a ',' seperated string of multiple commands that will be executed as a composite.
$callable_guard callable optional: a php callable to call. eg: "function(){echo 'closure called';};"
$callable_transition callable optional: a php callable to call. eg: "izzum\MyClass::myStaticMethod"

__toString() public method

public __toString ( ) : string
return string

callCallable() protected method

calls the $callable as part of the transition
protected callCallable ( callable $callable, Context $context, $type = 'n/a' )
$callable callable
$context Context

can() public method

is a transition possible? Check the guard Rule with the domain object injected.
public can ( Context $context ) : boolean
$context Context
return boolean

getCommand() public method

the Command will be configured with the 'reference' of the stateful object. In case there have been multiple commands as input (',' seperated), this method will return a Composite command.
public getCommand ( Context $context ) : izzum\command\ICommand
$context Context
return izzum\command\ICommand

getCommandName() public method

one or more fully qualified command (sub)class name(s) to execute for a transition. This can actually be a ',' seperated string of multiple commands that will be executed as a composite.
public getCommandName ( ) : string
return string

getCopy() public method

We need to instantiate it with a different from and to state since either one of those states can be the regex states. All other fields need to be copied. Override this method in a subclass to add other fields. By using 'new static' we are already instantiating a possible subclass.
public getCopy ( State $from, State $to ) : Transition
$from State
$to State
return Transition

getDescription() public method

get the description for this transition (if any)
public getDescription ( ) : string
return string

getEvent() public method

get the event name by which this transition can be triggered
public getEvent ( ) : string
return string

getGuardCallable() public method

returns the callable for the guard logic.
public getGuardCallable ( ) : callable
return callable or null

getName() public method

the transition name is always unique for a statemachine since it constists of _to_
public getName ( ) : string
return string

getRule() public method

returns the associated Rule for this Transition, configured with a 'reference' (stateful) object
public getRule ( Context $context ) : izzum\rules\IRule
$context Context the associated Context for a our statemachine
return izzum\rules\IRule a Rule or chained AndRule if the rule input was a ',' seperated string of rules.

getRuleName() public method

public getRuleName ( )

getStateFrom() public method

get the state this transition points from
public getStateFrom ( ) : State
return State

getStateTo() public method

get the state this transition points to
public getStateTo ( ) : State
return State

getTransitionCallable() public method

returns the callable for the transition logic.
public getTransitionCallable ( ) : callable
return callable or null

isTriggeredBy() public method

Can this transition be triggered by a certain event? This also matches on the transition name.
public isTriggeredBy ( string $event ) : boolean
$event string
return boolean

process() public method

Process the transition for the statemachine and execute the associated Command with the domain object injected.
public process ( Context $context ) : void
$context Context
return void

setCommandName() public method

public setCommandName ( $command )

setDescription() public method

set the description of the transition (for uml generation for example)
public setDescription ( string $description )
$description string

setEvent() public method

In case the event name is null or an empty string, it defaults to the transition name.
public setEvent ( string $event )
$event string

setGuardCallable() public method

the callable to call as part of the transition guard
public setGuardCallable ( callable $callable )
$callable callable

setRuleName() public method

public setRuleName ( $rule )

setTransitionCallable() public method

the callable to call as part of the transition logic
public setTransitionCallable ( callable $callable )
$callable callable

toString() public method

public toString ( ) : string
return string

Property Details

$callable_guard protected_oe property

the callable to call as part of the transition guard (should return a boolean)
protected callable $callable_guard
return callable

$callable_transition protected_oe property

the callable to call as part of the transition logic
protected callable $callable_transition
return callable

$command protected_oe property

This can actually be a ',' seperated string of multiple commands.
protected string $command
return string

$description protected_oe property

a description for the state
protected string $description
return string

$event protected_oe property

an event code that can trigger this transitions
protected string $event
return string

$rule protected_oe property

This can actually be a ',' seperated string of multiple rules.
protected string $rule
return string

$state_from protected_oe property

the state this transition starts from
protected State,izzum\statemachine $state_from
return State

$state_to protected_oe property

the state this transition points to
protected State,izzum\statemachine $state_to
return State