PHP code example of acacha / stateful-eloquent

1. Go to this page and download the library: Download acacha/stateful-eloquent library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

acacha / stateful-eloquent example snippets


Acacha\Stateful\Providers\StatefulServiceProvider::class,

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

class Transaction extends Model implements Stateful
{
    use StatefulTrait;
}

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

/**
 * 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");
    }


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();
        });
    }

$transaction = new Transaction();

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

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