PHPackages                             ziguss/fsm - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. ziguss/fsm

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

ziguss/fsm
==========

a PHP final state machine library

08PHP

Since Aug 17Pushed 9y ago1 watchersCompare

[ Source](https://github.com/ziguss/fsm)[ Packagist](https://packagist.org/packages/ziguss/fsm)[ RSS](/packages/ziguss-fsm/feed)WikiDiscussions master Synced 3w ago

READMEChangelogDependenciesVersions (1)Used By (0)

A PHP Finite State Machine Library
==================================

[](#a-php-finite-state-machine-library)

It's miniature and independent but powerful enough.

[![Build Status](https://camo.githubusercontent.com/6d1d23b5d57422f63d984daf5c67d33223e8d130c0dc66905b4e4515a7919f2a/68747470733a2f2f7472617669732d63692e6f72672f7a69677573732f66736d2e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/ziguss/fsm)

Getting started
---------------

[](#getting-started)

### Define your Stateful Object

[](#define-your-stateful-object)

```
use ziguss\fsm\StatefulInterface;
use ziguss\fsm\StateMachine;
use ziguss\fsm\TransitionEvent;

class Task implements StatefulInterface
{
    /**
     * @var StateMachine
     */
    private $sm;

    /**
     * @var string
     */
    private $state = 'unassigned';

    /**
     * {@inheritdoc}
     */
    public function getFiniteState()
    {
        return $this->state;
    }

    /**
     * {@inheritdoc}
     */
    public function setFiniteState($state)
    {
        $this->state = $state;
    }

    /**
     * @return StateMachine
     */
    public function getStateMachine()
    {
        if (null === $this->sm) {
            $this->sm = new StateMachine($this, array(
                'graph' => 'task',
                'states' => array('unassigned', 'assigned', 'done'),
                'transitions' => array(
                    'assign' => array(
                        'from' => array('unassigned', 'assigned'),
                        'to' => 'assigned',
                    ),
                    'unAssign' => array(
                        'from' => 'assigned',
                        'to' => 'unassigned',
                    ),
                    'finish' => array(
                        'from' => 'assigned',
                        'to' => 'done',
                        'condition' => array($this, 'justMe'),
                    ),
                ),
                'listeners' => array(
                    'test' => array(
                        array($this, 'testCondition'),
                    ),
                    'after' => array(
                        array('on' => 'assign', 'do' => array($this, 'emailSomeone')),
                    ),
                ),
            ));
        }

        return $this->sm;
    }

    /**
     * @param TransitionEvent $event
     */
    public function emailSomeone(TransitionEvent $event)
    {
        echo 'You have new work to do.';
    }

    /**
     * @param TransitionEvent $event
     */
    public function testCondition(TransitionEvent $event)
    {
        $config = $event->getConfig();
        if (!empty($config['condition'])) {
            $event->setRejected(!call_user_func($config['condition']));
        }
    }

    /**
     * @return bool
     */
    public function justMe()
    {
        // your can check if the task is assigned to someone
        return true;
    }
}
```

### Using it

[](#using-it)

```
$task = new Task();

// Retrieve all current enabled transitions
$transitions = $task->getStateMachine()->getEnabledTransitions();

// pick one and apply
$task->getStateMachine()->apply($transitions[array_rand($transitions)]);

// Retrieve current state by state machine
$task->getStateMachine()->getState();
```

###  Health Score

20

—

LowBetter than 13% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity4

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity41

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

### Community

Maintainers

![](https://www.gravatar.com/avatar/8456da57db81bdbf2c3164f81790fed5e6e08d81555116eefa2037d6e90615af?d=identicon)[ziguss](/maintainers/ziguss)

---

Top Contributors

[![ziguss](https://avatars.githubusercontent.com/u/1955188?v=4)](https://github.com/ziguss "ziguss (2 commits)")

### Embed Badge

![Health badge](/badges/ziguss-fsm/health.svg)

```
[![Health](https://phpackages.com/badges/ziguss-fsm/health.svg)](https://phpackages.com/packages/ziguss-fsm)
```

###  Alternatives

[fortawesome/font-awesome

The iconic font, CSS, and SVG framework

76.7k7.5M141](/packages/fortawesome-font-awesome)[kartik-v/yii2-builder

Build forms (single-row or multi-row/tabular) easily for Yii Framework 2.0

961.2M33](/packages/kartik-v-yii2-builder)[htmlburger/carbon-fields-yoast

Helper library that allows developers to choose which fields from Carbon Fields to include in the Yoast readability score meter

114.2k](/packages/htmlburger-carbon-fields-yoast)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
