Original file: src/Fsm/Data/TransitionInput.php
The TransitionInput class serves as a Data Transfer Object (DTO) designed to facilitate the passage of consistent input to various transition lifecycle hooks, such as guards, actions, and callbacks within a finite state machine (FSM) implementation. It is enhanced with readonly properties to ensure immutability and employs typed constants to improve type safety and assist in static analysis.
This class is essential for handling transitions in the FSM while providing a standardized structure for input parameters that govern transition behaviors.
| Property Name | Type | Description |
|---|---|---|
$model |
Model |
The model being transitioned. |
$fromState |
`FsmStateEnum | string |
$toState |
`FsmStateEnum | string |
$context |
`ArgonautDTOContract | null` |
$event |
`string | null` |
$isDryRun |
bool |
Indicates whether this is a simulation run. |
$mode |
string |
The transition execution mode, using predefined constants. |
$source |
string |
The source that initiated the transition. |
$metadata |
array<string, mixed> |
Additional metadata for the transition. |
$timestamp |
`\DateTimeInterface | null` |
public function __construct(
Model|array $model,
FsmStateEnum|string|null $fromState = null,
FsmStateEnum|string|null $toState = null,
ArgonautDTOContract|array|null $context = null,
?string $event = null,
bool $isDryRun = false,
string $mode = self::MODE_NORMAL,
string $source = self::SOURCE_USER,
array $metadata = [],
?\DateTimeInterface $timestamp = null
)
The constructor initializes a new instance of TransitionInput, allowing either direct model input or an associative array of parameters.
$model: The model being transitioned, or an associative array containing the model attributes.$fromState: The state being transitioned from (optional).$toState: The state being transitioned to (required).$context: Additional context data for the transition (optional).$event: Optional event that triggered the transition.$isDryRun: Whether this is a simulation run (default: false).$mode: The transition execution mode (default: self::MODE_NORMAL).$source: The source that initiated the transition (default: self::SOURCE_USER).$metadata: Additional metadata for the transition (default: empty array).$timestamp: When the transition was initiated (optional).The constructor normalizes the input arguments by either constructing the object using positional parameters or from an associative array if applicable. It ensures that required parameters, like $toState, are validated based on the specified transition mode. Furthermore, it hydrates the context only after preparing attributes.
If an invalid state transition is attempted, an InvalidArgumentException is thrown.
protected static function hydrateContext(ArgonautDTOContract|array|null $context): ?ArgonautDTOContract
Hydrates the context from either an instance of ArgonautDTOContract or an associative array containing class name and payload.
$context: The context data to be hydrated.Returns an instance of ArgonautDTOContract or null if hydration fails.
This method checks if the given context is an instance of ArgonautDTOContract. If it's an array, it attempts to instantiate the DTO class defined within the array, passing the payload for correct reconstruction. Proper exceptions are thrown if class does not exist or fails to implement the required contract.
public function contextPayload(): ?array
Retrieves the payload from the context in structured format.
Returns an associative array with keys class and payload, or null if the context is not set or serialization fails.
This method converts the context to an array format while ensuring it is indeed an array. If the conversion fails, it logs an error to help with diagnostics without interrupting the transition process.
public function isDryRun(): bool
Checks if the transition is a dry run.
Returns a boolean indicating whether the transition is in dry run mode.
public function isForced(): bool
Determines if the transition should bypass any guards.
Returns a boolean indicating whether the transition is forced.
public function isSilent(): bool
Checks if the transition should occur with minimal logging or events.
Returns a boolean indicating whether the transition is silent.
public function getSource(): string
Retrieves the source that initiated the transition.
Returns a string representing the transition source.
public function getMetadata(string $key, mixed $default = null): mixed
Retrieves a specific metadata value.
$key: The metadata key to retrieve.$default: Optional default value to return if the key does not exist.Returns the value associated with the key or the default if the key doesn't exist.
public function hasMetadata(string $key): bool
How can I help you explore Laravel packages today?