sbooker/workflow

Simple workflow for FSM implementation

2.0.0 2023-06-14 07:18 UTC

This package is auto-updated.

Last update: 2024-11-14 11:00:55 UTC


README

Latest Version Software License PHP Version Total Downloads

Installation

pecl install ds
composer require sbooker/workflow

Usage

// Step 1. Create Status Enum

use Sbooker\Workflow\Status;

enum ConcreteStatus: string implements Status
{
    use \Sbooker\Workflow\EnumTrait;
    
    case first = "first";
    case second = "second";
}

// Step 2. Define workflow

use Sbooker\Workflow\Workflow;
use Ds\Map;
use Ds\Set;

final class ConcreteWorkflow extends Workflow
{
    public function __construct()
    {
        parent::__construct(ConcreteStatus::first);
    }


    protected function buildTransitionMap(): Map
    {
        $map = new Map();
        $map->put(ConcreteStatus::first, new Set([ConcreteStatus::second]));
        return $map;
    }

    protected function getStatusClass(): string
    {
        return ConcreteStatus::class;
    }    
}

// Step 3. Use workflow in your entity for state control
class ConcreteEntity {
    // ...
    private ConcreteWorkflow $workflow;
    // ...

    public function __construct() {
        // ...
        $this->workflow = new ConcreteWorkflow();
        // ...
    }
    
    public function doSecond(): void {
        // ...
        $this->workflow->transitTo(ConcreteStatus::second());
        // ...
    }
} 

License

See LICENSE file.