2017 © Pedro Peláez
 

library stateful-eloquent

State machines for Eloquent classes

image

acacha/stateful-eloquent

State machines for Eloquent classes

  • Saturday, December 2, 2017
  • by acacha
  • Repository
  • 1 Watchers
  • 7 Stars
  • 2,363 Installations
  • PHP
  • 5 Dependents
  • 0 Suggesters
  • 4 Forks
  • 1 Open issues
  • 10 Versions
  • 6 % Grown

The README.md

Laravel 5 Eloquent State Machine

If you're familiar with my AASM, then this is a similar take – just implemented in Laravel 5 for Eloquent classes., (*1)

References

Forked from https://github.com/mikerice/stateful-eloquent, (*2)

Installation

Step 1: Install Through Composer

composer require acacha/stateful-eloquent

Step 2: Add the Service Provider

Acacha\Stateful\Providers\StatefulServiceProvider::class,

Step 3: Update your Eloquent Model

Your models should use the Stateful trait and interface, (*3)

use Acacha\Stateful\Traits\StatefulTrait;
use Acacha\Stateful\Contracts\Stateful;

class Transaction extends Model implements Stateful
{
    use StatefulTrait;
}

Step 4: Create your Model States

Your models should have an array name $states that define your model states., (*4)

/**
 * Transaction States
 *
 * @var array
 */
protected $states = [
    'draft' => ['initial' => true],
    'processing',
    'errored',
    'active',
    'closed' => ['final' => true]
];

Step 5: Create your Model State Transitions

/**
 * Transaction State Transitions
 *
 * @var array
 */
protected $transitions = [
    'process' => [
        'from' => ['draft', 'errored'],
        'to' => 'processing'
    ],
    'activate' => [
        'from' => 'processing',
        'to' => 'active'
    ],
    'fail' => [
        'from' => 'processing',
        'to' => 'errored'
    ],
    'close' => [
        'from' => 'active',
        'to' => 'close'
    ]
];

    /**
     * @return bool
     */
    protected function validateProcess()
    {
        $validate = true;
        if (!$validate) {
            $this->addValidateProcessMessage();
        }

        return $validate;
    }

    /**
     * @return bool
     */
    protected function validateActivate()
    {
        //dd("validateActivate");
        return true;
    }

    /**
     * @return bool
     */
    protected function validateFail()
    {
        //dd("validateFail");
        return true;
    }

    /**
     * @return bool
     */
    protected function validateClose()
    {
        //dd("validateClose");
        return true;
    }

    protected function beforeProcess() {
        //dd("doing something before entering processing state");
    }

    protected function afterProcess() {
        //dd("doing something after leaving processing state");
    }

Database configuration

You model migration should have a state field like:, (*5)

class CreateModelTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('model', function (Blueprint $table) {
            $table->increments('id');
            ...
            $table->string('state');
            ...
            $table->timestamps();
        });
    }

Usage

$transaction = new Transaction();

//Execute transition
$transaction->process();

//Check if a state is active (return true or false)
$transaction->active();

The Versions

02/12 2017

dev-master

9999999-dev https://github.com/mikerice/stateful-eloquent

State machines for Eloquent classes

  Sources   Download

MIT

The Requires

 

The Development Requires

by Mike Rice

laravel eloquent statemachine

11/05 2017

0.1.8

0.1.8.0 https://github.com/mikerice/stateful-eloquent

State machines for Eloquent classes

  Sources   Download

MIT

The Requires

 

The Development Requires

by Mike Rice

laravel eloquent statemachine

10/05 2017

0.1.7

0.1.7.0 https://github.com/mikerice/stateful-eloquent

State machines for Eloquent classes

  Sources   Download

MIT

The Requires

 

The Development Requires

by Mike Rice

laravel eloquent statemachine

10/05 2017

0.1.6

0.1.6.0 https://github.com/mikerice/stateful-eloquent

State machines for Eloquent classes

  Sources   Download

MIT

The Requires

 

The Development Requires

by Mike Rice

laravel eloquent statemachine

10/05 2017

0.1.5

0.1.5.0 https://github.com/mikerice/stateful-eloquent

State machines for Eloquent classes

  Sources   Download

MIT

The Requires

 

The Development Requires

by Mike Rice

laravel eloquent statemachine

10/05 2017

0.1.4

0.1.4.0 https://github.com/mikerice/stateful-eloquent

State machines for Eloquent classes

  Sources   Download

MIT

The Requires

 

The Development Requires

by Mike Rice

laravel eloquent statemachine

09/05 2017

0.1.3

0.1.3.0 https://github.com/mikerice/stateful-eloquent

State machines for Eloquent classes

  Sources   Download

MIT

The Requires

 

The Development Requires

by Mike Rice

laravel eloquent statemachine

05/04 2017

0.1.2

0.1.2.0 https://github.com/mikerice/stateful-eloquent

State machines for Eloquent classes

  Sources   Download

MIT

The Requires

 

The Development Requires

by Mike Rice

laravel eloquent statemachine

04/04 2017

0.1.1

0.1.1.0 https://github.com/mikerice/stateful-eloquent

State machines for Eloquent classes

  Sources   Download

MIT

The Requires

 

The Development Requires

by Mike Rice

laravel eloquent statemachine

20/11 2016

0.1.0

0.1.0.0 https://github.com/mikerice/stateful-eloquent

State machines for Eloquent classes

  Sources   Download

MIT

The Requires

 

The Development Requires

by Mike Rice

laravel eloquent statemachine