Created
August 20, 2015 15:09
-
-
Save azraai/8b6129e68c23b026db65 to your computer and use it in GitHub Desktop.
Implementing Finite in Eloquent
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use Finite\StateMachine\StateMachine; | |
use Finite\State\State; | |
use Finite\State\StateInterface; | |
trait EloquentFinite | |
{ | |
protected $states = []; | |
protected $transitions = []; | |
protected $state_machine; | |
public function setFiniteState($state) | |
{ | |
$this->attributes['state'] = $state; | |
} | |
public function getFiniteState($state) | |
{ | |
return $this->attributes['state']; | |
} | |
public function bootEloquentFinite() | |
{ | |
if(count($this->states) == 0) { | |
throw new Exception("No states define", 1); | |
} | |
if(count($this->transitions) == 0) { | |
throw new Exception("No transition define", 1); | |
} | |
$this->state_machine = new StateMachine(); | |
$this->state_machine->addState(new State($this->states[0]), StateInterface::TYPE_INITIAL); | |
foreach(array_slice($this->states, 1) as $state) { | |
$this->state_machine->addState($state); | |
} | |
foreach($this->transitions as $transition) { | |
$this->state_machine->addTransition($transition['name'], $transition['from'], $transition['to']); | |
} | |
$this->state_machine->setObject($this); | |
$this->state_machine->initialize(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment