2017 © Pedro Peláez
 

library state-machine

State machine

image

systream/state-machine

State machine

  • Tuesday, November 1, 2016
  • by systream
  • Repository
  • 1 Watchers
  • 1 Stars
  • 43 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 6 Versions
  • 5 % Grown

The README.md

State machine

Installation

You can install this package via packagist.org with composer., (*1)

composer require systream/state-machine, (*2)

composer.json:, (*3)

"require": {
    "systream/state-machine": "1.*"
}

Usage examples

Setup

You need to add transitions to state machine., (*4)

$stateMachine = new StateMachine(new \Systream\EventDispatcher());

$inStock = new StateMachine\State('In stock');
$ordered = new StateMachine\State('Ordered');
$shippingInProcess = new StateMachine\State('Shipping in process');
$deliveredToClient = new StateMachine\State('Order is at client');

$stateMachine->addTransition(
    new GenericTransition('Order'), $inStock, $ordered
);

$stateMachine->addTransition(
    new GenericTransition('Cancel order'), $ordered, $inStock
);

$stateMachine->addTransition(
    new GenericTransition('Shipping'), $ordered, $shippingInProcess
);

$stateMachine->addTransition(
    new GenericTransition('Handover to client'), $shippingInProcess, $deliveredToClient
);

Custom transitions

You have to implement the \Systream\StateMachine\TransitionInterface interface to create custom transition, (*5)

Process Transition

StateObject

To use state machine you need an object which has state. process method expect \Systream\StateMachine\State\StateObjectInterface interface., (*6)

So you need to implement it, or just use \Systream\StateMachine\State\StateObjectTrait., (*7)

Can

Testing, whether it can change the status to the target state, (*8)

$product = new DummyStateObject();
$product->setState($inStock);
$stateMachine->can($product, $ordered); // will return true
$stateMachine->can($product, $deliveredToClient); // will return false

Process

Set project state to In Stock and process it to Ordered., (*9)

$product = new DummyStateObject();
$product->setState($inStock);
$stateMachine->process($product, $ordered);

Set state without transition will trow an \Systream\StateMachine\Exception\CantSetStatusException exception., (*10)

$product = new DummyStateObject();
$product->setState($inStock);
$stateMachine->process($product, $deliveredToClient);

Get available states

$states = $stateMachine->getStates();

It will return array of \Systream\StateMachine\State\StateInterface objects, (*11)

Get next states

This method will return of the next possible states of an state object:, (*12)

$product = new DummyStateObject();
$states = $stateMachine->getNextStates($product);

It will return array of \Systream\StateMachine\State\StateInterface objects, (*13)

Visualization

With this library you are able to generate an image with the states and transitions. To get this work you need to install graphviz., (*14)

$doFileGenerator = new StateMachine\DotFileGenerator();
$image = $doFileGenerator->getImage($stateMachine);
file_put_contents('my_flow_chart.png', $image);

example-flow, (*15)

Test

Build Status, (*16)

The Versions

01/11 2016

dev-master

9999999-dev

State machine

  Sources   Download

MIT

The Requires

 

The Development Requires

by Peter Tihanyi

statemachine state machine transition state-machine

20/06 2016

1.1.2

1.1.2.0

State machine

  Sources   Download

MIT

The Requires

 

The Development Requires

by Peter Tihanyi

statemachine state machine transition state-machine

11/06 2016

1.1.1

1.1.1.0

State machine

  Sources   Download

MIT

The Requires

 

The Development Requires

by Peter Tihanyi

statemachine state machine transition state-machine

11/06 2016

1.1.0

1.1.0.0

State machine

  Sources   Download

MIT

The Requires

 

The Development Requires

by Peter Tihanyi

statemachine state machine transition state-machine

05/06 2016

1.0.1

1.0.1.0

State machine

  Sources   Download

MIT

The Requires

 

The Development Requires

by Peter Tihanyi

statemachine transaction state machine state-machine

05/06 2016

1.0.0

1.0.0.0

State machine

  Sources   Download

MIT

The Requires

 

The Development Requires

by Peter Tihanyi

statemachine transaction state machine state-machine