1. Go to this page and download the library: Download sofa/state-machine 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/ */
class Order extends Model implements \Sofa\StateMachine\StateMachineInterface
{
//...
public function getCurrentState() : string
{
return $this->status;
}
public function setState(string $state) : void
{
$this->status = $state;
$this->save();
}
}
@foreach($orders as $order)
{{ $order->reference }} status: {{ $order->status }}
@foreach($order->available_actions as $action)
<button>{{ $action }}</button>
@endforeach
@endforeach
// controller handling the action
public function handleAction($order_id, Request $request)
{
$order_state = new \Sofa\StateMachine\Fsm(Order::find($order_id), $transitions);
$this->validate($request, [
'action' => Rule::in($order_state->getAvailableActions()),
// ...
]);
$order_state->process($request->get('action'));
return Redirect::to('some/place');
}
class Refund extends \Sofa\StateMachine\Transition
{
public function __invoke(StateMachineInterface $order, $payload)
{
// $payload is any object you pass to the process method:
// $order_state->process('refund', $anything_you_need_here);
$order->refunded_at = $payload['time'];
$order->refunded_by = $payload['user_id'];
$order->setState($this->to_state);
}
}
// Then our transitions definition would like something like:
$transitions = [
// ...
Transition::make('processing_claim', 'close claim', 'complete'),
Refund::make('processing_claim', 'refund', 'refunded'),
];
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.