PHP code example of dive-be / laravel-stateful

1. Go to this page and download the library: Download dive-be/laravel-stateful 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/ */

    

dive-be / laravel-stateful example snippets


abstract class CheckoutState extends State
{
    /* You can define which methods need to be implemented for each particular state.
     * For example, you might want each state to have an associated view to resolve. */
    abstract public function view(): string;

    /* Like `view()`, each state should also have an associated index. 
     * We'll enforce that this method needs to be implemented by each class 
     * that inherits from this one. */
    abstract public function step(): int;
    
    /* Set up which state transitions are allowed. 
     * This is 

class AddressSelect extends CheckoutState
{
    public function view(): string
    { 
        return 'checkout.address_select'; 
    }
    
    public function step(): int
    {
        return 0; 
    }
}

class CheckoutWizard implements Stateful
{
    use InteractsWithState;
    
    public function __construct() 
    {
        $this->state = AddressSelect::make($this);
    }
    
    // Your code here
}

$checkout = new CheckoutWizard();

$checkout->getState()->transitionTo(Complete::class);

class AdvanceToShipping extends Transition
{
    public function from(): string { return AddressSelect::class; }
    
    public function to(): string { return ShippingSelect::class; }
}

public static function config(): Config
{
    return parent::config()->allowTransition(AdvanceToShipping::class);
}

class AdvanceToShipping extends Transition
{
    // omitted for brevity
    
    public function guard(CheckoutWizard $wizard, MyService $service)
    {
        return $service->isValid($wizard);
    }
}

class AdvanceToShipping extends Transition
{
    // omitted for brevity
    
    public function after(CheckoutWizard $wizard)
    {
        // run immediately after the transition
    }
    
    public function before(CheckoutWizard $wizard)
    {
        // run just before the transition
    }
}