1. Go to this page and download the library: Download star/state-machine library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
star / state-machine example snippets
// your context using the builder
public function isActive()
{
return $this->stateMachine()-hasAttribute("is_active");
}
// your context using the metadata
public function isActive()
{
return $this->state->hasAttribute("is_active");
}
class Post
{
/**
* @var string
*/
private $state;
public function publish()
{
$this->state = $this->stateMachine()->transit("publish", $this);
}
public function archive()
{
$this->state = $this->stateMachine()->transit("archive", $this);
}
public function unarchive()
{
$this->state = $this->stateMachine()->transit("unarchive", $this);
}
public function isClosed()
{
return $this->stateMachine()->hasAttribute("is_closed");
}
/**
* @return StateMachine
*/
private function stateMachine()
{
return StateBuilder::build()
->allowTransition("publish", "draft", "published")
->allowTransition("archive", "published", "archived")
->allowTransition("unarchive", "published", "draft")
->addAttribute("is_closed", ["archived", "drafted"])
->create($this->state);
}
}
final class MyStateWorkflow extends StateMetadata
{
protected function __construct()
{
parent::__construct('pending');
}
protected function createMachine(StateBuilder $builder)
{
$builder->allowTransition("publish", "draft", "published")
$builder->allowTransition("archive", "published", "archived")
$builder->allowTransition("unarchive", "published", "draft")
$builder->addAttribute("is_closed", ["archived", "drafted"])
}
}
class Post
{
/**
* @var string
*/
private $state;
public function __construct()
{
$this->>state = new MyStateWorkflow();
}
public function publish()
{
$this->state = $this->state->transit("publish", $this);
}
public function archive()
{
$this->state = $this->state->transit("archive", $this);
}
public function unarchive()
{
$this->state = $this->state->transit("unarchive", $this);
}
public function isClosed()
{
return $this->state->hasAttribute("is_closed");
}
}
$stateMachine->addListener(
StateEventStore::BEFORE_TRANSITION,
function(TransitionWasRequested $event) {
// do something
}
);
$this->state->transit("transition", $this, new DoSomethingOnSuccessIfConditionMatches());
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.