Original file: src/Fsm/Guards/CompositeGuard.php
The CompositeGuard class is a manager for executing a collection of guards during state transitions in a finite state machine (FSM). Its primary purpose is to offer advanced composition and execution strategies for guards, allowing them to be evaluated according to various strategies such as priority-based execution and short-circuit evaluation. The class raises exceptions with enhanced error reporting when guards fail, aiding in debugging and state management.
namespace Fsm\Guards;
use Fsm\Data\TransitionGuard;
use Fsm\Data\TransitionInput;
use Fsm\Exceptions\FsmTransitionFailedException;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\App;
use Throwable;
TransitionGuard instances used for evaluation.public function __construct(
private readonly Collection $guards,
private readonly string $evaluationStrategy = self::STRATEGY_ALL_MUST_PASS,
)
Initializes the composite guard with a collection of guards and an optional evaluation strategy.
Collection<int, TransitionGuard> $guards: A collection containing guard objects.string $evaluationStrategy: The strategy to evaluate the guards (default is self::STRATEGY_ALL_MUST_PASS).public static function create(array $guards, string $strategy = self::STRATEGY_ALL_MUST_PASS): self
Creates a new instance of the CompositeGuard class with a given array of guards and evaluation strategy.
array<TransitionGuard> $guards: An array of guard instances.string $strategy: The evaluation strategy to use.CompositeGuard.public function evaluate(TransitionInput $input, string $columnName): bool
Evaluates all the instantiated guards based on the specified evaluation strategy.
TransitionInput $input: The input containing transition data used for evaluating the guards.string $columnName: The name of the FSM state column (e.g., 'status').true if the evaluation passes, otherwise throws FsmTransitionFailedException.public function count(): int
Retrieves the count of guards in the composite guard.
public function getStrategy(): string
Returns the evaluation strategy currently being used by the composite guard.
public function getGuards(): Collection
Retrieves all the guards in the composite, ordered by their priority.
TransitionGuard instances sorted by priority.private function evaluateAllMustPass(TransitionInput $input, string $columnName): bool
Evaluates all guards, requiring that each must pass for the evaluation to succeed.
true if all guards pass; otherwise, throws FsmTransitionFailedException.private function evaluateAnyMustPass(TransitionInput $input, string $columnName): bool
Evaluates the guards such that at least one must pass for the evaluation to succeed.
true if at least one guard passes; otherwise, throws FsmTransitionFailedException.private function evaluatePriorityFirst(TransitionInput $input, string $columnName): bool
Executes guards in priority order until one returns true or all fail.
true on the first successful guard; throws FsmTransitionFailedException if all guards fail.private function executeGuard(TransitionGuard $guard, TransitionInput $input): mixed
Executes an individual guard and returns its result.
TransitionGuard $guard: The guard to execute.TransitionInput $input: The input data used for the execution.private function executeCallableWithInstance(mixed $callable, array $parameters): mixed
Executes a callable which may include an object instance, ensuring correct parameter mapping.
mixed $callable: The callable to execute, either as an array containing an object instance and method name or just a function/closure.array<string, mixed> $parameters: The parameters to pass to the callable.private function formatGuardDescription(TransitionGuard $guard): string
Formats and returns a description for a given guard for error message reporting.
TransitionGuard $guard: The guard instance to format.private function getStateValue(mixed $state): ?string
Converts a state value into its string representation for cleaner logging and exception messages.
mixed $state: The state value to convert.null.This documentation provides a comprehensive overview of the CompositeGuard class, detailing its methods, functionality, and purpose within the system. By understanding this documentation, developers can gain insights into the workings of FSM guard evaluation and can effectively utilize the CompositeGuard class in their applications.
How can I help you explore Laravel packages today?