You are here

class StateMachine_Event in State Machine 6

Same name and namespace in other branches
  1. 7.3 inc/base.inc \StateMachine_Event
  2. 7 inc/base.inc \StateMachine_Event
  3. 7.2 inc/base.inc \StateMachine_Event

Hierarchy

Expanded class hierarchy of StateMachine_Event

File

inc/base.inc, line 236

View source
class StateMachine_Event {
  protected $machine;
  protected $options;
  public function __construct($machine, $options = array()) {
    $this->machine = $machine;

    // Normalize the origin option.
    $origin = empty($options['origin']) ? array() : $options['origin'];
    $options['origin'] = is_array($origin) ? $origin : array(
      $origin,
    );
    $this->options = $options;
  }

  /**
   * Validate the transition by processing the guard condition.
   */
  protected function validate() {

    // Execute guard condition if it exists.
    if (!empty($this->options['guard'])) {
      if (call_user_func($this->options['guard'], $this) === FALSE) {
        return FALSE;
      }
    }
    return TRUE;
  }

  /**
   * Execute the event. Tests to make sure the current state is a valid origin
   * state, checks the guard condition,
   */
  public function execute() {
    if (!in_array($this->machine
      ->get_current_state(), $this->options['origin'])) {
      return FALSE;
    }
    if (!$this
      ->validate()) {
      return FALSE;
    }
    if (!empty($this->options['before_transition'])) {
      call_user_func($this->options['before_transition']);
    }
    return $this->options['target'];
  }

  /**
   * Allow the event to finish after the machine has changed state.
   */
  public function finish() {
    if (!empty($this->options['after_transition'])) {
      call_user_func($this->options['after_transition']);
    }
  }

  /**
   * Can this event be used to transition from the specified state?
   */
  public function can_transition_from($state) {
    return in_array($state, $this->options['origin']);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
StateMachine_Event::$machine protected property
StateMachine_Event::$options protected property
StateMachine_Event::can_transition_from public function Can this event be used to transition from the specified state?
StateMachine_Event::execute public function Execute the event. Tests to make sure the current state is a valid origin state, checks the guard condition,
StateMachine_Event::finish public function Allow the event to finish after the machine has changed state.
StateMachine_Event::validate protected function Validate the transition by processing the guard condition. 1
StateMachine_Event::__construct public function