izzum\statemachine\StateMachine::handle PHP Метод

handle() публичный Метод

If the event is applicable for a transition then that transition from the current state will be applied. If there are multiple transitions possible for the event, the transitions will be tried until one of them is possible. This type of (event/trigger) handling is found in mealy machines.
public handle ( string $event, string $message = null ) : boolean
$event string in case the transition will be triggered by an event code (mealy machine) this will also match on the transition name (_to_)
$message string optional message. this can be used by the persistence adapter to be part of the transition history to provide extra information about the transition.
Результат boolean true in case a transition was triggered by the event, false otherwise
    public function handle($event, $message = null)
    {
        $transitioned = false;
        $transitions = $this->getCurrentState()->getTransitionsTriggeredByEvent($event);
        foreach ($transitions as $transition) {
            $transitioned = $this->performTransition($transition, $message);
            if ($transitioned) {
                break;
            }
        }
        return $transitioned;
    }

Usage Example

     echo "'" . $transition->getName() . "' aka event '" . $transition->getEvent() . "'" . PHP_EOL;
 }
 echo PHP_EOL;
 //get input from the user
 $event = readline("Enter an event or transition name: ");
 try {
     $status = 0;
     $transitioned = false;
     //we allow transitions by name or by event
     if (strstr($event, '_to_')) {
         $transitioned = $machine->transition($event);
         if ($transitioned) {
             $status = 1;
         }
     } else {
         $transitioned = $machine->handle($event);
         if ($transitioned) {
             $status = 2;
         }
     }
 } catch (\Exception $e) {
     //for instance, when providing a bad transition name.
     echo "An exception occured: " . $e->getMessage() . PHP_EOL;
 }
 //check what happened
 switch ($status) {
     case 1:
         echo "--- transition by transition name '{$event}' succesful";
         break;
     case 2:
         echo "--- transition by event name '{$event}' succesful";