php-extended / php-workflow-object
A library that implements the php-extended/php-workflow-interface package
Requires
- php: >=8.0
- php-extended/php-workflow-interface: ^7
Requires (Dev)
- dev-master
- 7.0.6
- 7.0.5
- 7.0.4
- 7.0.3
- 7.0.2
- 7.0.1
- 7.0.0
- 6.0.8
- 6.0.7
- 6.0.6
- 6.0.5
- 6.0.4
- 6.0.3
- 6.0.2
- 6.0.1
- 6.0.0
- 5.0.1
- 5.0.0
- 4.0.2
- 4.0.1
- 4.0.0
- 3.1.32
- 3.1.31
- 3.1.30
- 3.1.29
- 3.1.28
- 3.1.27
- 3.1.26
- 3.1.25
- 3.1.24
- 3.1.23
- 3.1.22
- 3.1.21
- 3.1.20
- 3.1.19
- 3.1.18
- 3.1.17
- 3.1.16
- 3.1.15
- 3.1.14
- 3.1.13
- 3.1.12
- 3.1.11
- 3.1.10
- 3.1.9
- 3.1.8
- 3.1.7
- 3.1.6
- 3.1.5
- 3.1.4
- 3.1.3
- 3.1.2
- 3.1.1
- 3.1.0
- 3.0.3
- 3.0.2
- 3.0.1
- 3.0.0
- 2.0.0
This package is auto-updated.
Last update: 2024-10-31 00:23:05 UTC
README
A library that implements the php-extended/php-workflow-interface package
Installation
The installation of this library is made via composer and the autoloading of all classes of this library is made through their autoloader.
- Download
composer.phar
from their website. - Then run the following command to install this library as dependency :
php composer.phar php-extended/php-workflow-object ^7
Basic Usage
Lets say you want to express the workflow of a door (which can be opened or closed). You have the following schema :
+--------+ open +--------+
| | ---------> | |
| closed | | opened |
| | <--------- | |
+--------+ close +--------+
Then, you'll have to have the following classes:
use PhpExtended\Workflow\SubjectInterface; use PhpExtended\Workflow\StateInterface; use PhpExtended\Workflow\ConditionInterface; use PhpExtended\Workflow\WorkflowBuilder;
class Door implements SubjectInterface {
public $_state = null;
public function __construct()
{
$this->_state = new Closed();
}
public function getState(WorkflowInterface $workflow) : StateInterface
{
if($workflow->getName() === 'usage')
{
return $this->_state;
}
return new Closed();
}
public function execute(TransitionInterface $transition) : StateInterface
{
if($transition->getName() === 'open')
{
return $this->_state = new Opened();
}
if($transition->getName() === 'close')
{
return $this->_state = new Closed();
}
return new Closed();
}
}
class Opened implements StateInterface; {
public function getName() : string
{
return 'opened';
}
}
class Closed implements StateInterface {
public function getName() : string
{
return 'closed';
}
}
class DoorIsClosed implements ConditionInterface {
public function isSatisfiedBy(SubjectInterface $subject) : bool
{
return $subject instanceof Door
&& $subject->getState() instanceof Closed;
}
}
class DoorIsOpened implements ICondition {
public function isSatisfiedBy(ISubject $subject) : bool
{
return $subject instanceof Door
$subject->getState() instanceof Opened;
}
}
$definition = new Definition('usage'); $opened = new Opened(); $closed = new Closed(); $open = new Transition('open', $opened, $closed); $close = new Transition('close', $closed, $opened); $workflowBuilder->addCondition('open', new DoorIsClosed()); $workflowBuilder->addTransition('close', $opened, $closed); $workflowBuilder->addCondition('close', new DoorIsOpened());
$workflow = $workflowBuilder->getWorkflow('usage');
$door = new Door(); // at instanciation, door is closed $workflow->can($door, $closed); // returns false $workflow->car($door, $opened); // returns true $workflow->perform($door, $opened); // door is now opened $workflow->can($door, $opened); // returns false $workflow->can($door, $closed); // returns true $workflow->perform($door, $closed); // door is now closed
License
MIT (See license file).