2017 © Pedro Peláez
 

project state-machine

A simple finite state machine with minimal dependencies

image

carlescliment/state-machine

A simple finite state machine with minimal dependencies

  • Tuesday, October 1, 2013
  • by carlescliment
  • Repository
  • 1 Watchers
  • 4 Stars
  • 2,828 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 5 Versions
  • 11 % Grown

The README.md

State Machine

This is a simple, no-deps PHP state machine., (*1)

Installation

Update your composer.json file adding the following dependency and run php composer.phar update carlescliment/state-machine:, (*2)

    "carlescliment/state-machine": "0.0.4"

Usage

Implementing a state machine is straight-forward. You need four components; a statable object, states, transitions and the state machine., (*3)

<?php

use carlescliment\StateMachine\Model\Statable,
    carlescliment\StateMachine\Model\StateMachine,
    carlescliment\StateMachine\Model\Transition;


class SemaphoreStates
{
    const OPEN = 'semaphore.open';
    const WARNING = 'semaphore.warning';
    const LOCKED = 'semaphore.locked';

    public static function all()
    {
        return array(self::OPEN, self::WARNING, self::LOCKED);
    }
}


class SemaphoreTransitions
{
    const GIVE_PASS = 'semaphore.give_pass';
    const WARN = 'semaphore.warn';
    const LOCK = 'semaphore.lock';
}


class Semaphore implements Statable
{
    private $state = SemaphoreStates::LOCKED;

    public function getState()
    {
        return $this->state;
    }

    public function setState($state)
    {
        $this->state = $state;
        return $this;
    }
}


class SemaphoreStateMachine extends StateMachine
{
    public function __construct()
    {
        parent::construct();

        foreach (SemaphoreStates::all() as $state) {
            $this->addState($state);
        }

        $transitions = array(
            new Transition(AuctionTransitions::GIVE_PASS, AuctionStates::LOCKED, AuctionStates::OPEN),
            new Transition(AuctionTransitions::GIVE_PASS, AuctionStates::WARNING, AuctionStates::OPEN),
            new Transition(AuctionTransitions::WARN, AuctionStates::OPEN, AuctionStates::WARNING),
            new Transition(AuctionTransitions::WARN, AuctionStates::LOCKED, AuctionStates::WARNING),
            new Transition(AuctionTransitions::LOCK, AuctionStates::OPEN, AuctionStates::LOCKED),
            new Transition(AuctionTransitions::LOCK, AuctionStates::WARNING, AuctionStates::LOCKED),
            );
        foreach ($transitions as $transition) {
            $this->addTransition($transition);
        }
    }
}

Now, you can operate with your state machine:, (*4)

<?php

$semaphore = new Semaphore;

$machine = new SemaphoreStateMachine;
$machine->execute(SemaphoreTransitions::GIVE_PASS, $semaphore);
$machine->execute(SemaphoreTransitions::WARN, $semaphore);
$machine->execute(SemaphoreTransitions::LOCK, $semaphore);

The Versions

01/10 2013

dev-master

9999999-dev

A simple finite state machine with minimal dependencies

  Sources   Download

MIT

The Requires

  • php >=5.3.2

 

state machine state machine

24/09 2013

0.0.4

0.0.4.0

A simple finite state machine with minimal dependencies

  Sources   Download

MIT

The Requires

  • php >=5.3.2

 

state machine state machine

24/09 2013

0.0.4-alpha

0.0.4.0-alpha

A simple finite state machine with minimal dependencies

  Sources   Download

MIT

The Requires

  • php >=5.3.2

 

state machine state machine

05/08 2013

0.0.3

0.0.3.0

A simple finite state machine with minimal dependencies

  Sources   Download

MIT

The Requires

  • php >=5.3.2

 

state machine state machine

02/08 2013

0.0.1

0.0.1.0

A simple finite state machine with minimal dependencies

  Sources   Download

MIT

The Requires

  • php >=5.3.2

 

state machine state machine