2017 © Pedro Peláez
 

library state-machine

Simple state machine

image

mstovicek/state-machine

Simple state machine

  • Tuesday, January 16, 2018
  • by mstovicek
  • Repository
  • 1 Watchers
  • 0 Stars
  • 3 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 1 Versions
  • 0 % Grown

The README.md

Objection

Create a library which will be validating transitions between states and implement the following graph of advert states using it:, (*1)

               ,---------.
      |------> | Limited |
      |        `---------'
      |            |
      |            ˅
   ,-----.     ,--------.     ,----------.     ,---------.
   | New | --> | Active | --> | Outdated | --> | Removed |
   `-----'     `--------'     `----------'     `---------'
                   ˄               |
                   |---------------|

Package is for demonstration purpose only!, (*2)

Usage

Install as a project dependency

$ composer require mstovicek/state-machine

Classes and methods

\StateMachine\StateMachine implements \StateMachine\StateMachineInterface with following methods: - allowStates - accepts array of allowed states as an only parameter, transitions are allowed only for such states - allowTransition - accepts 2 parameters - from and to - and registers a transition between them - canTransit - accepts 2 parameters - from and to and return boolean true if such transition is allowed, false otherwise, (*3)

Developer usage

Install dependencies

$ composer install

Run tests

$ composer tests

Run example

$ cd examples/user-account

# install dependencies, incl. mstovicek/state-machine
$ composer install

# get current state
$ php index.php get

# set state
$ php index.php set <state>

# reset state to default
$ php index.php reset

Example output:

$ php index.php get
Current state: new

$ php index.php set active
State was set to: active

$ php index.php get
Current state: active

$ php index.php set outdated
State was set to: outdated

$ php index.php get
Current state: outdated

$ php index.php set limited
Cannot set state to limited! Check current state and allowed transitions.

$ php index.php get
Current state: outdated

$ php index.php set removed
State was set to: removed

$ php index.php get
Current state: removed
This state is terminal.

$ php index.php set active
Cannot set state to active! Check current state and allowed transitions.

$ php index.php reset
State was reset to default

$ php index.php get
Current state: new

The Versions

16/01 2018

dev-master

9999999-dev

Simple state machine

  Sources   Download

The Development Requires

by Milan Stovicek