PHP Class izzum\statemachine\State

A State instance can (and should) be shared by multiple Transition objects when it is the same State for their origin/from State. A State can be a regex state (or negated regex). A regex state can be used in a transition and when added to a statemachine the regular expression will be matched on all currently known states on that statemachine and new Transitions will be added to the statemachine that match the from/to state regexes. This is very useful to build a lot of transitions very quickly. to build a full mesh of transitions (all states to all states): $a = new State('a'); $b = new State('b'); $c = new State('c'); $machine->addState($a); $machine->addState($b); $machine->addState($c); $state_regex_all = new State('regex:|.*|'); $machine->addTransition(new Transition($state_regex_all, $state_regex_all));
Mostrar archivo Open project: rolfvreijdenberger/izzum-statemachine Class Usage Examples

Protected Properties

Property Type Description
$callable_entry callable the entry callable method
$callable_exit callable the exit callable method
$command_entry_name string This can actually be a ',' seperated string of multiple commands that will be executed as a composite.
$command_exit_name string This can actually be a ',' seperated string of multiple commands that will be executed as a composite.
$description string a description for the state
$name string The name of the state
$transitions Transition[] These will be set by Transition objects (they provide the association) this is not a hashmap, so the order of Transitions *might* be important. whenever a State is asked for it's transitions, the first transition might be tried first. this might have performance and configuration benefits
$type string The state type: - State::TYPE_INITIAL - State::TYPE_NORMAL - State::TYPE_FINAL - State::TYPE_REGEX

Public Methods

Method Description
__construct ( string $name, string $type = self::TYPE_NORMAL, $command_entry_name = self::COMMAND_EMPTY, $command_exit_name = self::COMMAND_EMPTY, callable $callable_entry = self::CALLABLE_NULL, callable $callable_exit = self::CALLABLE_NULL )
__toString ( ) : string
addTransition ( Transition $transition ) : boolan add an outgoing transition from this state.
entryAction ( Context $context ) An action executed every time a state is entered.
exitAction ( Context $context ) An action executed every time a state is exited.
getDescription ( ) : string get the description for this state (if any)
getEntryCallable ( ) : callable get the entry callable, the callable to be called when entering this state
getEntryCommandName ( ) : string get the fully qualified command name for entry of the state
getExitCallable ( ) : callable get the exit callable, the callable to be called when exiting this state
getExitCommandName ( ) : string get the fully qualified command name for exit of the state
getName ( ) gets the name of this state
getTransitions ( ) : Transition[] get all outgoing transitions
getTransitionsTriggeredByEvent ( string $event ) : Transition[] get the transition for this state that can be triggered by an event code.
getType ( ) : string get the state type
hasTransition ( string $transition_name ) : boolean Do we have a transition from this state with a certain name?
isFinal ( ) : boolean is it a final state
isInitial ( ) : boolean is it an initial state
isNegatedRegex ( ) : boolean is this state a negated regex type of state? "not-regex:"
isNormal ( ) : boolean is it a normal state
isNormalRegex ( ) : boolean is this state a normal regex type of state? "regex:"
isRegex ( ) : boolean is this state a regex type of state? formats: "regex:" "not-regex:"
setDescription ( string $description ) set the description of the state (for uml generation for example)
setEntryCallable ( callable $callable ) set the entry callable, the callable to be called when entering this state
setEntryCommandName ( string $name ) set the entry command name
setExitCallable ( callable $callable ) set the exit callable, the callable to be called when exiting this state
setExitCommandName ( string $name ) set the exit command name

Protected Methods

Method Description
callCallable ( callable $callable, Context $context, string $type = 'n/a' ) calls a $callable if it exists, with the arguments $context->getEntity()
execute ( izzum\command\ICommand $command ) helper method
getCommand ( string $command_name, Context $context ) : izzum\command\ICommand returns the associated Command for the entry/exit action.
setName ( string $name ) sets the name of this state
setType ( string $type ) set the state type

Method Details

__construct() public method

public __construct ( string $name, string $type = self::TYPE_NORMAL, $command_entry_name = self::COMMAND_EMPTY, $command_exit_name = self::COMMAND_EMPTY, callable $callable_entry = self::CALLABLE_NULL, callable $callable_exit = self::CALLABLE_NULL )
$name string the name of the state (can also be a regex in format: [not-]regex://)
$type string the type of the state (on of self::TYPE_<*>)
$command_entry_name optional: a command to be executed when a transition enters this state One or more fully qualified command (sub)class name(s) to execute when entering this state. This can actually be a ',' seperated string of multiple commands that will be executed as a composite.
$command_exit_name optional: a command to be executed when a transition leaves this state One or more fully qualified command (sub)class name(s) to execute when exiting this state. This can actually be a ',' seperated string of multiple commands that will be executed as a composite.
$callable_entry callable optional: a php callable to call. eg: "function(){echo 'closure called';};"
$callable_exit callable optional: a php callable to call. eg: "izzum\MyClass::myStaticMethod"

__toString() public method

public __toString ( ) : string
return string

addTransition() public method

TRICKY: this method should be package visibility only, so don't use directly. it is used to set the bidirectional association for State and Transition from a Transition instance on the state the transition will be allowed to run from ('state from').
public addTransition ( Transition $transition ) : boolan
$transition Transition
return boolan yes in case the transition was not on the State already or in case of an invalid transition

callCallable() protected method

calls a $callable if it exists, with the arguments $context->getEntity()
protected callCallable ( callable $callable, Context $context, string $type = 'n/a' )
$callable callable
$context Context
$type string the type of callable (self::CALLABLE_ENTRY | self::CALLABLE_EXIT)

entryAction() public method

An entry action will not be executed for an 'initial' state.
public entryAction ( Context $context )
$context Context

execute() protected method

helper method
protected execute ( izzum\command\ICommand $command )
$command izzum\command\ICommand

exitAction() public method

An exit action will not be executed for a 'final' state since a machine will not leave a 'final' state.
public exitAction ( Context $context )
$context Context

getCommand() protected method

the Command will be configured with the domain model via dependency injection
protected getCommand ( string $command_name, Context $context ) : izzum\command\ICommand
$command_name string entry or exit command name
$context Context
return izzum\command\ICommand

getDescription() public method

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

getEntryCallable() public method

get the entry callable, the callable to be called when entering this state
public getEntryCallable ( ) : callable
return callable

getEntryCommandName() public method

get the fully qualified command name for entry of the state
public getEntryCommandName ( ) : string
return string

getExitCallable() public method

get the exit callable, the callable to be called when exiting this state
public getExitCallable ( ) : callable
return callable

getExitCommandName() public method

get the fully qualified command name for exit of the state
public getExitCommandName ( ) : string
return string

getName() public method

gets the name of this state
public getName ( )

getTransitions() public method

get all outgoing transitions
public getTransitions ( ) : Transition[]
return Transition[] an array of transitions

getTransitionsTriggeredByEvent() public method

get the transition for this state that can be triggered by an event code.
public getTransitionsTriggeredByEvent ( string $event ) : Transition[]
$event string the event code that can trigger a transition (mealy machine)
return Transition[]

getType() public method

get the state type
public getType ( ) : string
return string

hasTransition() public method

Do we have a transition from this state with a certain name?
public hasTransition ( string $transition_name ) : boolean
$transition_name string
return boolean

isFinal() public method

is it a final state
public isFinal ( ) : boolean
return boolean

isInitial() public method

is it an initial state
public isInitial ( ) : boolean
return boolean

isNegatedRegex() public method

is this state a negated regex type of state? "not-regex:"
public isNegatedRegex ( ) : boolean
return boolean

isNormal() public method

is it a normal state
public isNormal ( ) : boolean
return boolean

isNormalRegex() public method

is this state a normal regex type of state? "regex:"
public isNormalRegex ( ) : boolean
return boolean

isRegex() public method

is this state a regex type of state? formats: "regex:" "not-regex:"
public isRegex ( ) : boolean
return boolean

setDescription() public method

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

setEntryCallable() public method

set the entry callable, the callable to be called when entering this state
public setEntryCallable ( callable $callable )
$callable callable

setEntryCommandName() public method

set the entry command name
public setEntryCommandName ( string $name )
$name string a fully qualified command name

setExitCallable() public method

set the exit callable, the callable to be called when exiting this state
public setExitCallable ( callable $callable )
$callable callable

setExitCommandName() public method

set the exit command name
public setExitCommandName ( string $name )
$name string a fully qualified command name

setName() protected method

sets the name of this state
protected setName ( string $name )
$name string

setType() protected method

set the state type
protected setType ( string $type )
$type string

Property Details

$callable_entry protected_oe property

the entry callable method
protected callable $callable_entry
return callable

$callable_exit protected_oe property

the exit callable method
protected callable $callable_exit
return callable

$command_entry_name protected_oe property

This can actually be a ',' seperated string of multiple commands that will be executed as a composite.
protected string $command_entry_name
return string

$command_exit_name protected_oe property

This can actually be a ',' seperated string of multiple commands that will be executed as a composite.
protected string $command_exit_name
return string

$description protected_oe property

a description for the state
protected string $description
return string

$name protected_oe property

The name of the state
protected string $name
return string

$transitions protected_oe property

These will be set by Transition objects (they provide the association) this is not a hashmap, so the order of Transitions *might* be important. whenever a State is asked for it's transitions, the first transition might be tried first. this might have performance and configuration benefits
protected Transition[],izzum\statemachine $transitions
return Transition[]

$type protected_oe property

The state type: - State::TYPE_INITIAL - State::TYPE_NORMAL - State::TYPE_FINAL - State::TYPE_REGEX
protected string $type
return string