This is a fairly simple state machine module for Kohana 3. It can be used standalone or with ORM.
// Array of valid states, and the list of valid transitions from that state.
$transitions = array(
'pending' => array('active', 'rejected'),
'active' => array('deleted'),
'rejected' => array('deleted'),
'deleted' => array(),
);
// Array of callbacks to be executed when transitioning to a state.
$transition_callbacks = array(
'rejected' => array($this, 'transition_to_rejected_callback'),
);
// The intial state
$initial_state = 'pending';
// Initialize the StateMachine
$state_machine = StateMachine::factory($initial_state, array(
'transitions' => $transitions,
'transition_callbacks' => $transition_callbacks,
));
$state_machine->state();
$state_machine->can_transition('deleted');
$state_machine->can_transition('deleted', 'pending');
$state_machine->can_transition('active');
$state_machine->generate_diagram('png');
class Model_User extends ORM_StateMachine {
/**
* @var string Initial State
*/
protected $_initial_state = 'pending';
/**
* @var string Column to use for state management
*/
protected $_state_column = 'state';
/**
* Returns the transitions array
*
* @return array
*/
public function transitions()
{
return array(
'pending' => array('active', 'rejected'),
'active' => array('deleted'),
'rejected' => array('deleted'),
'deleted' => array(),
);
}
/**
* Returns the transition callbacks array
*
* @return array
*/
public function transition_callbacks()
{
return array(
'rejected' => array($this, 'transition_to_rejected_callback'),
);
}
/**
* Callback triggered when a user is rejected
*
* @return array
*/
public function transition_to_rejected_callback($state_to, $state_from)
{
// Do something... Maybe email a rejection letter?
// Remember that this is called regardless of if the model has been
// saved or not!
}
}
$model->state();
$model->can_transition('deleted');
$model->can_transition('deleted', 'pending');
$model->transition('active')->save();