You are here

class StateMachine_Event in State Machine 7.3

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

Base class for events.

Hierarchy

Expanded class hierarchy of StateMachine_Event

File

inc/base.inc, line 342
Defines the base classes of the state machine.

View source
class StateMachine_Event {
  public $name;
  protected $machine;
  protected $options;

  /**
   * Instantiate event.
   *
   * @param string $name
   *   The machine readable name of the state.
   * @param StateMachine $machine
   *   The related machine.
   * @param array $options
   *   The options array.
   */
  public function __construct($name, $machine, $options = array()) {
    $this->name = $name;
    $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;
    $this->title = isset($this->options['title']) ? $this->options['title'] : $name;
  }

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

  /**
   * Return a specific key value from the $options array.
   *
   * @param string $key
   *   The options key.
   *
   * @return mixed
   *   The value of the option or FALSE if the option wasn't found.
   */
  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']);
    }
  }

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

  /**
   * Returns the target state of this event.
   *
   * @return \StateMachine_State
   *   The target state of this event.
   */
  public function get_target_state() {
    return $this->machine
      ->get_state($this
      ->get_option('target'));
  }

}

Members

Namesort descending Modifiers Type Description Overrides
StateMachine_Event::$machine protected property
StateMachine_Event::$name public property
StateMachine_Event::$options protected property
StateMachine_Event::can_transition_from public function Evaluates if this event can 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::get_target_state public function Returns the target state of this event.
StateMachine_Event::validate public function Validate that the given event can take place. 1
StateMachine_Event::__construct public function Instantiate event.