PHPackages                             romanfranko/yii2-state-machine - 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. romanfranko/yii2-state-machine

ActiveYii2-extension[Utility &amp; Helpers](/categories/utility)

romanfranko/yii2-state-machine
==============================

Yii2 State Machine

0.1.2(1y ago)00PHP

Since Mar 22Pushed 1y agoCompare

[ Source](https://github.com/roman-franko/yii2-state-machine)[ Packagist](https://packagist.org/packages/romanfranko/yii2-state-machine)[ RSS](/packages/romanfranko-yii2-state-machine/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (1)Versions (3)Used By (0)

Yii2 State Machine
==================

[](#yii2-state-machine)

This package enable state machine usage into attributes of a Model (Active Record).

[![Latest Stable Version](https://camo.githubusercontent.com/ea4e33de1c576951dee24f2cf81f109f2b389a2eced08f611fcc8d678c8bf18a/68747470733a2f2f706f7365722e707567782e6f72672f726f6d616e6672616e6b6f2f796969322d73746174652d6d616368696e652f762f737461626c65)](https://packagist.org/packages/romanfranko/yii2-state-machine)[![Total Downloads](https://camo.githubusercontent.com/59828a2d2828aff0f6ec5ab12b1bb54a3941b8cf8bb57e6a094bbfe8d91b0681/68747470733a2f2f706f7365722e707567782e6f72672f726f6d616e6672616e6b6f2f796969322d73746174652d6d616368696e652f646f776e6c6f6164732e706e67)](https://packagist.org/packages/romanfranko/yii2-state-machine)

Installation
------------

[](#installation)

The preferred way to install this extension is through [composer](http://getcomposer.org/download/).

Either run

```
composer require romanfranko/yii2-state-machine

```

or add

```
"romanfranko/yii2-state-machine": "dev-master"
```

to the `require` section of your composer.json.

Usage
-----

[](#usage)

Model definition:

```
/// Model

namespace app\models;

class User extends \yii\db\ActiveRecord {

    public function modelLabel() {
        return 'User';
    }

    public function behaviors() {
        return \yii\helpers\ArrayHelper::merge(parent::behvaiors(), [
            [
                'class' => romanfranko\state\Machine::class,
                'initial' => 'active', /// Initial status
                'attr' => 'status', /// Attribute that will use this state machine
                'namespace' => 'app\models\status\user', /// Namespace for the Status class definitions
                'model_label' => $this->modelLabel(),
                'transitions' => [
                    'active' => ['inactive', 'disabled'],
                    'inactive' => ['active', 'disabled'],
                    'disabled' => ['inactive']
                ]
            ]
        ]);
    }
}
```

Status definitions:

```
/// Active status
namespace app\models\status\user;

use romanfranko\state\Status;

class Active extends Status {
    public const ID = 'active';
    public $label = 'Active';
    public $labelColor = 'primary';

    public function onExit($id, $event)
    {
        /// event triggered when the status is changed from Active to another status
        return true;
    }

    public function onEntry($id, $event)
    {
        /// event triggered when the status is changed from another status to Active
        return true;
    }

}
```

```
/// Inactive Status
namespace app\models\status\user;

use romanfranko\state\Status;

class Inactive extends Status {
    public const ID = 'inactive';
    public $label = 'Inactive';
    public $labelColor = 'danger';

    public function onExit($id, $event)
    {
        /// event triggered when the status is changed from Inactive to another status
        return true;
    }

    public function onEntry($id, $event)
    {
        /// event triggered when the status is changed from another status to Inactive
        return true;
    }

}
```

```
/// Disabled Status
namespace app\models\status\user;

use romanfranko\state\status;

class Disabled extends Status {
    public const ID = 'disabled';
    public $label = 'Disabled';
    public $labelColor = 'muted';

    public function onExit($id, $event)
    {
        /// event triggered when the status is changed from Disabled to another status
        return true;
    }

    public function onEntry($id, $event)
    {
        /// event triggered when the status is changed from another status to Disabled
        return true;
    }

}
```

### Example:

[](#example)

```
$user = new User();
/// Returns the current status: new Active()
$user->status;
/// Returns the allowed status IDs that can be changed to in this case: ['inactive', 'disabled']
$user->allowedStatusChanges();

/// Returns a boolean value in this case: true
$user->canChangeTo('inactive');
/// in this case: false. Since this status is not defined in the transitions key values.
$user->canChangeTo('unknown');
/// Returns all the defined Status in the Model, in this case:
/// ['active' => new Active(), 'inactive' => new Inactive(), 'disabled' => new Disabled()]
$user->availableStatus();

/// Change from Active to Inactive triggering the events onEntry of inactive and onExit of Active
$user->changeTo('inactive');

/// Returns the current status: new Inactive()
$user->status;

/// Change from Inactive to Disabled triggering the events onExit of inactive and onEntry of Disabled
$user->changeTo('disabled');

/// Throws a error since disabled cant be changed to active.
$user->changeTo('active');
```

###  Health Score

24

—

LowBetter than 32% of packages

Maintenance34

Infrequent updates — may be unmaintained

Popularity0

Limited adoption so far

Community12

Small or concentrated contributor base

Maturity47

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 50% 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.

###  Release Activity

Cadence

Every ~1627 days

Total

2

Last Release

620d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/2291426b047fc0cd198f50bf65fe2490ead02f7a3e5bb84a033118db0e2dcd17?d=identicon)[roman-franko](/maintainers/roman-franko)

---

Top Contributors

[![pedromalta](https://avatars.githubusercontent.com/u/3971810?v=4)](https://github.com/pedromalta "pedromalta (11 commits)")[![andremetzen](https://avatars.githubusercontent.com/u/636229?v=4)](https://github.com/andremetzen "andremetzen (4 commits)")[![roman-franko](https://avatars.githubusercontent.com/u/798373?v=4)](https://github.com/roman-franko "roman-franko (3 commits)")[![engylemure](https://avatars.githubusercontent.com/u/26777018?v=4)](https://github.com/engylemure "engylemure (2 commits)")[![bazaglia](https://avatars.githubusercontent.com/u/6966980?v=4)](https://github.com/bazaglia "bazaglia (1 commits)")[![ezsky](https://avatars.githubusercontent.com/u/383284?v=4)](https://github.com/ezsky "ezsky (1 commits)")

### Embed Badge

![Health badge](/badges/romanfranko-yii2-state-machine/health.svg)

```
[![Health](https://phpackages.com/badges/romanfranko-yii2-state-machine/health.svg)](https://phpackages.com/packages/romanfranko-yii2-state-machine)
```

###  Alternatives

[dmstr/yii2-cookie-consent

Yii2 Cookie Consent Widget

1452.6k](/packages/dmstr-yii2-cookie-consent)

PHPackages © 2026

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