Original file: src/Fsm/Data/HierarchicalStateDefinition.php
The HierarchicalStateDefinition class extends the functionality of the StateDefinition class within the finite state machine (FSM) framework. This class is designed to support nested state machines, enabling complex hierarchical state representations. This capability is crucial for applications that require a structured approach to managing state transitions in various contexts, exemplified by user interfaces, workflows, and complex data processing.
By allowing the inclusion of child state machines, this class enriches the FSM implementation with the ability to encapsulate related states and transitions, promoting reusability and separation of concerns.
public function __construct(
FsmStateEnum|string $name,
array|Collection $onEntryCallbacks = [],
array|Collection $onExitCallbacks = [],
?string $description = null,
string $type = self::TYPE_INTERMEDIATE,
?string $category = null,
string $behavior = self::BEHAVIOR_PERSISTENT,
array $metadata = [],
bool $isTerminal = false,
int $priority = 50,
public readonly ?FsmRuntimeDefinition $childStateMachine = null,
public readonly ?string $parentState = null,
)
The constructor initializes a new instance of the HierarchicalStateDefinition class, setting the parameters that define the state, its transitions, and any hierarchical relationships.
| Parameter | Type | Description |
|---|---|---|
$name |
`FsmStateEnum | string` |
$onEntryCallbacks |
`array<int, TransitionCallback> | Collection<int, TransitionCallback>` |
$onExitCallbacks |
`array<int, TransitionCallback> | Collection<int, TransitionCallback>` |
$description |
?string |
An optional description of the state. |
$type |
string |
The type of the state. Defaults to self::TYPE_INTERMEDIATE. |
$category |
?string |
An optional category for grouping states. |
$behavior |
string |
Defines the behavior of the state. Defaults to self::BEHAVIOR_PERSISTENT. |
$metadata |
array<string, mixed> |
Additional metadata associated with the state. |
$isTerminal |
bool |
Indicates whether the state is terminal (final) or not. Defaults to false. |
$priority |
int |
The priority of the state, which determines the order of processing. Defaults to 50. |
$childStateMachine |
?FsmRuntimeDefinition |
An optional child state machine encapsulated within this state. |
$parentState |
?string |
An optional reference to the parent state, forming a hierarchical relationship. |
The constructor sets the initial state properties and establishes relationships with potential child and parent states. Upon calling the constructor, the following actions are performed:
This structure not only organizes state-related information but also effectively manages complex state transition scenarios, making it particularly useful for developers building dynamic applications that rely on an FSM framework.
use Fsm\Data\HierarchicalStateDefinition;
use Fsm\Contracts\MyStateEnum;
$state = new HierarchicalStateDefinition(
name: MyStateEnum::INITIAL,
onEntryCallbacks: [$this->handleInitialEntry()],
onExitCallbacks: [$this->handleInitialExit()],
description: 'The initial state of the workflow.',
childStateMachine: $childStateDefinition, // Assuming $childStateDefinition is defined
parentState: null // This is a root state
);
The HierarchicalStateDefinition class serves as a foundational component in building complex state machines that require organized and manageable state hierarchies. By leveraging this class, developers can create robust applications that effectively handle various states and transitions, enhancing the overall software architecture.
How can I help you explore Laravel packages today?