Created
April 6, 2026 05:39
-
-
Save edgrosvenor/f7c54dbc56e8f912cb2c9f7949c8b395 to your computer and use it in GitHub Desktop.
Fat Enums: Publishing workflow state machine example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?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