Sons of PHP
Source Code
  • 🏠Home
  • Contributor Covenant Code of Conduct
  • Getting Help
  • Reporting Issues
  • Security Policy
  • 🪈Bard
    • Overview
    • Commands
  • Symfony Bundles
    • Feature Toggle
  • Contracts
    • Contracts Overview
    • Common
    • Cookie
    • CQRS
    • Filesystem
    • Mailer
    • Pager
    • Registry
    • State Machine
  • 📦Components
    • Assert
    • Cache
      • Adapters
      • Marshallers
    • Clock
    • Container
    • Cookie
    • CQRS
    • Event Dispatcher
    • Event Sourcing
      • Aggregates
        • Aggregate Repository
      • Event Messages
        • Using the Serializable Event Message
        • Message Enrichers
        • Message Serializers
        • Message Repository
        • Message Upcasters
    • Feature Toggle
    • Filesystem
      • Adapters
    • Http Factory
    • Http Handler
    • Http Message
    • JSON
    • Link
    • Logger
      • Handlers
      • Enrichers
      • Filters
      • Formatters
    • Mailer
      • Transports
    • Money
      • Currency Providers
      • Operators
      • Queries
    • Pager
      • Adapters
    • Registry
    • State Machine
    • Version
  • 💁Contributing
    • Contributing Overview
    • Contributing Code
    • Discussions
    • Documentation
Powered by GitBook
On this page
  • Installation
  • Usage

Was this helpful?

Edit on GitHub
  1. Components

State Machine

Installation

composer require sonsofphp/state-machine

Usage

Basic Usage

<?php

use SonsOfPHP\Component\StateMachine\StateMachine;

$sm = new StateMachine([
    'graph'         => 'order',
    'state_getter'  => 'getState',
    'state_setter'  => 'setState',
    'supports' => [
        OrderInterface::class,
    ],
    'transitions' => [
        'create' => [
            'from' => 'draft',
            'to' => 'new',
            'callbacks' => [
                'guard' => [
                    'guard-create' => [
                        'do' => function () { return true; },
                    ],
                ],
                'pre' => [
                    'pre-create' => [
                        'do' => function () { },
                    ],
                    'another-pre-create' => [
                        'do' => function () {},
                    ],
                ],
                'post' => [
                    'post-create' => [
                        'do' => function () {},
                    ],
                    'another-post-create' => [
                        'do' => function () {},
                    ],
                ],
            ],
        ],
        'fulfill' => [
            'from' => 'new',
            'to' => 'fulfilled',
        ],
        'cancel' => [
            'from' => ['draft', 'new', 'fulfilled'],
            'to' => 'fulfilled',
        ],
    ],
]);

// Check if state can change
$sm->can($order, 'create');

// Apply transition
$sm->apply($order, 'fulfil');

// Get Current State
$sm->getState($order);
PreviousRegistryNextVersion

Last updated 8 months ago

Was this helpful?

📦