class StateMachine_Event in State Machine 7.2
Same name and namespace in other branches
- 6 inc/base.inc \StateMachine_Event
- 7.3 inc/base.inc \StateMachine_Event
- 7 inc/base.inc \StateMachine_Event
Hierarchy
- class \StateMachine_Event
Expanded class hierarchy of StateMachine_Event
File
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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
StateMachine_Event:: |
public | property | ||
StateMachine_Event:: |
protected | property | ||
StateMachine_Event:: |
protected | property | ||
StateMachine_Event:: |
public | function | Can this event be used to transition from the specified state? | |
StateMachine_Event:: |
public | function | Execute the event. | |
StateMachine_Event:: |
public | function | Allow the event to finish after the machine has changed state. | |
StateMachine_Event:: |
public | function | Return a specific key value from the $options array | |
StateMachine_Event:: |
public | function | Return the $options array | |
StateMachine_Event:: |
public | function | Validate that the given event can take place. | 1 |
StateMachine_Event:: |
public | function |