class StateMachine_Event in State Machine 7.3
Same name and namespace in other branches
- 6 inc/base.inc \StateMachine_Event
- 7 inc/base.inc \StateMachine_Event
- 7.2 inc/base.inc \StateMachine_Event
Base class for events.
Hierarchy
- class \StateMachine_Event
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
Name![]() |
Modifiers | Type | Description | Overrides |
---|---|---|---|---|
StateMachine_Event:: |
protected | property | ||
StateMachine_Event:: |
public | property | ||
StateMachine_Event:: |
protected | property | ||
StateMachine_Event:: |
public | function | Evaluates if this event can 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 | Returns the target state of this event. | |
StateMachine_Event:: |
public | function | Validate that the given event can take place. | 1 |
StateMachine_Event:: |
public | function | Instantiate event. |