You are here

class StateMachine_Event in State Machine 7.2

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

Hierarchy

Expanded class hierarchy of StateMachine_Event

File

inc/base.inc, line 270
base.inc

View source
class StateMachine_Event {
  public $key;
  protected $machine;
  protected $options;
  public function __construct($key, $machine, $options = array()) {
    $this->key = $key;
    $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;
  }

  /**
   * Return the $options array
   */
  public function get_options() {
    return $this->options;
  }

  /**
   * Return a specific key value from the $options array
   */
  public function get_option($key) {
    return array_key_exists($key, $this->options) ? $this->options[$key] : FALSE;
  }

  /**
   * Validate that the given event can take place.
   */
  public function validate() {

    // Check that the current state is a valid origin for the given transition.
    if (!in_array($this->machine
      ->get_current_state(), $this->options['origin'])) {
      return FALSE;
    }

    // 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.
   */
  public function execute() {
    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::$key public property
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.
StateMachine_Event::finish public function Allow the event to finish after the machine has changed state.
StateMachine_Event::get_option public function Return a specific key value from the $options array
StateMachine_Event::get_options public function Return the $options array
StateMachine_Event::validate public function Validate that the given event can take place. 1
StateMachine_Event::__construct public function