Download the PHP package carlescliment/state-machine without Composer
On this page you can find all versions of the php package carlescliment/state-machine. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Table of contents
Download carlescliment/state-machine
More information about carlescliment/state-machine
Files in carlescliment/state-machine
Download carlescliment/state-machine
More information about carlescliment/state-machine
Files in carlescliment/state-machine
Vendor carlescliment
Package state-machine
Short Description A simple finite state machine with minimal dependencies
License MIT
Package state-machine
Short Description A simple finite state machine with minimal dependencies
License MIT
Please rate this library. Is it a good library?
Informations about the package state-machine
State Machine
This is a simple, no-deps PHP state machine.
Installation
Update your composer.json
file adding the following dependency and run php composer.phar update carlescliment/state-machine
:
"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.
<?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:
<?php
$semaphore = new Semaphore;
$machine = new SemaphoreStateMachine;
$machine->execute(SemaphoreTransitions::GIVE_PASS, $semaphore);
$machine->execute(SemaphoreTransitions::WARN, $semaphore);
$machine->execute(SemaphoreTransitions::LOCK, $semaphore);
All versions of state-machine with dependencies
PHP Build Version
Package Version
Requires
php Version
>=5.3.2
The package carlescliment/state-machine contains the following files
Loading the files please wait ....