Code Monkey home page Code Monkey logo

finite's Introduction

Finite, A Simple PHP Finite State Machine

Finite is a Simple State Machine, written in PHP. It can manage any Stateful object by defining states and transitions between these states.

Build Status Latest Stable Version Total Downloads License Scrutinizer Code Quality Code Coverage SensioLabsInsight Dependency Status Reference Status Gitter

Features

  • Managing State/Transition graph for an object
  • Defining and retrieving properties for states
  • Event Listenable transitions
  • Symfony2 integration
  • Custom state graph loaders
  • Twig Extension

Documentation

Documentation for master (1.1)

Getting started

Installation (via composer)

{
      "require": {
        "yohang/finite": "~1.1"
    }
}

Version note :

If your are using this library in a Symfony project, 1.1 version is only compatible with Symfony >=2.6. 1.0 is compatible with Symfony >=2.3, <2.6.

Define your Stateful Object

Your stateful object just need to implement the StatefulInterface Interface.

use Finite\StatefulInterface;

class Document implements StatefulInterface
{
        private $state;
        public function setFiniteState($state)
        {
                $this->state = $state;
        }

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

Initializing a simple StateMachine

use Finite\StateMachine\StateMachine;
use Finite\State\State;
use Finite\State\StateInterface;

// $document = retrieve your stateful object

$sm = new StateMachine();

// Define states
$sm->addState(new State('s1', StateInterface::TYPE_INITIAL));
$sm->addState('s2');
$sm->addState('s3');
$sm->addState(new State('s4', StateInterface::TYPE_FINAL));

// Define transitions
$sm->addTransition('t12', 's1', 's2');
$sm->addTransition('t23', 's2', 's3');
$sm->addTransition('t34', 's3', 's4');
$sm->addTransition('t42', 's4', 's2');

// Initialize
$sm->setObject($document);
$sm->initialize();

// Retrieve current state
$sm->getCurrentState();

// Can we process a transition ?
$sm->can('t34');

finite's People

Contributors

yohang avatar winzou avatar navitronic avatar padam87 avatar ronrademaker avatar liuggio avatar tortuetorche avatar dannykopping avatar k-phoen avatar realshadow avatar arikal avatar defrag avatar nidup avatar pborreli avatar reiz avatar acorncom avatar ksn135 avatar tompedals avatar

Watchers

James Cloos avatar Maxime Veber avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.