Skip to content

Instantly share code, notes, and snippets.

@edgrosvenor
Created April 6, 2026 05:39
Show Gist options
  • Select an option

  • Save edgrosvenor/f7c54dbc56e8f912cb2c9f7949c8b395 to your computer and use it in GitHub Desktop.

Select an option

Save edgrosvenor/f7c54dbc56e8f912cb2c9f7949c8b395 to your computer and use it in GitHub Desktop.
Fat Enums: Publishing workflow state machine example
<?php
namespace App\Enums;
enum PublishingWorkflow: string implements StateMachine
{
use IsStateMachine;
public const DEFAULT = self::Draft;
#[CanTransitionTo([self::InReview])]
#[CanTransitionToSelf] // Keep editing
case Draft = 'draft';
#[CanTransitionTo([self::Draft, self::Published])]
// Can reject back to Draft, or approve to Published
case InReview = 'in_review';
#[CanTransitionTo([self::Archived])]
case Published = 'published';
#[FinalState]
case Archived = 'archived';
}
// The workflow in action:
$article = Article::create(['title' => 'Fat Enums Rock']);
// status: Draft
$article->status = PublishingWorkflow::InReview;
$article->save(); // Works
$article->status = PublishingWorkflow::Draft; // Rejected!
$article->save(); // Works - reviewer sent it back
$article->status = PublishingWorkflow::Published;
$article->save();
// InvalidStateTransition: "Cannot transition from Draft to Published"
// Must go through InReview first!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment