### Install Finite using Composer Source: https://github.com/yohang/finite/blob/main/README.md This command installs the Finite library using Composer, the dependency manager for PHP. Ensure Composer is installed and accessible in your environment. ```bash $ composer req yohang/finite ``` -------------------------------- ### Initialize and Use StateMachine (PHP) Source: https://github.com/yohang/finite/blob/main/README.md This PHP code demonstrates how to initialize a `StateMachine` and interact with a `Document` object. It shows how to check if a transition is possible and how to apply a transition to change the object's state. ```php use Finite\StateMachine; $document = new Document; $sm = new StateMachine; // Can we process a transition ? $sm->can($document, 'publish'); // Apply a transition $sm->apply($document, 'publish'); ``` -------------------------------- ### Accessing State Logic from Object (PHP) Source: https://github.com/yohang/finite/blob/main/README.md This PHP code shows how to access the business logic methods defined within the `DocumentState` enum directly from the `Document` object. This approach simplifies domain code by allowing state-specific checks without needing to instantiate or interact with the state machine. ```php var_dump($document->getState()->isDeletable()); var_dump($document->getState()->isPrintable()); ``` -------------------------------- ### Define Document States with Transitions (PHP) Source: https://github.com/yohang/finite/blob/main/README.md This PHP code defines an enum `DocumentState` that implements the `State` interface. It specifies possible states for a document and defines the allowed transitions between these states using the `Transition` class. ```php enum DocumentState: string implements State { case DRAFT = 'draft'; case PUBLISHED = 'published'; case REPORTED = 'reported'; case DISABLED = 'disabled'; public static function getTransitions(): array { return [ new Transition('publish', [self::DRAFT], self::PUBLISHED), new Transition('clear', [self::REPORTED, self::DISABLED], self::PUBLISHED), new Transition('report', [self::PUBLISHED], self::REPORTED), new Transition('disable', [self::REPORTED, self::PUBLISHED], self::DISABLED), ]; } } ``` -------------------------------- ### Add Business Logic to States using Enum Methods (PHP) Source: https://github.com/yohang/finite/blob/main/README.md This PHP code extends the `DocumentState` enum to include methods like `isDeletable` and `isPrintable`. These methods encapsulate business logic directly within the state enum, allowing for cleaner state management without explicit state machine checks in domain code. ```php enum DocumentState: string implements State { // ... public function isDeletable(): bool { return in_array($this, [self::DRAFT, self::DISABLED]); } public function isPrintable(): bool { return in_array($this, [self::PUBLISHED, self::REPORTED]); } } ``` -------------------------------- ### Define a Stateful Document Object (PHP) Source: https://github.com/yohang/finite/blob/main/README.md This PHP code defines a `Document` class that holds its state. It includes a private property `$state` typed as `DocumentState` and provides getter and setter methods for accessing and modifying the document's state. ```php class Document { private DocumentState $state = DocumentState::DRAFT; public function getState(): DocumentState { return $this->state; } public function setState(DocumentState $state): void { $this->state = $state; } } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.