exabyssus/laravel-finite-state

Mini Laravel Finite State Machine.

v1.0.6 2019-03-19 12:32 UTC

This package is auto-updated.

Last update: 2024-12-07 02:23:16 UTC


README

Latest Stable Version Latest Unstable Version Total Downloads License

Package gives an easy way to add StateMachine to your Eloquent Models.

StateMachine helps you control state flow and records state history.

Installation

Add package to your composer.

composer require exabyssus/laravel-finite-state

Publish package to copy config and migration files.

php artisan vendor:publish --provider="Exabyssus\StateMachine\StateMachineServiceProvider"   

Add Traits to Models you want to add State Machine

use Exabyssus\StateMachine\Traits\HasStateHistory;
use Exabyssus\StateMachine\Traits\HasStateMachine;

class YourModel extends Model
{
    use HasStateMachine;
    use HasStateHistory;
    ...
}

Configure State Machine transitions

config/state-machine.php

Configuration

<?php

return [
    'order' => [  // Name of your object
        'state_property_name' => 'state',  // Objects current state property name
        'states' => [  // All available states
           'pending',
           'confirmed',
        ],
        'transitions' => [  // Transition mapping
            'confirm' => [
                'from' => ['pending'],
                'to' => 'confirmed'
            ],
            'cancel' => [
                'from' => ['pending', 'confirmed'],
                'to' => 'canceled'
            ],
        ]
    ],
];

Usage

Check if transition is allow for Object

transitionAllowed (strign $status): bool

Apply transition

transition(string $transition): void

Get available transitions

getPossibleTransitions(): array

Events

To catch Object before state is change add afterStateChange method to your class.

function beforeStateChange($transition, $stateFrom, $stateTo)

If function returns false state won't be changed.

After state is changed afterStateChange method is called.

function afterStateChange($transition, $stateTo)

And StateChanged event is dispatched.