PHP code example of lzpeng / php-state-processor

1. Go to this page and download the library: Download lzpeng/php-state-processor 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/ */

    

lzpeng / php-state-processor example snippets



$transitions = [
    'init' => [
        'from' => ['unknown'], // 来源状态
        'to' => 'inited', // 转换后的状态
        'action' => SubmitAction::class, // 状态转换时执行的动作
    ],
    'audit' => [
        'from' => ['inited'],
        'to' => 'audited',
        'action' => AuditAction::class,
    ], ...
];

// 动作与最终状态更改需要在同一事务中,保证原子性
// 通过 \Lzpeng\StateProcess\Contracts\TxInterface 实现自己的事务对象
$txCreator = NullTx::class; 

$processor = Factory::create($transitions, $txCreator);

// 业务域对象
$order = new Order();
$order->setState(new State('unknown'));

// 判断是否能执行指定流转
if ($processor->can('init', $order)) {
    // ...
}

// 执行流转
try {
    $processor->execut('init', $order);
} catch(StateException $ex) {
    // exception handle
}